Repository navigation
Expand file tree
/
Copy pathtest_nixprovider.py
More file actions
364 lines (320 loc) 路 13.9 KB
/
Copy pathtest_nixprovider.py
File metadata and controls
364 lines (320 loc) 路 13.9 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
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_nix import (
NIX_INSTALLER_VERSION,
nix_installer_artifact,
)
from abxpkg.binprovider import BinProvider
class TestNixProvider:
@pytest.mark.parametrize(
("system", "machine", "artifact_name", "sha256"),
(
(
"linux",
"x86_64",
"nix-installer-x86_64-linux",
"eecf66f62b044f40b0632d8e9ea72ffc3bd3214357d09317117febff748e71b3",
),
(
"darwin",
"arm64",
"nix-installer-aarch64-darwin",
"f22909a4a816710dddd813bb5ad5958bb2ed100549c9d0edb535bfee8d252e48",
),
),
)
def test_supported_ci_targets_select_pinned_nix_installer_artifact(
self,
system,
machine,
artifact_name,
sha256,
):
artifact = nix_installer_artifact(system=system, machine=machine)
assert artifact.name == artifact_name
assert artifact.sha256 == sha256
assert artifact.url == (
"https://github.com/DeterminateSystems/nix-installer/releases/download/"
f"v{NIX_INSTALLER_VERSION}/{artifact_name}"
)
def test_unsupported_nix_installer_target_fails_explicitly(self):
with pytest.raises(
RuntimeError,
match=(
"does not support plan9/mips64; supported targets are "
"linux/x86_64 and darwin/arm64"
),
):
nix_installer_artifact(system="plan9", machine="mips64")
def test_managed_nix_installer_artifact_is_verified_and_executable(self, tmp_path):
provider = NixProvider(install_root=tmp_path / "nix-profile")
artifact = nix_installer_artifact()
installer_path = provider.download_nix_installer()
assert installer_path.is_file()
assert (
provider.get_sha256(
"nix-installer",
abspath=installer_path,
no_cache=True,
)
== artifact.sha256
)
proc = provider.exec(
bin_name=installer_path,
cmd=["--version"],
quiet=True,
)
assert proc.returncode == 0, proc.stderr or proc.stdout
assert proc.stdout.strip() == f"nix-installer {NIX_INSTALLER_VERSION}"
installer_path.write_bytes(b"corrupt cached installer")
repaired_path = provider.download_nix_installer()
assert repaired_path == installer_path
assert (
provider.get_sha256(
"nix-installer",
abspath=repaired_path,
no_cache=True,
)
== artifact.sha256
)
def test_nix_installer_command_is_noninteractive_and_disables_diagnostics(self):
assert NixProvider.nix_installer_install_command() == (
"install",
"--no-confirm",
"--diagnostic-endpoint",
"",
)
def test_installer_binary_is_projected_through_env_bin(self, test_machine):
provider = NixProvider()
installer = provider.INSTALLER_BINARY(no_cache=True)
test_machine.assert_shallow_binary_loaded(
installer,
assert_version_command=True,
)
assert installer.loaded_binprovider.name == "env"
assert installer.loaded_abspath is not None
assert installer.loaded_abspath.parent.name == "bin"
assert installer.loaded_abspath.parent.parent.name == "env"
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 == Path(temp_dir).resolve() / "nix-profile"
assert provider.bin_dir == Path(temp_dir).resolve() / "nix-profile" / "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=3,
)
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=3,
)
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=3,
)
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=3,
)
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=3,
)
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 == temp_dir_path.resolve() / "nix-profile"
assert provider.bin_dir == temp_dir_path.resolve() / "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=3,
).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=3,
),
],
),
postinstall_scripts=True,
min_release_age=3,
)
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=3,
)
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=3,
)
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": [
"https://channels.nixos.org/nixpkgs-unstable/"
"nixexprs.tar.xz#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"