Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
Chris Sosa | 781ba6d | 2012-04-11 12:44:43 -0700 | [diff] [blame] | 3 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 7 | """Unit tests for common_util module.""" |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 8 | |
| 9 | import os |
| 10 | import shutil |
Chris Sosa | ea148d9 | 2012-03-06 16:22:04 -0800 | [diff] [blame] | 11 | import subprocess |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 12 | import tempfile |
| 13 | import unittest |
| 14 | |
Gilad Arnold | abb352e | 2012-09-23 01:24:27 -0700 | [diff] [blame] | 15 | import mox |
| 16 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 17 | import build_artifact |
| 18 | import common_util |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 19 | import gsutil_util |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 20 | |
| 21 | |
| 22 | # Fake Dev Server Layout: |
| 23 | TEST_LAYOUT = { |
| 24 | 'test-board-1': ['R17-1413.0.0-a1-b1346', 'R17-18.0.0-a1-b1346'], |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 25 | 'test-board-2': ['R16-2241.0.0-a0-b2', 'R17-2.0.0-a1-b1346'], |
| 26 | 'test-board-3': [] |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | |
Gilad Arnold | 17fe03d | 2012-10-02 10:05:01 -0700 | [diff] [blame] | 30 | class CommonUtilTest(mox.MoxTestBase): |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 31 | |
| 32 | def setUp(self): |
Chris Sosa | ea148d9 | 2012-03-06 16:22:04 -0800 | [diff] [blame] | 33 | mox.MoxTestBase.setUp(self) |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 34 | self._static_dir = tempfile.mkdtemp('common_util_unittest') |
| 35 | self._outside_sandbox_dir = tempfile.mkdtemp('common_util_unittest') |
| 36 | self._install_dir = tempfile.mkdtemp('common_util_unittest') |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 37 | |
| 38 | for board, builds in TEST_LAYOUT.iteritems(): |
| 39 | board_path = os.path.join(self._static_dir, board) |
| 40 | os.mkdir(board_path) |
| 41 | for build in builds: |
| 42 | build_path = os.path.join(board_path, build) |
| 43 | os.mkdir(build_path) |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 44 | with open(os.path.join( |
| 45 | build_path, build_artifact.TEST_IMAGE), 'w') as f: |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 46 | f.write('TEST_IMAGE') |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 47 | with open(os.path.join( |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 48 | build_path, build_artifact.STATEFUL_UPDATE), 'w') as f: |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 49 | f.write('STATEFUL_UPDATE') |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 50 | with open(os.path.join( |
| 51 | build_path, build_artifact.ROOT_UPDATE), 'w') as f: |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 52 | f.write('ROOT_UPDATE') |
| 53 | # AU payloads. |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 54 | au_dir = os.path.join(build_path, common_util.AU_BASE) |
| 55 | nton_dir = os.path.join(au_dir, build + common_util.NTON_DIR_SUFFIX) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 56 | os.makedirs(nton_dir) |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 57 | with open(os.path.join(nton_dir, build_artifact.ROOT_UPDATE), 'w') as f: |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 58 | f.write('ROOT_UPDATE') |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 59 | mton_dir = os.path.join(au_dir, build + common_util.MTON_DIR_SUFFIX) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 60 | os.makedirs(mton_dir) |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 61 | with open(os.path.join(mton_dir, build_artifact.ROOT_UPDATE), 'w') as f: |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 62 | f.write('ROOT_UPDATE') |
| 63 | |
Chris Sosa | ea148d9 | 2012-03-06 16:22:04 -0800 | [diff] [blame] | 64 | self._good_mock_process = self.mox.CreateMock(subprocess.Popen) |
| 65 | self._good_mock_process.returncode = 0 |
| 66 | self._bad_mock_process = self.mox.CreateMock(subprocess.Popen) |
| 67 | self._bad_mock_process.returncode = 1 |
| 68 | |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 69 | def tearDown(self): |
| 70 | shutil.rmtree(self._static_dir) |
| 71 | shutil.rmtree(self._outside_sandbox_dir) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 72 | shutil.rmtree(self._install_dir) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 73 | |
Chris Sosa | 1228a1a | 2012-05-22 17:12:13 -0700 | [diff] [blame] | 74 | def testParsePayloadListWithoutDeltas(self): |
| 75 | """Tests we can parse the payload list when no delta updates exist.""" |
| 76 | archive_url_prefix = ('gs://chromeos-image-archive/x86-mario-release/' |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 77 | 'R17-1413.0.0-a1-b1346') |
| 78 | full_basename = ('chromeos_R17-1413.0.0-a1_x86-mario_full_dev.bin') |
| 79 | full_url = '/'.join([archive_url_prefix, full_basename]) |
Vadim Bendebury | fee389c | 2013-01-23 14:14:43 -0800 | [diff] [blame] | 80 | full_url_out, nton_url_out, mton_url_out, fw_url_out = ( |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 81 | common_util.ParsePayloadList( |
| 82 | archive_url_prefix, [full_basename, '', ''])) |
Vadim Bendebury | fee389c | 2013-01-23 14:14:43 -0800 | [diff] [blame] | 83 | self.assertEqual([full_url, None, None, None], |
| 84 | [full_url_out, nton_url_out, mton_url_out, fw_url_out]) |
Chris Sosa | 1228a1a | 2012-05-22 17:12:13 -0700 | [diff] [blame] | 85 | |
Chris Sosa | 781ba6d | 2012-04-11 12:44:43 -0700 | [diff] [blame] | 86 | def testParsePartialPayloadList(self): |
| 87 | """Tests that we can parse a payload list with missing optional payload.""" |
| 88 | archive_url_prefix = ('gs://chromeos-image-archive/x86-mario-release/' |
| 89 | 'R17-1413.0.0-a1-b1346/') |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 90 | nton_basename = ('chromeos_R17-1413.0.0-a1_R17-1413.0.0-a1_x86-' |
| 91 | 'mario_delta_dev.bin') |
| 92 | full_basename = ('chromeos_R17-1413.0.0-a1_x86-mario_full_dev.bin') |
| 93 | |
| 94 | nton_url = '/'.join([archive_url_prefix, nton_basename]) |
| 95 | full_url = '/'.join([archive_url_prefix, full_basename]) |
| 96 | |
Vadim Bendebury | fee389c | 2013-01-23 14:14:43 -0800 | [diff] [blame] | 97 | full_url_out, nton_url_out, mton_url_out, fw_url_out = ( |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 98 | common_util.ParsePayloadList(archive_url_prefix, |
| 99 | [full_basename, nton_basename])) |
Vadim Bendebury | fee389c | 2013-01-23 14:14:43 -0800 | [diff] [blame] | 100 | self.assertEqual([full_url, nton_url, None, None], |
| 101 | [full_url_out, nton_url_out, mton_url_out, fw_url_out]) |
Chris Sosa | 781ba6d | 2012-04-11 12:44:43 -0700 | [diff] [blame] | 102 | |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 103 | def testInstallBuild(self): |
Simon Glass | f5019de | 2012-03-20 12:14:41 -0700 | [diff] [blame] | 104 | # TODO(frankf): Implement this test |
| 105 | # self.fail('Not implemented.') |
| 106 | pass |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 107 | |
| 108 | def testPrepareAutotestPkgs(self): |
Simon Glass | f5019de | 2012-03-20 12:14:41 -0700 | [diff] [blame] | 109 | # TODO(frankf): Implement this test |
| 110 | # self.fail('Not implemented.') |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 111 | # TODO: implement |
Simon Glass | f5019de | 2012-03-20 12:14:41 -0700 | [diff] [blame] | 112 | pass |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 113 | |
| 114 | def testSafeSandboxAccess(self): |
| 115 | # Path is in sandbox. |
| 116 | self.assertTrue( |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 117 | common_util.SafeSandboxAccess( |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 118 | self._static_dir, os.path.join(self._static_dir, 'some-board'))) |
| 119 | |
| 120 | # Path is sandbox. |
| 121 | self.assertFalse( |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 122 | common_util.SafeSandboxAccess(self._static_dir, self._static_dir)) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 123 | |
| 124 | # Path is outside the sandbox. |
| 125 | self.assertFalse( |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 126 | common_util.SafeSandboxAccess( |
| 127 | self._static_dir, self._outside_sandbox_dir)) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 128 | |
| 129 | # Path contains '..'. |
| 130 | self.assertFalse( |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 131 | common_util.SafeSandboxAccess( |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 132 | self._static_dir, os.path.join(self._static_dir, os.pardir))) |
| 133 | |
| 134 | # Path contains symbolic link references. |
| 135 | os.chdir(self._static_dir) |
| 136 | os.symlink(os.pardir, 'parent') |
| 137 | self.assertFalse( |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 138 | common_util.SafeSandboxAccess( |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 139 | self._static_dir, os.path.join(self._static_dir, os.pardir))) |
| 140 | |
| 141 | def testAcquireReleaseLocks(self): |
Gilad Arnold | 5174ca2 | 2012-09-12 10:49:09 -0700 | [diff] [blame] | 142 | # Successful lock and unlock, removing the newly created directory. |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 143 | lock_file = common_util.AcquireLock(self._static_dir, 'test-lock') |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 144 | self.assertTrue(os.path.exists(lock_file)) |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 145 | common_util.ReleaseLock(self._static_dir, 'test-lock', destroy=True) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 146 | self.assertFalse(os.path.exists(lock_file)) |
| 147 | |
Gilad Arnold | 5174ca2 | 2012-09-12 10:49:09 -0700 | [diff] [blame] | 148 | # Attempt to freshly create and lock an existing directory. |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 149 | common_util.AcquireLock(self._static_dir, 'test-lock') |
| 150 | common_util.ReleaseLock(self._static_dir, 'test-lock') |
Gilad Arnold | 17fe03d | 2012-10-02 10:05:01 -0700 | [diff] [blame] | 151 | self.assertRaises(common_util.CommonUtilError, common_util.AcquireLock, |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 152 | self._static_dir, 'test-lock') |
| 153 | common_util.AcquireLock(self._static_dir, 'test-lock', create_once=False) |
| 154 | common_util.ReleaseLock(self._static_dir, 'test-lock', destroy=True) |
Gilad Arnold | 5174ca2 | 2012-09-12 10:49:09 -0700 | [diff] [blame] | 155 | |
| 156 | # Sucessfully re-lock a pre-existing directory. |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 157 | common_util.AcquireLock(self._static_dir, 'test-lock') |
| 158 | common_util.ReleaseLock(self._static_dir, 'test-lock') |
| 159 | common_util.AcquireLock(self._static_dir, 'test-lock', create_once=False) |
| 160 | common_util.ReleaseLock(self._static_dir, 'test-lock', destroy=True) |
Gilad Arnold | 5174ca2 | 2012-09-12 10:49:09 -0700 | [diff] [blame] | 161 | |
| 162 | # Attempt to lock an already locked directory. |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 163 | common_util.AcquireLock(self._static_dir, 'test-lock') |
Gilad Arnold | 17fe03d | 2012-10-02 10:05:01 -0700 | [diff] [blame] | 164 | self.assertRaises(common_util.CommonUtilError, common_util.AcquireLock, |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 165 | self._static_dir, 'test-lock') |
| 166 | common_util.ReleaseLock(self._static_dir, 'test-lock', destroy=True) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 167 | |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 168 | def testGetLatestBuildVersion(self): |
| 169 | self.assertEqual( |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 170 | common_util.GetLatestBuildVersion(self._static_dir, 'test-board-1'), |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 171 | 'R17-1413.0.0-a1-b1346') |
| 172 | |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 173 | def testGetLatestBuildVersionLatest(self): |
Gilad Arnold | 17fe03d | 2012-10-02 10:05:01 -0700 | [diff] [blame] | 174 | """Test that we raise CommonUtilError when a build dir is empty.""" |
| 175 | self.assertRaises(common_util.CommonUtilError, |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 176 | common_util.GetLatestBuildVersion, |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 177 | self._static_dir, 'test-board-3') |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 178 | |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 179 | def testGetLatestBuildVersionUnknownBuild(self): |
Gilad Arnold | 17fe03d | 2012-10-02 10:05:01 -0700 | [diff] [blame] | 180 | """Test that we raise CommonUtilError when a build dir does not exist.""" |
| 181 | self.assertRaises(common_util.CommonUtilError, |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 182 | common_util.GetLatestBuildVersion, |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 183 | self._static_dir, 'bad-dir') |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 184 | |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 185 | def testGetLatestBuildVersionMilestone(self): |
| 186 | """Test that we can get builds based on milestone.""" |
| 187 | expected_build_str = 'R16-2241.0.0-a0-b2' |
| 188 | milestone = 'R16' |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 189 | build_str = common_util.GetLatestBuildVersion( |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 190 | self._static_dir, 'test-board-2', milestone) |
| 191 | self.assertEqual(expected_build_str, build_str) |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 192 | |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 193 | def testGetControlFile(self): |
| 194 | control_file_dir = os.path.join( |
Chris Sosa | ea148d9 | 2012-03-06 16:22:04 -0800 | [diff] [blame] | 195 | self._static_dir, 'test-board-1', 'R17-1413.0.0-a1-b1346', 'autotest', |
| 196 | 'server', 'site_tests', 'network_VPN') |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 197 | os.makedirs(control_file_dir) |
| 198 | with open(os.path.join(control_file_dir, 'control'), 'w') as f: |
| 199 | f.write('hello!') |
| 200 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 201 | control_content = common_util.GetControlFile( |
Chris Sosa | ea148d9 | 2012-03-06 16:22:04 -0800 | [diff] [blame] | 202 | self._static_dir, 'test-board-1/R17-1413.0.0-a1-b1346', |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 203 | os.path.join('server', 'site_tests', 'network_VPN', 'control')) |
| 204 | self.assertEqual(control_content, 'hello!') |
| 205 | |
Vadim Bendebury | fee389c | 2013-01-23 14:14:43 -0800 | [diff] [blame] | 206 | def commonGatherArtifactDownloads(self, payload_names): |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 207 | """Tests that we can gather the correct download requirements.""" |
| 208 | build = 'R17-1413.0.0-a1-b1346' |
| 209 | archive_url_prefix = ('gs://chromeos-image-archive/x86-mario-release/' + |
| 210 | build) |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 211 | mock_data = 'mock data\nmock_data' |
Vadim Bendebury | fee389c | 2013-01-23 14:14:43 -0800 | [diff] [blame] | 212 | payloads = map(lambda x: '/'.join([archive_url_prefix, x]), payload_names) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 213 | expected_payloads = payloads + map( |
| 214 | lambda x: '/'.join([archive_url_prefix, x]), |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 215 | [build_artifact.STATEFUL_UPDATE, |
| 216 | build_artifact.AUTOTEST_ZIPPED_PACKAGE, |
| 217 | build_artifact.TEST_SUITES_PACKAGE]) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 218 | self.mox.StubOutWithMock(gsutil_util, 'GSUtilRun') |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 219 | self.mox.StubOutWithMock(common_util, 'IsAvailable') |
| 220 | self.mox.StubOutWithMock(common_util, 'ParsePayloadList') |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 221 | |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 222 | # GSUtil cat gs://archive_url_prefix/UPLOADED. |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 223 | gsutil_util.GSUtilRun(mox.StrContains(common_util.UPLOADED_LIST), |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 224 | mox.IgnoreArg()).AndReturn(mock_data) |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 225 | common_util.IsAvailable( |
| 226 | mox.IgnoreArg(), mock_data.splitlines()).AndReturn(True) |
| 227 | common_util.ParsePayloadList(archive_url_prefix, |
Vadim Bendebury | fee389c | 2013-01-23 14:14:43 -0800 | [diff] [blame] | 228 | mock_data.splitlines()).AndReturn( |
| 229 | payloads + [None] * (4 - len(payload_names))) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 230 | self.mox.ReplayAll() |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 231 | artifacts = common_util.GatherArtifactDownloads( |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 232 | self._static_dir, archive_url_prefix, self._install_dir, build) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 233 | for index, artifact in enumerate(artifacts): |
| 234 | self.assertEqual(artifact._gs_path, expected_payloads[index]) |
| 235 | self.assertTrue(artifact._tmp_staging_dir.startswith(self._static_dir)) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 236 | |
| 237 | self.mox.VerifyAll() |
| 238 | |
Vadim Bendebury | fee389c | 2013-01-23 14:14:43 -0800 | [diff] [blame] | 239 | |
| 240 | def testGatherArtifactDownloadsWithoutFirmware(self): |
| 241 | """Tests that we can gather the correct download requirements.""" |
| 242 | self.commonGatherArtifactDownloads(['p1', 'p2', 'p3']) |
| 243 | |
| 244 | def testGatherArtifactDownloads(self): |
| 245 | """Tests that we can gather the correct download requirements.""" |
| 246 | self.commonGatherArtifactDownloads(['p1', 'p2', 'p3', 'p4']) |
| 247 | |
Chris Sosa | 781ba6d | 2012-04-11 12:44:43 -0700 | [diff] [blame] | 248 | def testGatherArtifactDownloadsWithoutMton(self): |
| 249 | """Gather the correct download requirements without mton delta.""" |
Vadim Bendebury | fee389c | 2013-01-23 14:14:43 -0800 | [diff] [blame] | 250 | self.commonGatherArtifactDownloads(['p1', 'p2']) |
Chris Sosa | 1228a1a | 2012-05-22 17:12:13 -0700 | [diff] [blame] | 251 | |
| 252 | def testGatherArtifactDownloadsWithoutMtonOrNton(self): |
| 253 | """Gather the correct download requirements without delta payloads.""" |
Vadim Bendebury | fee389c | 2013-01-23 14:14:43 -0800 | [diff] [blame] | 254 | self.commonGatherArtifactDownloads(['p1']) |
Chris Sosa | 781ba6d | 2012-04-11 12:44:43 -0700 | [diff] [blame] | 255 | |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 256 | def testGatherSymbolArtifactDownloads(self): |
| 257 | """Tests that we can find debug symbol artifacts to download.""" |
| 258 | build = 'R17-1413.0.0-a1-b1346' |
| 259 | archive_url_prefix = ('gs://chromeos-image-archive/x86-mario-release/' + |
| 260 | build) |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 261 | symbol_url = '/'.join([archive_url_prefix, |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 262 | build_artifact.DEBUG_SYMBOLS]) |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 263 | mock_data = 'mock-tarball.tgz\nmock-debug.tgz' |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 264 | self.mox.StubOutWithMock(gsutil_util, 'GSUtilRun') |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 265 | |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 266 | # GSUtil cat gs://archive_url_prefix/UPLOADED. |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 267 | gsutil_util.GSUtilRun(mox.StrContains(common_util.UPLOADED_LIST), |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 268 | mox.IgnoreArg()).AndReturn(mock_data) |
| 269 | |
| 270 | self.mox.ReplayAll() |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 271 | artifacts = common_util.GatherSymbolArtifactDownloads( |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 272 | self._static_dir, archive_url_prefix, self._install_dir) |
Vadim Bendebury | fee389c | 2013-01-23 14:14:43 -0800 | [diff] [blame] | 273 | for _, artifact in enumerate(artifacts): |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 274 | self.assertEqual(artifact._gs_path, symbol_url) |
| 275 | self.assertTrue(artifact._tmp_staging_dir.startswith(self._static_dir)) |
| 276 | |
| 277 | self.mox.VerifyAll() |
| 278 | |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 279 | def testIsAvailable(self): |
| 280 | """Test that we can detect whether the target artifacts are avaialble.""" |
| 281 | # Test when the all target files are available |
| 282 | pattern_list = ['_full_', 'autotest.tar'] |
| 283 | uploaded_list = ['chromeos_R17-1413.0.0-a1_x86-mario_full_dev.bin', |
| 284 | 'debug.tgz', |
| 285 | 'autotest.tar.bz2'] |
| 286 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 287 | available = common_util.IsAvailable(pattern_list, uploaded_list) |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 288 | self.assertTrue(available) |
| 289 | |
| 290 | # Test when some target files are missing |
| 291 | pattern_list = ['_full_', 'autotest.tar'] |
| 292 | uploaded_list = ['chromeos_R17-1413.0.0-a1_x86-mario_full_dev.bin', |
| 293 | 'debug.tgz'] |
| 294 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 295 | available = common_util.IsAvailable(pattern_list, uploaded_list) |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 296 | self.assertFalse(available) |
| 297 | |
| 298 | def testWaitUntilAvailable(self): |
| 299 | """Test that we can poll until all target artifacts are available.""" |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 300 | archive_url = ('gs://chromeos-image-archive/x86-mario-release/' |
| 301 | 'R17-1413.0.0-a1-b1346') |
| 302 | to_wait_list = ['_full_'] |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 303 | mock_data = 'mock data\nmock_data\nmock_data' |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 304 | |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 305 | self.mox.StubOutWithMock(gsutil_util, 'GSUtilRun') |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 306 | self.mox.StubOutWithMock(common_util, 'IsAvailable') |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 307 | |
| 308 | # GSUtil cat gs://archive_url_prefix/UPLOADED. |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 309 | gsutil_util.GSUtilRun(mox.StrContains(common_util.UPLOADED_LIST), |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 310 | mox.IgnoreArg()).AndReturn(mock_data) |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 311 | common_util.IsAvailable(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(True) |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 312 | |
| 313 | self.mox.ReplayAll() |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 314 | uploaded_list = common_util.WaitUntilAvailable( |
| 315 | to_wait_list, archive_url, 'UNIT TEST', delay=1) |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 316 | self.assertEqual(uploaded_list, mock_data.splitlines()) |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 317 | self.mox.VerifyAll() |
| 318 | |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 319 | def testWaitUntilAvailableWithRetry(self): |
| 320 | """Test that we can poll until all target artifacts are available.""" |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 321 | archive_url = ('gs://chromeos-image-archive/x86-mario-release/' |
| 322 | 'R17-1413.0.0-a1-b1346') |
| 323 | to_wait_list = ['_full_'] |
| 324 | mock_data = 'mock data\nmock_data\nmock_data' |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 325 | |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 326 | self.mox.StubOutWithMock(gsutil_util, 'GSUtilRun') |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 327 | self.mox.StubOutWithMock(common_util, 'IsAvailable') |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 328 | |
| 329 | # GSUtil cat gs://archive_url_prefix/UPLOADED. |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 330 | gsutil_util.GSUtilRun(mox.StrContains(common_util.UPLOADED_LIST), |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 331 | mox.IgnoreArg()).AndReturn(mock_data) |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 332 | common_util.IsAvailable(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(False) |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 333 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 334 | gsutil_util.GSUtilRun(mox.StrContains(common_util.UPLOADED_LIST), |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 335 | mox.IgnoreArg()).AndReturn(mock_data) |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 336 | common_util.IsAvailable(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(True) |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 337 | |
| 338 | self.mox.ReplayAll() |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 339 | uploaded_list = common_util.WaitUntilAvailable( |
| 340 | to_wait_list, archive_url, 'UNIT TEST', delay=1) |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 341 | self.assertEqual(uploaded_list, mock_data.splitlines()) |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 342 | self.mox.VerifyAll() |
| 343 | |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 344 | def testWaitUntilAvailableTimeout(self): |
| 345 | """Test that we wait for the target artifacts until timeout occurs.""" |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 346 | archive_url = ('gs://chromeos-image-archive/x86-mario-release/' |
| 347 | 'R17-1413.0.0-a1-b1346') |
| 348 | to_wait_list = ['_full_'] |
| 349 | mock_data = 'mock data\nmock_data\nmock_data' |
| 350 | |
| 351 | self.mox.StubOutWithMock(gsutil_util, 'GSUtilRun') |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 352 | self.mox.StubOutWithMock(common_util, 'IsAvailable') |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 353 | |
| 354 | # GSUtil cat gs://archive_url_prefix/UPLOADED. |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 355 | gsutil_util.GSUtilRun(mox.StrContains(common_util.UPLOADED_LIST), |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 356 | mox.IgnoreArg()).AndReturn(mock_data) |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 357 | common_util.IsAvailable(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(False) |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 358 | |
| 359 | self.mox.ReplayAll() |
Gilad Arnold | 17fe03d | 2012-10-02 10:05:01 -0700 | [diff] [blame] | 360 | self.assertRaises(common_util.CommonUtilError, |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 361 | common_util.WaitUntilAvailable, |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 362 | to_wait_list, |
| 363 | archive_url, |
| 364 | 'UNIT TEST', |
| 365 | delay=2, |
| 366 | timeout=1) |
| 367 | self.mox.VerifyAll() |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 368 | |
| 369 | if __name__ == '__main__': |
| 370 | unittest.main() |