blob: 7e0ee896f1ac4704a743b26a467154a3b5c85e4e [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')
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 Honge61cbe92012-07-10 14:10:26 -070032
Chris Sosa47a7d4e2012-03-28 11:26:55 -070033
Chris Sosa76e44b92013-01-31 12:11:38 -080034# pylint: disable=W0212
Gilad Arnoldc65330c2012-09-20 15:17:48 -070035class BuildArtifactTest(mox.MoxTestBase):
Chris Sosa47a7d4e2012-03-28 11:26:55 -070036
37 def setUp(self):
38 mox.MoxTestBase.setUp(self)
Chris Sosa76e44b92013-01-31 12:11:38 -080039 self.work_dir = tempfile.mkdtemp('build_artifact_unittest')
Chris Sosa47a7d4e2012-03-28 11:26:55 -070040
41 def tearDown(self):
42 shutil.rmtree(self.work_dir)
43
Chris Sosa76e44b92013-01-31 12:11:38 -080044 def testProcessBuildArtifact(self):
45 """Processes a real tarball from GSUtil and stages it."""
Gilad Arnoldc65330c2012-09-20 15:17:48 -070046 artifact = build_artifact.BuildArtifact(
Chris Sosa76e44b92013-01-31 12:11:38 -080047 self.work_dir,
48 _TEST_GOLO_ARCHIVE, build_artifact.TEST_SUITES_FILE, _VERSION)
49 artifact.Process(False)
Chris Sosa47a7d4e2012-03-28 11:26:55 -070050 self.assertTrue(os.path.exists(os.path.join(
Chris Sosa76e44b92013-01-31 12:11:38 -080051 self.work_dir, build_artifact.TEST_SUITES_FILE)))
Chris Sosa47a7d4e2012-03-28 11:26:55 -070052
Chris Sosa76e44b92013-01-31 12:11:38 -080053 def testProcessTarball(self):
Chris Sosa47a7d4e2012-03-28 11:26:55 -070054 """Downloads a real tarball and untars it."""
Gilad Arnoldc65330c2012-09-20 15:17:48 -070055 artifact = build_artifact.TarballBuildArtifact(
Chris Sosa76e44b92013-01-31 12:11:38 -080056 self.work_dir, _TEST_GOLO_ARCHIVE, build_artifact.TEST_SUITES_FILE,
57 _VERSION)
58 artifact.Process(False)
Chris Sosa47a7d4e2012-03-28 11:26:55 -070059 self.assertTrue(os.path.isdir(os.path.join(
Chris Sosa76e44b92013-01-31 12:11:38 -080060 self.work_dir, 'autotest', 'test_suites')))
Chris Sosa47a7d4e2012-03-28 11:26:55 -070061
Chris Sosa76e44b92013-01-31 12:11:38 -080062 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 Sosa47a7d4e2012-03-28 11:26:55 -070071
72 def testDownloadAndStageAutotest(self):
Yu-Ju Honge61cbe92012-07-10 14:10:26 -070073 """Downloads a real autotest tarball for test."""
Gilad Arnoldc65330c2012-09-20 15:17:48 -070074 self.mox.StubOutWithMock(build_artifact.AutotestTarballBuildArtifact,
Chris Sosa76e44b92013-01-31 12:11:38 -080075 '_Extract')
76 artifact = build_artifact.AutotestTarballBuildArtifact(
77 self.work_dir, _TEST_GOLO_ARCHIVE, build_artifact.AUTOTEST_FILE,
78 _VERSION, None, ['autotest/test_suites'])
79
80 stage_dir = os.path.join(self.work_dir, 'stage')
81 os.makedirs(stage_dir)
82 artifact.staging_dir = stage_dir
83 artifact._Download()
Chris Sosa47a7d4e2012-03-28 11:26:55 -070084 self.mox.StubOutWithMock(subprocess, 'check_call')
Chris Sosa76e44b92013-01-31 12:11:38 -080085 artifact._Extract()
86 subprocess.check_call(mox.In('autotest/utils/packager.py'), cwd=stage_dir)
Chris Sosa47a7d4e2012-03-28 11:26:55 -070087 self.mox.ReplayAll()
Chris Sosa76e44b92013-01-31 12:11:38 -080088 artifact._Stage()
Chris Sosa47a7d4e2012-03-28 11:26:55 -070089 self.mox.VerifyAll()
Chris Sosa76e44b92013-01-31 12:11:38 -080090 self.assertTrue(os.path.isdir(
91 os.path.join(self.work_dir, 'autotest', 'packages')))
Chris Sosa47a7d4e2012-03-28 11:26:55 -070092
Gilad Arnoldc65330c2012-09-20 15:17:48 -070093 def testAUTestPayloadBuildArtifact(self):
Chris Sosa47a7d4e2012-03-28 11:26:55 -070094 """Downloads a real tarball and treats it like an AU payload."""
Gilad Arnoldc65330c2012-09-20 15:17:48 -070095 artifact = build_artifact.AUTestPayloadBuildArtifact(
Chris Sosa76e44b92013-01-31 12:11:38 -080096 self.work_dir, _TEST_GOLO_ARCHIVE, build_artifact.TEST_SUITES_FILE,
97 _VERSION)
98 artifact.Process(False)
Chris Sosa47a7d4e2012-03-28 11:26:55 -070099 self.assertTrue(os.path.exists(os.path.join(
Chris Sosa76e44b92013-01-31 12:11:38 -0800100 self.work_dir, 'update.gz')))
101
102 def testDeltaPayloadsArtifact(self):
103 """Downloads delta paylaods from test bucket."""
104 artifact = build_artifact.DeltaPayloadsArtifact(
105 self.work_dir, _TEST_GOLO_FOR_DELTAS, '.*_delta_.*', _DELTA_VERSION)
106 artifact.Process(False)
107 nton_dir = os.path.join(self.work_dir, 'au', '%s_nton' % _DELTA_VERSION)
108 mton_dir = os.path.join(self.work_dir, 'au', '%s_mton' % _DELTA_VERSION)
109 self.assertTrue(os.path.exists(os.path.join(nton_dir, 'update.gz')))
110 self.assertTrue(os.path.exists(os.path.join(mton_dir, 'update.gz')))
111
112 def testImageUnzip(self):
113 """Downloads and stages a zip file and extracts a test image."""
114 artifact = build_artifact.ZipfileBuildArtifact(
115 self.work_dir, _TEST_GOLO_ARCHIVE, build_artifact.IMAGE_FILE,
116 _VERSION, ['chromiumos_test_image.bin'])
117 artifact.Process(False)
Chris Sosa47a7d4e2012-03-28 11:26:55 -0700118 self.assertTrue(os.path.exists(os.path.join(
Chris Sosa76e44b92013-01-31 12:11:38 -0800119 self.work_dir, 'chromiumos_test_image.bin')))
120
121 def testImageUnzipWithExcludes(self):
122 """Downloads and stages a zip file while excluding all large files."""
123 artifact = build_artifact.ZipfileBuildArtifact(
124 self.work_dir, _TEST_GOLO_ARCHIVE, build_artifact.IMAGE_FILE,
125 _VERSION, None, ['*.bin'])
126 artifact.Process(False)
127 self.assertFalse(os.path.exists(os.path.join(
128 self.work_dir, 'chromiumos_test_image.bin')))
Chris Sosa47a7d4e2012-03-28 11:26:55 -0700129
130
131if __name__ == '__main__':
132 unittest.main()