blob: f6dbe9840c7d89b4b9bccb044877458d058d8228 [file] [log] [blame]
Luis Hector Chavezdca9dd72018-06-12 19:56:301#!/usr/bin/env python2
2# -*- coding: utf-8 -*-
Chris Sosa47a7d4e2012-03-28 18:26:553# 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 Arnoldc65330c2012-09-20 22:17:487"""Unit tests for downloader module."""
Chris Sosa47a7d4e2012-03-28 18:26:558
Gabe Black3b567202015-09-23 21:07:599from __future__ import print_function
10
Chris Sosa47a7d4e2012-03-28 18:26:5511import os
12import shutil
13import tempfile
14import unittest
15
Achuith Bhandarkar46877ad2019-09-26 13:54:0216import mock
17
Gilad Arnoldc65330c2012-09-20 22:17:4818import build_artifact
Chris Sosa47a7d4e2012-03-28 18:26:5519import downloader
20
21
Achuith Bhandarkar46877ad2019-09-26 13:54:0222# pylint: disable=protected-access,no-value-for-parameter
23class DownloaderTestBase(unittest.TestCase):
Simran Basi4243a862014-12-12 20:48:3324 """Downloader Unittests."""
Chris Sosa47a7d4e2012-03-28 18:26:5525
26 def setUp(self):
Chris Sosa47a7d4e2012-03-28 18:26:5527 self._work_dir = tempfile.mkdtemp('downloader-test')
Chris Sosa76e44b92013-01-31 20:11:3828 self.board = 'x86-mario-release'
Chris Sosa47a7d4e2012-03-28 18:26:5529 self.build = 'R17-1413.0.0-a1-b1346'
Chris Sosa76e44b92013-01-31 20:11:3830 self.archive_url = (
31 'gs://chromeos-image-archive/%s/%s' % (self.board, self.build))
Simran Basi4243a862014-12-12 20:48:3332 self.local_path = ('/local/path/x86-mario-release/R17-1413.0.0-a1-b1346')
Chris Sosa47a7d4e2012-03-28 18:26:5533
34 def tearDown(self):
Gilad Arnold0b8c3f32012-09-19 21:35:4435 shutil.rmtree(self._work_dir, ignore_errors=True)
Chris Sosa47a7d4e2012-03-28 18:26:5536
Achuith Bhandarkar46877ad2019-09-26 13:54:0237 @mock.patch('downloader.Downloader._DownloadArtifactsSerially')
38 @mock.patch('downloader.Downloader._DownloadArtifactsInBackground')
39 def _SimpleDownloadOfTestSuites(self, downloader_instance, bg_mock,
40 serial_mock):
Simran Basi4243a862014-12-12 20:48:3341 """Helper to verify test_suites are downloaded correctly.
Chris Sosa47a7d4e2012-03-28 18:26:5542
Simran Basi4243a862014-12-12 20:48:3343 Args:
Gabe Black3b567202015-09-23 21:07:5944 downloader_instance: Downloader object to test with.
Achuith Bhandarkar46877ad2019-09-26 13:54:0245 bg_mock: background download method mock.
46 serial_mock: serial download method mock.
Chris Sosa47a7d4e2012-03-28 18:26:5547 """
Gabe Black3b567202015-09-23 21:07:5948 factory = build_artifact.ChromeOSArtifactFactory(
49 downloader_instance.GetBuildDir(), ['test_suites'],
50 None, downloader_instance.GetBuild())
Chris Sosa47a7d4e2012-03-28 18:26:5551
Gabe Black3b567202015-09-23 21:07:5952 downloader_instance.Download(factory)
Chris Sosa76e44b92013-01-31 20:11:3853 # Sanity check the timestamp file exists.
Achuith Bhandarkar46877ad2019-09-26 13:54:0254 install_dir = os.path.join(self._work_dir, self.board, self.build)
Alex Millera44d5022012-07-27 18:34:1655 self.assertTrue(os.path.exists(
Achuith Bhandarkar46877ad2019-09-26 13:54:0256 os.path.join(install_dir, downloader.Downloader._TIMESTAMP_FILENAME)))
57 serial_mock.assert_called()
58 bg_mock.assert_called()
Chris Sosa9164ca32012-03-28 18:04:5059
Simran Basi4243a862014-12-12 20:48:3360 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 Black3b567202015-09-23 21:07:5966 self._SimpleDownloadOfTestSuites(
Luis Hector Chavezdca9dd72018-06-12 19:56:3067 downloader.GoogleStorageDownloader(
68 self._work_dir, self.archive_url,
69 downloader.GoogleStorageDownloader.GetBuildIdFromArchiveURL(
70 self.archive_url)))
Simran Basi4243a862014-12-12 20:48:3371
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 Black3b567202015-09-23 21:07:5978 self._SimpleDownloadOfTestSuites(
79 downloader.LocalDownloader(self._work_dir, self.local_path))
Simran Basi4243a862014-12-12 20:48:3380
Achuith Bhandarkar46877ad2019-09-26 13:54:0281 @mock.patch('downloader.Downloader._DownloadArtifactsSerially')
82 @mock.patch('downloader.Downloader._DownloadArtifactsInBackground')
83 def _DownloadSymbolsHelper(self, downloader_instance, bg_mock, serial_mock):
Chris Sosa76e44b92013-01-31 20:11:3884 """Basic symbols download."""
Gabe Black3b567202015-09-23 21:07:5985 factory = build_artifact.ChromeOSArtifactFactory(
86 downloader_instance.GetBuildDir(), ['symbols'],
87 None, downloader_instance.GetBuild())
88
Chris Sosa76e44b92013-01-31 20:11:3889 # Should not get called but mocking so that we know it wasn't called.
Gabe Black3b567202015-09-23 21:07:5990 downloader_instance.Download(factory)
Achuith Bhandarkar46877ad2019-09-26 13:54:0291 serial_mock.assert_called()
92 bg_mock.assert_not_called()
Chris Masone816e38c2012-05-02 19:22:3693
Simran Basi4243a862014-12-12 20:48:3394 def testDownloadSymbolsFromGS(self):
95 """Basic symbols download from Google Storage."""
Gabe Black3b567202015-09-23 21:07:5996 self._DownloadSymbolsHelper(
Luis Hector Chavezdca9dd72018-06-12 19:56:3097 downloader.GoogleStorageDownloader(
98 self._work_dir, self.archive_url,
99 downloader.GoogleStorageDownloader.GetBuildIdFromArchiveURL(
100 self.archive_url)))
Simran Basi4243a862014-12-12 20:48:33101
102 def testDownloadSymbolsFromLocal(self):
103 """Basic symbols download from a Local Path."""
Gabe Black3b567202015-09-23 21:07:59104 self._DownloadSymbolsHelper(
105 downloader.LocalDownloader(self._work_dir, self.local_path))
106
107
Achuith Bhandarkar46877ad2019-09-26 13:54:02108class AndroidDownloaderTestBase(unittest.TestCase):
Gabe Black3b567202015-09-23 21:07:59109 """Android Downloader Unittests."""
110
111 def setUp(self):
Gabe Black3b567202015-09-23 21:07:59112 self._work_dir = tempfile.mkdtemp('downloader-test')
Dan Shi72b16132015-10-08 19:10:33113 self.branch = 'release'
Gabe Black3b567202015-09-23 21:07:59114 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 Bhandarkar46877ad2019-09-26 13:54:02120 @mock.patch('downloader.Downloader._DownloadArtifactsSerially')
121 @mock.patch('downloader.Downloader._DownloadArtifactsInBackground')
122 def testDownloadFromAndroidBuildServer(self, bg_mock, serial_mock):
Dan Shi72b16132015-10-08 19:10:33123 """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 Black3b567202015-09-23 21:07:59126 factory = build_artifact.AndroidArtifactFactory(
127 downloader_instance.GetBuildDir(), ['fastboot'],
128 None, downloader_instance.GetBuild())
Gabe Black3b567202015-09-23 21:07:59129
Gabe Black3b567202015-09-23 21:07:59130 downloader_instance.Download(factory)
131 # Sanity check the timestamp file exists.
132 self.assertTrue(os.path.exists(
Dan Shi72b16132015-10-08 19:10:33133 os.path.join(self._work_dir, self.branch, self.target, self.build_id,
Gabe Black3b567202015-09-23 21:07:59134 downloader.Downloader._TIMESTAMP_FILENAME)))
Achuith Bhandarkar46877ad2019-09-26 13:54:02135 serial_mock.assert_called()
136 bg_mock.assert_not_called()
Simran Basi4243a862014-12-12 20:48:33137
Chris Masone816e38c2012-05-02 19:22:36138
Chris Sosa47a7d4e2012-03-28 18:26:55139if __name__ == '__main__':
140 unittest.main()