1#!/usr/bin/env python3
2import importlib.util
3import json
4from pathlib import Path
5import sys
6import subprocess
7import os
8import socket
9import struct
10import threading
11import unittest
12from unittest.mock import patch
13
14
15spec = importlib.util.spec_from_file_location("dashboard_host", Path(__file__).with_name("dashboard-host.py"))
16host_module = importlib.util.module_from_spec(spec)
17spec.loader.exec_module(host_module)
18
19
20class Boundary(unittest.TestCase):
21 def setUp(self):
22 self.host = host_module.Host("snowglobe-demo")
23
24 @unittest.skipUnless(sys.platform == "linux", "Linux Unix peer credentials")
25 def test_large_json_header_preserves_stream_bytes_and_enforces_limit(self):
26 def exchange(payload, expected=None):
27 client, server = socket.socketpair()
28 client.settimeout(5)
29 handler = unittest.mock.Mock()
30 handler.handle.return_value = True
31 def stream_console(connection, host, request):
32 self.assertEqual(connection.recv(len(expected)), expected)
33 host_module.send_response(connection, {"value": True})
34 with patch.object(host_module, "stream_console", side_effect=stream_console):
35 worker = threading.Thread(target=host_module.serve_connection, args=(server, handler, os.getuid()), daemon=True)
36 worker.start()
37 with client:
38 client.sendall(payload)
39 length = struct.unpack('>I', client.recv(4))[0]
40 response = bytearray()
41 while len(response) < length:
42 response.extend(client.recv(length - len(response)))
43 worker.join(timeout=5)
44 self.assertFalse(worker.is_alive())
45 return json.loads(response), handler
46 message = {"operation": "shale.write", "arguments": {"comment": "\x01" * 240000}}
47 response, handler = exchange(json.dumps(message).encode() + b"\n")
48 self.assertEqual(response, {"value": True})
49 handler.handle.assert_called_once_with(message)
50 tail = b"stream bytes\nnot JSON"
51 response, handler = exchange(b'{"operation":"vm.serial"}\n' + tail, tail)
52 self.assertEqual(response, {"value": True})
53 handler.handle.assert_not_called()
54 response, handler = exchange(b'x' * (host_module.MAX_REQUEST + 1))
55 self.assertEqual(response['status'], 400)
56 handler.handle.assert_not_called()
57
58 def test_rejects_before_executing(self):
59 requests = [None, [], {}, {"operation": []},
60 {"operation": "command", "argv": ["sh", "-c", "id"]},
61 {"operation": "storage.datasets", "pool": "other"},
62 {"operation": "storage.datasets", "env": {"PATH": "/tmp"}},
63 {"operation": "host.sample", "path": "/root/private"},
64 {"operation": "host.usage", "refresh": True, "pid": 1},
65 {"operation": "deploy.managed", "path": "/var/lib/snowglobe/nomad.token"},
66 {"operation": "storage.snapshots"}]
67 for refresh in [None, [], {}, 0, 1, "true"]:
68 requests.append({"operation": "host.usage", "refresh": refresh})
69 for dataset in [None, [], "other", "snowglobe-demo-other", "snowglobe-demo/../other",
70 "snowglobe-demo//child", "snowglobe-demo/child\nother", "-r", "snowglobe-demo/a@snap"]:
71 requests.append({"operation": "storage.snapshots", "dataset": dataset})
72 for snapshot in [None, [], "a%b", "a,b", "a@b", "a\nb", "../other", "a;id"]:
73 requests.append({"operation": "storage.reclaim", "dataset": "snowglobe-demo/a",
74 "from": snapshot, "to": "safe"})
75 with patch.object(host_module, "command") as command:
76 for request in requests:
77 with self.subTest(request=request), self.assertRaises(host_module.Rejected):
78 self.host.handle(request)
79 command.assert_not_called()
80
81 def test_share_management_requires_only_its_declared_context(self):
82 request = {"operation": "copyparty.manage", "action": "state", "payload": {}, "users": []}
83 with patch.object(host_module.copyparty, "handle", return_value={"links": []}) as handle:
84 self.assertEqual(self.host.handle(request), {"links": []})
85 handle.assert_called_once_with(request)
86 for changed in [
87 {key: value for key, value in request.items() if key != "users"},
88 {**request, "root": "/srv/private"},
89 {**request, "env": {"NOMAD_TOKEN": "test"}},
90 ]:
91 with self.assertRaises(host_module.Rejected):
92 self.host.handle(changed)
93 self.assertEqual(handle.call_count, 1)
94
95 def test_reclaim_is_always_a_dry_run(self):
96 with patch.object(host_module, "command", return_value="reclaim\t1024") as command:
97 self.host.handle({"operation": "storage.reclaim", "dataset": "snowglobe-demo/a",
98 "from": "snapshot one", "to": "snapshot two"})
99 command.assert_called_once_with("zfs", "destroy", "-nvp",
100 "snowglobe-demo/a@snapshot one%snapshot two")
101
102 def test_mounts_exclude_other_pools(self):
103 mounts = {"filesystems": [{"source": source, "target": "/srv"}
104 for source in ["snowglobe-demo", "snowglobe-demo/a", "snowglobe-demo-other/a", "other/a", "snowglobe-demo/a@dash-123"]]}
105 with patch.object(host_module, "command", return_value=json.dumps(mounts)):
106 self.assertEqual([m["source"] for m in self.host.handle({"operation": "storage.mounts"})["filesystems"]],
107 ["snowglobe-demo", "snowglobe-demo/a"])
108
109 def test_snapshot_arguments_cannot_be_options(self):
110 with patch.object(host_module, "command", return_value="") as command:
111 self.host.handle({"operation": "storage.removed", "dataset": "snowglobe-demo/a", "snapshot": "snapshot -r"})
112 command.assert_called_once_with("zfs", "diff", "-H", "snowglobe-demo/a@snapshot -r", "snowglobe-demo/a")
113
114 def test_snapshot_mutations_are_scoped(self):
115 requests = []
116 for datasets in [None, {}, [], [None], ["other/private"], ["snowglobe-demo/../escape"], ["-r"]]:
117 requests.append({"operation": "files.snapshot", "datasets": datasets})
118 for snapshot in ["before", "dash-1%after", "dash-1,after", "dash-../other", "dash--r"]:
119 requests.append({"operation": "files.discard", "dataset": "snowglobe-demo/a", "snapshot": snapshot})
120 for value in ["before%after", "-r", "before,after", "before@after"]:
121 requests.append({"operation": "storage.destroy", "dataset": "snowglobe-demo/a", "from": value, "to": "after"})
122 requests.append({"operation": "files.snapshot", "datasets": ["snowglobe-demo/a"], "recursive": True})
123 with patch.object(host_module, "command") as command:
124 for request in requests:
125 with self.subTest(request=request), self.assertRaises(host_module.Rejected):
126 self.host.handle(request)
127 command.assert_not_called()
128
129 def test_file_snapshots_use_only_the_undo_namespace(self):
130 with patch.object(host_module.time, "time_ns", return_value=123), patch.object(host_module, "command", return_value="") as command:
131 name = self.host.handle({"operation": "files.snapshot", "datasets": ["snowglobe-demo/b", "snowglobe-demo/a", "snowglobe-demo/a"]})
132 self.assertEqual(name, "dash-123")
133 command.assert_called_once_with("zfs", "snapshot", "snowglobe-demo/a@dash-123", "snowglobe-demo/b@dash-123")
134 with patch.object(host_module, "command", return_value="snowglobe-demo/a@before\nsnowglobe-demo/a@dash-123\nsnowglobe-demo/a@dash-manual\n"):
135 self.assertEqual(self.host.handle({"operation": "files.snapshots", "datasets": ["snowglobe-demo/a"]}), ["snowglobe-demo/a@dash-123"])
136 with patch.object(host_module, "command", return_value="") as command:
137 self.host.handle({"operation": "files.discard", "dataset": "snowglobe-demo/a", "snapshot": "dash-123"})
138 command.assert_called_once_with("zfs", "destroy", "snowglobe-demo/a@dash-123")
139
140 def test_child_output_is_bounded(self):
141 with patch.object(host_module, "MAX_RESPONSE", 4096):
142 with self.assertRaisesRegex(RuntimeError, "too large"):
143 host_module.command(sys.executable, "-c", "import os; os.write(2, b'x' * 1000000)")
144
145 def test_index_operations_preserve_other_snapshots(self):
146 dataset = "snowglobe-demo/a"
147 with patch.object(host_module, "command") as command:
148 for name in ["manual", "dash-123", "index-fixture", "index-123%manual", "index-../escape"]:
149 for request in [{"operation": "index.discard", "dataset": dataset, "snapshot": name},
150 {"operation": "index.diff", "dataset": dataset, "from": name, "to": "index-456"}]:
151 with self.subTest(request=request), self.assertRaises(host_module.Rejected):
152 self.host.handle(request)
153 command.assert_not_called()
154 with patch.object(host_module.time, "time_ns", return_value=123), patch.object(host_module, "command", return_value="") as command:
155 self.assertEqual(self.host.handle({"operation": "index.snapshot", "dataset": dataset}), dataset + "@index-123")
156 command.assert_called_once_with("zfs", "snapshot", dataset + "@index-123")
157 legacy = "index-123-12345678-1234-1234-1234-123456789abc"
158 with patch.object(host_module, "command", return_value="\n".join(dataset + "@" + name for name in ["manual", "index-123", legacy, "index-fixture"])):
159 self.assertEqual(self.host.handle({"operation": "index.snapshots", "dataset": dataset}),
160 [dataset + "@index-123", dataset + "@" + legacy])
161 with patch.object(host_module, "command", return_value="") as command:
162 self.host.handle({"operation": "index.diff", "dataset": dataset, "from": legacy, "to": "index-456"})
163 command.assert_called_once_with("zfs", "diff", "-FH", dataset + "@" + legacy, dataset + "@index-456")
164 with patch.object(host_module, "command", return_value="") as command:
165 self.host.handle({"operation": "index.discard", "dataset": dataset, "snapshot": legacy})
166 command.assert_called_once_with("zfs", "destroy", "-d", dataset + "@" + legacy)
167
168 def test_nonzero_exit_is_reported(self):
169 with self.assertRaises(subprocess.CalledProcessError) as failed:
170 host_module.command(sys.executable, "-c", "import sys; sys.stderr.write('fixture problem'); sys.exit(1)")
171 self.assertEqual(failed.exception.stderr, "fixture problem")
172
173 def test_child_deadline_is_enforced(self):
174 with self.assertRaises(TimeoutError):
175 host_module.command(sys.executable, "-c", "import time; time.sleep(10)", timeout=.05)
176
177 def test_vm_requests_reject_before_executing(self):
178 spec = {"mode": "iso", "firmware": "bios", "platform": "linux", "name": "fixture", "owner": "fixture-user", "description": "", "image": "blank", "vcpus": 1,
179 "memory": 2**29, "disk": 2**30, "autostart": False, "start": False}
180 with patch.object(host_module.vms, "node", return_value={"cpus": 8, "memory": 8 * 2**30}), patch.object(host_module, "command") as command:
181 for field, value in [("name", "../escape"), ("image", "/root/disk.img"), ("vcpus", True),
182 ("memory", 16 * 2**30), ("disk", 2**64), ("vcpus", 9), ("argv", ["sh"])]:
183 with self.subTest(field=field), self.assertRaises(host_module.Rejected):
184 self.host.handle({"operation": "vm.create", "payload": {**spec, field: value}})
185 for request in [{"operation": "vm.xml", "payload": {}}, {"operation": "vm.node", "payload": {}},
186 {"operation": "vm.act", "payload": {"name": "fixture", "action": []}},
187 {"operation": "vm.update", "payload": {"name": "fixture"}}]:
188 with self.assertRaises(host_module.Rejected):
189 self.host.handle(request)
190 command.assert_not_called()
191
192
193if __name__ == "__main__":
194 unittest.main()