blob: b6ef0892b64eb256de626333e0ba8dd72a9f633a [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])
27
28class DownloadableArtifactTest(mox.MoxTestBase):
29
30 def setUp(self):
31 mox.MoxTestBase.setUp(self)
32 self.work_dir = tempfile.mkdtemp('downloadable_artifact')
33
34 def tearDown(self):
35 shutil.rmtree(self.work_dir)
36
37 def testDownloadAndStage(self):
38 """Downloads a real tarball from GSUtil."""
39 artifact = downloadable_artifact.DownloadableArtifact(
40 _TEST_SUITES_TAR, os.path.join(self.work_dir, 'stage'),
41 os.path.join(self.work_dir, 'install', 'file'), True)
42 artifact.Download()
43 artifact.Stage()
44 self.assertTrue(os.path.exists(os.path.join(
45 self.work_dir, 'install', 'file')))
46
47 def testDownloadAndStageTarball(self):
48 """Downloads a real tarball and untars it."""
49 artifact = downloadable_artifact.Tarball(
50 _TEST_SUITES_TAR, os.path.join(self.work_dir, 'stage'),
51 os.path.join(self.work_dir, 'install'), True)
52 artifact.Download()
53 artifact.Stage()
54 self.assertTrue(os.path.isdir(os.path.join(
55 self.work_dir, 'install', 'autotest', 'test_suites')))
56
57
58 def testDownloadAndStageAutotest(self):
Scott Zawalski4b09efa2012-07-09 22:53:19 -070059 """Downloads a real tarball, untars it, and treats it like Autotest."""
Chris Sosa47a7d4e2012-03-28 11:26:55 -070060 artifact = downloadable_artifact.AutotestTarball(
Scott Zawalski4b09efa2012-07-09 22:53:19 -070061 _TEST_SUITES_TAR, os.path.join(self.work_dir, 'stage'),
Chris Sosa47a7d4e2012-03-28 11:26:55 -070062 os.path.join(self.work_dir, 'install'), True)
63 artifact.Download()
Scott Zawalski4b09efa2012-07-09 22:53:19 -070064 self.mox.StubOutWithMock(downloadable_artifact.Tarball, '_ExtractTarball')
Chris Sosa47a7d4e2012-03-28 11:26:55 -070065 self.mox.StubOutWithMock(subprocess, 'check_call')
Scott Zawalski4b09efa2012-07-09 22:53:19 -070066 downloadable_artifact.Tarball._ExtractTarball(
Chris Sosa47a7d4e2012-03-28 11:26:55 -070067 exclude='autotest/test_suites')
68 subprocess.check_call(mox.StrContains('autotest/utils/packager.py'),
69 cwd=os.path.join(self.work_dir, 'stage'), shell=True)
70 subprocess.check_call('cp %s %s' % (
71 os.path.join(self.work_dir, 'install', 'autotest', 'packages/*'),
72 os.path.join(self.work_dir, 'install', 'autotest')), shell=True)
73 self.mox.ReplayAll()
74 artifact.Stage()
75 self.mox.VerifyAll()
76 self.assertTrue(os.path.isdir(os.path.join(self.work_dir, 'install',
77 'autotest', 'packages')))
78
79 def testAUTestPayload(self):
80 """Downloads a real tarball and treats it like an AU payload."""
81 open(os.path.join(self.work_dir, downloadable_artifact.STATEFUL_UPDATE),
82 'a').close()
83 open(os.path.join(self.work_dir, downloadable_artifact.TEST_IMAGE),
84 'a').close()
85
86 artifact = downloadable_artifact.AUTestPayload(
87 _TEST_SUITES_TAR, os.path.join(self.work_dir, 'stage'),
88 os.path.join(self.work_dir, 'install', 'payload', 'payload.gz'), True)
89 artifact.Download()
90 artifact.Stage()
91 self.assertTrue(os.path.exists(os.path.join(
92 self.work_dir, 'install', 'payload', 'payload.gz')))
93 self.assertTrue(os.path.exists(os.path.join(
94 self.work_dir, 'install', 'payload',
95 downloadable_artifact.STATEFUL_UPDATE)))
96 self.assertTrue(os.path.exists(os.path.join(
97 self.work_dir, 'install', 'payload', downloadable_artifact.TEST_IMAGE)))
98
99
100if __name__ == '__main__':
101 unittest.main()