tests: set git author name + email via environment variables

This commit is contained in:
Jörg Thalheim
2024-09-29 18:44:29 +02:00
parent 489977bf3b
commit 1e0f00e6d2
4 changed files with 62 additions and 28 deletions

View File

@@ -59,9 +59,29 @@ def set_machine_settings(
config_path.write_text(json.dumps(machine_settings, indent=2)) config_path.write_text(json.dumps(machine_settings, indent=2))
def init_git(monkeypatch: pytest.MonkeyPatch, flake: Path) -> None:
monkeypatch.setenv("GIT_AUTHOR_NAME", "clan-tool")
monkeypatch.setenv("GIT_AUTHOR_EMAIL", "clan@example.com")
monkeypatch.setenv("GIT_COMMITTER_NAME", "clan-tool")
monkeypatch.setenv("GIT_COMMITTER_EMAIL", "clan@example.com")
# TODO: Find out why test_vms_api.py fails in nix build
# but works in pytest when this bottom line is commented out
sp.run(
["git", "config", "--global", "init.defaultBranch", "main"],
cwd=flake,
check=True,
)
sp.run(["git", "init"], cwd=flake, check=True)
sp.run(["git", "add", "."], cwd=flake, check=True)
sp.run(["git", "commit", "-a", "-m", "Initial commit"], cwd=flake, check=True)
def generate_flake( def generate_flake(
temporary_home: Path, temporary_home: Path,
flake_template: Path, flake_template: Path,
monkeypatch: pytest.MonkeyPatch,
substitutions: dict[str, str] | None = None, substitutions: dict[str, str] | None = None,
# define the machines directly including their config # define the machines directly including their config
machine_configs: dict[str, dict] | None = None, machine_configs: dict[str, dict] | None = None,
@@ -125,7 +145,7 @@ def generate_flake(
configuration_nix.write_text(""" configuration_nix.write_text("""
{ imports = [ (builtins.fromJSON (builtins.readFile ./configuration.json)) ]; } { imports = [ (builtins.fromJSON (builtins.readFile ./configuration.json)) ]; }
""") """)
set_machine_config(flake, machine_name, machine_config) set_machine_settings(flake, machine_name, machine_config)
if "/tmp" not in str(os.environ.get("HOME")): if "/tmp" not in str(os.environ.get("HOME")):
log.warning( log.warning(
@@ -139,17 +159,15 @@ def generate_flake(
cwd=flake, cwd=flake,
check=True, check=True,
) )
sp.run(["git", "init"], cwd=flake, check=True) init_git(monkeypatch, flake)
sp.run(["git", "add", "."], cwd=flake, check=True)
sp.run(["git", "config", "user.name", "clan-tool"], cwd=flake, check=True)
sp.run(["git", "config", "user.email", "clan@example.com"], cwd=flake, check=True)
sp.run(["git", "commit", "-a", "-m", "Initial commit"], cwd=flake, check=True)
return FlakeForTest(flake) return FlakeForTest(flake)
def create_flake( def create_flake(
temporary_home: Path, temporary_home: Path,
flake_template: str | Path, flake_template: str | Path,
monkeypatch: pytest.MonkeyPatch,
clan_core_flake: Path | None = None, clan_core_flake: Path | None = None,
# names referring to pre-defined machines from ../machines # names referring to pre-defined machines from ../machines
machines: list[str] | None = None, machines: list[str] | None = None,
@@ -202,18 +220,7 @@ def create_flake(
f"!! $HOME does not point to a temp directory!! HOME={os.environ['HOME']}" f"!! $HOME does not point to a temp directory!! HOME={os.environ['HOME']}"
) )
# TODO: Find out why test_vms_api.py fails in nix build init_git(monkeypatch, flake)
# but works in pytest when this bottom line is commented out
sp.run(
["git", "config", "--global", "init.defaultBranch", "main"],
cwd=flake,
check=True,
)
sp.run(["git", "init"], cwd=flake, check=True)
sp.run(["git", "add", "."], cwd=flake, check=True)
sp.run(["git", "config", "user.name", "clan-tool"], cwd=flake, check=True)
sp.run(["git", "config", "user.email", "clan@example.com"], cwd=flake, check=True)
sp.run(["git", "commit", "-a", "-m", "Initial commit"], cwd=flake, check=True)
if remote: if remote:
with tempfile.TemporaryDirectory(prefix="flake-"): with tempfile.TemporaryDirectory(prefix="flake-"):
@@ -226,7 +233,11 @@ def create_flake(
def test_flake( def test_flake(
monkeypatch: pytest.MonkeyPatch, temporary_home: Path monkeypatch: pytest.MonkeyPatch, temporary_home: Path
) -> Iterator[FlakeForTest]: ) -> Iterator[FlakeForTest]:
yield from create_flake(temporary_home, "test_flake") yield from create_flake(
temporary_home=temporary_home,
flake_template="test_flake",
monkeypatch=monkeypatch,
)
# check that git diff on ./sops is empty # check that git diff on ./sops is empty
if (temporary_home / "test_flake" / "sops").exists(): if (temporary_home / "test_flake" / "sops").exists():
git_proc = sp.run( git_proc = sp.run(
@@ -248,9 +259,10 @@ def test_flake_with_core(
msg = "clan-core flake not found. This test requires the clan-core flake to be present" msg = "clan-core flake not found. This test requires the clan-core flake to be present"
raise FixtureError(msg) raise FixtureError(msg)
yield from create_flake( yield from create_flake(
temporary_home, temporary_home=temporary_home,
"test_flake_with_core", flake_template="test_flake_with_core",
CLAN_CORE, clan_core_flake=CLAN_CORE,
monkeypatch=monkeypatch,
) )
@@ -280,9 +292,10 @@ def test_flake_with_core_and_pass(
msg = "clan-core flake not found. This test requires the clan-core flake to be present" msg = "clan-core flake not found. This test requires the clan-core flake to be present"
raise FixtureError(msg) raise FixtureError(msg)
yield from create_flake( yield from create_flake(
temporary_home, temporary_home=temporary_home,
"test_flake_with_core_and_pass", flake_template="test_flake_with_core_and_pass",
CLAN_CORE, clan_core_flake=CLAN_CORE,
monkeypatch=monkeypatch,
) )
@@ -294,7 +307,8 @@ def test_flake_minimal(
msg = "clan-core flake not found. This test requires the clan-core flake to be present" msg = "clan-core flake not found. This test requires the clan-core flake to be present"
raise FixtureError(msg) raise FixtureError(msg)
yield from create_flake( yield from create_flake(
temporary_home, temporary_home=temporary_home,
CLAN_CORE / "templates" / "minimal", flake_template=CLAN_CORE / "templates" / "minimal",
CLAN_CORE, monkeypatch=monkeypatch,
clan_core_flake=CLAN_CORE,
) )

View File

@@ -90,6 +90,7 @@ def test_generate_public_var(
flake = generate_flake( flake = generate_flake(
temporary_home, temporary_home,
flake_template=CLAN_CORE / "templates" / "minimal", flake_template=CLAN_CORE / "templates" / "minimal",
monkeypatch=monkeypatch,
machine_configs={"my_machine": config}, machine_configs={"my_machine": config},
) )
monkeypatch.chdir(flake.path) monkeypatch.chdir(flake.path)
@@ -130,6 +131,7 @@ def test_generate_secret_var_sops(
flake = generate_flake( flake = generate_flake(
temporary_home, temporary_home,
flake_template=CLAN_CORE / "templates" / "minimal", flake_template=CLAN_CORE / "templates" / "minimal",
monkeypatch=monkeypatch,
machine_configs={"my_machine": config}, machine_configs={"my_machine": config},
) )
monkeypatch.chdir(flake.path) monkeypatch.chdir(flake.path)
@@ -172,6 +174,7 @@ def test_generate_secret_var_sops_with_default_group(
flake = generate_flake( flake = generate_flake(
temporary_home, temporary_home,
flake_template=CLAN_CORE / "templates" / "minimal", flake_template=CLAN_CORE / "templates" / "minimal",
monkeypatch=monkeypatch,
machine_configs={"my_machine": config}, machine_configs={"my_machine": config},
) )
monkeypatch.chdir(flake.path) monkeypatch.chdir(flake.path)
@@ -211,6 +214,7 @@ def test_generated_shared_secret_sops(
flake = generate_flake( flake = generate_flake(
temporary_home, temporary_home,
flake_template=CLAN_CORE / "templates" / "minimal", flake_template=CLAN_CORE / "templates" / "minimal",
monkeypatch=monkeypatch,
machine_configs={"machine1": m1_config, "machine2": m2_config}, machine_configs={"machine1": m1_config, "machine2": m2_config},
) )
monkeypatch.chdir(flake.path) monkeypatch.chdir(flake.path)
@@ -255,6 +259,7 @@ def test_generate_secret_var_password_store(
flake = generate_flake( flake = generate_flake(
temporary_home, temporary_home,
flake_template=CLAN_CORE / "templates" / "minimal", flake_template=CLAN_CORE / "templates" / "minimal",
monkeypatch=monkeypatch,
machine_configs={"my_machine": config}, machine_configs={"my_machine": config},
) )
monkeypatch.chdir(flake.path) monkeypatch.chdir(flake.path)
@@ -309,6 +314,7 @@ def test_generate_secret_for_multiple_machines(
flake = generate_flake( flake = generate_flake(
temporary_home, temporary_home,
flake_template=CLAN_CORE / "templates" / "minimal", flake_template=CLAN_CORE / "templates" / "minimal",
monkeypatch=monkeypatch,
machine_configs={"machine1": machine1_config, "machine2": machine2_config}, machine_configs={"machine1": machine1_config, "machine2": machine2_config},
) )
monkeypatch.chdir(flake.path) monkeypatch.chdir(flake.path)
@@ -354,6 +360,7 @@ def test_dependant_generators(
flake = generate_flake( flake = generate_flake(
temporary_home, temporary_home,
flake_template=CLAN_CORE / "templates" / "minimal", flake_template=CLAN_CORE / "templates" / "minimal",
monkeypatch=monkeypatch,
machine_configs={"my_machine": config}, machine_configs={"my_machine": config},
) )
monkeypatch.chdir(flake.path) monkeypatch.chdir(flake.path)
@@ -393,6 +400,7 @@ def test_prompt(
flake = generate_flake( flake = generate_flake(
temporary_home, temporary_home,
flake_template=CLAN_CORE / "templates" / "minimal", flake_template=CLAN_CORE / "templates" / "minimal",
monkeypatch=monkeypatch,
machine_configs={"my_machine": config}, machine_configs={"my_machine": config},
) )
monkeypatch.chdir(flake.path) monkeypatch.chdir(flake.path)
@@ -432,6 +440,7 @@ def test_share_flag(
flake = generate_flake( flake = generate_flake(
temporary_home, temporary_home,
flake_template=CLAN_CORE / "templates" / "minimal", flake_template=CLAN_CORE / "templates" / "minimal",
monkeypatch=monkeypatch,
machine_configs={"my_machine": config}, machine_configs={"my_machine": config},
) )
monkeypatch.chdir(flake.path) monkeypatch.chdir(flake.path)
@@ -482,6 +491,7 @@ def test_prompt_create_file(
flake = generate_flake( flake = generate_flake(
temporary_home, temporary_home,
flake_template=CLAN_CORE / "templates" / "minimal", flake_template=CLAN_CORE / "templates" / "minimal",
monkeypatch=monkeypatch,
machine_configs={"my_machine": config}, machine_configs={"my_machine": config},
) )
monkeypatch.chdir(flake.path) monkeypatch.chdir(flake.path)
@@ -510,6 +520,7 @@ def test_api_get_prompts(
flake = generate_flake( flake = generate_flake(
temporary_home, temporary_home,
flake_template=CLAN_CORE / "templates" / "minimal", flake_template=CLAN_CORE / "templates" / "minimal",
monkeypatch=monkeypatch,
machine_configs={"my_machine": config}, machine_configs={"my_machine": config},
) )
monkeypatch.chdir(flake.path) monkeypatch.chdir(flake.path)
@@ -538,6 +549,7 @@ def test_api_set_prompts(
flake = generate_flake( flake = generate_flake(
temporary_home, temporary_home,
flake_template=CLAN_CORE / "templates" / "minimal", flake_template=CLAN_CORE / "templates" / "minimal",
monkeypatch=monkeypatch,
machine_configs={"my_machine": config}, machine_configs={"my_machine": config},
) )
monkeypatch.chdir(flake.path) monkeypatch.chdir(flake.path)
@@ -584,6 +596,7 @@ def test_commit_message(
flake = generate_flake( flake = generate_flake(
temporary_home, temporary_home,
flake_template=CLAN_CORE / "templates" / "minimal", flake_template=CLAN_CORE / "templates" / "minimal",
monkeypatch=monkeypatch,
machine_configs={"my_machine": config}, machine_configs={"my_machine": config},
) )
monkeypatch.chdir(flake.path) monkeypatch.chdir(flake.path)
@@ -643,6 +656,7 @@ def test_default_value(
flake = generate_flake( flake = generate_flake(
temporary_home, temporary_home,
flake_template=CLAN_CORE / "templates" / "minimal", flake_template=CLAN_CORE / "templates" / "minimal",
monkeypatch=monkeypatch,
machine_configs={"my_machine": config}, machine_configs={"my_machine": config},
) )
monkeypatch.chdir(flake.path) monkeypatch.chdir(flake.path)
@@ -688,6 +702,7 @@ def test_stdout_of_generate(
flake = generate_flake( flake = generate_flake(
temporary_home, temporary_home,
flake_template=CLAN_CORE / "templates" / "minimal", flake_template=CLAN_CORE / "templates" / "minimal",
monkeypatch=monkeypatch,
machine_configs={"my_machine": config}, machine_configs={"my_machine": config},
) )
monkeypatch.chdir(flake.path) monkeypatch.chdir(flake.path)
@@ -767,6 +782,7 @@ def test_migration_skip(
flake = generate_flake( flake = generate_flake(
temporary_home, temporary_home,
flake_template=CLAN_CORE / "templates" / "minimal", flake_template=CLAN_CORE / "templates" / "minimal",
monkeypatch=monkeypatch,
machine_configs={"my_machine": config}, machine_configs={"my_machine": config},
) )
monkeypatch.chdir(flake.path) monkeypatch.chdir(flake.path)
@@ -798,6 +814,7 @@ def test_migration(
flake = generate_flake( flake = generate_flake(
temporary_home, temporary_home,
flake_template=CLAN_CORE / "templates" / "minimal", flake_template=CLAN_CORE / "templates" / "minimal",
monkeypatch=monkeypatch,
machine_configs={"my_machine": config}, machine_configs={"my_machine": config},
) )
monkeypatch.chdir(flake.path) monkeypatch.chdir(flake.path)
@@ -832,6 +849,7 @@ def test_fails_when_files_are_left_from_other_backend(
flake = generate_flake( flake = generate_flake(
temporary_home, temporary_home,
flake_template=CLAN_CORE / "templates" / "minimal", flake_template=CLAN_CORE / "templates" / "minimal",
monkeypatch=monkeypatch,
machine_configs={"my_machine": config}, machine_configs={"my_machine": config},
) )
monkeypatch.chdir(flake.path) monkeypatch.chdir(flake.path)

View File

@@ -63,6 +63,7 @@ def test_vm_deployment(
flake = generate_flake( flake = generate_flake(
temporary_home, temporary_home,
flake_template=CLAN_CORE / "templates" / "minimal", flake_template=CLAN_CORE / "templates" / "minimal",
monkeypatch=monkeypatch,
machine_configs={"m1_machine": machine1_config, "m2_machine": machine2_config}, machine_configs={"m1_machine": machine1_config, "m2_machine": machine2_config},
) )

View File

@@ -83,6 +83,7 @@ def test_vm_persistence(
flake = generate_flake( flake = generate_flake(
temporary_home, temporary_home,
flake_template=CLAN_CORE / "templates" / "minimal", flake_template=CLAN_CORE / "templates" / "minimal",
monkeypatch=monkeypatch,
machine_configs=config, machine_configs=config,
) )