From 9825c179a895680c7831257bbfce47b5276ddbdd Mon Sep 17 00:00:00 2001 From: DavHau Date: Fri, 22 Sep 2023 18:32:09 +0200 Subject: [PATCH] impure-tests: migrate bash to pytest and fix stuff --- checks/impure/flake-module.nix | 88 ++++--------------- .../clan_cli/secrets/sops_generate.py | 5 +- pkgs/clan-cli/shell.nix | 2 +- pkgs/clan-cli/tests/end_to_end/test_basics.py | 31 +++++++ pkgs/clan-cli/tests/sshd.py | 7 +- 5 files changed, 57 insertions(+), 76 deletions(-) create mode 100644 pkgs/clan-cli/tests/end_to_end/test_basics.py diff --git a/checks/impure/flake-module.nix b/checks/impure/flake-module.nix index c2c76b0c8..b4628c81c 100644 --- a/checks/impure/flake-module.nix +++ b/checks/impure/flake-module.nix @@ -1,75 +1,19 @@ -{ self, ... }: { - perSystem = { pkgs, lib, self', ... }: - let - impureChecks = { - clan-pytest-impure = pkgs.writeShellScriptBin "clan-pytest-impure" '' - #!${pkgs.bash}/bin/bash - set -euo pipefail +{ ... }: { + perSystem = { pkgs, lib, ... }: { + packages = { + # a script that executes all other checks + impure-checks = pkgs.writeShellScriptBin "impure-checks" '' + #!${pkgs.bash}/bin/bash + set -euo pipefail - export PATH="${lib.makeBinPath [ - pkgs.gitMinimal - pkgs.nix - ]}" - 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 -euox pipefail - - 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 - pkgs.curl - pkgs.gitMinimal - pkgs.gnugrep - pkgs.jq - pkgs.openssh - pkgs.nix - self'.packages.clan-cli - ]}" - - cd $CLANTMP - - echo initialize new clan - nix flake init -t ${self}#new-clan - - echo override clan input to the current version - nix flake lock --override-input clan-core ${self} - nix flake lock --override-input nixpkgs ${self.inputs.nixpkgs} - - echo ensure flake outputs can be listed - nix flake show - - echo create a machine - clan machines create machine1 - - echo check machine1 exists - clan machines list | grep -q machine1 - - echo check machine1 appears in nixosConfigurations - nix flake show --json | jq '.nixosConfigurations' | grep -q machine1 - - echo check machine1 jsonschema can be evaluated - nix eval .#nixosConfigurations.machine1.config.clanSchema - ''; - }; - in - { - packages = - impureChecks // { - # a script that executes all other checks - impure-checks = pkgs.writeShellScriptBin "impure-checks" '' - #!${pkgs.bash}/bin/bash - set -euo pipefail - ${lib.concatMapStringsSep "\n" (name: '' - echo -e "\n\nrunning check ${name}\n" - ${impureChecks.${name}}/bin/* "$@" - '') (lib.attrNames impureChecks)} - ''; - }; + export PATH="${lib.makeBinPath [ + pkgs.gitMinimal + pkgs.nix + ]}" + 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' + ''; }; + }; } diff --git a/pkgs/clan-cli/clan_cli/secrets/sops_generate.py b/pkgs/clan-cli/clan_cli/secrets/sops_generate.py index 18f701ca0..0be562ce3 100644 --- a/pkgs/clan-cli/clan_cli/secrets/sops_generate.py +++ b/pkgs/clan-cli/clan_cli/secrets/sops_generate.py @@ -6,6 +6,8 @@ from pathlib import Path from tempfile import TemporaryDirectory from typing import Any +from clan_cli.nix import nix_shell + from ..dirs import get_clan_flake_toplevel from ..errors import ClanError from ..ssh import parse_deployment_address @@ -47,7 +49,8 @@ secrets={shlex.quote(str(secrets_dir))} {generator} """ try: - subprocess.run(["bash", "-c", text], check=True) + cmd = nix_shell(["bash"], ["bash", "-c", text]) + subprocess.run(cmd, check=True) except subprocess.CalledProcessError: msg = "failed to the following command:\n" msg += text diff --git a/pkgs/clan-cli/shell.nix b/pkgs/clan-cli/shell.nix index 8db3c9651..b44934ac3 100644 --- a/pkgs/clan-cli/shell.nix +++ b/pkgs/clan-cli/shell.nix @@ -32,6 +32,6 @@ mkShell { register-python-argcomplete --shell fish clan > $tmp_path/share/fish/vendor_completions.d/clan.fish register-python-argcomplete --shell bash clan > $tmp_path/share/bash-completion/completions/clan - ${clan-cli}/bin/clan machines create example + ./bin/clan machines create example ''; } diff --git a/pkgs/clan-cli/tests/end_to_end/test_basics.py b/pkgs/clan-cli/tests/end_to_end/test_basics.py new file mode 100644 index 000000000..f00fe074d --- /dev/null +++ b/pkgs/clan-cli/tests/end_to_end/test_basics.py @@ -0,0 +1,31 @@ +import json +import subprocess +from pathlib import Path + +import pytest +from cli import Cli + + +@pytest.mark.impure +def test_basics( + monkeypatch: pytest.MonkeyPatch, temporary_dir: Path, capsys: pytest.CaptureFixture +) -> None: + monkeypatch.chdir(temporary_dir) + cli = Cli() + cli.run(["create"]) + assert (temporary_dir / ".clan-flake").exists() + cli.run(["machines", "create", "machine1"]) + capsys.readouterr() # flush cache + cli.run(["machines", "list"]) + assert "machine1" in capsys.readouterr().out + flake_show = subprocess.run( + ["nix", "flake", "show", "--json"], + check=True, + capture_output=True, + text=True, + ) + flake_outputs = json.loads(flake_show.stdout) + try: + flake_outputs["nixosConfigurations"]["machine1"] + except KeyError: + pytest.fail("nixosConfigurations.machine1 not found in flake outputs") diff --git a/pkgs/clan-cli/tests/sshd.py b/pkgs/clan-cli/tests/sshd.py index d175a2390..901d1e734 100644 --- a/pkgs/clan-cli/tests/sshd.py +++ b/pkgs/clan-cli/tests/sshd.py @@ -106,7 +106,10 @@ exec {bash} -l "${{@}}" @pytest.fixture def sshd( - sshd_config: SshdConfig, command: "Command", unused_tcp_port: "PortFunction" + sshd_config: SshdConfig, + command: "Command", + unused_tcp_port: "PortFunction", + monkeypatch: pytest.MonkeyPatch, ) -> Iterator[Sshd]: import subprocess @@ -121,7 +124,7 @@ def sshd( proc = command.run( [sshd, "-f", str(sshd_config.path), "-D", "-p", str(port)], extra_env=env ) - + monkeypatch.delenv("SSH_AUTH_SOCK", raising=False) while True: print(sshd_config.path) if (