Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 1 | #!/usr/bin/python2 |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 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 22:17:48 | [diff] [blame] | 7 | """Unit tests for downloader module.""" |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 8 | |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 9 | from __future__ import print_function |
| 10 | |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 11 | import mox |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 12 | import os |
| 13 | import shutil |
| 14 | import tempfile |
| 15 | import unittest |
| 16 | |
Gilad Arnold | c65330c | 2012-09-20 22:17:48 | [diff] [blame] | 17 | import build_artifact |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 18 | import downloader |
| 19 | |
| 20 | |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 21 | # pylint: disable=W0212,E1120 |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 22 | class DownloaderTestBase(mox.MoxTestBase): |
Simran Basi | 4243a86 | 2014-12-12 20:48:33 | [diff] [blame] | 23 | """Downloader Unittests.""" |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 24 | |
| 25 | def setUp(self): |
| 26 | mox.MoxTestBase.setUp(self) |
| 27 | self._work_dir = tempfile.mkdtemp('downloader-test') |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 28 | self.board = 'x86-mario-release' |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 29 | self.build = 'R17-1413.0.0-a1-b1346' |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 30 | self.archive_url = ( |
| 31 | 'gs://chromeos-image-archive/%s/%s' % (self.board, self.build)) |
Simran Basi | 4243a86 | 2014-12-12 20:48:33 | [diff] [blame] | 32 | self.local_path = ('/local/path/x86-mario-release/R17-1413.0.0-a1-b1346') |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 33 | |
| 34 | def tearDown(self): |
Gilad Arnold | 0b8c3f3 | 2012-09-19 21:35:44 | [diff] [blame] | 35 | shutil.rmtree(self._work_dir, ignore_errors=True) |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 36 | |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 37 | def _SimpleDownloadOfTestSuites(self, downloader_instance): |
Simran Basi | 4243a86 | 2014-12-12 20:48:33 | [diff] [blame] | 38 | """Helper to verify test_suites are downloaded correctly. |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 39 | |
Simran Basi | 4243a86 | 2014-12-12 20:48:33 | [diff] [blame] | 40 | Args: |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 41 | downloader_instance: Downloader object to test with. |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 42 | """ |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 43 | factory = build_artifact.ChromeOSArtifactFactory( |
| 44 | downloader_instance.GetBuildDir(), ['test_suites'], |
| 45 | None, downloader_instance.GetBuild()) |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 46 | self.mox.StubOutWithMock(downloader.Downloader, |
| 47 | '_DownloadArtifactsSerially') |
| 48 | self.mox.StubOutWithMock(downloader.Downloader, |
| 49 | '_DownloadArtifactsInBackground') |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 50 | |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 51 | downloader.Downloader._DownloadArtifactsInBackground(mox.In(mox.IsA( |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 52 | build_artifact.AutotestTarball))) |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 53 | downloader.Downloader._DownloadArtifactsSerially( |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 54 | [mox.IsA(build_artifact.BundledArtifact)], no_wait=True) |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 55 | self.mox.ReplayAll() |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 56 | downloader_instance.Download(factory) |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 57 | # Sanity check the timestamp file exists. |
Alex Miller | a44d502 | 2012-07-27 18:34:16 | [diff] [blame] | 58 | self.assertTrue(os.path.exists( |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 59 | os.path.join(self._work_dir, self.board, self.build, |
| 60 | downloader.Downloader._TIMESTAMP_FILENAME))) |
| 61 | self.mox.VerifyAll() |
Chris Sosa | 9164ca3 | 2012-03-28 18:04:50 | [diff] [blame] | 62 | |
Simran Basi | 4243a86 | 2014-12-12 20:48:33 | [diff] [blame] | 63 | def testSimpleDownloadOfTestSuitesFromGS(self): |
| 64 | """Basic test_suites test. |
| 65 | |
| 66 | Verifies that if we request the test_suites from Google Storage, it gets |
| 67 | downloaded and the autotest tarball is attempted in the background. |
| 68 | """ |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 69 | self._SimpleDownloadOfTestSuites( |
| 70 | downloader.GoogleStorageDownloader(self._work_dir, self.archive_url)) |
Simran Basi | 4243a86 | 2014-12-12 20:48:33 | [diff] [blame] | 71 | |
| 72 | def testSimpleDownloadOfTestSuitesFromLocal(self): |
| 73 | """Basic test_suites test. |
| 74 | |
| 75 | Verifies that if we request the test_suites from a local path, it gets |
| 76 | downloaded and the autotest tarball is attempted in the background. |
| 77 | """ |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 78 | self._SimpleDownloadOfTestSuites( |
| 79 | downloader.LocalDownloader(self._work_dir, self.local_path)) |
Simran Basi | 4243a86 | 2014-12-12 20:48:33 | [diff] [blame] | 80 | |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 81 | def _DownloadSymbolsHelper(self, downloader_instance): |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 82 | """Basic symbols download.""" |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 83 | factory = build_artifact.ChromeOSArtifactFactory( |
| 84 | downloader_instance.GetBuildDir(), ['symbols'], |
| 85 | None, downloader_instance.GetBuild()) |
| 86 | |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 87 | self.mox.StubOutWithMock(downloader.Downloader, |
| 88 | '_DownloadArtifactsSerially') |
| 89 | # Should not get called but mocking so that we know it wasn't called. |
| 90 | self.mox.StubOutWithMock(downloader.Downloader, |
| 91 | '_DownloadArtifactsInBackground') |
| 92 | downloader.Downloader._DownloadArtifactsSerially( |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 93 | [mox.IsA(build_artifact.BundledArtifact)], no_wait=True) |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 94 | self.mox.ReplayAll() |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 95 | downloader_instance.Download(factory) |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 96 | self.mox.VerifyAll() |
| 97 | |
Simran Basi | 4243a86 | 2014-12-12 20:48:33 | [diff] [blame] | 98 | def testDownloadSymbolsFromGS(self): |
| 99 | """Basic symbols download from Google Storage.""" |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 100 | self._DownloadSymbolsHelper( |
| 101 | downloader.GoogleStorageDownloader(self._work_dir, self.archive_url)) |
Simran Basi | 4243a86 | 2014-12-12 20:48:33 | [diff] [blame] | 102 | |
| 103 | def testDownloadSymbolsFromLocal(self): |
| 104 | """Basic symbols download from a Local Path.""" |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 105 | self._DownloadSymbolsHelper( |
| 106 | downloader.LocalDownloader(self._work_dir, self.local_path)) |
| 107 | |
| 108 | |
| 109 | class AndroidDownloaderTestBase(mox.MoxTestBase): |
| 110 | """Android Downloader Unittests.""" |
| 111 | |
| 112 | def setUp(self): |
| 113 | mox.MoxTestBase.setUp(self) |
| 114 | self._work_dir = tempfile.mkdtemp('downloader-test') |
Dan Shi | 72b1613 | 2015-10-08 19:10:33 | [diff] [blame] | 115 | self.branch = 'release' |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 116 | self.target = 'shamu-userdebug' |
| 117 | self.build_id = '123456' |
| 118 | |
| 119 | def tearDown(self): |
| 120 | shutil.rmtree(self._work_dir, ignore_errors=True) |
| 121 | |
Dan Shi | 72b1613 | 2015-10-08 19:10:33 | [diff] [blame] | 122 | def testDownloadFromAndroidBuildServer(self): |
| 123 | """Basic test to check download from Android's build server works.""" |
| 124 | downloader_instance = downloader.AndroidBuildDownloader( |
| 125 | self._work_dir, self.branch, self.build_id, self.target) |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 126 | factory = build_artifact.AndroidArtifactFactory( |
| 127 | downloader_instance.GetBuildDir(), ['fastboot'], |
| 128 | None, downloader_instance.GetBuild()) |
| 129 | self.mox.StubOutWithMock(downloader.Downloader, |
| 130 | '_DownloadArtifactsSerially') |
| 131 | self.mox.StubOutWithMock(downloader.Downloader, |
| 132 | '_DownloadArtifactsInBackground') |
| 133 | |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 134 | downloader.Downloader._DownloadArtifactsSerially( |
| 135 | [mox.IsA(build_artifact.Artifact)], no_wait=True) |
| 136 | self.mox.ReplayAll() |
| 137 | downloader_instance.Download(factory) |
| 138 | # Sanity check the timestamp file exists. |
| 139 | self.assertTrue(os.path.exists( |
Dan Shi | 72b1613 | 2015-10-08 19:10:33 | [diff] [blame] | 140 | os.path.join(self._work_dir, self.branch, self.target, self.build_id, |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 141 | downloader.Downloader._TIMESTAMP_FILENAME))) |
| 142 | self.mox.VerifyAll() |
Simran Basi | 4243a86 | 2014-12-12 20:48:33 | [diff] [blame] | 143 | |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 144 | |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 145 | if __name__ == '__main__': |
| 146 | unittest.main() |