blob: 119f6d3ac6816fbc079b809cf26bb5b271bd87de [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
Chris Sosa47a7d4e2012-03-28 11:26:55 -070014import os
15import 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
Chris Sosa47a7d4e2012-03-28 11:26:55 -070023
Gilad Arnoldabb352e2012-09-23 01:24:27 -070024
Chris Sosa47a7d4e2012-03-28 11:26:55 -070025_TEST_GOLO_ARCHIVE = (
26 'gs://chromeos-image-archive/x86-alex-release/R19-2003.0.0-a1-b1819')
27_TEST_SUITES_TAR = '/'.join([_TEST_GOLO_ARCHIVE,
Gilad Arnoldc65330c2012-09-20 15:17:48 -070028 build_artifact.TEST_SUITES_PACKAGE])
Yu-Ju Honge61cbe92012-07-10 14:10:26 -070029_TEST_TEMP_ARCHIVE = (
30 'gs://chromeos-image-archive/trybot-lumpy-paladin/R22-2531.0.0-a1-b145')
31_AUTOTEST_TAR = '/'.join([_TEST_TEMP_ARCHIVE,
Gilad Arnoldc65330c2012-09-20 15:17:48 -070032 build_artifact.AUTOTEST_PACKAGE])
Yu-Ju Honge61cbe92012-07-10 14:10:26 -070033
Chris Sosa47a7d4e2012-03-28 11:26:55 -070034
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)
Gilad Arnoldc65330c2012-09-20 15:17:48 -070039 self.work_dir = tempfile.mkdtemp('build_artifact')
Chris Sosa47a7d4e2012-03-28 11:26:55 -070040
41 def tearDown(self):
42 shutil.rmtree(self.work_dir)
43
44 def testDownloadAndStage(self):
45 """Downloads a real tarball from GSUtil."""
Gilad Arnoldc65330c2012-09-20 15:17:48 -070046 artifact = build_artifact.BuildArtifact(
Chris Sosa47a7d4e2012-03-28 11:26:55 -070047 _TEST_SUITES_TAR, os.path.join(self.work_dir, 'stage'),
48 os.path.join(self.work_dir, 'install', 'file'), True)
49 artifact.Download()
50 artifact.Stage()
51 self.assertTrue(os.path.exists(os.path.join(
52 self.work_dir, 'install', 'file')))
53
54 def testDownloadAndStageTarball(self):
55 """Downloads a real tarball and untars it."""
Gilad Arnoldc65330c2012-09-20 15:17:48 -070056 artifact = build_artifact.TarballBuildArtifact(
Chris Sosa47a7d4e2012-03-28 11:26:55 -070057 _TEST_SUITES_TAR, os.path.join(self.work_dir, 'stage'),
58 os.path.join(self.work_dir, 'install'), True)
59 artifact.Download()
60 artifact.Stage()
61 self.assertTrue(os.path.isdir(os.path.join(
62 self.work_dir, 'install', 'autotest', 'test_suites')))
63
64
65 def testDownloadAndStageAutotest(self):
Yu-Ju Honge61cbe92012-07-10 14:10:26 -070066 """Downloads a real autotest tarball for test."""
Gilad Arnoldc65330c2012-09-20 15:17:48 -070067 artifact = build_artifact.AutotestTarballBuildArtifact(
Yu-Ju Honge61cbe92012-07-10 14:10:26 -070068 _AUTOTEST_TAR, os.path.join(self.work_dir, 'stage'),
Chris Sosa47a7d4e2012-03-28 11:26:55 -070069 os.path.join(self.work_dir, 'install'), True)
70 artifact.Download()
Gilad Arnoldc65330c2012-09-20 15:17:48 -070071 self.mox.StubOutWithMock(build_artifact.AutotestTarballBuildArtifact,
Yu-Ju Honge61cbe92012-07-10 14:10:26 -070072 '_ExtractTarball')
Chris Sosa47a7d4e2012-03-28 11:26:55 -070073 self.mox.StubOutWithMock(subprocess, 'check_call')
Gilad Arnoldc65330c2012-09-20 15:17:48 -070074 build_artifact.AutotestTarballBuildArtifact._ExtractTarball(
Chris Sosa47a7d4e2012-03-28 11:26:55 -070075 exclude='autotest/test_suites')
76 subprocess.check_call(mox.StrContains('autotest/utils/packager.py'),
77 cwd=os.path.join(self.work_dir, 'stage'), shell=True)
78 subprocess.check_call('cp %s %s' % (
79 os.path.join(self.work_dir, 'install', 'autotest', 'packages/*'),
80 os.path.join(self.work_dir, 'install', 'autotest')), shell=True)
81 self.mox.ReplayAll()
82 artifact.Stage()
83 self.mox.VerifyAll()
84 self.assertTrue(os.path.isdir(os.path.join(self.work_dir, 'install',
85 'autotest', 'packages')))
86
Gilad Arnoldc65330c2012-09-20 15:17:48 -070087 def testAUTestPayloadBuildArtifact(self):
Chris Sosa47a7d4e2012-03-28 11:26:55 -070088 """Downloads a real tarball and treats it like an AU payload."""
Gilad Arnoldc65330c2012-09-20 15:17:48 -070089 open(os.path.join(self.work_dir, build_artifact.STATEFUL_UPDATE),
Chris Sosa47a7d4e2012-03-28 11:26:55 -070090 'a').close()
Gilad Arnoldc65330c2012-09-20 15:17:48 -070091 open(os.path.join(self.work_dir, build_artifact.TEST_IMAGE),
Chris Sosa47a7d4e2012-03-28 11:26:55 -070092 'a').close()
93
Gilad Arnoldc65330c2012-09-20 15:17:48 -070094 artifact = build_artifact.AUTestPayloadBuildArtifact(
Chris Sosa47a7d4e2012-03-28 11:26:55 -070095 _TEST_SUITES_TAR, os.path.join(self.work_dir, 'stage'),
96 os.path.join(self.work_dir, 'install', 'payload', 'payload.gz'), True)
97 artifact.Download()
98 artifact.Stage()
99 self.assertTrue(os.path.exists(os.path.join(
100 self.work_dir, 'install', 'payload', 'payload.gz')))
101 self.assertTrue(os.path.exists(os.path.join(
102 self.work_dir, 'install', 'payload',
Gilad Arnoldc65330c2012-09-20 15:17:48 -0700103 build_artifact.STATEFUL_UPDATE)))
Chris Sosa47a7d4e2012-03-28 11:26:55 -0700104 self.assertTrue(os.path.exists(os.path.join(
Gilad Arnoldc65330c2012-09-20 15:17:48 -0700105 self.work_dir, 'install', 'payload', build_artifact.TEST_IMAGE)))
Chris Sosa47a7d4e2012-03-28 11:26:55 -0700106
107
108if __name__ == '__main__':
109 unittest.main()