blob: 64ca12eae6a79bed1f3407e12f98d08b510fb525 [file] [log] [blame]
Chris Sosa47a7d4e2012-03-28 11:26:55 -07001#!/usr/bin/python
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 Arnoldc65330c2012-09-20 15:17:48 -07007"""Unit tests for downloader module."""
Chris Sosa47a7d4e2012-03-28 11:26:55 -07008
Chris Sosa76e44b92013-01-31 12:11:38 -08009import mox
Chris Sosa47a7d4e2012-03-28 11:26:55 -070010import os
11import shutil
12import tempfile
13import unittest
14
Gilad Arnoldc65330c2012-09-20 15:17:48 -070015import build_artifact
Chris Sosa47a7d4e2012-03-28 11:26:55 -070016import downloader
17
18
Chris Sosa76e44b92013-01-31 12:11:38 -080019# pylint: disable=W0212,E1120
Chris Masone816e38c2012-05-02 12:22:36 -070020class DownloaderTestBase(mox.MoxTestBase):
Simran Basi4243a862014-12-12 12:48:33 -080021 """Downloader Unittests."""
Chris Sosa47a7d4e2012-03-28 11:26:55 -070022
23 def setUp(self):
24 mox.MoxTestBase.setUp(self)
25 self._work_dir = tempfile.mkdtemp('downloader-test')
Chris Sosa76e44b92013-01-31 12:11:38 -080026 self.board = 'x86-mario-release'
Chris Sosa47a7d4e2012-03-28 11:26:55 -070027 self.build = 'R17-1413.0.0-a1-b1346'
Chris Sosa76e44b92013-01-31 12:11:38 -080028 self.archive_url = (
29 'gs://chromeos-image-archive/%s/%s' % (self.board, self.build))
Simran Basi4243a862014-12-12 12:48:33 -080030 self.local_path = ('/local/path/x86-mario-release/R17-1413.0.0-a1-b1346')
Chris Sosa47a7d4e2012-03-28 11:26:55 -070031
32 def tearDown(self):
Gilad Arnold0b8c3f32012-09-19 14:35:44 -070033 shutil.rmtree(self._work_dir, ignore_errors=True)
Chris Sosa47a7d4e2012-03-28 11:26:55 -070034
Simran Basi4243a862014-12-12 12:48:33 -080035 def _SimpleDownloadOfTestSuites(self, archive_path):
36 """Helper to verify test_suites are downloaded correctly.
Chris Sosa47a7d4e2012-03-28 11:26:55 -070037
Simran Basi4243a862014-12-12 12:48:33 -080038 Args:
39 archive_path: Archive url or local path to test with.
Chris Sosa47a7d4e2012-03-28 11:26:55 -070040 """
Chris Sosa76e44b92013-01-31 12:11:38 -080041 downloader_instance = downloader.Downloader(self._work_dir,
Simran Basi4243a862014-12-12 12:48:33 -080042 archive_path)
Chris Sosa76e44b92013-01-31 12:11:38 -080043 self.mox.StubOutWithMock(downloader.Downloader,
44 '_DownloadArtifactsSerially')
45 self.mox.StubOutWithMock(downloader.Downloader,
46 '_DownloadArtifactsInBackground')
Chris Sosa47a7d4e2012-03-28 11:26:55 -070047
Chris Sosa76e44b92013-01-31 12:11:38 -080048 downloader.Downloader._DownloadArtifactsInBackground(mox.In(mox.IsA(
49 build_artifact.AutotestTarballBuildArtifact)))
50 downloader.Downloader._DownloadArtifactsSerially(
Simran Basi4243a862014-12-12 12:48:33 -080051 [mox.IsA(build_artifact.BundledBuildArtifact)], no_wait=True)
Chris Sosa47a7d4e2012-03-28 11:26:55 -070052 self.mox.ReplayAll()
Chris Sosa6b0c6172013-08-05 17:01:33 -070053 downloader_instance.Download(artifacts=['test_suites'],
54 files=None)
Chris Sosa76e44b92013-01-31 12:11:38 -080055 # Sanity check the timestamp file exists.
Alex Millera44d5022012-07-27 11:34:16 -070056 self.assertTrue(os.path.exists(
Chris Sosa76e44b92013-01-31 12:11:38 -080057 os.path.join(self._work_dir, self.board, self.build,
58 downloader.Downloader._TIMESTAMP_FILENAME)))
59 self.mox.VerifyAll()
Chris Sosa9164ca32012-03-28 11:04:50 -070060
Simran Basi4243a862014-12-12 12:48:33 -080061 def testSimpleDownloadOfTestSuitesFromGS(self):
62 """Basic test_suites test.
63
64 Verifies that if we request the test_suites from Google Storage, it gets
65 downloaded and the autotest tarball is attempted in the background.
66 """
67 self._SimpleDownloadOfTestSuites(self.archive_url)
68
69 def testSimpleDownloadOfTestSuitesFromLocal(self):
70 """Basic test_suites test.
71
72 Verifies that if we request the test_suites from a local path, it gets
73 downloaded and the autotest tarball is attempted in the background.
74 """
75 self._SimpleDownloadOfTestSuites(self.local_path)
76
77 def _DownloadSymbolsHelper(self, archive_path):
Chris Sosa76e44b92013-01-31 12:11:38 -080078 """Basic symbols download."""
Simran Basi4243a862014-12-12 12:48:33 -080079 downloader_instance = downloader.Downloader(self._work_dir, archive_path)
Chris Sosa76e44b92013-01-31 12:11:38 -080080 self.mox.StubOutWithMock(downloader.Downloader,
81 '_DownloadArtifactsSerially')
82 # Should not get called but mocking so that we know it wasn't called.
83 self.mox.StubOutWithMock(downloader.Downloader,
84 '_DownloadArtifactsInBackground')
85 downloader.Downloader._DownloadArtifactsSerially(
Simran Basi4243a862014-12-12 12:48:33 -080086 [mox.IsA(build_artifact.BundledBuildArtifact)], no_wait=True)
Chris Masone816e38c2012-05-02 12:22:36 -070087 self.mox.ReplayAll()
Chris Sosa6b0c6172013-08-05 17:01:33 -070088 downloader_instance.Download(artifacts=['symbols'],
89 files=None)
Chris Masone816e38c2012-05-02 12:22:36 -070090 self.mox.VerifyAll()
91
Simran Basi4243a862014-12-12 12:48:33 -080092 def testDownloadSymbolsFromGS(self):
93 """Basic symbols download from Google Storage."""
94 self._DownloadSymbolsHelper(self.archive_url)
95
96 def testDownloadSymbolsFromLocal(self):
97 """Basic symbols download from a Local Path."""
98 self._DownloadSymbolsHelper(self.local_path)
99
Chris Masone816e38c2012-05-02 12:22:36 -0700100
Chris Sosa47a7d4e2012-03-28 11:26:55 -0700101if __name__ == '__main__':
102 unittest.main()