blob: 6964b72cd30cbe784efae9644fdcbe9e6b2e3d23 [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
7"""Unit tests for downloadable_artifact module.
8
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
21import downloadable_artifact
22
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,
26 downloadable_artifact.TEST_SUITES_PACKAGE])
Yu-Ju Hongbee1ba62012-07-02 13:12:55 -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,
30 downloadable_artifact.AUTOTEST_PACKAGE])
31
Chris Sosa47a7d4e2012-03-28 11:26:55 -070032
33class DownloadableArtifactTest(mox.MoxTestBase):
34
35 def setUp(self):
36 mox.MoxTestBase.setUp(self)
37 self.work_dir = tempfile.mkdtemp('downloadable_artifact')
38
39 def tearDown(self):
40 shutil.rmtree(self.work_dir)
41
42 def testDownloadAndStage(self):
43 """Downloads a real tarball from GSUtil."""
44 artifact = downloadable_artifact.DownloadableArtifact(
45 _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."""
54 artifact = downloadable_artifact.Tarball(
55 _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 Hongbee1ba62012-07-02 13:12:55 -070064 """Downloads a real autotest tarball for test."""
Chris Sosa47a7d4e2012-03-28 11:26:55 -070065 artifact = downloadable_artifact.AutotestTarball(
Yu-Ju Hongbee1ba62012-07-02 13:12:55 -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()
Yu-Ju Hongbee1ba62012-07-02 13:12:55 -070069 self.mox.StubOutWithMock(downloadable_artifact.AutotestTarball,
70 '_ExtractTarball')
Chris Sosa47a7d4e2012-03-28 11:26:55 -070071 self.mox.StubOutWithMock(subprocess, 'check_call')
Yu-Ju Hongbee1ba62012-07-02 13:12:55 -070072 downloadable_artifact.AutotestTarball._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
85 def testAUTestPayload(self):
86 """Downloads a real tarball and treats it like an AU payload."""
87 open(os.path.join(self.work_dir, downloadable_artifact.STATEFUL_UPDATE),
88 'a').close()
89 open(os.path.join(self.work_dir, downloadable_artifact.TEST_IMAGE),
90 'a').close()
91
92 artifact = downloadable_artifact.AUTestPayload(
93 _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',
101 downloadable_artifact.STATEFUL_UPDATE)))
102 self.assertTrue(os.path.exists(os.path.join(
103 self.work_dir, 'install', 'payload', downloadable_artifact.TEST_IMAGE)))
104
105
106if __name__ == '__main__':
107 unittest.main()