-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_nixprovider.py
More file actions
254 lines (220 loc) · 10.2 KB
/
test_nixprovider.py
File metadata and controls
254 lines (220 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import json
import tempfile
from pathlib import Path
import logging
from typing import cast
import pytest
from abxpkg import Binary, NixProvider, SemVer
from abxpkg.binprovider import BinProvider
class TestNixProvider:
def test_install_root_alias_installs_into_the_requested_profile(self, test_machine):
assert NixProvider().INSTALLER_BINARY(), "nix is required on this host"
with tempfile.TemporaryDirectory() as temp_dir:
install_root = Path(temp_dir) / "nix-profile"
provider = NixProvider.model_validate(
{
"install_root": install_root,
"postinstall_scripts": True,
"min_release_age": 0,
"install_timeout": 300,
},
)
installed = provider.install("hello")
test_machine.assert_shallow_binary_loaded(installed)
assert installed is not None
assert installed.loaded_abspath is not None
assert provider.install_root == install_root
assert provider.bin_dir == install_root / "bin"
assert installed.loaded_abspath.parent == provider.bin_dir
def test_provider_direct_methods_exercise_real_lifecycle(self, test_machine):
assert NixProvider().INSTALLER_BINARY(), "nix is required on this host"
with tempfile.TemporaryDirectory() as temp_dir:
provider = NixProvider(
install_root=Path(temp_dir) / "nix-profile",
postinstall_scripts=True,
min_release_age=0,
)
test_machine.exercise_provider_lifecycle(provider, bin_name="hello")
def test_repeated_install_does_not_duplicate_profile_entries(self, test_machine):
assert NixProvider().INSTALLER_BINARY(), "nix is required on this host"
with tempfile.TemporaryDirectory() as temp_dir:
provider = NixProvider(
install_root=Path(temp_dir) / "nix-profile",
postinstall_scripts=True,
min_release_age=0,
)
first = provider.install("hello", no_cache=True)
second = provider.install("hello", no_cache=True)
test_machine.assert_shallow_binary_loaded(first)
test_machine.assert_shallow_binary_loaded(second)
installer_bin = provider.INSTALLER_BINARY(no_cache=True).loaded_abspath
assert installer_bin is not None
proc = provider.exec(
bin_name=installer_bin,
cmd=[
"profile",
"list",
"--json",
"--extra-experimental-features",
"nix-command",
"--extra-experimental-features",
"flakes",
"--profile",
str(provider.install_root),
],
quiet=True,
)
assert proc.returncode == 0, proc.stderr or proc.stdout
assert set(json.loads(proc.stdout).get("elements", {})) == {"hello"}
def test_provider_direct_min_version_revalidates_final_installed_package(
self,
test_machine,
):
assert NixProvider().INSTALLER_BINARY(), "nix is required on this host"
with tempfile.TemporaryDirectory() as temp_dir:
provider = NixProvider(
install_root=Path(temp_dir) / "nix-profile",
postinstall_scripts=True,
min_release_age=0,
)
with pytest.raises(ValueError):
provider.install("hello", min_version=SemVer("999.0.0"))
loaded = provider.load("hello", quiet=True, no_cache=True)
test_machine.assert_shallow_binary_loaded(loaded)
assert loaded is not None
assert loaded.loaded_version is not None
required_version = SemVer.parse("999.0.0")
assert required_version is not None
assert loaded.loaded_version < required_version
def test_nix_profile_bin_dir_takes_precedence_over_existing_PATH_entries(
self,
test_machine,
):
assert NixProvider().INSTALLER_BINARY(), "nix is required on this host"
with tempfile.TemporaryDirectory() as temp_dir:
temp_dir_path = Path(temp_dir)
ambient_provider = NixProvider(
install_root=temp_dir_path / "ambient-profile",
postinstall_scripts=True,
min_release_age=0,
)
ambient_installed = ambient_provider.install("hello")
assert ambient_installed is not None
nix_profile = temp_dir_path / "nix-profile"
provider = NixProvider(
PATH=f"{ambient_provider.bin_dir}:{NixProvider().PATH}",
install_root=nix_profile,
postinstall_scripts=True,
min_release_age=0,
)
installed = provider.install("hello")
test_machine.assert_shallow_binary_loaded(installed)
assert installed is not None
assert installed.loaded_abspath is not None
assert provider.install_root == nix_profile
assert provider.bin_dir == nix_profile / "bin"
assert installed.loaded_abspath.parent == provider.bin_dir
assert ambient_installed.loaded_abspath is not None
assert ambient_installed.loaded_abspath.parent == ambient_provider.bin_dir
def test_uninstall_preserves_other_profile_entries(self, test_machine):
assert NixProvider().INSTALLER_BINARY(), "nix is required on this host"
with tempfile.TemporaryDirectory() as temp_dir:
provider = NixProvider(
install_root=Path(temp_dir) / "nix-profile",
postinstall_scripts=True,
min_release_age=0,
).get_provider_with_overrides(
overrides={
"hello": {"install_args": ["hello", "figlet"]},
},
)
installed = provider.install("hello")
test_machine.assert_shallow_binary_loaded(installed)
assert provider.load("figlet", quiet=True, no_cache=True) is not None
assert provider.uninstall("hello")
assert provider.load("hello", quiet=True, no_cache=True) is None
assert provider.load("figlet", quiet=True, no_cache=True) is not None
def test_unsupported_security_controls_warn_and_continue(
self,
test_machine,
caplog,
):
assert NixProvider().INSTALLER_BINARY(), "nix is required on this host"
with tempfile.TemporaryDirectory() as temp_dir:
with caplog.at_level(logging.WARNING, logger="abxpkg.binprovider"):
installed = NixProvider(
install_root=Path(temp_dir) / "bad-profile",
postinstall_scripts=False,
min_release_age=1,
).install("hello")
test_machine.assert_shallow_binary_loaded(installed)
assert "ignoring unsupported min_release_age=1" in caplog.text
assert "ignoring unsupported postinstall_scripts=False" in caplog.text
caplog.clear()
binary = Binary(
name="hello",
binproviders=cast(
list[BinProvider],
[
NixProvider(
install_root=Path(temp_dir) / "ok-profile",
postinstall_scripts=False,
min_release_age=1,
),
],
),
postinstall_scripts=False,
min_release_age=1,
)
with caplog.at_level(logging.WARNING, logger="abxpkg.binprovider"):
installed = binary.install()
test_machine.assert_shallow_binary_loaded(installed)
assert "ignoring unsupported min_release_age=1" in caplog.text
assert "ignoring unsupported postinstall_scripts=False" in caplog.text
def test_binary_direct_methods_exercise_real_lifecycle(self, test_machine):
assert NixProvider().INSTALLER_BINARY(), "nix is required on this host"
with tempfile.TemporaryDirectory() as temp_dir:
binary = Binary(
name="hello",
binproviders=cast(
list[BinProvider],
[
NixProvider(
install_root=Path(temp_dir) / "nix-profile",
postinstall_scripts=True,
min_release_age=0,
),
],
),
postinstall_scripts=True,
min_release_age=0,
)
test_machine.exercise_binary_lifecycle(binary)
def test_provider_dry_run_does_not_install_hello(self, test_machine):
assert NixProvider().INSTALLER_BINARY(), "nix is required on this host"
with tempfile.TemporaryDirectory() as temp_dir:
provider = NixProvider(
install_root=Path(temp_dir) / "nix-profile",
postinstall_scripts=True,
min_release_age=0,
)
test_machine.exercise_provider_dry_run(provider, bin_name="hello")
def test_search_finds_real_nixpkgs_attr_and_install_works(self, test_machine):
assert NixProvider().INSTALLER_BINARY(), "nix is required on this host"
with tempfile.TemporaryDirectory() as temp_dir:
provider = NixProvider(
install_root=Path(temp_dir) / "nix-profile",
postinstall_scripts=True,
min_release_age=0,
)
results = provider.search("hello")
assert results, "nix search nixpkgs hello should return matches"
names = [r.name for r in results]
assert "hello" in names
match = next(r for r in results if r.name == "hello")
assert match.overrides == {"nix": {"install_args": ["nixpkgs#hello"]}}
assert match.loaded_abspath is None
assert match.loaded_version is None
installed = match.install()
test_machine.assert_shallow_binary_loaded(installed)
assert installed.name == "hello"