Repository navigation
Expand file tree
/
Copy pathtest_artifacts.py
More file actions
68 lines (58 loc) · 3.07 KB
/
Copy pathtest_artifacts.py
File metadata and controls
68 lines (58 loc) · 3.07 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
import importlib.util
import json
from pathlib import Path
import tempfile
import unittest
spec = importlib.util.spec_from_file_location("prepare", Path(__file__).with_name("prepare-artifacts.py"))
prepare = importlib.util.module_from_spec(spec)
spec.loader.exec_module(prepare)
class ArtifactTests(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory()
self.addCleanup(self.temp.cleanup)
self.root = Path(self.temp.name)
(self.root / "installer").mkdir()
(self.root / "os").mkdir()
self.bootstrap = self.root / "production.sh"
self.bootstrap.write_text("\n".join([
"https://cdn.gravitylinux.org/installer/latest",
"https://cdn.gravitylinux.org/installer/installer_data.json",
"https://cdn.gravitylinux.org/installer",
]))
(self.root / "installer/latest").write_text("v1\n")
(self.root / "installer/installer-v1.tar.gz").write_bytes(b"fixture")
(self.root / "os/boot.bin").write_bytes(b"stage2")
(self.root / "os/gravity-alpha1.zip").write_bytes(b"image")
self.metadata = self.root / "installer/installer_data.json"
self.metadata.write_text(json.dumps({"os_list": [{"package": "gravity-alpha1.zip"}]}))
def test_only_explicit_artifacts(self):
(self.root / "secret.key").write_text("secret")
result = prepare.generate(self.root, self.bootstrap)
self.assertEqual(set(result), {"index.sh", "installer/latest", "installer/installer_data.json",
"installer/installer-v1.tar.gz", "os/boot.bin", "os/gravity-alpha1.zip"})
self.assertEqual(result["os/gravity-alpha1.zip"]["sha256"],
"6105d6cc76af400325e94d588ce511be5bfdbb73b437dc51eca43917d7a43e3d")
def test_reject_lan_bootstrap(self):
with self.bootstrap.open("a") as stream:
stream.write("\nexport IPSW_BASE=https://cdn.gravitylinux.org/ipsw")
with self.assertRaises(ValueError):
prepare.generate(self.root, self.bootstrap)
def test_reject_bad_names(self):
for name in ("../escape.zip", "https://elsewhere/image.zip", "gravity-test.zip"):
with self.subTest(name=name):
self.metadata.write_text(json.dumps({"os_list": [{"package": name}]}))
with self.assertRaises(ValueError):
prepare.generate(self.root, self.bootstrap)
def test_missing_payload(self):
(self.root / "os/boot.bin").unlink()
with self.assertRaises(ValueError):
prepare.generate(self.root, self.bootstrap)
def test_retain_releases_and_reject_overwrite(self):
old = prepare.generate(self.root, self.bootstrap)
old["os/older.zip"] = dict(old["os/gravity-alpha1.zip"])
self.assertIn("os/older.zip", prepare.generate(self.root, self.bootstrap, old))
(self.root / "os/gravity-alpha1.zip").write_bytes(b"different")
with self.assertRaises(ValueError):
prepare.generate(self.root, self.bootstrap, old)
if __name__ == "__main__":
unittest.main()