Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 1 | #!/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 | |
| 9 | These unit tests take tarball from google storage locations to fully test |
| 10 | the artifact download process. Please make sure to set up your boto file and |
| 11 | run these unittests from within the chroot. The tools are self-explanatory. |
| 12 | """ |
| 13 | |
| 14 | import mox |
| 15 | import os |
| 16 | import shutil |
| 17 | import subprocess |
| 18 | import tempfile |
| 19 | import unittest |
| 20 | |
| 21 | import 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 | |
| 28 | class 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 Zawalski | 4b09efa | 2012-07-09 22:53:19 -0700 | [diff] [blame] | 59 | """Downloads a real tarball, untars it, and treats it like Autotest.""" |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 60 | artifact = downloadable_artifact.AutotestTarball( |
Scott Zawalski | 4b09efa | 2012-07-09 22:53:19 -0700 | [diff] [blame] | 61 | _TEST_SUITES_TAR, os.path.join(self.work_dir, 'stage'), |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 62 | os.path.join(self.work_dir, 'install'), True) |
| 63 | artifact.Download() |
Scott Zawalski | 4b09efa | 2012-07-09 22:53:19 -0700 | [diff] [blame] | 64 | self.mox.StubOutWithMock(downloadable_artifact.Tarball, '_ExtractTarball') |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 65 | self.mox.StubOutWithMock(subprocess, 'check_call') |
Scott Zawalski | 4b09efa | 2012-07-09 22:53:19 -0700 | [diff] [blame] | 66 | downloadable_artifact.Tarball._ExtractTarball( |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 67 | 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 | |
| 100 | if __name__ == '__main__': |
| 101 | unittest.main() |