okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221terminal: strata_cli, strata_control, bin/strata and the terminal tests64a91dcdc

main
2.7 KiB54 linesraw
1"""An owned server attests the bytes and arguments it actually received."""
2
3import json
4import unittest
5from unittest import mock
6
7from serve.launch_identity import attestation
8from strata_control import readiness
9
10
11class LaunchIdentityTests(unittest.TestCase):
12    def test_attestation_changes_with_source_args_and_key(self):
13        key = bytes(range(32))
14        cfg = b'{"api_key":"a private setting","model_name":"fake"}'
15        args = ["--config", "/tmp/run.json", "--parallel", "4"]
16        value = attestation(key, cfg, args)
17        self.assertEqual(len(value), 64)
18        self.assertEqual(value, attestation(key, cfg, args))
19        self.assertNotEqual(value, attestation(key, cfg + b" ", args))
20        self.assertNotEqual(value, attestation(key, cfg, [*args, "--batch-telemetry"]))
21        self.assertNotEqual(value, attestation(bytes(reversed(key)), cfg, args))
22        self.assertNotIn("private", value)
23
24    def test_wrong_or_absent_server_attestation_rejects_readiness_before_generation(self):
25        ready = {"bound_host": "127.0.0.1", "bound_port": 1234,
26                 "model_id": "fake", "model_state": "ready"}
27        cfg = json.dumps({"model_name": "fake"}).encode()
28        for reported in (None, "0" * 64):
29            with self.subTest(reported=reported), mock.patch.object(readiness, "_request") as request:
30                request.return_value = {"instance": {"id": "one", "launch_attestation": reported},
31                                        "model": {"id": "fake", "loaded": True}}
32                with self.assertRaisesRegex(readiness.ReadinessError,
33                                            "probe_launch_attestation_mismatch"):
34                    readiness.verify(ready, cfg, expected_host="127.0.0.1",
35                                     expected_instance="one", expected_attestation="f" * 64,
36                                     timeout=1)
37                self.assertEqual(request.call_count, 1)
38
39    def test_attested_lazy_model_reports_unloaded_without_generation(self):
40        ready = {"bound_host": "127.0.0.1", "bound_port": 1234,
41                 "model_id": "fake", "model_state": "unloaded"}
42        with mock.patch.object(readiness, "_request", return_value={
43                "instance": {"id": "one", "launch_attestation": "f" * 64},
44                "model": {"id": "fake", "loaded": False}}) as request:
45            result = readiness.verify(ready, b'{"model_name":"fake"}',
46                                      expected_host="127.0.0.1", expected_instance="one",
47                                      expected_attestation="f" * 64, timeout=1)
48        self.assertTrue(result["launch_attested"])
49        self.assertFalse(result["generation_ready"])
50        self.assertEqual(request.call_count, 1)
51
52
53if __name__ == "__main__":
54    unittest.main()