1 #!/usr/bin/env vpython3
2 # Copyright 2022 The Chromium Authors
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
7 from unittest import mock
9 from parameterized import parameterized
11 from update_sdk import _GetHostArch
12 from update_sdk import _GetTarballPath
13 from update_sdk import GetSDKOverrideGCSPath
16 @mock.patch('platform.machine')
17 class TestGetHostArch(unittest.TestCase):
18 @parameterized.expand([('x86_64', 'amd64'), ('AMD64', 'amd64'),
19 ('aarch64', 'arm64')])
20 def testSupportedArchs(self, mock_machine, arch, expected):
21 mock_machine.return_value = arch
22 self.assertEqual(_GetHostArch(), expected)
24 def testUnsupportedArch(self, mock_machine):
25 mock_machine.return_value = 'bad_arch'
26 with self.assertRaises(Exception):
30 @mock.patch('builtins.open')
31 @mock.patch('os.path.isfile')
32 class TestGetSDKOverrideGCSPath(unittest.TestCase):
33 def testFileNotFound(self, mock_isfile, mock_open):
34 mock_isfile.return_value = False
36 actual = GetSDKOverrideGCSPath('this-file-does-not-exist.txt')
37 self.assertIsNone(actual)
39 def testDefaultPath(self, mock_isfile, mock_open):
40 mock_isfile.return_value = False
42 with mock.patch('os.path.dirname', return_value='./'):
43 GetSDKOverrideGCSPath()
45 mock_isfile.assert_called_with('./sdk_override.txt')
47 def testRead(self, mock_isfile, mock_open):
48 fake_path = '\n\ngs://fuchsia-artifacts/development/abc123/sdk\n\n'
50 mock_isfile.return_value = True
51 mock_open.side_effect = mock.mock_open(read_data=fake_path)
53 actual = GetSDKOverrideGCSPath()
54 self.assertEqual(actual, 'gs://fuchsia-artifacts/development/abc123/sdk')
57 @mock.patch('update_sdk._GetHostArch')
58 @mock.patch('update_sdk.get_host_os')
59 class TestGetTarballPath(unittest.TestCase):
60 def testGetTarballPath(self, mock_get_host_os, mock_host_arch):
61 mock_get_host_os.return_value = 'linux'
62 mock_host_arch.return_value = 'amd64'
64 actual = _GetTarballPath('gs://bucket/sdk')
65 self.assertEqual(actual, 'gs://bucket/sdk/linux-amd64/gn.tar.gz')
68 if __name__ == '__main__':