blob: eb5bf4a98c30273a07eaca2720d7aa19c0a9a956 [file] [log] [blame]
Chris Sosa47a7d4e2012-03-28 11:26:55 -07001#!/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 Arnoldc65330c2012-09-20 15:17:48 -07007"""Unit tests for build_artifact module.
Chris Sosa47a7d4e2012-03-28 11:26:55 -07008
9These unit tests take tarball from google storage locations to fully test
Chris Sosa76e44b92013-01-31 12:11:38 -080010the artifact download process. Please make sure to set up your boto file.
Chris Sosa47a7d4e2012-03-28 11:26:55 -070011"""
12
Chris Sosa47a7d4e2012-03-28 11:26:55 -070013import os
Gilad Arnold1638d822013-11-07 23:38:16 -080014import random
Chris Sosa47a7d4e2012-03-28 11:26:55 -070015import shutil
16import subprocess
17import tempfile
18import unittest
19
Gilad Arnoldabb352e2012-09-23 01:24:27 -070020import mox
21
Gilad Arnoldc65330c2012-09-20 15:17:48 -070022import build_artifact
Gilad Arnold1638d822013-11-07 23:38:16 -080023import devserver_constants
Chris Sosa47a7d4e2012-03-28 11:26:55 -070024
Gilad Arnoldabb352e2012-09-23 01:24:27 -070025
Chris Sosa76e44b92013-01-31 12:11:38 -080026_VERSION = 'R26-3646.0.0-rc1'
Chris Sosa47a7d4e2012-03-28 11:26:55 -070027_TEST_GOLO_ARCHIVE = (
Chris Sosa76e44b92013-01-31 12:11:38 -080028 'gs://chromeos-image-archive/x86-generic-chromium-pfq/R26-3646.0.0-rc1')
Dan Shi6e50c722013-08-19 15:05:06 -070029_TEST_NON_EXISTING_GOLO_ARCHIVE = (
30 'gs://chromeos-image-archive/x86-generic-chromium-pfq/R26-no_such_build')
Chris Sosa76e44b92013-01-31 12:11:38 -080031
Gilad Arnold1638d822013-11-07 23:38:16 -080032_TEST_GOLO_ARCHIVE_TEST_TARBALL_CONTENT = [
33 'autotest/test_suites/control.PGO_record',
34 'autotest/test_suites/control.au',
35 'autotest/test_suites/control.audio',
36 'autotest/test_suites/control.browsertests',
37 'autotest/test_suites/control.bvt',
38 'autotest/test_suites/control.dummy',
39 'autotest/test_suites/control.enterprise',
40 'autotest/test_suites/control.enterprise_enroll',
41 'autotest/test_suites/control.faft_dev',
42 'autotest/test_suites/control.faft_ec',
43 'autotest/test_suites/control.faft_normal',
44 'autotest/test_suites/control.graphics',
45 'autotest/test_suites/control.graphicsGLES',
46 'autotest/test_suites/control.hwqual',
47 'autotest/test_suites/control.kernel_daily_benchmarks',
48 'autotest/test_suites/control.kernel_daily_regression',
49 'autotest/test_suites/control.kernel_per-build_benchmarks',
50 'autotest/test_suites/control.kernel_per-build_regression',
51 'autotest/test_suites/control.kernel_weekly_regression',
52 'autotest/test_suites/control.link_perf',
53 'autotest/test_suites/control.network3g',
54 'autotest/test_suites/control.network3g_gobi',
55 'autotest/test_suites/control.network_wifi',
56 'autotest/test_suites/control.onccell',
57 'autotest/test_suites/control.pagecycler',
58 'autotest/test_suites/control.perfalerts',
59 'autotest/test_suites/control.power_build',
60 'autotest/test_suites/control.power_daily',
61 'autotest/test_suites/control.power_requirements',
62 'autotest/test_suites/control.pyauto',
63 'autotest/test_suites/control.pyauto_basic',
64 'autotest/test_suites/control.pyauto_endurance',
65 'autotest/test_suites/control.pyauto_perf',
66 'autotest/test_suites/control.regression',
67 'autotest/test_suites/control.security',
68 'autotest/test_suites/control.servo',
69 'autotest/test_suites/control.smoke',
70 'autotest/test_suites/control.sync',
71 'autotest/test_suites/control.vda',
72 'autotest/test_suites/control.video',
73 'autotest/test_suites/control.webrtc',
74 'autotest/test_suites/control.wificell',
75 'autotest/test_suites/control.wifichaos',
76 'autotest/test_suites/dependency_info',
77 'autotest/test_suites/dev_harness.py',
78]
79
80_TEST_GOLO_ARCHIVE_IMAGE_ZIPFILE_CONTENT = [
81 'au-generator.zip',
82 'boot.config',
83 'boot.desc',
84 'chromiumos_qemu_image.bin',
85 'chromiumos_test_image.bin',
86 'config.txt',
87 'mount_image.sh',
88 'oem.image',
89 'pack_partitions.sh',
90 'umount_image.sh',
91 'unpack_partitions.sh',
92]
93
94
Chris Sosa76e44b92013-01-31 12:11:38 -080095# Different as the above does not have deltas (for smaller artifacts).
96_DELTA_VERSION = 'R26-3645.0.0'
97_TEST_GOLO_FOR_DELTAS = (
98 'gs://chromeos-image-archive/x86-mario-release/R26-3645.0.0')
Yu-Ju Honge61cbe92012-07-10 14:10:26 -070099
Chris Sosa47a7d4e2012-03-28 11:26:55 -0700100
Chris Sosa76e44b92013-01-31 12:11:38 -0800101# pylint: disable=W0212
Gilad Arnoldc65330c2012-09-20 15:17:48 -0700102class BuildArtifactTest(mox.MoxTestBase):
Chris Sosa47a7d4e2012-03-28 11:26:55 -0700103
104 def setUp(self):
105 mox.MoxTestBase.setUp(self)
Chris Sosa76e44b92013-01-31 12:11:38 -0800106 self.work_dir = tempfile.mkdtemp('build_artifact_unittest')
Chris Sosa47a7d4e2012-03-28 11:26:55 -0700107
108 def tearDown(self):
109 shutil.rmtree(self.work_dir)
110
Gilad Arnold1638d822013-11-07 23:38:16 -0800111 def _CheckMarker(self, marker_file, installed_files):
112 with open(os.path.join(self.work_dir, marker_file)) as f:
113 self.assertItemsEqual(installed_files, [line.strip() for line in f])
114
Chris Sosa76e44b92013-01-31 12:11:38 -0800115 def testProcessBuildArtifact(self):
116 """Processes a real tarball from GSUtil and stages it."""
Gilad Arnoldc65330c2012-09-20 15:17:48 -0700117 artifact = build_artifact.BuildArtifact(
Chris Sosa76e44b92013-01-31 12:11:38 -0800118 self.work_dir,
119 _TEST_GOLO_ARCHIVE, build_artifact.TEST_SUITES_FILE, _VERSION)
120 artifact.Process(False)
Gilad Arnold1638d822013-11-07 23:38:16 -0800121 self.assertItemsEqual(
122 artifact.installed_files,
123 [os.path.join(self.work_dir, build_artifact.TEST_SUITES_FILE)])
Chris Sosa47a7d4e2012-03-28 11:26:55 -0700124 self.assertTrue(os.path.exists(os.path.join(
Chris Sosa76e44b92013-01-31 12:11:38 -0800125 self.work_dir, build_artifact.TEST_SUITES_FILE)))
Gilad Arnold1638d822013-11-07 23:38:16 -0800126 self._CheckMarker(artifact.marker_name, artifact.installed_files)
Chris Sosa47a7d4e2012-03-28 11:26:55 -0700127
Chris Sosa76e44b92013-01-31 12:11:38 -0800128 def testProcessTarball(self):
Chris Sosa47a7d4e2012-03-28 11:26:55 -0700129 """Downloads a real tarball and untars it."""
Gilad Arnoldc65330c2012-09-20 15:17:48 -0700130 artifact = build_artifact.TarballBuildArtifact(
Chris Sosa76e44b92013-01-31 12:11:38 -0800131 self.work_dir, _TEST_GOLO_ARCHIVE, build_artifact.TEST_SUITES_FILE,
132 _VERSION)
Gilad Arnold1638d822013-11-07 23:38:16 -0800133 expected_installed_files = [
134 os.path.join(self.work_dir, filename)
135 for filename in ([build_artifact.TEST_SUITES_FILE] +
136 _TEST_GOLO_ARCHIVE_TEST_TARBALL_CONTENT)]
Chris Sosa76e44b92013-01-31 12:11:38 -0800137 artifact.Process(False)
Gilad Arnold1638d822013-11-07 23:38:16 -0800138 self.assertItemsEqual(artifact.installed_files, expected_installed_files)
Chris Sosa47a7d4e2012-03-28 11:26:55 -0700139 self.assertTrue(os.path.isdir(os.path.join(
Chris Sosa76e44b92013-01-31 12:11:38 -0800140 self.work_dir, 'autotest', 'test_suites')))
Gilad Arnold1638d822013-11-07 23:38:16 -0800141 self._CheckMarker(artifact.marker_name, artifact.installed_files)
Chris Sosa47a7d4e2012-03-28 11:26:55 -0700142
Chris Sosa76e44b92013-01-31 12:11:38 -0800143 def testProcessTarballWithFile(self):
144 """Downloads a real tarball and only untars one file from it."""
145 file_to_download = 'autotest/test_suites/control.au'
146 artifact = build_artifact.TarballBuildArtifact(
147 self.work_dir, _TEST_GOLO_ARCHIVE, build_artifact.TEST_SUITES_FILE,
Gilad Arnold1638d822013-11-07 23:38:16 -0800148 _VERSION, files_to_extract=[file_to_download])
149 expected_installed_files = [
150 os.path.join(self.work_dir, filename)
151 for filename in [build_artifact.TEST_SUITES_FILE] + [file_to_download]]
Chris Sosa76e44b92013-01-31 12:11:38 -0800152 artifact.Process(False)
Gilad Arnold1638d822013-11-07 23:38:16 -0800153 self.assertItemsEqual(artifact.installed_files, expected_installed_files)
Chris Sosa76e44b92013-01-31 12:11:38 -0800154 self.assertTrue(os.path.exists(os.path.join(
155 self.work_dir, file_to_download)))
Gilad Arnold1638d822013-11-07 23:38:16 -0800156 self._CheckMarker(artifact.marker_name, artifact.installed_files)
Chris Sosa47a7d4e2012-03-28 11:26:55 -0700157
joychen0a8e34e2013-06-24 17:58:36 -0700158 def testDownloadAutotest(self):
Yu-Ju Honge61cbe92012-07-10 14:10:26 -0700159 """Downloads a real autotest tarball for test."""
Gilad Arnoldc65330c2012-09-20 15:17:48 -0700160 self.mox.StubOutWithMock(build_artifact.AutotestTarballBuildArtifact,
Chris Sosa76e44b92013-01-31 12:11:38 -0800161 '_Extract')
162 artifact = build_artifact.AutotestTarballBuildArtifact(
163 self.work_dir, _TEST_GOLO_ARCHIVE, build_artifact.AUTOTEST_FILE,
164 _VERSION, None, ['autotest/test_suites'])
165
joychen0a8e34e2013-06-24 17:58:36 -0700166 install_dir = self.work_dir
167 artifact.staging_dir = install_dir
Chris Sosa47a7d4e2012-03-28 11:26:55 -0700168 self.mox.StubOutWithMock(subprocess, 'check_call')
joychen0a8e34e2013-06-24 17:58:36 -0700169 subprocess.check_call(mox.In('autotest/utils/packager.py'), cwd=install_dir)
Gilad Arnold02dc6552013-11-14 11:27:54 -0800170 self.mox.StubOutWithMock(artifact, '_WaitForArtifactToExist')
171 artifact._WaitForArtifactToExist(1)
Gilad Arnold1638d822013-11-07 23:38:16 -0800172 artifact._Download()
173 artifact._Extract()
Chris Sosa47a7d4e2012-03-28 11:26:55 -0700174 self.mox.ReplayAll()
Gilad Arnold1638d822013-11-07 23:38:16 -0800175 artifact.Process(True)
Chris Sosa47a7d4e2012-03-28 11:26:55 -0700176 self.mox.VerifyAll()
Gilad Arnold1638d822013-11-07 23:38:16 -0800177 self.assertItemsEqual(artifact.installed_files, [])
Chris Sosa76e44b92013-01-31 12:11:38 -0800178 self.assertTrue(os.path.isdir(
179 os.path.join(self.work_dir, 'autotest', 'packages')))
Gilad Arnold1638d822013-11-07 23:38:16 -0800180 self._CheckMarker(artifact.marker_name, [])
Chris Sosa47a7d4e2012-03-28 11:26:55 -0700181
Gilad Arnoldc65330c2012-09-20 15:17:48 -0700182 def testAUTestPayloadBuildArtifact(self):
Chris Sosa47a7d4e2012-03-28 11:26:55 -0700183 """Downloads a real tarball and treats it like an AU payload."""
Gilad Arnoldc65330c2012-09-20 15:17:48 -0700184 artifact = build_artifact.AUTestPayloadBuildArtifact(
Chris Sosa76e44b92013-01-31 12:11:38 -0800185 self.work_dir, _TEST_GOLO_ARCHIVE, build_artifact.TEST_SUITES_FILE,
186 _VERSION)
Gilad Arnold1638d822013-11-07 23:38:16 -0800187 expected_installed_files = [
188 os.path.join(self.work_dir, devserver_constants.UPDATE_FILE)]
Chris Sosa76e44b92013-01-31 12:11:38 -0800189 artifact.Process(False)
Gilad Arnold1638d822013-11-07 23:38:16 -0800190 self.assertItemsEqual(artifact.installed_files, expected_installed_files)
Chris Sosa47a7d4e2012-03-28 11:26:55 -0700191 self.assertTrue(os.path.exists(os.path.join(
Gilad Arnold1638d822013-11-07 23:38:16 -0800192 self.work_dir, devserver_constants.UPDATE_FILE)))
193 self._CheckMarker(artifact.marker_name, artifact.installed_files)
Chris Sosa76e44b92013-01-31 12:11:38 -0800194
195 def testDeltaPayloadsArtifact(self):
196 """Downloads delta paylaods from test bucket."""
197 artifact = build_artifact.DeltaPayloadsArtifact(
Gilad Arnold950569b2013-08-27 14:38:01 -0700198 self.work_dir, _TEST_GOLO_FOR_DELTAS, 'DONTCARE', _DELTA_VERSION)
Gilad Arnold1638d822013-11-07 23:38:16 -0800199 delta_installed_files = ('update.gz', 'stateful.tgz')
Chris Sosa76e44b92013-01-31 12:11:38 -0800200 nton_dir = os.path.join(self.work_dir, 'au', '%s_nton' % _DELTA_VERSION)
201 mton_dir = os.path.join(self.work_dir, 'au', '%s_mton' % _DELTA_VERSION)
Gilad Arnold1638d822013-11-07 23:38:16 -0800202 expected_installed_files = ([os.path.join(nton_dir, filename)
203 for filename in delta_installed_files] +
204 [os.path.join(mton_dir, filename)
205 for filename in delta_installed_files])
206 artifact.Process(False)
207 self.assertItemsEqual(artifact.installed_files, expected_installed_files)
Chris Sosa76e44b92013-01-31 12:11:38 -0800208 self.assertTrue(os.path.exists(os.path.join(nton_dir, 'update.gz')))
209 self.assertTrue(os.path.exists(os.path.join(mton_dir, 'update.gz')))
Gilad Arnold1638d822013-11-07 23:38:16 -0800210 self._CheckMarker(artifact.marker_name, artifact.installed_files)
Chris Sosa76e44b92013-01-31 12:11:38 -0800211
212 def testImageUnzip(self):
213 """Downloads and stages a zip file and extracts a test image."""
Gilad Arnold1638d822013-11-07 23:38:16 -0800214 files_to_extract = ['chromiumos_test_image.bin']
Chris Sosa76e44b92013-01-31 12:11:38 -0800215 artifact = build_artifact.ZipfileBuildArtifact(
216 self.work_dir, _TEST_GOLO_ARCHIVE, build_artifact.IMAGE_FILE,
Gilad Arnold1638d822013-11-07 23:38:16 -0800217 _VERSION, files_to_extract=files_to_extract)
218 expected_installed_files = [
219 os.path.join(self.work_dir, filename)
220 for filename in [build_artifact.IMAGE_FILE] + files_to_extract]
Chris Sosa76e44b92013-01-31 12:11:38 -0800221 artifact.Process(False)
Gilad Arnold1638d822013-11-07 23:38:16 -0800222 self.assertItemsEqual(expected_installed_files, artifact.installed_files)
Chris Sosa47a7d4e2012-03-28 11:26:55 -0700223 self.assertTrue(os.path.exists(os.path.join(
Chris Sosa76e44b92013-01-31 12:11:38 -0800224 self.work_dir, 'chromiumos_test_image.bin')))
Gilad Arnold1638d822013-11-07 23:38:16 -0800225 self._CheckMarker(artifact.marker_name, artifact.installed_files)
Chris Sosa76e44b92013-01-31 12:11:38 -0800226
227 def testImageUnzipWithExcludes(self):
228 """Downloads and stages a zip file while excluding all large files."""
229 artifact = build_artifact.ZipfileBuildArtifact(
230 self.work_dir, _TEST_GOLO_ARCHIVE, build_artifact.IMAGE_FILE,
Gilad Arnold950569b2013-08-27 14:38:01 -0700231 _VERSION, exclude=['*.bin'])
Gilad Arnold1638d822013-11-07 23:38:16 -0800232 expected_extracted_files = [
233 filename for filename in _TEST_GOLO_ARCHIVE_IMAGE_ZIPFILE_CONTENT
234 if not filename.endswith('.bin')]
235 expected_installed_files = [
236 os.path.join(self.work_dir, filename)
237 for filename in [build_artifact.IMAGE_FILE] + expected_extracted_files]
Chris Sosa76e44b92013-01-31 12:11:38 -0800238 artifact.Process(False)
Gilad Arnold1638d822013-11-07 23:38:16 -0800239 self.assertItemsEqual(expected_installed_files, artifact.installed_files)
Chris Sosa76e44b92013-01-31 12:11:38 -0800240 self.assertFalse(os.path.exists(os.path.join(
241 self.work_dir, 'chromiumos_test_image.bin')))
Gilad Arnold1638d822013-11-07 23:38:16 -0800242 self._CheckMarker(artifact.marker_name, artifact.installed_files)
Chris Sosa47a7d4e2012-03-28 11:26:55 -0700243
Chris Sosa6b0c6172013-08-05 17:01:33 -0700244 def testArtifactFactory(self):
245 """Tests that BuildArtifact logic works for both named and file artifacts.
246 """
247 name_artifact = 'test_suites' # This file is in every real GS dir.
248 file_artifact = 'metadata.json' # This file is in every real GS dir.
249 factory = build_artifact.ArtifactFactory(self.work_dir, _TEST_GOLO_ARCHIVE,
250 [name_artifact], [file_artifact],
251 _VERSION)
252 artifacts = factory.RequiredArtifacts()
253 self.assertEqual(len(artifacts), 2)
Gilad Arnold1638d822013-11-07 23:38:16 -0800254 expected_installed_files_0 = [
255 os.path.join(self.work_dir, filename) for filename
256 in ([build_artifact.TEST_SUITES_FILE] +
257 _TEST_GOLO_ARCHIVE_TEST_TARBALL_CONTENT)]
258 expected_installed_files_1 = [os.path.join(self.work_dir, file_artifact)]
Chris Sosa6b0c6172013-08-05 17:01:33 -0700259 artifacts[0].Process(False)
260 artifacts[1].Process(False)
Gilad Arnold1638d822013-11-07 23:38:16 -0800261 self.assertItemsEqual(artifacts[0].installed_files,
262 expected_installed_files_0)
263 self.assertItemsEqual(artifacts[1].installed_files,
264 expected_installed_files_1)
Chris Sosa6b0c6172013-08-05 17:01:33 -0700265 # Test suites directory exists.
266 self.assertTrue(os.path.exists(os.path.join(
267 self.work_dir, 'autotest', 'test_suites')))
268 # File artifact was staged.
269 self.assertTrue(os.path.exists(os.path.join(self.work_dir,
270 file_artifact)))
Gilad Arnold1638d822013-11-07 23:38:16 -0800271 self._CheckMarker(artifacts[0].marker_name, artifacts[0].installed_files)
272 self._CheckMarker(artifacts[1].marker_name, artifacts[1].installed_files)
Chris Sosa6b0c6172013-08-05 17:01:33 -0700273
Dan Shi6e50c722013-08-19 15:05:06 -0700274 def testProcessBuildArtifactWithException(self):
275 """Test processing a non-existing artifact from GSUtil."""
276 artifact = build_artifact.BuildArtifact(
277 self.work_dir, _TEST_NON_EXISTING_GOLO_ARCHIVE,
278 build_artifact.TEST_SUITES_FILE, _VERSION)
279 try:
280 artifact.Process(False)
281 except Exception as e:
282 expected_exception = e
283 exception = artifact.GetException()
284 self.assertEqual(str(exception), str(expected_exception))
285
Gilad Arnold1638d822013-11-07 23:38:16 -0800286 def testArtifactStaged(self):
287 """Tests the artifact staging verification logic."""
288 artifact = build_artifact.TarballBuildArtifact(
289 self.work_dir, _TEST_GOLO_ARCHIVE, build_artifact.TEST_SUITES_FILE,
290 _VERSION)
291 expected_installed_files = [
292 os.path.join(self.work_dir, filename)
293 for filename in ([build_artifact.TEST_SUITES_FILE] +
294 _TEST_GOLO_ARCHIVE_TEST_TARBALL_CONTENT)]
295 artifact.Process(False)
296
297 # Check that it works when all files are there.
298 self.assertTrue(artifact.ArtifactStaged())
299
300 # Remove an arbitrary file among the ones staged, ensure the check fails
301 # and that the marker files is removed.
302 os.remove(random.choice(expected_installed_files))
303 self.assertTrue(os.path.exists(os.path.join(self.work_dir,
304 artifact.marker_name)))
305 self.assertFalse(artifact.ArtifactStaged())
306 self.assertFalse(os.path.exists(os.path.join(self.work_dir,
307 artifact.marker_name)))
308
Chris Sosa47a7d4e2012-03-28 11:26:55 -0700309
310if __name__ == '__main__':
311 unittest.main()