|
| 1 | +# -------------------------------------------------------------------------------------------- |
| 2 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +# Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | +# -------------------------------------------------------------------------------------------- |
| 5 | + |
| 6 | +import subprocess |
| 7 | +import unittest |
| 8 | +from pathlib import Path |
| 9 | +from unittest import mock |
| 10 | + |
| 11 | +from azext_confcom import cose_proxy, rootfs_proxy |
| 12 | +from azext_confcom.lib import images |
| 13 | + |
| 14 | + |
| 15 | +class TestDarwinBinarySelection(unittest.TestCase): |
| 16 | + def test_dmverity_binary_selection(self): |
| 17 | + for machine, expected in ( |
| 18 | + ("arm64", "dmverity-vhd-darwin-arm64"), |
| 19 | + ("x86_64", "dmverity-vhd-darwin-amd64"), |
| 20 | + ("amd64", "dmverity-vhd-darwin-amd64"), |
| 21 | + ): |
| 22 | + with self.subTest(machine=machine): |
| 23 | + with mock.patch.object(rootfs_proxy, "host_os", "Darwin"), \ |
| 24 | + mock.patch.object(rootfs_proxy, "machine", machine), \ |
| 25 | + mock.patch.object(rootfs_proxy.os.path, "exists", return_value=True), \ |
| 26 | + mock.patch.object(rootfs_proxy.os, "access", return_value=True): |
| 27 | + self.assertEqual( |
| 28 | + Path(rootfs_proxy.SecurityPolicyProxy().policy_bin).name, |
| 29 | + expected, |
| 30 | + ) |
| 31 | + |
| 32 | + def test_cose_binary_selection(self): |
| 33 | + for machine, expected in ( |
| 34 | + ("arm64", "sign1util-darwin-arm64"), |
| 35 | + ("x86_64", "sign1util-darwin-amd64"), |
| 36 | + ("amd64", "sign1util-darwin-amd64"), |
| 37 | + ): |
| 38 | + with self.subTest(machine=machine): |
| 39 | + with mock.patch.object(cose_proxy, "host_os", "Darwin"), \ |
| 40 | + mock.patch.object(cose_proxy, "machine", machine), \ |
| 41 | + mock.patch.object(cose_proxy.os.path, "exists", return_value=True), \ |
| 42 | + mock.patch.object(cose_proxy.os, "access", return_value=True): |
| 43 | + self.assertEqual( |
| 44 | + Path(cose_proxy.CoseSignToolProxy().policy_bin).name, |
| 45 | + expected, |
| 46 | + ) |
| 47 | + |
| 48 | + def test_unsupported_dmverity_architecture(self): |
| 49 | + with mock.patch.object(rootfs_proxy, "host_os", "Darwin"), \ |
| 50 | + mock.patch.object(rootfs_proxy, "machine", "powerpc64"): |
| 51 | + with self.assertRaises(SystemExit) as exception: |
| 52 | + rootfs_proxy.get_dmverity_vhd_path() |
| 53 | + self.assertEqual(exception.exception.code, 1) |
| 54 | + |
| 55 | + def test_unsupported_cose_architecture(self): |
| 56 | + with mock.patch.object(cose_proxy, "host_os", "Darwin"), \ |
| 57 | + mock.patch.object(cose_proxy, "machine", "powerpc64"): |
| 58 | + with self.assertRaises(SystemExit) as exception: |
| 59 | + cose_proxy.CoseSignToolProxy() |
| 60 | + self.assertEqual(exception.exception.code, 1) |
| 61 | + |
| 62 | + @mock.patch.object(images, "get_image") |
| 63 | + @mock.patch.object(images.subprocess, "run") |
| 64 | + @mock.patch.object(images, "get_dmverity_vhd_path") |
| 65 | + def test_image_layer_hashing_uses_host_binary(self, get_binary_path, run, get_image): |
| 66 | + binary_path = Path("/tmp/dmverity-vhd-darwin-arm64") |
| 67 | + get_binary_path.return_value = binary_path |
| 68 | + run.return_value = subprocess.CompletedProcess( |
| 69 | + args=[], |
| 70 | + returncode=0, |
| 71 | + stdout="Layer 0 root hash: abc123\n", |
| 72 | + ) |
| 73 | + |
| 74 | + self.assertEqual(images.get_image_layers("image:tag"), ["abc123"]) |
| 75 | + self.assertEqual(run.call_args[0][0][0], binary_path.as_posix()) |
| 76 | + get_image.assert_called_once_with("image:tag") |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == "__main__": |
| 80 | + unittest.main() |
0 commit comments