File size: 4,448 Bytes
06671aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import hashlib
import json
import os
import shutil
import subprocess
import sys
import unittest
from pathlib import Path

SERVICE_ROOT = Path(__file__).resolve().parents[1]
REPO_ROOT = SERVICE_ROOT.parent.parent
BACKEND_ROOT = SERVICE_ROOT.parent
if str(SERVICE_ROOT) not in sys.path:
    sys.path.insert(0, str(SERVICE_ROOT))

from inference.config import manifest_sha256, reset_inference_manifest_cache  # noqa: E402


CANONICAL_MANIFEST = BACKEND_ROOT / "config" / "inference.manifest.json"


def _host_manifest_sha() -> str:
    return hashlib.sha256(CANONICAL_MANIFEST.read_bytes()).hexdigest()


class ContainerManifestTests(unittest.TestCase):
    def setUp(self) -> None:
        reset_inference_manifest_cache()
        self._old_manifest_path = os.environ.pop("INFERENCE_MANIFEST_PATH", None)

    def tearDown(self) -> None:
        reset_inference_manifest_cache()
        if self._old_manifest_path is not None:
            os.environ["INFERENCE_MANIFEST_PATH"] = self._old_manifest_path
        else:
            os.environ.pop("INFERENCE_MANIFEST_PATH", None)

    def test_manifest_endpoint_contract(self) -> None:
        os.environ["INFERENCE_MANIFEST_PATH"] = str(CANONICAL_MANIFEST)
        self.assertEqual(manifest_sha256(), _host_manifest_sha())

    @unittest.skipUnless(shutil.which("docker"), "docker not available")
    def test_running_python_container_loads_same_manifest_sha(self) -> None:
        compose_file = REPO_ROOT / "docker-compose.yml"
        existing_container = subprocess.run(
            [
                "docker",
                "compose",
                "-f",
                str(compose_file),
                "ps",
                "-q",
                "python-service",
            ],
            cwd=REPO_ROOT,
            capture_output=True,
            text=True,
        ).stdout.strip()
        was_running = False
        if existing_container:
            was_running = (
                subprocess.run(
                    [
                        "docker",
                        "inspect",
                        "-f",
                        "{{.State.Running}}",
                        existing_container,
                    ],
                    cwd=REPO_ROOT,
                    capture_output=True,
                    text=True,
                ).stdout.strip()
                == "true"
            )
        config = subprocess.run(
            ["docker", "compose", "-f", str(compose_file), "config"],
            cwd=REPO_ROOT,
            capture_output=True,
            text=True,
            check=True,
        )
        self.assertIn("INFERENCE_MANIFEST_PATH", config.stdout)
        self.assertIn("/config/inference.manifest.json", config.stdout)

        up = subprocess.run(
            [
                "docker",
                "compose",
                "-f",
                str(compose_file),
                "up",
                "-d",
                "python-service",
            ],
            cwd=REPO_ROOT,
            capture_output=True,
            text=True,
        )
        self.assertEqual(up.returncode, 0, up.stderr)

        try:
            exec_result = subprocess.run(
                [
                    "docker",
                    "compose",
                    "-f",
                    str(compose_file),
                    "exec",
                    "-T",
                    "python-service",
                    "python",
                    "-c",
                    "from inference.config import manifest_sha256; print(manifest_sha256())",
                ],
                cwd=REPO_ROOT,
                capture_output=True,
                text=True,
            )
            self.assertEqual(exec_result.returncode, 0, exec_result.stderr)
            container_sha = exec_result.stdout.strip()
            self.assertEqual(container_sha, _host_manifest_sha())
        finally:
            if not was_running:
                subprocess.run(
                    [
                        "docker",
                        "compose",
                        "-f",
                        str(compose_file),
                        "stop",
                        "python-service",
                    ],
                    cwd=REPO_ROOT,
                    capture_output=True,
                    text=True,
                )


if __name__ == "__main__":
    unittest.main()