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.
11 from unittest import mock
13 import update_product_bundles
15 sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__),
21 class TestUpdateProductBundles(unittest.TestCase):
23 ffx_mock = mock.Mock()
24 ffx_mock.returncode = 0
25 self._ffx_patcher = mock.patch('common.run_ffx_command',
26 return_value=ffx_mock)
27 self._ffx_mock = self._ffx_patcher.start()
28 self.addCleanup(self._ffx_mock.stop)
30 def testConvertToProductBundleDefaultsUnknownImage(self):
32 update_product_bundles.convert_to_products(['unknown-image']),
35 def testConvertToProductBundleWarnsDeprecated(self):
36 with self.assertLogs(level='WARNING') as logs:
38 'qemu.arm64', 'qemu.x64', 'core.x64-dfv2-release',
39 'workstation_eng.chromebook-x64-release'
42 update_product_bundles.convert_to_products(deprecated_images), [
43 'terminal.qemu-arm64', 'terminal.x64', 'core.x64-dfv2',
44 'workstation_eng.chromebook-x64'
46 for i, deprecated_image in enumerate(deprecated_images):
47 self.assertIn(f'Image name {deprecated_image} has been deprecated',
51 @mock.patch('common.run_ffx_command')
52 def testRemoveRepositoriesRunsRemoveOnGivenRepos(self, ffx_mock):
53 update_product_bundles.remove_repositories(['foo', 'bar', 'fizz', 'buzz'])
55 ffx_mock.assert_has_calls([
56 mock.call(cmd=('repository', 'remove', 'foo'), check=True),
57 mock.call(cmd=('repository', 'remove', 'bar'), check=True),
58 mock.call(cmd=('repository', 'remove', 'fizz'), check=True),
59 mock.call(cmd=('repository', 'remove', 'buzz'), check=True),
62 @mock.patch('os.path.exists')
63 @mock.patch('os.path.abspath')
64 def testGetRepositoriesPrunesReposThatDoNotExist(self, mock_abspath,
66 with mock.patch('common.SDK_ROOT', 'some/path'):
67 self._ffx_mock.return_value.stdout = json.dumps([{
68 "name": "terminal.x64",
71 "path": "some/path/that/exists"
74 "name": "workstation-eng.chromebook-x64",
77 "path": "some/path/that/does/not/exist"
80 mock_exists.side_effect = [True, False]
81 mock_abspath.side_effect = lambda x: x
83 self.assertEqual(update_product_bundles.get_repositories(), [{
84 "name": "terminal.x64",
87 "path": "some/path/that/exists"
91 self._ffx_mock.assert_has_calls([
92 mock.call(cmd=('--machine', 'json', 'repository', 'list'),
95 mock.call(cmd=('repository', 'remove',
96 'workstation-eng.chromebook-x64'),
100 @mock.patch('common.get_hash_from_sdk', return_value='abc')
101 # Disallow reading sdk_override.
102 @mock.patch('os.path.isfile', return_value=False)
103 def testLookupAndDownloadWithAuth(self, get_hash_mock, isfile_mock):
107 # Ignore unsupported platforms. common.get_host_os used in
108 # update_product_bundles.main throws an unsupported exception.
110 auth_file = os.path.abspath(
111 os.path.join(os.path.dirname(__file__), 'get_auth_token.py'))
112 self._ffx_mock.return_value.stdout = 'http://download-url'
113 with mock.patch('sys.argv',
114 ['update_product_bundles.py', 'terminal.x64', '--auth']):
115 update_product_bundles.main()
116 self._ffx_mock.assert_has_calls([
118 'product', 'lookup', 'terminal.x64', 'abc', '--base-url',
119 'gs://fuchsia/development/abc', '--auth', auth_file
124 'product', 'download', 'http://download-url',
125 os.path.join(common.IMAGES_ROOT, 'terminal', 'x64'), '--auth',
132 if __name__ == '__main__':