Luis Hector Chavez | dca9dd7 | 2018-06-12 19:56:30 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
| 2 | # -*- coding: utf-8 -*- |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [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 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 | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 11 | import os |
| 12 | import shutil |
| 13 | import tempfile |
| 14 | import unittest |
| 15 | |
Achuith Bhandarkar | 46877ad | 2019-09-26 13:54:02 | [diff] [blame] | 16 | import mock |
| 17 | |
Gilad Arnold | c65330c | 2012-09-20 22:17:48 | [diff] [blame] | 18 | import build_artifact |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 19 | import downloader |
| 20 | |
| 21 | |
Achuith Bhandarkar | 46877ad | 2019-09-26 13:54:02 | [diff] [blame] | 22 | # pylint: disable=protected-access,no-value-for-parameter |
| 23 | class DownloaderTestBase(unittest.TestCase): |
Simran Basi | 4243a86 | 2014-12-12 20:48:33 | [diff] [blame] | 24 | """Downloader Unittests.""" |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 25 | |
| 26 | def setUp(self): |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 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 | |
Achuith Bhandarkar | 46877ad | 2019-09-26 13:54:02 | [diff] [blame] | 37 | @mock.patch('downloader.Downloader._DownloadArtifactsSerially') |
| 38 | @mock.patch('downloader.Downloader._DownloadArtifactsInBackground') |
| 39 | def _SimpleDownloadOfTestSuites(self, downloader_instance, bg_mock, |
| 40 | serial_mock): |
Simran Basi | 4243a86 | 2014-12-12 20:48:33 | [diff] [blame] | 41 | """Helper to verify test_suites are downloaded correctly. |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 42 | |
Simran Basi | 4243a86 | 2014-12-12 20:48:33 | [diff] [blame] | 43 | Args: |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 44 | downloader_instance: Downloader object to test with. |
Achuith Bhandarkar | 46877ad | 2019-09-26 13:54:02 | [diff] [blame] | 45 | bg_mock: background download method mock. |
| 46 | serial_mock: serial download method mock. |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 47 | """ |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 48 | factory = build_artifact.ChromeOSArtifactFactory( |
| 49 | downloader_instance.GetBuildDir(), ['test_suites'], |
| 50 | None, downloader_instance.GetBuild()) |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 51 | |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 52 | downloader_instance.Download(factory) |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 53 | # Sanity check the timestamp file exists. |
Achuith Bhandarkar | 46877ad | 2019-09-26 13:54:02 | [diff] [blame] | 54 | install_dir = os.path.join(self._work_dir, self.board, self.build) |
Alex Miller | a44d502 | 2012-07-27 18:34:16 | [diff] [blame] | 55 | self.assertTrue(os.path.exists( |
Achuith Bhandarkar | 46877ad | 2019-09-26 13:54:02 | [diff] [blame] | 56 | os.path.join(install_dir, downloader.Downloader._TIMESTAMP_FILENAME))) |
| 57 | serial_mock.assert_called() |
| 58 | bg_mock.assert_called() |
Chris Sosa | 9164ca3 | 2012-03-28 18:04:50 | [diff] [blame] | 59 | |
Simran Basi | 4243a86 | 2014-12-12 20:48:33 | [diff] [blame] | 60 | def testSimpleDownloadOfTestSuitesFromGS(self): |
| 61 | """Basic test_suites test. |
| 62 | |
| 63 | Verifies that if we request the test_suites from Google Storage, it gets |
| 64 | downloaded and the autotest tarball is attempted in the background. |
| 65 | """ |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 66 | self._SimpleDownloadOfTestSuites( |
Luis Hector Chavez | dca9dd7 | 2018-06-12 19:56:30 | [diff] [blame] | 67 | downloader.GoogleStorageDownloader( |
| 68 | self._work_dir, self.archive_url, |
| 69 | downloader.GoogleStorageDownloader.GetBuildIdFromArchiveURL( |
| 70 | 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 | |
Achuith Bhandarkar | 46877ad | 2019-09-26 13:54:02 | [diff] [blame] | 81 | @mock.patch('downloader.Downloader._DownloadArtifactsSerially') |
| 82 | @mock.patch('downloader.Downloader._DownloadArtifactsInBackground') |
| 83 | def _DownloadSymbolsHelper(self, downloader_instance, bg_mock, serial_mock): |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 84 | """Basic symbols download.""" |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 85 | factory = build_artifact.ChromeOSArtifactFactory( |
| 86 | downloader_instance.GetBuildDir(), ['symbols'], |
| 87 | None, downloader_instance.GetBuild()) |
| 88 | |
Chris Sosa | 76e44b9 | 2013-01-31 20:11:38 | [diff] [blame] | 89 | # Should not get called but mocking so that we know it wasn't called. |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 90 | downloader_instance.Download(factory) |
Achuith Bhandarkar | 46877ad | 2019-09-26 13:54:02 | [diff] [blame] | 91 | serial_mock.assert_called() |
| 92 | bg_mock.assert_not_called() |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 93 | |
Simran Basi | 4243a86 | 2014-12-12 20:48:33 | [diff] [blame] | 94 | def testDownloadSymbolsFromGS(self): |
| 95 | """Basic symbols download from Google Storage.""" |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 96 | self._DownloadSymbolsHelper( |
Luis Hector Chavez | dca9dd7 | 2018-06-12 19:56:30 | [diff] [blame] | 97 | downloader.GoogleStorageDownloader( |
| 98 | self._work_dir, self.archive_url, |
| 99 | downloader.GoogleStorageDownloader.GetBuildIdFromArchiveURL( |
| 100 | self.archive_url))) |
Simran Basi | 4243a86 | 2014-12-12 20:48:33 | [diff] [blame] | 101 | |
| 102 | def testDownloadSymbolsFromLocal(self): |
| 103 | """Basic symbols download from a Local Path.""" |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 104 | self._DownloadSymbolsHelper( |
| 105 | downloader.LocalDownloader(self._work_dir, self.local_path)) |
| 106 | |
| 107 | |
Achuith Bhandarkar | 46877ad | 2019-09-26 13:54:02 | [diff] [blame] | 108 | class AndroidDownloaderTestBase(unittest.TestCase): |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 109 | """Android Downloader Unittests.""" |
| 110 | |
| 111 | def setUp(self): |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 112 | self._work_dir = tempfile.mkdtemp('downloader-test') |
Dan Shi | 72b1613 | 2015-10-08 19:10:33 | [diff] [blame] | 113 | self.branch = 'release' |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 114 | self.target = 'shamu-userdebug' |
| 115 | self.build_id = '123456' |
| 116 | |
| 117 | def tearDown(self): |
| 118 | shutil.rmtree(self._work_dir, ignore_errors=True) |
| 119 | |
Achuith Bhandarkar | 46877ad | 2019-09-26 13:54:02 | [diff] [blame] | 120 | @mock.patch('downloader.Downloader._DownloadArtifactsSerially') |
| 121 | @mock.patch('downloader.Downloader._DownloadArtifactsInBackground') |
| 122 | def testDownloadFromAndroidBuildServer(self, bg_mock, serial_mock): |
Dan Shi | 72b1613 | 2015-10-08 19:10:33 | [diff] [blame] | 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()) |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 129 | |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 130 | downloader_instance.Download(factory) |
| 131 | # Sanity check the timestamp file exists. |
| 132 | self.assertTrue(os.path.exists( |
Dan Shi | 72b1613 | 2015-10-08 19:10:33 | [diff] [blame] | 133 | os.path.join(self._work_dir, self.branch, self.target, self.build_id, |
Gabe Black | 3b56720 | 2015-09-23 21:07:59 | [diff] [blame] | 134 | downloader.Downloader._TIMESTAMP_FILENAME))) |
Achuith Bhandarkar | 46877ad | 2019-09-26 13:54:02 | [diff] [blame] | 135 | serial_mock.assert_called() |
| 136 | bg_mock.assert_not_called() |
Simran Basi | 4243a86 | 2014-12-12 20:48:33 | [diff] [blame] | 137 | |
Chris Masone | 816e38c | 2012-05-02 19:22:36 | [diff] [blame] | 138 | |
Chris Sosa | 47a7d4e | 2012-03-28 18:26:55 | [diff] [blame] | 139 | if __name__ == '__main__': |
| 140 | unittest.main() |