blob: fddfccb31f94519e2ad75327d900d0df51bd7fc2 [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
14import shutil
15import subprocess
16import tempfile
17import unittest
18
Gilad Arnoldabb352e2012-09-23 01:24:27 -070019import mox
20
Gilad Arnoldc65330c2012-09-20 15:17:48 -070021import build_artifact
Chris Sosa47a7d4e2012-03-28 11:26:55 -070022
Gilad Arnoldabb352e2012-09-23 01:24:27 -070023
Chris Sosa76e44b92013-01-31 12:11:38 -080024_VERSION = 'R26-3646.0.0-rc1'
Chris Sosa47a7d4e2012-03-28 11:26:55 -070025_TEST_GOLO_ARCHIVE = (
Chris Sosa76e44b92013-01-31 12:11:38 -080026 'gs://chromeos-image-archive/x86-generic-chromium-pfq/R26-3646.0.0-rc1')
Dan Shi6e50c722013-08-19 15:05:06 -070027_TEST_NON_EXISTING_GOLO_ARCHIVE = (
28 'gs://chromeos-image-archive/x86-generic-chromium-pfq/R26-no_such_build')
Chris Sosa76e44b92013-01-31 12:11:38 -080029
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 Honge61cbe92012-07-10 14:10:26 -070034
Chris Sosa47a7d4e2012-03-28 11:26:55 -070035
Chris Sosa76e44b92013-01-31 12:11:38 -080036# pylint: disable=W0212
Gilad Arnoldc65330c2012-09-20 15:17:48 -070037class BuildArtifactTest(mox.MoxTestBase):
Chris Sosa47a7d4e2012-03-28 11:26:55 -070038
39 def setUp(self):
40 mox.MoxTestBase.setUp(self)
Chris Sosa76e44b92013-01-31 12:11:38 -080041 self.work_dir = tempfile.mkdtemp('build_artifact_unittest')
Chris Sosa47a7d4e2012-03-28 11:26:55 -070042
43 def tearDown(self):
44 shutil.rmtree(self.work_dir)
45
Chris Sosa76e44b92013-01-31 12:11:38 -080046 def testProcessBuildArtifact(self):
47 """Processes a real tarball from GSUtil and stages it."""
Gilad Arnoldc65330c2012-09-20 15:17:48 -070048 artifact = build_artifact.BuildArtifact(
Chris Sosa76e44b92013-01-31 12:11:38 -080049 self.work_dir,
50 _TEST_GOLO_ARCHIVE, build_artifact.TEST_SUITES_FILE, _VERSION)
51 artifact.Process(False)
Chris Sosa47a7d4e2012-03-28 11:26:55 -070052 self.assertTrue(os.path.exists(os.path.join(
Chris Sosa76e44b92013-01-31 12:11:38 -080053 self.work_dir, build_artifact.TEST_SUITES_FILE)))
Chris Sosa47a7d4e2012-03-28 11:26:55 -070054
Chris Sosa76e44b92013-01-31 12:11:38 -080055 def testProcessTarball(self):
Chris Sosa47a7d4e2012-03-28 11:26:55 -070056 """Downloads a real tarball and untars it."""
Gilad Arnoldc65330c2012-09-20 15:17:48 -070057 artifact = build_artifact.TarballBuildArtifact(
Chris Sosa76e44b92013-01-31 12:11:38 -080058 self.work_dir, _TEST_GOLO_ARCHIVE, build_artifact.TEST_SUITES_FILE,
59 _VERSION)
60 artifact.Process(False)
Chris Sosa47a7d4e2012-03-28 11:26:55 -070061 self.assertTrue(os.path.isdir(os.path.join(
Chris Sosa76e44b92013-01-31 12:11:38 -080062 self.work_dir, 'autotest', 'test_suites')))
Chris Sosa47a7d4e2012-03-28 11:26:55 -070063
Chris Sosa76e44b92013-01-31 12:11:38 -080064 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 Sosa47a7d4e2012-03-28 11:26:55 -070073
joychen0a8e34e2013-06-24 17:58:36 -070074 def testDownloadAutotest(self):
Yu-Ju Honge61cbe92012-07-10 14:10:26 -070075 """Downloads a real autotest tarball for test."""
Gilad Arnoldc65330c2012-09-20 15:17:48 -070076 self.mox.StubOutWithMock(build_artifact.AutotestTarballBuildArtifact,
Chris Sosa76e44b92013-01-31 12:11:38 -080077 '_Extract')
78 artifact = build_artifact.AutotestTarballBuildArtifact(
79 self.work_dir, _TEST_GOLO_ARCHIVE, build_artifact.AUTOTEST_FILE,
80 _VERSION, None, ['autotest/test_suites'])
81
joychen0a8e34e2013-06-24 17:58:36 -070082 install_dir = self.work_dir
83 artifact.staging_dir = install_dir
Chris Sosa76e44b92013-01-31 12:11:38 -080084 artifact._Download()
Chris Sosa47a7d4e2012-03-28 11:26:55 -070085 self.mox.StubOutWithMock(subprocess, 'check_call')
Chris Sosa76e44b92013-01-31 12:11:38 -080086 artifact._Extract()
joychen0a8e34e2013-06-24 17:58:36 -070087 subprocess.check_call(mox.In('autotest/utils/packager.py'), cwd=install_dir)
Chris Sosa47a7d4e2012-03-28 11:26:55 -070088 self.mox.ReplayAll()
joychen0a8e34e2013-06-24 17:58:36 -070089 artifact._Setup()
Chris Sosa47a7d4e2012-03-28 11:26:55 -070090 self.mox.VerifyAll()
Chris Sosa76e44b92013-01-31 12:11:38 -080091 self.assertTrue(os.path.isdir(
92 os.path.join(self.work_dir, 'autotest', 'packages')))
Chris Sosa47a7d4e2012-03-28 11:26:55 -070093
Gilad Arnoldc65330c2012-09-20 15:17:48 -070094 def testAUTestPayloadBuildArtifact(self):
Chris Sosa47a7d4e2012-03-28 11:26:55 -070095 """Downloads a real tarball and treats it like an AU payload."""
Gilad Arnoldc65330c2012-09-20 15:17:48 -070096 artifact = build_artifact.AUTestPayloadBuildArtifact(
Chris Sosa76e44b92013-01-31 12:11:38 -080097 self.work_dir, _TEST_GOLO_ARCHIVE, build_artifact.TEST_SUITES_FILE,
98 _VERSION)
99 artifact.Process(False)
Chris Sosa47a7d4e2012-03-28 11:26:55 -0700100 self.assertTrue(os.path.exists(os.path.join(
Chris Sosa76e44b92013-01-31 12:11:38 -0800101 self.work_dir, 'update.gz')))
102
103 def testDeltaPayloadsArtifact(self):
104 """Downloads delta paylaods from test bucket."""
105 artifact = build_artifact.DeltaPayloadsArtifact(
106 self.work_dir, _TEST_GOLO_FOR_DELTAS, '.*_delta_.*', _DELTA_VERSION)
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,
117 _VERSION, ['chromiumos_test_image.bin'])
118 artifact.Process(False)
Chris Sosa47a7d4e2012-03-28 11:26:55 -0700119 self.assertTrue(os.path.exists(os.path.join(
Chris Sosa76e44b92013-01-31 12:11:38 -0800120 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,
126 _VERSION, None, ['*.bin'])
127 artifact.Process(False)
128 self.assertFalse(os.path.exists(os.path.join(
129 self.work_dir, 'chromiumos_test_image.bin')))
Chris Sosa47a7d4e2012-03-28 11:26:55 -0700130
Chris Sosa6b0c6172013-08-05 17:01:33 -0700131 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 Shi6e50c722013-08-19 15:05:06 -0700150 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 Sosa47a7d4e2012-03-28 11:26:55 -0700162
163if __name__ == '__main__':
164 unittest.main()