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