blob: 4ef347d89ce1a854bcca6c5c54090bf34ba89874 [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
10the artifact download process. Please make sure to set up your boto file and
11run these unittests from within the chroot. The tools are self-explanatory.
12"""
13
14import mox
15import os
16import shutil
17import subprocess
18import tempfile
19import unittest
20
Gilad Arnoldc65330c2012-09-20 15:17:48 -070021import build_artifact
Chris Sosa47a7d4e2012-03-28 11:26:55 -070022
23_TEST_GOLO_ARCHIVE = (
24 'gs://chromeos-image-archive/x86-alex-release/R19-2003.0.0-a1-b1819')
25_TEST_SUITES_TAR = '/'.join([_TEST_GOLO_ARCHIVE,
Gilad Arnoldc65330c2012-09-20 15:17:48 -070026 build_artifact.TEST_SUITES_PACKAGE])
Yu-Ju Honge61cbe92012-07-10 14:10:26 -070027_TEST_TEMP_ARCHIVE = (
28 'gs://chromeos-image-archive/trybot-lumpy-paladin/R22-2531.0.0-a1-b145')
29_AUTOTEST_TAR = '/'.join([_TEST_TEMP_ARCHIVE,
Gilad Arnoldc65330c2012-09-20 15:17:48 -070030 build_artifact.AUTOTEST_PACKAGE])
Yu-Ju Honge61cbe92012-07-10 14:10:26 -070031
Chris Sosa47a7d4e2012-03-28 11:26:55 -070032
Gilad Arnoldc65330c2012-09-20 15:17:48 -070033class BuildArtifactTest(mox.MoxTestBase):
Chris Sosa47a7d4e2012-03-28 11:26:55 -070034
35 def setUp(self):
36 mox.MoxTestBase.setUp(self)
Gilad Arnoldc65330c2012-09-20 15:17:48 -070037 self.work_dir = tempfile.mkdtemp('build_artifact')
Chris Sosa47a7d4e2012-03-28 11:26:55 -070038
39 def tearDown(self):
40 shutil.rmtree(self.work_dir)
41
42 def testDownloadAndStage(self):
43 """Downloads a real tarball from GSUtil."""
Gilad Arnoldc65330c2012-09-20 15:17:48 -070044 artifact = build_artifact.BuildArtifact(
Chris Sosa47a7d4e2012-03-28 11:26:55 -070045 _TEST_SUITES_TAR, os.path.join(self.work_dir, 'stage'),
46 os.path.join(self.work_dir, 'install', 'file'), True)
47 artifact.Download()
48 artifact.Stage()
49 self.assertTrue(os.path.exists(os.path.join(
50 self.work_dir, 'install', 'file')))
51
52 def testDownloadAndStageTarball(self):
53 """Downloads a real tarball and untars it."""
Gilad Arnoldc65330c2012-09-20 15:17:48 -070054 artifact = build_artifact.TarballBuildArtifact(
Chris Sosa47a7d4e2012-03-28 11:26:55 -070055 _TEST_SUITES_TAR, os.path.join(self.work_dir, 'stage'),
56 os.path.join(self.work_dir, 'install'), True)
57 artifact.Download()
58 artifact.Stage()
59 self.assertTrue(os.path.isdir(os.path.join(
60 self.work_dir, 'install', 'autotest', 'test_suites')))
61
62
63 def testDownloadAndStageAutotest(self):
Yu-Ju Honge61cbe92012-07-10 14:10:26 -070064 """Downloads a real autotest tarball for test."""
Gilad Arnoldc65330c2012-09-20 15:17:48 -070065 artifact = build_artifact.AutotestTarballBuildArtifact(
Yu-Ju Honge61cbe92012-07-10 14:10:26 -070066 _AUTOTEST_TAR, os.path.join(self.work_dir, 'stage'),
Chris Sosa47a7d4e2012-03-28 11:26:55 -070067 os.path.join(self.work_dir, 'install'), True)
68 artifact.Download()
Gilad Arnoldc65330c2012-09-20 15:17:48 -070069 self.mox.StubOutWithMock(build_artifact.AutotestTarballBuildArtifact,
Yu-Ju Honge61cbe92012-07-10 14:10:26 -070070 '_ExtractTarball')
Chris Sosa47a7d4e2012-03-28 11:26:55 -070071 self.mox.StubOutWithMock(subprocess, 'check_call')
Gilad Arnoldc65330c2012-09-20 15:17:48 -070072 build_artifact.AutotestTarballBuildArtifact._ExtractTarball(
Chris Sosa47a7d4e2012-03-28 11:26:55 -070073 exclude='autotest/test_suites')
74 subprocess.check_call(mox.StrContains('autotest/utils/packager.py'),
75 cwd=os.path.join(self.work_dir, 'stage'), shell=True)
76 subprocess.check_call('cp %s %s' % (
77 os.path.join(self.work_dir, 'install', 'autotest', 'packages/*'),
78 os.path.join(self.work_dir, 'install', 'autotest')), shell=True)
79 self.mox.ReplayAll()
80 artifact.Stage()
81 self.mox.VerifyAll()
82 self.assertTrue(os.path.isdir(os.path.join(self.work_dir, 'install',
83 'autotest', 'packages')))
84
Gilad Arnoldc65330c2012-09-20 15:17:48 -070085 def testAUTestPayloadBuildArtifact(self):
Chris Sosa47a7d4e2012-03-28 11:26:55 -070086 """Downloads a real tarball and treats it like an AU payload."""
Gilad Arnoldc65330c2012-09-20 15:17:48 -070087 open(os.path.join(self.work_dir, build_artifact.STATEFUL_UPDATE),
Chris Sosa47a7d4e2012-03-28 11:26:55 -070088 'a').close()
Gilad Arnoldc65330c2012-09-20 15:17:48 -070089 open(os.path.join(self.work_dir, build_artifact.TEST_IMAGE),
Chris Sosa47a7d4e2012-03-28 11:26:55 -070090 'a').close()
91
Gilad Arnoldc65330c2012-09-20 15:17:48 -070092 artifact = build_artifact.AUTestPayloadBuildArtifact(
Chris Sosa47a7d4e2012-03-28 11:26:55 -070093 _TEST_SUITES_TAR, os.path.join(self.work_dir, 'stage'),
94 os.path.join(self.work_dir, 'install', 'payload', 'payload.gz'), True)
95 artifact.Download()
96 artifact.Stage()
97 self.assertTrue(os.path.exists(os.path.join(
98 self.work_dir, 'install', 'payload', 'payload.gz')))
99 self.assertTrue(os.path.exists(os.path.join(
100 self.work_dir, 'install', 'payload',
Gilad Arnoldc65330c2012-09-20 15:17:48 -0700101 build_artifact.STATEFUL_UPDATE)))
Chris Sosa47a7d4e2012-03-28 11:26:55 -0700102 self.assertTrue(os.path.exists(os.path.join(
Gilad Arnoldc65330c2012-09-20 15:17:48 -0700103 self.work_dir, 'install', 'payload', build_artifact.TEST_IMAGE)))
Chris Sosa47a7d4e2012-03-28 11:26:55 -0700104
105
106if __name__ == '__main__':
107 unittest.main()