• 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 testFileNotFound(self, mock_isfile, mock_open):
34    mock_isfile.return_value = False
35
36    actual = GetSDKOverrideGCSPath('this-file-does-not-exist.txt')
37    self.assertIsNone(actual)
38
39  def testDefaultPath(self, mock_isfile, mock_open):
40    mock_isfile.return_value = False
41
42    with mock.patch('os.path.dirname', return_value='./'):
43      GetSDKOverrideGCSPath()
44
45    mock_isfile.assert_called_with('./sdk_override.txt')
46
47  def testRead(self, mock_isfile, mock_open):
48    fake_path = '\n\ngs://fuchsia-artifacts/development/abc123/sdk\n\n'
49
50    mock_isfile.return_value = True
51    mock_open.side_effect = mock.mock_open(read_data=fake_path)
52
53    actual = GetSDKOverrideGCSPath()
54    self.assertEqual(actual, 'gs://fuchsia-artifacts/development/abc123/sdk')
55
56
57@mock.patch('update_sdk._GetHostArch')
58@mock.patch('update_sdk.get_host_os')
59class 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'
63
64    actual = _GetTarballPath('gs://bucket/sdk')
65    self.assertEqual(actual, 'gs://bucket/sdk/linux-amd64/core.tar.gz')
66
67
68if __name__ == '__main__':
69  unittest.main()
70