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. 5 6import argparse 7import os 8import sys 9import unittest 10from unittest import mock 11 12from parameterized import parameterized 13from subprocess import CompletedProcess 14 15from update_sdk import _GetHostArch 16from update_sdk import GetSDKOverrideGCSPath 17from update_sdk import main as update_sdk_main 18 19sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), 20 'test'))) 21 22from common import SDK_ROOT 23 24 25@mock.patch('platform.machine') 26class TestGetHostArch(unittest.TestCase): 27 @parameterized.expand([('x86_64', 'amd64'), ('AMD64', 'amd64'), 28 ('aarch64', 'arm64')]) 29 def testSupportedArchs(self, mock_machine, arch, expected): 30 mock_machine.return_value = arch 31 self.assertEqual(_GetHostArch(), expected) 32 33 def testUnsupportedArch(self, mock_machine): 34 mock_machine.return_value = 'bad_arch' 35 with self.assertRaises(Exception): 36 _GetHostArch() 37 38 39@mock.patch('builtins.open') 40@mock.patch('os.path.isfile') 41class TestGetSDKOverrideGCSPath(unittest.TestCase): 42 def testDefaultPath(self, mock_isfile, mock_open): 43 mock_isfile.return_value = False 44 45 with mock.patch('os.path.dirname', return_value='./'): 46 GetSDKOverrideGCSPath() 47 48 mock_isfile.assert_called_with('./sdk_override.txt') 49 50 def testRead(self, mock_isfile, mock_open): 51 fake_path = '\n\ngs://fuchsia-artifacts/development/abc123/sdk\n\n' 52 53 mock_isfile.return_value = True 54 mock_open.side_effect = mock.mock_open(read_data=fake_path) 55 56 actual = GetSDKOverrideGCSPath() 57 self.assertEqual(actual, 'gs://fuchsia-artifacts/development/abc123/sdk') 58 59 60@mock.patch('update_sdk._GetHostArch', return_value='amd64') 61@mock.patch('update_sdk.get_host_os', return_value='linux') 62@mock.patch('subprocess.run', 63 return_value=CompletedProcess(args=['/bin'], returncode=0)) 64@mock.patch('os.utime', return_value=None) 65@mock.patch('update_sdk.make_clean_directory') 66@mock.patch('update_sdk.DownloadAndUnpackFromCloudStorage') 67class TestGetTarballPath(unittest.TestCase): 68 69 def setUp(self): 70 os.environ['FUCHSIA_SDK_OVERRIDE'] = 'gs://bucket/sdk' 71 72 def tearDown(self): 73 del os.environ['FUCHSIA_SDK_OVERRIDE'] 74 75 @mock.patch('argparse.ArgumentParser.parse_args', 76 return_value=argparse.Namespace(version='1.1.1.1', 77 verbose=False, 78 file='core')) 79 def testGetTarballPath(self, mock_arg, mock_download, *_): 80 update_sdk_main() 81 mock_download.assert_called_with('gs://bucket/sdk/linux-amd64/core.tar.gz', 82 SDK_ROOT) 83 84 @mock.patch('argparse.ArgumentParser.parse_args', 85 return_value=argparse.Namespace(version='1.1.1.1', 86 verbose=False, 87 file='google')) 88 def testOverrideFile(self, mock_arg, mock_download, *_): 89 update_sdk_main() 90 mock_download.assert_called_with( 91 'gs://bucket/sdk/linux-amd64/google.tar.gz', SDK_ROOT) 92 93 94if __name__ == '__main__': 95 unittest.main() 96