Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 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 build_artifact module. |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 8 | |
| 9 | These unit tests take tarball from google storage locations to fully test |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 10 | the artifact download process. Please make sure to set up your boto file. |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 11 | """ |
| 12 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 13 | import os |
| 14 | import shutil |
| 15 | import subprocess |
| 16 | import tempfile |
| 17 | import unittest |
| 18 | |
Gilad Arnold | abb352e | 2012-09-23 01:24:27 -0700 | [diff] [blame] | 19 | import mox |
| 20 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 21 | import build_artifact |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 22 | |
Gilad Arnold | abb352e | 2012-09-23 01:24:27 -0700 | [diff] [blame] | 23 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 24 | _VERSION = 'R26-3646.0.0-rc1' |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 25 | _TEST_GOLO_ARCHIVE = ( |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 26 | 'gs://chromeos-image-archive/x86-generic-chromium-pfq/R26-3646.0.0-rc1') |
Dan Shi | 6e50c72 | 2013-08-19 15:05:06 -0700 | [diff] [blame] | 27 | _TEST_NON_EXISTING_GOLO_ARCHIVE = ( |
| 28 | 'gs://chromeos-image-archive/x86-generic-chromium-pfq/R26-no_such_build') |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 29 | |
| 30 | # Different as the above does not have deltas (for smaller artifacts). |
| 31 | _DELTA_VERSION = 'R26-3645.0.0' |
| 32 | _TEST_GOLO_FOR_DELTAS = ( |
| 33 | 'gs://chromeos-image-archive/x86-mario-release/R26-3645.0.0') |
Yu-Ju Hong | e61cbe9 | 2012-07-10 14:10:26 -0700 | [diff] [blame] | 34 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 35 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 36 | # pylint: disable=W0212 |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 37 | class BuildArtifactTest(mox.MoxTestBase): |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 38 | |
| 39 | def setUp(self): |
| 40 | mox.MoxTestBase.setUp(self) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 41 | self.work_dir = tempfile.mkdtemp('build_artifact_unittest') |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 42 | |
| 43 | def tearDown(self): |
| 44 | shutil.rmtree(self.work_dir) |
| 45 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 46 | def testProcessBuildArtifact(self): |
| 47 | """Processes a real tarball from GSUtil and stages it.""" |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 48 | artifact = build_artifact.BuildArtifact( |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 49 | self.work_dir, |
| 50 | _TEST_GOLO_ARCHIVE, build_artifact.TEST_SUITES_FILE, _VERSION) |
| 51 | artifact.Process(False) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 52 | self.assertTrue(os.path.exists(os.path.join( |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 53 | self.work_dir, build_artifact.TEST_SUITES_FILE))) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 54 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 55 | def testProcessTarball(self): |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 56 | """Downloads a real tarball and untars it.""" |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 57 | artifact = build_artifact.TarballBuildArtifact( |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 58 | self.work_dir, _TEST_GOLO_ARCHIVE, build_artifact.TEST_SUITES_FILE, |
| 59 | _VERSION) |
| 60 | artifact.Process(False) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 61 | self.assertTrue(os.path.isdir(os.path.join( |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 62 | self.work_dir, 'autotest', 'test_suites'))) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 63 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 64 | def testProcessTarballWithFile(self): |
| 65 | """Downloads a real tarball and only untars one file from it.""" |
| 66 | file_to_download = 'autotest/test_suites/control.au' |
| 67 | artifact = build_artifact.TarballBuildArtifact( |
| 68 | self.work_dir, _TEST_GOLO_ARCHIVE, build_artifact.TEST_SUITES_FILE, |
| 69 | _VERSION, [file_to_download]) |
| 70 | artifact.Process(False) |
| 71 | self.assertTrue(os.path.exists(os.path.join( |
| 72 | self.work_dir, file_to_download))) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 73 | |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 74 | def testDownloadAutotest(self): |
Yu-Ju Hong | e61cbe9 | 2012-07-10 14:10:26 -0700 | [diff] [blame] | 75 | """Downloads a real autotest tarball for test.""" |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 76 | self.mox.StubOutWithMock(build_artifact.AutotestTarballBuildArtifact, |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 77 | '_Extract') |
| 78 | artifact = build_artifact.AutotestTarballBuildArtifact( |
| 79 | self.work_dir, _TEST_GOLO_ARCHIVE, build_artifact.AUTOTEST_FILE, |
| 80 | _VERSION, None, ['autotest/test_suites']) |
| 81 | |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 82 | install_dir = self.work_dir |
| 83 | artifact.staging_dir = install_dir |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 84 | artifact._Download() |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 85 | self.mox.StubOutWithMock(subprocess, 'check_call') |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 86 | artifact._Extract() |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 87 | subprocess.check_call(mox.In('autotest/utils/packager.py'), cwd=install_dir) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 88 | self.mox.ReplayAll() |
joychen | 0a8e34e | 2013-06-24 17:58:36 -0700 | [diff] [blame] | 89 | artifact._Setup() |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 90 | self.mox.VerifyAll() |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 91 | self.assertTrue(os.path.isdir( |
| 92 | os.path.join(self.work_dir, 'autotest', 'packages'))) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 93 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 94 | def testAUTestPayloadBuildArtifact(self): |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 95 | """Downloads a real tarball and treats it like an AU payload.""" |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 96 | artifact = build_artifact.AUTestPayloadBuildArtifact( |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 97 | self.work_dir, _TEST_GOLO_ARCHIVE, build_artifact.TEST_SUITES_FILE, |
| 98 | _VERSION) |
| 99 | artifact.Process(False) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 100 | self.assertTrue(os.path.exists(os.path.join( |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 101 | self.work_dir, 'update.gz'))) |
| 102 | |
| 103 | def testDeltaPayloadsArtifact(self): |
| 104 | """Downloads delta paylaods from test bucket.""" |
| 105 | artifact = build_artifact.DeltaPayloadsArtifact( |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 106 | self.work_dir, _TEST_GOLO_FOR_DELTAS, 'DONTCARE', _DELTA_VERSION) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 107 | artifact.Process(False) |
| 108 | nton_dir = os.path.join(self.work_dir, 'au', '%s_nton' % _DELTA_VERSION) |
| 109 | mton_dir = os.path.join(self.work_dir, 'au', '%s_mton' % _DELTA_VERSION) |
| 110 | self.assertTrue(os.path.exists(os.path.join(nton_dir, 'update.gz'))) |
| 111 | self.assertTrue(os.path.exists(os.path.join(mton_dir, 'update.gz'))) |
| 112 | |
| 113 | def testImageUnzip(self): |
| 114 | """Downloads and stages a zip file and extracts a test image.""" |
| 115 | artifact = build_artifact.ZipfileBuildArtifact( |
| 116 | self.work_dir, _TEST_GOLO_ARCHIVE, build_artifact.IMAGE_FILE, |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 117 | _VERSION, files_to_extract=['chromiumos_test_image.bin']) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 118 | artifact.Process(False) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 119 | self.assertTrue(os.path.exists(os.path.join( |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 120 | self.work_dir, 'chromiumos_test_image.bin'))) |
| 121 | |
| 122 | def testImageUnzipWithExcludes(self): |
| 123 | """Downloads and stages a zip file while excluding all large files.""" |
| 124 | artifact = build_artifact.ZipfileBuildArtifact( |
| 125 | self.work_dir, _TEST_GOLO_ARCHIVE, build_artifact.IMAGE_FILE, |
Gilad Arnold | 950569b | 2013-08-27 14:38:01 -0700 | [diff] [blame] | 126 | _VERSION, exclude=['*.bin']) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 127 | artifact.Process(False) |
| 128 | self.assertFalse(os.path.exists(os.path.join( |
| 129 | self.work_dir, 'chromiumos_test_image.bin'))) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 130 | |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 131 | def testArtifactFactory(self): |
| 132 | """Tests that BuildArtifact logic works for both named and file artifacts. |
| 133 | """ |
| 134 | name_artifact = 'test_suites' # This file is in every real GS dir. |
| 135 | file_artifact = 'metadata.json' # This file is in every real GS dir. |
| 136 | factory = build_artifact.ArtifactFactory(self.work_dir, _TEST_GOLO_ARCHIVE, |
| 137 | [name_artifact], [file_artifact], |
| 138 | _VERSION) |
| 139 | artifacts = factory.RequiredArtifacts() |
| 140 | self.assertEqual(len(artifacts), 2) |
| 141 | artifacts[0].Process(False) |
| 142 | artifacts[1].Process(False) |
| 143 | # Test suites directory exists. |
| 144 | self.assertTrue(os.path.exists(os.path.join( |
| 145 | self.work_dir, 'autotest', 'test_suites'))) |
| 146 | # File artifact was staged. |
| 147 | self.assertTrue(os.path.exists(os.path.join(self.work_dir, |
| 148 | file_artifact))) |
| 149 | |
Dan Shi | 6e50c72 | 2013-08-19 15:05:06 -0700 | [diff] [blame] | 150 | def testProcessBuildArtifactWithException(self): |
| 151 | """Test processing a non-existing artifact from GSUtil.""" |
| 152 | artifact = build_artifact.BuildArtifact( |
| 153 | self.work_dir, _TEST_NON_EXISTING_GOLO_ARCHIVE, |
| 154 | build_artifact.TEST_SUITES_FILE, _VERSION) |
| 155 | try: |
| 156 | artifact.Process(False) |
| 157 | except Exception as e: |
| 158 | expected_exception = e |
| 159 | exception = artifact.GetException() |
| 160 | self.assertEqual(str(exception), str(expected_exception)) |
| 161 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 162 | |
| 163 | if __name__ == '__main__': |
| 164 | unittest.main() |