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]) |
Yu-Ju Hong | e61cbe9 | 2012-07-10 14:10:26 -0700 | [diff] [blame^] | 27 | _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 Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 32 | |
| 33 | class 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 Hong | e61cbe9 | 2012-07-10 14:10:26 -0700 | [diff] [blame^] | 64 | """Downloads a real autotest tarball for test.""" |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 65 | artifact = downloadable_artifact.AutotestTarball( |
Yu-Ju Hong | e61cbe9 | 2012-07-10 14:10:26 -0700 | [diff] [blame^] | 66 | _AUTOTEST_TAR, os.path.join(self.work_dir, 'stage'), |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 67 | os.path.join(self.work_dir, 'install'), True) |
| 68 | artifact.Download() |
Yu-Ju Hong | e61cbe9 | 2012-07-10 14:10:26 -0700 | [diff] [blame^] | 69 | self.mox.StubOutWithMock(downloadable_artifact.AutotestTarball, |
| 70 | '_ExtractTarball') |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 71 | self.mox.StubOutWithMock(subprocess, 'check_call') |
Yu-Ju Hong | e61cbe9 | 2012-07-10 14:10:26 -0700 | [diff] [blame^] | 72 | downloadable_artifact.AutotestTarball._ExtractTarball( |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 73 | 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 | |
| 106 | if __name__ == '__main__': |
| 107 | unittest.main() |