summaryrefslogtreecommitdiff
path: root/chromium/build/fuchsia/update_sdk_test.py
blob: 7d8bcc7b958debf396b1501dc749c2842349c17f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env vpython3
# Copyright 2022 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import unittest
from unittest import mock

from parameterized import parameterized

from update_sdk import _GetHostArch
from update_sdk import _GetTarballPath
from update_sdk import GetSDKOverrideGCSPath


@mock.patch('platform.machine')
class TestGetHostArch(unittest.TestCase):
  @parameterized.expand([('x86_64', 'amd64'), ('AMD64', 'amd64'),
                         ('aarch64', 'arm64')])
  def testSupportedArchs(self, mock_machine, arch, expected):
    mock_machine.return_value = arch
    self.assertEqual(_GetHostArch(), expected)

  def testUnsupportedArch(self, mock_machine):
    mock_machine.return_value = 'bad_arch'
    with self.assertRaises(Exception):
      _GetHostArch()


@mock.patch('builtins.open')
@mock.patch('os.path.isfile')
class TestGetSDKOverrideGCSPath(unittest.TestCase):
  def testFileNotFound(self, mock_isfile, mock_open):
    mock_isfile.return_value = False

    actual = GetSDKOverrideGCSPath('this-file-does-not-exist.txt')
    self.assertIsNone(actual)

  def testDefaultPath(self, mock_isfile, mock_open):
    mock_isfile.return_value = False

    with mock.patch('os.path.dirname', return_value='./'):
      GetSDKOverrideGCSPath()

    mock_isfile.assert_called_with('./sdk_override.txt')

  def testRead(self, mock_isfile, mock_open):
    fake_path = '\n\ngs://fuchsia-artifacts/development/abc123/sdk\n\n'

    mock_isfile.return_value = True
    mock_open.side_effect = mock.mock_open(read_data=fake_path)

    actual = GetSDKOverrideGCSPath()
    self.assertEqual(actual, 'gs://fuchsia-artifacts/development/abc123/sdk')


@mock.patch('update_sdk._GetHostArch')
@mock.patch('update_sdk.GetHostOsFromPlatform')
class TestGetTarballPath(unittest.TestCase):
  def testGetTarballPath(self, mock_get_host_os, mock_host_arch):
    mock_get_host_os.return_value = 'linux'
    mock_host_arch.return_value = 'amd64'

    actual = _GetTarballPath('gs://bucket/sdk')
    self.assertEqual(actual, 'gs://bucket/sdk/linux-amd64/gn.tar.gz')


if __name__ == '__main__':
  unittest.main()