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 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 7 | """Unit tests for downloader module.""" |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 8 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 9 | import mox |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 10 | import os |
| 11 | import shutil |
| 12 | import tempfile |
| 13 | import unittest |
| 14 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 15 | import build_artifact |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 16 | import downloader |
| 17 | |
| 18 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 19 | # pylint: disable=W0212,E1120 |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 20 | class DownloaderTestBase(mox.MoxTestBase): |
Simran Basi | 4243a86 | 2014-12-12 12:48:33 -0800 | [diff] [blame] | 21 | """Downloader Unittests.""" |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 22 | |
| 23 | def setUp(self): |
| 24 | mox.MoxTestBase.setUp(self) |
| 25 | self._work_dir = tempfile.mkdtemp('downloader-test') |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 26 | self.board = 'x86-mario-release' |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 27 | self.build = 'R17-1413.0.0-a1-b1346' |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 28 | self.archive_url = ( |
| 29 | 'gs://chromeos-image-archive/%s/%s' % (self.board, self.build)) |
Simran Basi | 4243a86 | 2014-12-12 12:48:33 -0800 | [diff] [blame] | 30 | self.local_path = ('/local/path/x86-mario-release/R17-1413.0.0-a1-b1346') |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 31 | |
| 32 | def tearDown(self): |
Gilad Arnold | 0b8c3f3 | 2012-09-19 14:35:44 -0700 | [diff] [blame] | 33 | shutil.rmtree(self._work_dir, ignore_errors=True) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 34 | |
Simran Basi | 4243a86 | 2014-12-12 12:48:33 -0800 | [diff] [blame] | 35 | def _SimpleDownloadOfTestSuites(self, archive_path): |
| 36 | """Helper to verify test_suites are downloaded correctly. |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 37 | |
Simran Basi | 4243a86 | 2014-12-12 12:48:33 -0800 | [diff] [blame] | 38 | Args: |
| 39 | archive_path: Archive url or local path to test with. |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 40 | """ |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 41 | downloader_instance = downloader.Downloader(self._work_dir, |
Simran Basi | 4243a86 | 2014-12-12 12:48:33 -0800 | [diff] [blame] | 42 | archive_path) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 43 | self.mox.StubOutWithMock(downloader.Downloader, |
| 44 | '_DownloadArtifactsSerially') |
| 45 | self.mox.StubOutWithMock(downloader.Downloader, |
| 46 | '_DownloadArtifactsInBackground') |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 47 | |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 48 | downloader.Downloader._DownloadArtifactsInBackground(mox.In(mox.IsA( |
| 49 | build_artifact.AutotestTarballBuildArtifact))) |
| 50 | downloader.Downloader._DownloadArtifactsSerially( |
Simran Basi | 4243a86 | 2014-12-12 12:48:33 -0800 | [diff] [blame] | 51 | [mox.IsA(build_artifact.BundledBuildArtifact)], no_wait=True) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 52 | self.mox.ReplayAll() |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 53 | downloader_instance.Download(artifacts=['test_suites'], |
| 54 | files=None) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 55 | # Sanity check the timestamp file exists. |
Alex Miller | a44d502 | 2012-07-27 11:34:16 -0700 | [diff] [blame] | 56 | self.assertTrue(os.path.exists( |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 57 | os.path.join(self._work_dir, self.board, self.build, |
| 58 | downloader.Downloader._TIMESTAMP_FILENAME))) |
| 59 | self.mox.VerifyAll() |
Chris Sosa | 9164ca3 | 2012-03-28 11:04:50 -0700 | [diff] [blame] | 60 | |
Simran Basi | 4243a86 | 2014-12-12 12:48:33 -0800 | [diff] [blame] | 61 | def testSimpleDownloadOfTestSuitesFromGS(self): |
| 62 | """Basic test_suites test. |
| 63 | |
| 64 | Verifies that if we request the test_suites from Google Storage, it gets |
| 65 | downloaded and the autotest tarball is attempted in the background. |
| 66 | """ |
| 67 | self._SimpleDownloadOfTestSuites(self.archive_url) |
| 68 | |
| 69 | def testSimpleDownloadOfTestSuitesFromLocal(self): |
| 70 | """Basic test_suites test. |
| 71 | |
| 72 | Verifies that if we request the test_suites from a local path, it gets |
| 73 | downloaded and the autotest tarball is attempted in the background. |
| 74 | """ |
| 75 | self._SimpleDownloadOfTestSuites(self.local_path) |
| 76 | |
| 77 | def _DownloadSymbolsHelper(self, archive_path): |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 78 | """Basic symbols download.""" |
Simran Basi | 4243a86 | 2014-12-12 12:48:33 -0800 | [diff] [blame] | 79 | downloader_instance = downloader.Downloader(self._work_dir, archive_path) |
Chris Sosa | 76e44b9 | 2013-01-31 12:11:38 -0800 | [diff] [blame] | 80 | self.mox.StubOutWithMock(downloader.Downloader, |
| 81 | '_DownloadArtifactsSerially') |
| 82 | # Should not get called but mocking so that we know it wasn't called. |
| 83 | self.mox.StubOutWithMock(downloader.Downloader, |
| 84 | '_DownloadArtifactsInBackground') |
| 85 | downloader.Downloader._DownloadArtifactsSerially( |
Simran Basi | 4243a86 | 2014-12-12 12:48:33 -0800 | [diff] [blame] | 86 | [mox.IsA(build_artifact.BundledBuildArtifact)], no_wait=True) |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 87 | self.mox.ReplayAll() |
Chris Sosa | 6b0c617 | 2013-08-05 17:01:33 -0700 | [diff] [blame] | 88 | downloader_instance.Download(artifacts=['symbols'], |
| 89 | files=None) |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 90 | self.mox.VerifyAll() |
| 91 | |
Simran Basi | 4243a86 | 2014-12-12 12:48:33 -0800 | [diff] [blame] | 92 | def testDownloadSymbolsFromGS(self): |
| 93 | """Basic symbols download from Google Storage.""" |
| 94 | self._DownloadSymbolsHelper(self.archive_url) |
| 95 | |
| 96 | def testDownloadSymbolsFromLocal(self): |
| 97 | """Basic symbols download from a Local Path.""" |
| 98 | self._DownloadSymbolsHelper(self.local_path) |
| 99 | |
Chris Masone | 816e38c | 2012-05-02 12:22:36 -0700 | [diff] [blame] | 100 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 101 | if __name__ == '__main__': |
| 102 | unittest.main() |