Richard Barnette | 56844c0 | 2016-10-04 14:24:05 -0700 | [diff] [blame] | 1 | # Copyright 2016 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | import json |
| 6 | import os |
| 7 | import sys |
| 8 | import unittest |
| 9 | |
| 10 | import common |
Richard Barnette | 7412fa8 | 2016-10-21 15:29:51 -0700 | [diff] [blame^] | 11 | from autotest_lib.site_utils.stable_images import assign_stable_images |
Richard Barnette | 56844c0 | 2016-10-04 14:24:05 -0700 | [diff] [blame] | 12 | |
| 13 | |
| 14 | # _OMAHA_TEST_DATA - File with JSON data to be used as test input to |
| 15 | # `_make_omaha_versions()`. In the file, the various items in the |
| 16 | # `omaha_data` list are selected to capture various specific test |
| 17 | # cases: |
| 18 | # + Board with no "beta" channel. |
| 19 | # + Board with "beta" and another channel. |
| 20 | # + Board with only a "beta" channel. |
| 21 | # + Obsolete board with "is_active" set to false. |
| 22 | # The JSON content of the file is a subset of an actual |
| 23 | # `omaha_status.json` file copied when this unit test was new. |
| 24 | # |
| 25 | # _EXPECTED_OMAHA_VERSIONS - The expected output produced by |
| 26 | # _STUB_OMAHA_DATA. |
| 27 | # |
| 28 | _OMAHA_TEST_DATA = 'test_omaha_status.json' |
| 29 | |
| 30 | _EXPECTED_OMAHA_VERSIONS = {'arkham': 'R53-8530.71.1', |
| 31 | 'auron-paine': 'R54-8743.44.0', |
| 32 | 'zako-freon': 'R41-6680.52.0'} |
| 33 | |
| 34 | _DEFAULT_BOARD = assign_stable_images._DEFAULT_BOARD |
| 35 | |
| 36 | |
| 37 | class OmahaDataTests(unittest.TestCase): |
| 38 | """ |
| 39 | Tests for the `_make_omaha_versions()` function. |
| 40 | """ |
| 41 | |
| 42 | def test_make_omaha_versions(self): |
| 43 | """ |
| 44 | Test `_make_omaha_versions()` against one simple input. |
| 45 | |
| 46 | This is a trivial sanity test that confirms that a single |
| 47 | hard-coded input returns a correct hard-coded output. |
| 48 | """ |
| 49 | module_dir = os.path.dirname(sys.modules[__name__].__file__) |
| 50 | data_file_path = os.path.join(module_dir, _OMAHA_TEST_DATA) |
| 51 | omaha_versions = assign_stable_images._make_omaha_versions( |
| 52 | json.load(open(data_file_path, 'r'))) |
| 53 | self.assertEqual(omaha_versions, _EXPECTED_OMAHA_VERSIONS) |
| 54 | |
| 55 | |
| 56 | class UpgradeVersionsTests(unittest.TestCase): |
| 57 | """ |
| 58 | Tests for the `_get_upgrade_versions()` function. |
| 59 | """ |
| 60 | |
| 61 | # _VERSIONS - a list of sample version strings such as may be used |
| 62 | # for Chrome OS, sorted from oldest to newest. These are used to |
| 63 | # construct test data in multiple test cases, below. |
| 64 | _VERSIONS = ['R1-1.0.0', 'R1-1.1.0', 'R2-4.0.0'] |
| 65 | |
| 66 | def test_board_conversions(self): |
| 67 | """ |
| 68 | Test proper mapping of names from the AFE to Omaha. |
| 69 | |
| 70 | Board names in Omaha don't have '_' characters; when an AFE |
| 71 | board contains '_' characters, they must be converted to '-'. |
| 72 | |
| 73 | Assert that for various forms of name in the AFE mapping, the |
| 74 | converted name is the one looked up in the Omaha mapping. |
| 75 | """ |
| 76 | board_equivalents = [ |
| 77 | ('a-b', 'a-b'), ('c_d', 'c-d'), |
| 78 | ('e_f-g', 'e-f-g'), ('hi', 'hi')] |
| 79 | afe_versions = { |
| 80 | _DEFAULT_BOARD: self._VERSIONS[0] |
| 81 | } |
| 82 | omaha_versions = {} |
| 83 | expected = {} |
| 84 | boards = set() |
| 85 | for afe_board, omaha_board in board_equivalents: |
| 86 | boards.add(afe_board) |
| 87 | afe_versions[afe_board] = self._VERSIONS[1] |
| 88 | omaha_versions[omaha_board] = self._VERSIONS[2] |
| 89 | expected[afe_board] = self._VERSIONS[2] |
| 90 | upgrades, _ = assign_stable_images._get_upgrade_versions( |
| 91 | afe_versions, omaha_versions, boards) |
| 92 | self.assertEqual(upgrades, expected) |
| 93 | |
| 94 | def test_afe_default(self): |
| 95 | """ |
| 96 | Test that the AFE default board mapping is honored. |
| 97 | |
| 98 | If a board isn't present in the AFE dictionary, the mapping |
| 99 | for `_DEFAULT_BOARD` should be used. |
| 100 | |
| 101 | Primary assertions: |
| 102 | * When a board is present in the AFE mapping, its version |
| 103 | mapping is used. |
| 104 | * When a board is not present in the AFE mapping, the default |
| 105 | version mapping is used. |
| 106 | |
| 107 | Secondarily, assert that when a mapping is absent from Omaha, |
| 108 | the AFE mapping is left unchanged. |
| 109 | """ |
| 110 | afe_versions = { |
| 111 | _DEFAULT_BOARD: self._VERSIONS[0], |
| 112 | 'a': self._VERSIONS[1] |
| 113 | } |
| 114 | boards = set(['a', 'b']) |
| 115 | expected = { |
| 116 | 'a': self._VERSIONS[1], |
| 117 | 'b': self._VERSIONS[0] |
| 118 | } |
| 119 | upgrades, _ = assign_stable_images._get_upgrade_versions( |
| 120 | afe_versions, {}, boards) |
| 121 | self.assertEqual(upgrades, expected) |
| 122 | |
| 123 | def test_omaha_upgrade(self): |
| 124 | """ |
| 125 | Test that upgrades from Omaha are detected. |
| 126 | |
| 127 | Primary assertion: |
| 128 | * If a board is found in Omaha, and the version in Omaha is |
| 129 | newer than the AFE version, the Omaha version is the one |
| 130 | used. |
| 131 | |
| 132 | Secondarily, asserts that version comparisons between various |
| 133 | specific version strings are all correct. |
| 134 | """ |
| 135 | boards = set(['a']) |
| 136 | for i in range(0, len(self._VERSIONS) - 1): |
| 137 | afe_versions = {_DEFAULT_BOARD: self._VERSIONS[i]} |
| 138 | for j in range(i+1, len(self._VERSIONS)): |
| 139 | omaha_versions = {b: self._VERSIONS[j] for b in boards} |
| 140 | upgrades, _ = assign_stable_images._get_upgrade_versions( |
| 141 | afe_versions, omaha_versions, boards) |
| 142 | self.assertEqual(upgrades, omaha_versions) |
| 143 | |
| 144 | def test_no_upgrade(self): |
| 145 | """ |
| 146 | Test that if Omaha is behind the AFE, it is ignored. |
| 147 | |
| 148 | Primary assertion: |
| 149 | * If a board is found in Omaha, and the version in Omaha is |
| 150 | older than the AFE version, the AFE version is the one used. |
| 151 | |
| 152 | Secondarily, asserts that version comparisons between various |
| 153 | specific version strings are all correct. |
| 154 | """ |
| 155 | boards = set(['a']) |
| 156 | for i in range(1, len(self._VERSIONS)): |
| 157 | afe_versions = {_DEFAULT_BOARD: self._VERSIONS[i]} |
| 158 | expected = {b: self._VERSIONS[i] for b in boards} |
| 159 | for j in range(0, i): |
| 160 | omaha_versions = {b: self._VERSIONS[j] for b in boards} |
| 161 | upgrades, _ = assign_stable_images._get_upgrade_versions( |
| 162 | afe_versions, omaha_versions, boards) |
| 163 | self.assertEqual(upgrades, expected) |
| 164 | |
| 165 | def test_ignore_unused_boards(self): |
| 166 | """ |
| 167 | Test that unlisted boards are ignored. |
| 168 | |
| 169 | Assert that boards present in the AFE or Omaha mappings aren't |
| 170 | included in the return mappings when they aren't in the passed |
| 171 | in set of boards. |
| 172 | """ |
| 173 | unused_boards = set(['a', 'b']) |
| 174 | used_boards = set(['c', 'd']) |
| 175 | afe_versions = {b: self._VERSIONS[0] for b in unused_boards} |
| 176 | afe_versions[_DEFAULT_BOARD] = self._VERSIONS[1] |
| 177 | expected = {b: self._VERSIONS[1] for b in used_boards} |
| 178 | omaha_versions = expected.copy() |
| 179 | omaha_versions.update( |
| 180 | {b: self._VERSIONS[0] for b in unused_boards}) |
| 181 | upgrades, _ = assign_stable_images._get_upgrade_versions( |
| 182 | afe_versions, omaha_versions, used_boards) |
| 183 | self.assertEqual(upgrades, expected) |
| 184 | |
| 185 | def test_default_unchanged(self): |
| 186 | """ |
| 187 | Test correct handling when the default build is unchanged. |
| 188 | |
| 189 | Assert that if in Omaha, one board in a set of three upgrades |
| 190 | from the AFE default, that the returned default board mapping is |
| 191 | the original default in the AFE. |
| 192 | """ |
| 193 | boards = set(['a', 'b', 'c']) |
| 194 | afe_versions = {_DEFAULT_BOARD: self._VERSIONS[0]} |
| 195 | omaha_versions = {b: self._VERSIONS[0] for b in boards} |
| 196 | omaha_versions['c'] = self._VERSIONS[1] |
| 197 | _, new_default = assign_stable_images._get_upgrade_versions( |
| 198 | afe_versions, omaha_versions, boards) |
| 199 | self.assertEqual(new_default, self._VERSIONS[0]) |
| 200 | |
| 201 | def test_default_upgrade(self): |
| 202 | """ |
| 203 | Test correct handling when the default build must change. |
| 204 | |
| 205 | Assert that if in Omaha, two boards in a set of three upgrade |
| 206 | from the AFE default, that the returned default board mapping is |
| 207 | the new build in Omaha. |
| 208 | """ |
| 209 | boards = set(['a', 'b', 'c']) |
| 210 | afe_versions = {_DEFAULT_BOARD: self._VERSIONS[0]} |
| 211 | omaha_versions = {b: self._VERSIONS[1] for b in boards} |
| 212 | omaha_versions['c'] = self._VERSIONS[0] |
| 213 | _, new_default = assign_stable_images._get_upgrade_versions( |
| 214 | afe_versions, omaha_versions, boards) |
| 215 | self.assertEqual(new_default, self._VERSIONS[1]) |
| 216 | |
| 217 | |
| 218 | if __name__ == '__main__': |
| 219 | unittest.main() |