blob: 0082f75924515befce7fce34838631edfaf54443 [file] [log] [blame]
Luis Hector Chavezdca9dd72018-06-12 12:56:30 -07001#!/usr/bin/env python2
2# -*- coding: utf-8 -*-
Chris Sosa47a7d4e2012-03-28 11:26:55 -07003# 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 15:17:48 -07007"""Unit tests for downloader module."""
Chris Sosa47a7d4e2012-03-28 11:26:55 -07008
Gabe Black3b567202015-09-23 14:07:59 -07009from __future__ import print_function
10
Chris Sosa76e44b92013-01-31 12:11:38 -080011import mox
Chris Sosa47a7d4e2012-03-28 11:26:55 -070012import os
13import shutil
14import tempfile
15import unittest
16
Gilad Arnoldc65330c2012-09-20 15:17:48 -070017import build_artifact
Chris Sosa47a7d4e2012-03-28 11:26:55 -070018import downloader
19
20
Chris Sosa76e44b92013-01-31 12:11:38 -080021# pylint: disable=W0212,E1120
Chris Masone816e38c2012-05-02 12:22:36 -070022class DownloaderTestBase(mox.MoxTestBase):
Simran Basi4243a862014-12-12 12:48:33 -080023 """Downloader Unittests."""
Chris Sosa47a7d4e2012-03-28 11:26:55 -070024
25 def setUp(self):
26 mox.MoxTestBase.setUp(self)
27 self._work_dir = tempfile.mkdtemp('downloader-test')
Chris Sosa76e44b92013-01-31 12:11:38 -080028 self.board = 'x86-mario-release'
Chris Sosa47a7d4e2012-03-28 11:26:55 -070029 self.build = 'R17-1413.0.0-a1-b1346'
Chris Sosa76e44b92013-01-31 12:11:38 -080030 self.archive_url = (
31 'gs://chromeos-image-archive/%s/%s' % (self.board, self.build))
Simran Basi4243a862014-12-12 12:48:33 -080032 self.local_path = ('/local/path/x86-mario-release/R17-1413.0.0-a1-b1346')
Chris Sosa47a7d4e2012-03-28 11:26:55 -070033
34 def tearDown(self):
Gilad Arnold0b8c3f32012-09-19 14:35:44 -070035 shutil.rmtree(self._work_dir, ignore_errors=True)
Chris Sosa47a7d4e2012-03-28 11:26:55 -070036
Gabe Black3b567202015-09-23 14:07:59 -070037 def _SimpleDownloadOfTestSuites(self, downloader_instance):
Simran Basi4243a862014-12-12 12:48:33 -080038 """Helper to verify test_suites are downloaded correctly.
Chris Sosa47a7d4e2012-03-28 11:26:55 -070039
Simran Basi4243a862014-12-12 12:48:33 -080040 Args:
Gabe Black3b567202015-09-23 14:07:59 -070041 downloader_instance: Downloader object to test with.
Chris Sosa47a7d4e2012-03-28 11:26:55 -070042 """
Gabe Black3b567202015-09-23 14:07:59 -070043 factory = build_artifact.ChromeOSArtifactFactory(
44 downloader_instance.GetBuildDir(), ['test_suites'],
45 None, downloader_instance.GetBuild())
Chris Sosa76e44b92013-01-31 12:11:38 -080046 self.mox.StubOutWithMock(downloader.Downloader,
47 '_DownloadArtifactsSerially')
48 self.mox.StubOutWithMock(downloader.Downloader,
49 '_DownloadArtifactsInBackground')
Chris Sosa47a7d4e2012-03-28 11:26:55 -070050
Chris Sosa76e44b92013-01-31 12:11:38 -080051 downloader.Downloader._DownloadArtifactsInBackground(mox.In(mox.IsA(
Gabe Black3b567202015-09-23 14:07:59 -070052 build_artifact.AutotestTarball)))
Chris Sosa76e44b92013-01-31 12:11:38 -080053 downloader.Downloader._DownloadArtifactsSerially(
Gabe Black3b567202015-09-23 14:07:59 -070054 [mox.IsA(build_artifact.BundledArtifact)], no_wait=True)
Chris Sosa47a7d4e2012-03-28 11:26:55 -070055 self.mox.ReplayAll()
Gabe Black3b567202015-09-23 14:07:59 -070056 downloader_instance.Download(factory)
Chris Sosa76e44b92013-01-31 12:11:38 -080057 # Sanity check the timestamp file exists.
Alex Millera44d5022012-07-27 11:34:16 -070058 self.assertTrue(os.path.exists(
Chris Sosa76e44b92013-01-31 12:11:38 -080059 os.path.join(self._work_dir, self.board, self.build,
60 downloader.Downloader._TIMESTAMP_FILENAME)))
61 self.mox.VerifyAll()
Chris Sosa9164ca32012-03-28 11:04:50 -070062
Simran Basi4243a862014-12-12 12:48:33 -080063 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 Black3b567202015-09-23 14:07:59 -070069 self._SimpleDownloadOfTestSuites(
Luis Hector Chavezdca9dd72018-06-12 12:56:30 -070070 downloader.GoogleStorageDownloader(
71 self._work_dir, self.archive_url,
72 downloader.GoogleStorageDownloader.GetBuildIdFromArchiveURL(
73 self.archive_url)))
Simran Basi4243a862014-12-12 12:48:33 -080074
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 Black3b567202015-09-23 14:07:59 -070081 self._SimpleDownloadOfTestSuites(
82 downloader.LocalDownloader(self._work_dir, self.local_path))
Simran Basi4243a862014-12-12 12:48:33 -080083
Gabe Black3b567202015-09-23 14:07:59 -070084 def _DownloadSymbolsHelper(self, downloader_instance):
Chris Sosa76e44b92013-01-31 12:11:38 -080085 """Basic symbols download."""
Gabe Black3b567202015-09-23 14:07:59 -070086 factory = build_artifact.ChromeOSArtifactFactory(
87 downloader_instance.GetBuildDir(), ['symbols'],
88 None, downloader_instance.GetBuild())
89
Chris Sosa76e44b92013-01-31 12:11:38 -080090 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 Black3b567202015-09-23 14:07:59 -070096 [mox.IsA(build_artifact.BundledArtifact)], no_wait=True)
Chris Masone816e38c2012-05-02 12:22:36 -070097 self.mox.ReplayAll()
Gabe Black3b567202015-09-23 14:07:59 -070098 downloader_instance.Download(factory)
Chris Masone816e38c2012-05-02 12:22:36 -070099 self.mox.VerifyAll()
100
Simran Basi4243a862014-12-12 12:48:33 -0800101 def testDownloadSymbolsFromGS(self):
102 """Basic symbols download from Google Storage."""
Gabe Black3b567202015-09-23 14:07:59 -0700103 self._DownloadSymbolsHelper(
Luis Hector Chavezdca9dd72018-06-12 12:56:30 -0700104 downloader.GoogleStorageDownloader(
105 self._work_dir, self.archive_url,
106 downloader.GoogleStorageDownloader.GetBuildIdFromArchiveURL(
107 self.archive_url)))
Simran Basi4243a862014-12-12 12:48:33 -0800108
109 def testDownloadSymbolsFromLocal(self):
110 """Basic symbols download from a Local Path."""
Gabe Black3b567202015-09-23 14:07:59 -0700111 self._DownloadSymbolsHelper(
112 downloader.LocalDownloader(self._work_dir, self.local_path))
113
114
115class 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 Shi72b16132015-10-08 12:10:33 -0700121 self.branch = 'release'
Gabe Black3b567202015-09-23 14:07:59 -0700122 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 Shi72b16132015-10-08 12:10:33 -0700128 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 Black3b567202015-09-23 14:07:59 -0700132 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 Black3b567202015-09-23 14:07:59 -0700140 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 Shi72b16132015-10-08 12:10:33 -0700146 os.path.join(self._work_dir, self.branch, self.target, self.build_id,
Gabe Black3b567202015-09-23 14:07:59 -0700147 downloader.Downloader._TIMESTAMP_FILENAME)))
148 self.mox.VerifyAll()
Simran Basi4243a862014-12-12 12:48:33 -0800149
Chris Masone816e38c2012-05-02 12:22:36 -0700150
Chris Sosa47a7d4e2012-03-28 11:26:55 -0700151if __name__ == '__main__':
152 unittest.main()