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]) |
Chris Sosa | 1228a1a | 2012-05-22 17:12:13 -0700 | [diff] [blame] | 80 | full_url_out, nton_url_out, mton_url_out = ( |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 81 | common_util.ParsePayloadList( |
| 82 | archive_url_prefix, [full_basename, '', ''])) |
Chris Sosa | 1228a1a | 2012-05-22 17:12:13 -0700 | [diff] [blame] | 83 | self.assertEqual([full_url, None, None], |
| 84 | [full_url_out, nton_url_out, mton_url_out]) |
| 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 | |
Chris Sosa | 781ba6d | 2012-04-11 12:44:43 -0700 | [diff] [blame] | 97 | full_url_out, nton_url_out, mton_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])) |
Chris Sosa | 781ba6d | 2012-04-11 12:44:43 -0700 | [diff] [blame] | 100 | self.assertEqual([full_url, nton_url, None], |
| 101 | [full_url_out, nton_url_out, mton_url_out]) |
| 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 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 206 | def testGatherArtifactDownloads(self): |
| 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' |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 212 | payloads = map(lambda x: '/'.join([archive_url_prefix, x]), |
| 213 | ['p1', 'p2', 'p3']) |
| 214 | expected_payloads = payloads + map( |
| 215 | lambda x: '/'.join([archive_url_prefix, x]), |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 216 | [build_artifact.STATEFUL_UPDATE, |
| 217 | build_artifact.AUTOTEST_ZIPPED_PACKAGE, |
| 218 | build_artifact.TEST_SUITES_PACKAGE]) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 219 | self.mox.StubOutWithMock(gsutil_util, 'GSUtilRun') |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 220 | self.mox.StubOutWithMock(common_util, 'IsAvailable') |
| 221 | self.mox.StubOutWithMock(common_util, 'ParsePayloadList') |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 222 | |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 223 | # GSUtil cat gs://archive_url_prefix/UPLOADED. |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 224 | gsutil_util.GSUtilRun(mox.StrContains(common_util.UPLOADED_LIST), |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 225 | mox.IgnoreArg()).AndReturn(mock_data) |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 226 | common_util.IsAvailable( |
| 227 | mox.IgnoreArg(), mock_data.splitlines()).AndReturn(True) |
| 228 | common_util.ParsePayloadList(archive_url_prefix, |
| 229 | mock_data.splitlines()).AndReturn(payloads) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 230 | |
| 231 | self.mox.ReplayAll() |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 232 | artifacts = common_util.GatherArtifactDownloads( |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 233 | self._static_dir, archive_url_prefix, self._install_dir, build) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 234 | for index, artifact in enumerate(artifacts): |
| 235 | self.assertEqual(artifact._gs_path, expected_payloads[index]) |
| 236 | self.assertTrue(artifact._tmp_staging_dir.startswith(self._static_dir)) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 237 | |
| 238 | self.mox.VerifyAll() |
| 239 | |
Chris Sosa | 781ba6d | 2012-04-11 12:44:43 -0700 | [diff] [blame] | 240 | def testGatherArtifactDownloadsWithoutMton(self): |
| 241 | """Gather the correct download requirements without mton delta.""" |
| 242 | build = 'R17-1413.0.0-a1-b1346' |
| 243 | archive_url_prefix = ('gs://chromeos-image-archive/x86-mario-release/' + |
| 244 | build) |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 245 | mock_data = 'mock data\nmock_data\nmock_data' |
Chris Sosa | 781ba6d | 2012-04-11 12:44:43 -0700 | [diff] [blame] | 246 | payloads = map(lambda x: '/'.join([archive_url_prefix, x]), |
| 247 | ['p1', 'p2']) |
| 248 | expected_payloads = payloads + map( |
| 249 | lambda x: '/'.join([archive_url_prefix, x]), |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 250 | [build_artifact.STATEFUL_UPDATE, |
| 251 | build_artifact.AUTOTEST_ZIPPED_PACKAGE, |
| 252 | build_artifact.TEST_SUITES_PACKAGE]) |
Chris Sosa | 781ba6d | 2012-04-11 12:44:43 -0700 | [diff] [blame] | 253 | self.mox.StubOutWithMock(gsutil_util, 'GSUtilRun') |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 254 | self.mox.StubOutWithMock(common_util, 'IsAvailable') |
| 255 | self.mox.StubOutWithMock(common_util, 'ParsePayloadList') |
Chris Sosa | 781ba6d | 2012-04-11 12:44:43 -0700 | [diff] [blame] | 256 | |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 257 | # GSUtil cat gs://archive_url_prefix/UPLOADED. |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 258 | gsutil_util.GSUtilRun(mox.StrContains(common_util.UPLOADED_LIST), |
Chris Sosa | 781ba6d | 2012-04-11 12:44:43 -0700 | [diff] [blame] | 259 | mox.IgnoreArg()).AndReturn(mock_data) |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 260 | common_util.IsAvailable( |
| 261 | mox.IgnoreArg(), mock_data.splitlines()).AndReturn(True) |
| 262 | common_util.ParsePayloadList(archive_url_prefix, |
| 263 | mock_data.splitlines() |
| 264 | ).AndReturn(payloads + [None]) |
Chris Sosa | 781ba6d | 2012-04-11 12:44:43 -0700 | [diff] [blame] | 265 | |
| 266 | self.mox.ReplayAll() |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 267 | artifacts = common_util.GatherArtifactDownloads( |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 268 | self._static_dir, archive_url_prefix, self._install_dir, build) |
Chris Sosa | 781ba6d | 2012-04-11 12:44:43 -0700 | [diff] [blame] | 269 | for index, artifact in enumerate(artifacts): |
| 270 | self.assertEqual(artifact._gs_path, expected_payloads[index]) |
| 271 | self.assertTrue(artifact._tmp_staging_dir.startswith(self._static_dir)) |
Chris Sosa | 1228a1a | 2012-05-22 17:12:13 -0700 | [diff] [blame] | 272 | |
| 273 | self.mox.VerifyAll() |
| 274 | |
| 275 | def testGatherArtifactDownloadsWithoutMtonOrNton(self): |
| 276 | """Gather the correct download requirements without delta payloads.""" |
| 277 | build = 'R17-1413.0.0-a1-b1346' |
| 278 | archive_url_prefix = ('gs://chromeos-image-archive/x86-mario-release/' + |
| 279 | build) |
| 280 | mock_data = 'mock data\nmock_data' |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 281 | |
Chris Sosa | 1228a1a | 2012-05-22 17:12:13 -0700 | [diff] [blame] | 282 | payloads = map(lambda x: '/'.join([archive_url_prefix, x]), |
| 283 | ['p1']) |
| 284 | expected_payloads = payloads + map( |
| 285 | lambda x: '/'.join([archive_url_prefix, x]), |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 286 | [build_artifact.STATEFUL_UPDATE, |
| 287 | build_artifact.AUTOTEST_ZIPPED_PACKAGE, |
| 288 | build_artifact.TEST_SUITES_PACKAGE]) |
Chris Sosa | 1228a1a | 2012-05-22 17:12:13 -0700 | [diff] [blame] | 289 | self.mox.StubOutWithMock(gsutil_util, 'GSUtilRun') |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 290 | self.mox.StubOutWithMock(common_util, 'IsAvailable') |
| 291 | self.mox.StubOutWithMock(common_util, 'ParsePayloadList') |
Chris Sosa | 1228a1a | 2012-05-22 17:12:13 -0700 | [diff] [blame] | 292 | |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 293 | # GSUtil cat gs://archive_url_prefix/UPLOADED. |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 294 | gsutil_util.GSUtilRun(mox.StrContains(common_util.UPLOADED_LIST), |
Chris Sosa | 1228a1a | 2012-05-22 17:12:13 -0700 | [diff] [blame] | 295 | mox.IgnoreArg()).AndReturn(mock_data) |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 296 | common_util.IsAvailable( |
| 297 | mox.IgnoreArg(), mock_data.splitlines()).AndReturn(True) |
| 298 | common_util.ParsePayloadList(archive_url_prefix, |
| 299 | mock_data.splitlines() |
| 300 | ).AndReturn(payloads + [None, None]) |
Chris Sosa | 1228a1a | 2012-05-22 17:12:13 -0700 | [diff] [blame] | 301 | |
| 302 | self.mox.ReplayAll() |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 303 | artifacts = common_util.GatherArtifactDownloads( |
Gilad Arnold | 6f99b98 | 2012-09-12 10:49:40 -0700 | [diff] [blame] | 304 | self._static_dir, archive_url_prefix, self._install_dir, build) |
Chris Sosa | 1228a1a | 2012-05-22 17:12:13 -0700 | [diff] [blame] | 305 | for index, artifact in enumerate(artifacts): |
| 306 | self.assertEqual(artifact._gs_path, expected_payloads[index]) |
| 307 | self.assertTrue(artifact._tmp_staging_dir.startswith(self._static_dir)) |
Chris Sosa | 781ba6d | 2012-04-11 12:44:43 -0700 | [diff] [blame] | 308 | |
| 309 | self.mox.VerifyAll() |
| 310 | |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 311 | def testGatherSymbolArtifactDownloads(self): |
| 312 | """Tests that we can find debug symbol artifacts to download.""" |
| 313 | build = 'R17-1413.0.0-a1-b1346' |
| 314 | archive_url_prefix = ('gs://chromeos-image-archive/x86-mario-release/' + |
| 315 | build) |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 316 | symbol_url = '/'.join([archive_url_prefix, |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 317 | build_artifact.DEBUG_SYMBOLS]) |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 318 | uploaded_list_url = '/'.join([archive_url_prefix, |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 319 | common_util.UPLOADED_LIST]) |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 320 | mock_data = 'mock-tarball.tgz\nmock-debug.tgz' |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 321 | self.mox.StubOutWithMock(gsutil_util, 'GSUtilRun') |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 322 | |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 323 | # GSUtil cat gs://archive_url_prefix/UPLOADED. |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 324 | gsutil_util.GSUtilRun(mox.StrContains(common_util.UPLOADED_LIST), |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 325 | mox.IgnoreArg()).AndReturn(mock_data) |
| 326 | |
| 327 | self.mox.ReplayAll() |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 328 | artifacts = common_util.GatherSymbolArtifactDownloads( |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 329 | self._static_dir, archive_url_prefix, self._install_dir) |
| 330 | for index, artifact in enumerate(artifacts): |
| 331 | self.assertEqual(artifact._gs_path, symbol_url) |
| 332 | self.assertTrue(artifact._tmp_staging_dir.startswith(self._static_dir)) |
| 333 | |
| 334 | self.mox.VerifyAll() |
| 335 | |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 336 | def testIsAvailable(self): |
| 337 | """Test that we can detect whether the target artifacts are avaialble.""" |
| 338 | # Test when the all target files are available |
| 339 | pattern_list = ['_full_', 'autotest.tar'] |
| 340 | uploaded_list = ['chromeos_R17-1413.0.0-a1_x86-mario_full_dev.bin', |
| 341 | 'debug.tgz', |
| 342 | 'autotest.tar.bz2'] |
| 343 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 344 | available = common_util.IsAvailable(pattern_list, uploaded_list) |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 345 | self.assertTrue(available) |
| 346 | |
| 347 | # Test when some target files are missing |
| 348 | pattern_list = ['_full_', 'autotest.tar'] |
| 349 | uploaded_list = ['chromeos_R17-1413.0.0-a1_x86-mario_full_dev.bin', |
| 350 | 'debug.tgz'] |
| 351 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 352 | available = common_util.IsAvailable(pattern_list, uploaded_list) |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 353 | self.assertFalse(available) |
| 354 | |
| 355 | def testWaitUntilAvailable(self): |
| 356 | """Test that we can poll until all target artifacts are available.""" |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 357 | build = 'R17-1413.0.0-a1-b1346' |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 358 | archive_url = ('gs://chromeos-image-archive/x86-mario-release/' |
| 359 | 'R17-1413.0.0-a1-b1346') |
| 360 | to_wait_list = ['_full_'] |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 361 | mock_data = 'mock data\nmock_data\nmock_data' |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 362 | |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 363 | self.mox.StubOutWithMock(gsutil_util, 'GSUtilRun') |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 364 | self.mox.StubOutWithMock(common_util, 'IsAvailable') |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 365 | |
| 366 | # GSUtil cat gs://archive_url_prefix/UPLOADED. |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 367 | gsutil_util.GSUtilRun(mox.StrContains(common_util.UPLOADED_LIST), |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 368 | mox.IgnoreArg()).AndReturn(mock_data) |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 369 | common_util.IsAvailable(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(True) |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 370 | |
| 371 | self.mox.ReplayAll() |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 372 | uploaded_list = common_util.WaitUntilAvailable( |
| 373 | to_wait_list, archive_url, 'UNIT TEST', delay=1) |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 374 | self.assertEqual(uploaded_list, mock_data.splitlines()) |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 375 | self.mox.VerifyAll() |
| 376 | |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 377 | def testWaitUntilAvailableWithRetry(self): |
| 378 | """Test that we can poll until all target artifacts are available.""" |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 379 | build = 'R17-1413.0.0-a1-b1346' |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 380 | archive_url = ('gs://chromeos-image-archive/x86-mario-release/' |
| 381 | 'R17-1413.0.0-a1-b1346') |
| 382 | to_wait_list = ['_full_'] |
| 383 | mock_data = 'mock data\nmock_data\nmock_data' |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 384 | |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 385 | self.mox.StubOutWithMock(gsutil_util, 'GSUtilRun') |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 386 | self.mox.StubOutWithMock(common_util, 'IsAvailable') |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 387 | |
| 388 | # GSUtil cat gs://archive_url_prefix/UPLOADED. |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 389 | gsutil_util.GSUtilRun(mox.StrContains(common_util.UPLOADED_LIST), |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 390 | mox.IgnoreArg()).AndReturn(mock_data) |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 391 | common_util.IsAvailable(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(False) |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 392 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 393 | gsutil_util.GSUtilRun(mox.StrContains(common_util.UPLOADED_LIST), |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 394 | mox.IgnoreArg()).AndReturn(mock_data) |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 395 | common_util.IsAvailable(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(True) |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 396 | |
| 397 | self.mox.ReplayAll() |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 398 | uploaded_list = common_util.WaitUntilAvailable( |
| 399 | to_wait_list, archive_url, 'UNIT TEST', delay=1) |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 400 | self.assertEqual(uploaded_list, mock_data.splitlines()) |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 401 | self.mox.VerifyAll() |
| 402 | |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 403 | def testWaitUntilAvailableTimeout(self): |
| 404 | """Test that we wait for the target artifacts until timeout occurs.""" |
| 405 | build = 'R17-1413.0.0-a1-b1346' |
| 406 | archive_url = ('gs://chromeos-image-archive/x86-mario-release/' |
| 407 | 'R17-1413.0.0-a1-b1346') |
| 408 | to_wait_list = ['_full_'] |
| 409 | mock_data = 'mock data\nmock_data\nmock_data' |
| 410 | |
| 411 | self.mox.StubOutWithMock(gsutil_util, 'GSUtilRun') |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 412 | self.mox.StubOutWithMock(common_util, 'IsAvailable') |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 413 | |
| 414 | # GSUtil cat gs://archive_url_prefix/UPLOADED. |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 415 | gsutil_util.GSUtilRun(mox.StrContains(common_util.UPLOADED_LIST), |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 416 | mox.IgnoreArg()).AndReturn(mock_data) |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 417 | common_util.IsAvailable(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(False) |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 418 | |
| 419 | self.mox.ReplayAll() |
Gilad Arnold | 17fe03d | 2012-10-02 10:05:01 -0700 | [diff] [blame] | 420 | self.assertRaises(common_util.CommonUtilError, |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 421 | common_util.WaitUntilAvailable, |
Yu-Ju Hong | 825ddc3 | 2012-08-13 18:47:10 -0700 | [diff] [blame] | 422 | to_wait_list, |
| 423 | archive_url, |
| 424 | 'UNIT TEST', |
| 425 | delay=2, |
| 426 | timeout=1) |
| 427 | self.mox.VerifyAll() |
Frank Farzan | 37761d1 | 2011-12-01 14:29:08 -0800 | [diff] [blame] | 428 | |
| 429 | if __name__ == '__main__': |
| 430 | unittest.main() |