Skip to content

Commit

Permalink
nixos-rebuild-ng: convert side_effects mocks to function
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagokokada committed Dec 16, 2024
1 parent 3c6acbe commit 40042ff
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 64 deletions.
123 changes: 61 additions & 62 deletions pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,18 @@ def test_execute_nix_boot(mock_run: Any, tmp_path: Path) -> None:
nixpkgs_path.mkdir()
config_path = tmp_path / "test"
config_path.touch()
mock_run.side_effect = [
# update_nixpkgs_rev
CompletedProcess([], 0, str(nixpkgs_path)),
CompletedProcess([], 0, "nixpkgs-rev"),
CompletedProcess([], 0),
# nixos_build
CompletedProcess([], 0, str(config_path)),
# set_profile
CompletedProcess([], 0),
# switch_to_configuration
CompletedProcess([], 0),
]

def run_wrapper_side_effect(args, **kwargs): # type: ignore
if args[0] == "nix-instantiate":
return CompletedProcess([], 0, str(nixpkgs_path))
elif args[0] == "git" and "rev-parse" in args:
return CompletedProcess([], 0, "nixpkgs-rev")
elif args[0] == "nix-build":
return CompletedProcess([], 0, str(config_path))
else:
return CompletedProcess([], 0)

mock_run.side_effect = run_wrapper_side_effect

nr.execute(["nixos-rebuild", "boot", "--no-flake", "-vvv", "--fast"])

Expand Down Expand Up @@ -155,14 +155,14 @@ def test_execute_nix_boot(mock_run: Any, tmp_path: Path) -> None:
def test_execute_nix_switch_flake(mock_run: Any, tmp_path: Path) -> None:
config_path = tmp_path / "test"
config_path.touch()
mock_run.side_effect = [
# nixos_build_flake
CompletedProcess([], 0, str(config_path)),
# set_profile
CompletedProcess([], 0),
# switch_to_configuration
CompletedProcess([], 0),
]

def run_wrapper_side_effect(args, **kwargs): # type: ignore
if args[0] == "nix":
return CompletedProcess([], 0, str(config_path))
else:
return CompletedProcess([], 0)

mock_run.side_effect = run_wrapper_side_effect

nr.execute(
[
Expand Down Expand Up @@ -226,16 +226,14 @@ def test_execute_nix_switch_flake_target_host(
) -> None:
config_path = tmp_path / "test"
config_path.touch()
mock_run.side_effect = [
# nixos_build_flake
CompletedProcess([], 0, str(config_path)),
# set_profile
CompletedProcess([], 0),
# copy_closure
CompletedProcess([], 0),
# switch_to_configuration
CompletedProcess([], 0),
]

def run_wrapper_side_effect(args, **kwargs): # type: ignore
if args[0] == "nix":
return CompletedProcess([], 0, str(config_path))
else:
return CompletedProcess([], 0)

mock_run.side_effect = run_wrapper_side_effect

nr.execute(
[
Expand Down Expand Up @@ -317,18 +315,16 @@ def test_execute_nix_switch_flake_build_host(
) -> None:
config_path = tmp_path / "test"
config_path.touch()
mock_run.side_effect = [
# nixos_build_flake
CompletedProcess([], 0, str(config_path)),
CompletedProcess([], 0),
CompletedProcess([], 0, str(config_path)),
# set_profile
CompletedProcess([], 0),
# copy_closure
CompletedProcess([], 0),
# switch_to_configuration
CompletedProcess([], 0),
]

def run_wrapper_side_effect(args, **kwargs): # type: ignore
if args[0] == "nix" and "eval" in args:
return CompletedProcess([], 0, str(config_path))
if args[0] == "ssh" and "nix" in args:
return CompletedProcess([], 0, str(config_path))
else:
return CompletedProcess([], 0)

mock_run.side_effect = run_wrapper_side_effect

nr.execute(
[
Expand Down Expand Up @@ -478,12 +474,14 @@ def test_execute_build(mock_run: Any, tmp_path: Path) -> None:
def test_execute_test_flake(mock_run: Any, tmp_path: Path) -> None:
config_path = tmp_path / "test"
config_path.touch()
mock_run.side_effect = [
# nixos_build_flake
CompletedProcess([], 0, str(config_path)),
# switch_to_configuration
CompletedProcess([], 0),
]

def run_wrapper_side_effect(args, **kwargs): # type: ignore
if args[0] == "nix":
return CompletedProcess([], 0, str(config_path))
else:
return CompletedProcess([], 0)

mock_run.side_effect = run_wrapper_side_effect

nr.execute(
["nixos-rebuild", "test", "--flake", "github:user/repo#hostname", "--fast"]
Expand Down Expand Up @@ -522,20 +520,21 @@ def test_execute_test_rollback(
mock_path_exists: Any,
mock_run: Any,
) -> None:
mock_run.side_effect = [
# rollback_temporary_profile
CompletedProcess(
[],
0,
stdout=textwrap.dedent("""\
2082 2024-11-07 22:58:56
2083 2024-11-07 22:59:41
2084 2024-11-07 23:54:17 (current)
"""),
),
# switch_to_configuration
CompletedProcess([], 0),
]
def run_wrapper_side_effect(args, **kwargs): # type: ignore
if args[0] == "nix-env":
return CompletedProcess(
[],
0,
stdout=textwrap.dedent("""\
2082 2024-11-07 22:58:56
2083 2024-11-07 22:59:41
2084 2024-11-07 23:54:17 (current)
"""),
)
else:
return CompletedProcess([], 0)

mock_run.side_effect = run_wrapper_side_effect

nr.execute(
["nixos-rebuild", "test", "--rollback", "--profile-name", "foo", "--fast"]
Expand Down
6 changes: 4 additions & 2 deletions pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_nix.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import textwrap
import uuid
from pathlib import Path
from subprocess import PIPE, CompletedProcess
from typing import Any
from unittest.mock import ANY, call, patch

import pytest
import uuid

import nixos_rebuild.models as m
import nixos_rebuild.nix as n
Expand Down Expand Up @@ -86,7 +86,9 @@ def run_wrapper_side_effect(args, **kwargs): # type: ignore
elif args[0] == "mktemp":
return CompletedProcess([], 0, stdout=" \n/tmp/tmpdir\n ")
elif args[0] == "nix-store":
return CompletedProcess([], 0, stdout=" \n/tmp/tmpdir/00000000000000000000000000000000\n ")
return CompletedProcess(
[], 0, stdout=" \n/tmp/tmpdir/00000000000000000000000000000000\n "
)
elif args[0] == "readlink":
return CompletedProcess([], 0, stdout=" \n/path/to/config\n ")
else:
Expand Down

0 comments on commit 40042ff

Please sign in to comment.