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