• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 unittest
7from unittest import mock
8
9from parameterized import parameterized
10
11from update_sdk import _GetHostArch
12from update_sdk import _GetTarballPath
13from update_sdk import GetSDKOverrideGCSPath
14
15
16@mock.patch('platform.machine')
17class 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)
23
24  def testUnsupportedArch(self, mock_machine):
25    mock_machine.return_value = 'bad_arch'
26    with self.assertRaises(Exception):
27      _GetHostArch()
28
29
30@mock.patch('builtins.open')
31@mock.patch('os.path.isfile')
32class TestGetSDKOverrideGCSPath(unittest.TestCase):
33  def testDefaultPath(self, mock_isfile, mock_open):
34    mock_isfile.return_value = False
35
36    with mock.patch('os.path.dirname', return_value='./'):
37      GetSDKOverrideGCSPath()
38
39    mock_isfile.assert_called_with('./sdk_override.txt')
40
41  def testRead(self, mock_isfile, mock_open):
42    fake_path = '\n\ngs://fuchsia-artifacts/development/abc123/sdk\n\n'
43
44    mock_isfile.return_value = True
45    mock_open.side_effect = mock.mock_open(read_data=fake_path)
46
47    actual = GetSDKOverrideGCSPath()
48    self.assertEqual(actual, 'gs://fuchsia-artifacts/development/abc123/sdk')
49
50
51@mock.patch('update_sdk._GetHostArch')
52@mock.patch('update_sdk.get_host_os')
53class TestGetTarballPath(unittest.TestCase):
54  def testGetTarballPath(self, mock_get_host_os, mock_host_arch):
55    mock_get_host_os.return_value = 'linux'
56    mock_host_arch.return_value = 'amd64'
57
58    actual = _GetTarballPath('gs://bucket/sdk')
59    self.assertEqual(actual, 'gs://bucket/sdk/linux-amd64/core.tar.gz')
60
61
62if __name__ == '__main__':
63  unittest.main()
64