fix impure tests

This commit is contained in:
lassulus 2023-09-21 17:17:48 +02:00
parent 56bcd0cf0b
commit 17520e2553
7 changed files with 44 additions and 53 deletions

View File

@ -6,31 +6,20 @@
#!${pkgs.bash}/bin/bash #!${pkgs.bash}/bin/bash
set -euo pipefail set -euo pipefail
export TMPDIR=$(${pkgs.coreutils}/bin/mktemp -d) export PATH="${lib.makeBinPath [
trap "${pkgs.coreutils}/bin/chmod -R +w '$TMPDIR'; ${pkgs.coreutils}/bin/rm -rf '$TMPDIR'" EXIT
export PATH="${lib.makeBinPath ([
pkgs.coreutils
pkgs.gitMinimal pkgs.gitMinimal
pkgs.nix pkgs.nix
self'.packages.clan-cli.checkPython ]}"
] ++ self'.packages.clan-cli.pytestDependencies)}" ROOT=$(git rev-parse --show-toplevel)
cd "$ROOT/pkgs/clan-cli"
export CLAN_CORE=$TMPDIR/CLAN_CORE nix develop "$ROOT#clan-cli" -c bash -c 'TMPDIR=/tmp python -m pytest -m impure -s ./tests'
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 "" "$@"
''; '';
check-clan-template = pkgs.writeShellScriptBin "check-clan-template" '' check-clan-template = pkgs.writeShellScriptBin "check-clan-template" ''
#!${pkgs.bash}/bin/bash #!${pkgs.bash}/bin/bash
set -euo pipefail set -euox pipefail
export TMPDIR=$(${pkgs.coreutils}/bin/mktemp -d) export CLANTMP=$(${pkgs.coreutils}/bin/mktemp -d)
trap "${pkgs.coreutils}/bin/chmod -R +w '$TMPDIR'; ${pkgs.coreutils}/bin/rm -rf '$TMPDIR'" EXIT trap "${pkgs.coreutils}/bin/chmod -R +w '$CLANTMP'; ${pkgs.coreutils}/bin/rm -rf '$CLANTMP'" EXIT
export PATH="${lib.makeBinPath [ export PATH="${lib.makeBinPath [
pkgs.coreutils pkgs.coreutils
@ -43,7 +32,7 @@
self'.packages.clan-cli self'.packages.clan-cli
]}" ]}"
cd $TMPDIR cd $CLANTMP
echo initialize new clan echo initialize new clan
nix flake init -t ${self}#new-clan nix flake init -t ${self}#new-clan

View File

@ -1,8 +1,9 @@
import argparse import argparse
import json import json
import os
import subprocess import subprocess
from ..dirs import get_clan_flake_toplevel from ..dirs import get_clan_flake_toplevel, module_root
from ..errors import ClanError from ..errors import ClanError
from ..nix import nix_build, nix_config, nix_eval from ..nix import nix_build, nix_config, nix_eval
@ -22,6 +23,9 @@ def upload_secrets(machine: str) -> None:
text=True, text=True,
check=True, check=True,
) )
env = os.environ.copy()
env["PYTHONPATH"] = str(module_root().parent) # TODO do this in the clanCore module
host = json.loads( host = json.loads(
subprocess.run( subprocess.run(
nix_eval( nix_eval(
@ -41,6 +45,7 @@ def upload_secrets(machine: str) -> None:
secret_upload_script, secret_upload_script,
host, host,
], ],
env=env,
) )
if secret_upload.returncode != 0: if secret_upload.returncode != 0:

View File

@ -26,6 +26,7 @@
, zbar , zbar
, tor , tor
, git , git
, ipdb
}: }:
let let
@ -43,6 +44,7 @@ let
openssh openssh
git git
stdenv.cc stdenv.cc
ipdb # used for debugging
]; ];
# Optional dependencies for clan cli, we re-expose them here to make sure they all build. # Optional dependencies for clan cli, we re-expose them here to make sure they all build.

View File

@ -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 let
pythonWithDeps = python3.withPackages (
ps:
clan-cli.propagatedBuildInputs
++ clan-cli.devDependencies
++ [
ps.pip
ps.ipdb
]
);
checkScript = writeScriptBin "check" '' checkScript = writeScriptBin "check" ''
nix build .#checks.${system}.{treefmt,clan-pytest} -L "$@" nix build .#checks.${system}.{treefmt,clan-pytest} -L "$@"
''; '';
in in
mkShell { mkShell {
packages = [ packages = [
ruff
nix-unit nix-unit
pythonWithDeps openssh
clan-cli.checkPython
]; ];
# sets up an editable install and add enty points to $PATH # sets up an editable install and add enty points to $PATH
PYTHONPATH = "${pythonWithDeps}/${pythonWithDeps.sitePackages}";
PYTHONBREAKPOINT = "ipdb.set_trace"; PYTHONBREAKPOINT = "ipdb.set_trace";
shellHook = '' shellHook = ''

View File

@ -59,6 +59,7 @@ def sshd_config(project_root: Path, test_root: Path) -> Iterator[SshdConfig]:
MaxStartups 64:30:256 MaxStartups 64:30:256
AuthorizedKeysFile {host_key}.pub AuthorizedKeysFile {host_key}.pub
AcceptEnv REALPATH AcceptEnv REALPATH
PasswordAuthentication no
""" """
) )
login_shell = dir / "shell" login_shell = dir / "shell"
@ -109,7 +110,6 @@ def sshd(
) -> Iterator[Sshd]: ) -> Iterator[Sshd]:
import subprocess import subprocess
subprocess.run(["echo", "hello"], check=True)
port = unused_tcp_port() port = unused_tcp_port()
sshd = shutil.which("sshd") sshd = shutil.which("sshd")
assert sshd is not None, "no sshd binary found" assert sshd is not None, "no sshd binary found"
@ -123,6 +123,7 @@ def sshd(
) )
while True: while True:
print(sshd_config.path)
if ( if (
subprocess.run( subprocess.run(
[ [
@ -137,7 +138,7 @@ def sshd(
"-p", "-p",
str(port), str(port),
"true", "true",
] ],
).returncode ).returncode
== 0 == 0
): ):

View File

@ -5,25 +5,29 @@
# this placeholder is replaced by the path to nixpkgs # this placeholder is replaced by the path to nixpkgs
inputs.clan-core.url = "__CLAN_CORE__"; inputs.clan-core.url = "__CLAN_CORE__";
outputs = { self, clan-core }: { outputs = { self, clan-core }:
nixosConfigurations = clan-core.lib.buildClan { let
directory = self; clan = clan-core.lib.buildClan {
machines = { directory = self;
vm1 = { modulesPath, ... }: { machines = {
imports = [ "${toString modulesPath}/virtualisation/qemu-vm.nix" ]; vm1 = { modulesPath, ... }: {
clan.networking.deploymentAddress = "__CLAN_DEPLOYMENT_ADDRESS__"; imports = [ "${toString modulesPath}/virtualisation/qemu-vm.nix" ];
sops.age.keyFile = "__CLAN_SOPS_KEY_PATH__"; clan.networking.deploymentAddress = "__CLAN_DEPLOYMENT_ADDRESS__";
sops.age.keyFile = "__CLAN_SOPS_KEY_PATH__";
clanCore.secrets.testpassword = { clanCore.secrets.testpassword = {
generator = '' generator = ''
echo "secret1" > "$secrets/secret1" echo "secret1" > "$secrets/secret1"
echo "fact1" > "$facts/fact1" echo "fact1" > "$facts/fact1"
''; '';
secrets.secret1 = { }; secrets.secret1 = { };
facts.fact1 = { }; facts.fact1 = { };
};
}; };
}; };
}; };
in
{
inherit (clan) nixosConfigurations clanInternals;
}; };
};
} }

View File

@ -11,7 +11,7 @@ if TYPE_CHECKING:
@pytest.mark.impure @pytest.mark.impure
def test_upload_secret( def test_secrets_upload(
monkeypatch: pytest.MonkeyPatch, monkeypatch: pytest.MonkeyPatch,
test_flake_with_core: Path, test_flake_with_core: Path,
host_group: HostGroup, host_group: HostGroup,