From 17520e2553a9a587cde7c82ec4f94110610c85d8 Mon Sep 17 00:00:00 2001 From: lassulus Date: Thu, 21 Sep 2023 17:17:48 +0200 Subject: [PATCH] fix impure tests --- checks/impure/flake-module.nix | 29 +++++---------- pkgs/clan-cli/clan_cli/secrets/upload.py | 7 +++- pkgs/clan-cli/default.nix | 2 ++ pkgs/clan-cli/shell.nix | 16 ++------- pkgs/clan-cli/tests/sshd.py | 5 +-- .../tests/test_flake_with_core/flake.nix | 36 ++++++++++--------- pkgs/clan-cli/tests/test_secrets_upload.py | 2 +- 7 files changed, 44 insertions(+), 53 deletions(-) diff --git a/checks/impure/flake-module.nix b/checks/impure/flake-module.nix index 3cd0af96..c2c76b0c 100644 --- a/checks/impure/flake-module.nix +++ b/checks/impure/flake-module.nix @@ -6,31 +6,20 @@ #!${pkgs.bash}/bin/bash set -euo pipefail - export TMPDIR=$(${pkgs.coreutils}/bin/mktemp -d) - trap "${pkgs.coreutils}/bin/chmod -R +w '$TMPDIR'; ${pkgs.coreutils}/bin/rm -rf '$TMPDIR'" EXIT - export PATH="${lib.makeBinPath ([ - pkgs.coreutils + export PATH="${lib.makeBinPath [ pkgs.gitMinimal pkgs.nix - self'.packages.clan-cli.checkPython - ] ++ self'.packages.clan-cli.pytestDependencies)}" - - export CLAN_CORE=$TMPDIR/CLAN_CORE - cp -r ${self} $CLAN_CORE - chmod +w -R $CLAN_CORE - - cp -r ${self'.packages.clan-cli.src} $TMPDIR/src - chmod +w -R $TMPDIR/src - cd $TMPDIR/src - - python -m pytest -m "impure" -s ./tests --workers "" "$@" + ]}" + ROOT=$(git rev-parse --show-toplevel) + cd "$ROOT/pkgs/clan-cli" + nix develop "$ROOT#clan-cli" -c bash -c 'TMPDIR=/tmp python -m pytest -m impure -s ./tests' ''; check-clan-template = pkgs.writeShellScriptBin "check-clan-template" '' #!${pkgs.bash}/bin/bash - set -euo pipefail + set -euox pipefail - export TMPDIR=$(${pkgs.coreutils}/bin/mktemp -d) - trap "${pkgs.coreutils}/bin/chmod -R +w '$TMPDIR'; ${pkgs.coreutils}/bin/rm -rf '$TMPDIR'" EXIT + export CLANTMP=$(${pkgs.coreutils}/bin/mktemp -d) + trap "${pkgs.coreutils}/bin/chmod -R +w '$CLANTMP'; ${pkgs.coreutils}/bin/rm -rf '$CLANTMP'" EXIT export PATH="${lib.makeBinPath [ pkgs.coreutils @@ -43,7 +32,7 @@ self'.packages.clan-cli ]}" - cd $TMPDIR + cd $CLANTMP echo initialize new clan nix flake init -t ${self}#new-clan diff --git a/pkgs/clan-cli/clan_cli/secrets/upload.py b/pkgs/clan-cli/clan_cli/secrets/upload.py index 0d43608e..dd75c13a 100644 --- a/pkgs/clan-cli/clan_cli/secrets/upload.py +++ b/pkgs/clan-cli/clan_cli/secrets/upload.py @@ -1,8 +1,9 @@ import argparse import json +import os import subprocess -from ..dirs import get_clan_flake_toplevel +from ..dirs import get_clan_flake_toplevel, module_root from ..errors import ClanError from ..nix import nix_build, nix_config, nix_eval @@ -22,6 +23,9 @@ def upload_secrets(machine: str) -> None: text=True, check=True, ) + + env = os.environ.copy() + env["PYTHONPATH"] = str(module_root().parent) # TODO do this in the clanCore module host = json.loads( subprocess.run( nix_eval( @@ -41,6 +45,7 @@ def upload_secrets(machine: str) -> None: secret_upload_script, host, ], + env=env, ) if secret_upload.returncode != 0: diff --git a/pkgs/clan-cli/default.nix b/pkgs/clan-cli/default.nix index a92ba2f7..0bb83d7f 100644 --- a/pkgs/clan-cli/default.nix +++ b/pkgs/clan-cli/default.nix @@ -26,6 +26,7 @@ , zbar , tor , git +, ipdb }: let @@ -43,6 +44,7 @@ let openssh git stdenv.cc + ipdb # used for debugging ]; # Optional dependencies for clan cli, we re-expose them here to make sure they all build. diff --git a/pkgs/clan-cli/shell.nix b/pkgs/clan-cli/shell.nix index 136fabda..8db3c965 100644 --- a/pkgs/clan-cli/shell.nix +++ b/pkgs/clan-cli/shell.nix @@ -1,26 +1,16 @@ -{ nix-unit, clan-cli, ui-assets, python3, system, ruff, mkShell, writeScriptBin }: +{ nix-unit, clan-cli, ui-assets, system, mkShell, writeScriptBin, openssh }: let - pythonWithDeps = python3.withPackages ( - ps: - clan-cli.propagatedBuildInputs - ++ clan-cli.devDependencies - ++ [ - ps.pip - ps.ipdb - ] - ); checkScript = writeScriptBin "check" '' nix build .#checks.${system}.{treefmt,clan-pytest} -L "$@" ''; in mkShell { packages = [ - ruff nix-unit - pythonWithDeps + openssh + clan-cli.checkPython ]; # sets up an editable install and add enty points to $PATH - PYTHONPATH = "${pythonWithDeps}/${pythonWithDeps.sitePackages}"; PYTHONBREAKPOINT = "ipdb.set_trace"; shellHook = '' diff --git a/pkgs/clan-cli/tests/sshd.py b/pkgs/clan-cli/tests/sshd.py index b3013173..d175a239 100644 --- a/pkgs/clan-cli/tests/sshd.py +++ b/pkgs/clan-cli/tests/sshd.py @@ -59,6 +59,7 @@ def sshd_config(project_root: Path, test_root: Path) -> Iterator[SshdConfig]: MaxStartups 64:30:256 AuthorizedKeysFile {host_key}.pub AcceptEnv REALPATH + PasswordAuthentication no """ ) login_shell = dir / "shell" @@ -109,7 +110,6 @@ def sshd( ) -> Iterator[Sshd]: import subprocess - subprocess.run(["echo", "hello"], check=True) port = unused_tcp_port() sshd = shutil.which("sshd") assert sshd is not None, "no sshd binary found" @@ -123,6 +123,7 @@ def sshd( ) while True: + print(sshd_config.path) if ( subprocess.run( [ @@ -137,7 +138,7 @@ def sshd( "-p", str(port), "true", - ] + ], ).returncode == 0 ): diff --git a/pkgs/clan-cli/tests/test_flake_with_core/flake.nix b/pkgs/clan-cli/tests/test_flake_with_core/flake.nix index d5cdedf3..43f2d8de 100644 --- a/pkgs/clan-cli/tests/test_flake_with_core/flake.nix +++ b/pkgs/clan-cli/tests/test_flake_with_core/flake.nix @@ -5,25 +5,29 @@ # this placeholder is replaced by the path to nixpkgs inputs.clan-core.url = "__CLAN_CORE__"; - outputs = { self, clan-core }: { - nixosConfigurations = clan-core.lib.buildClan { - directory = self; - machines = { - vm1 = { modulesPath, ... }: { - imports = [ "${toString modulesPath}/virtualisation/qemu-vm.nix" ]; - clan.networking.deploymentAddress = "__CLAN_DEPLOYMENT_ADDRESS__"; - sops.age.keyFile = "__CLAN_SOPS_KEY_PATH__"; + outputs = { self, clan-core }: + let + clan = clan-core.lib.buildClan { + directory = self; + machines = { + vm1 = { modulesPath, ... }: { + imports = [ "${toString modulesPath}/virtualisation/qemu-vm.nix" ]; + clan.networking.deploymentAddress = "__CLAN_DEPLOYMENT_ADDRESS__"; + sops.age.keyFile = "__CLAN_SOPS_KEY_PATH__"; - clanCore.secrets.testpassword = { - generator = '' - echo "secret1" > "$secrets/secret1" - echo "fact1" > "$facts/fact1" - ''; - secrets.secret1 = { }; - facts.fact1 = { }; + clanCore.secrets.testpassword = { + generator = '' + echo "secret1" > "$secrets/secret1" + echo "fact1" > "$facts/fact1" + ''; + secrets.secret1 = { }; + facts.fact1 = { }; + }; }; }; }; + in + { + inherit (clan) nixosConfigurations clanInternals; }; - }; } diff --git a/pkgs/clan-cli/tests/test_secrets_upload.py b/pkgs/clan-cli/tests/test_secrets_upload.py index 18451e29..0003b012 100644 --- a/pkgs/clan-cli/tests/test_secrets_upload.py +++ b/pkgs/clan-cli/tests/test_secrets_upload.py @@ -11,7 +11,7 @@ if TYPE_CHECKING: @pytest.mark.impure -def test_upload_secret( +def test_secrets_upload( monkeypatch: pytest.MonkeyPatch, test_flake_with_core: Path, host_group: HostGroup,