Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 1 | #!/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 | |
| 7 | """Unit tests for devserver_util module.""" |
| 8 | |
| 9 | import mox |
| 10 | import os |
| 11 | import shutil |
| 12 | import tempfile |
| 13 | import unittest |
| 14 | |
Chris Masone | 3fe44db | 2012-05-02 10:50:21 -0700 | [diff] [blame^] | 15 | import artifact_download |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 16 | import devserver |
| 17 | import devserver_util |
| 18 | import downloader |
| 19 | |
| 20 | |
| 21 | # Fake Dev Server Layout: |
| 22 | TEST_LAYOUT = { |
| 23 | 'test-board-1': ['R17-1413.0.0-a1-b1346', 'R17-18.0.0-a1-b1346'], |
| 24 | 'test-board-2': ['R16-2241.0.0-a0-b2', 'R17-2.0.0-a1-b1346'], |
| 25 | 'test-board-3': [] |
| 26 | } |
| 27 | |
| 28 | |
| 29 | class DownloaderTest(mox.MoxTestBase): |
| 30 | |
| 31 | def setUp(self): |
| 32 | mox.MoxTestBase.setUp(self) |
| 33 | self._work_dir = tempfile.mkdtemp('downloader-test') |
| 34 | self.build = 'R17-1413.0.0-a1-b1346' |
| 35 | self.archive_url_prefix = ( |
| 36 | 'gs://chromeos-image-archive/x86-mario-release/' + self.build) |
| 37 | |
| 38 | def tearDown(self): |
| 39 | if os.path.exists(self._work_dir): |
| 40 | shutil.rmtree(self._work_dir) |
| 41 | |
| 42 | def _CommonDownloaderSetup(self): |
| 43 | """Common code to downloader tests. |
| 44 | |
| 45 | Sets up artifacts and sets up expectations for synchronous artifacts to |
| 46 | be downloaded first. |
| 47 | |
| 48 | Returns the artifacts to use in the test. |
| 49 | """ |
| 50 | board = 'x86-mario-release' |
| 51 | self.mox.StubOutWithMock(devserver_util, 'AcquireLock') |
| 52 | self.mox.StubOutWithMock(devserver_util, 'GatherArtifactDownloads') |
| 53 | self.mox.StubOutWithMock(devserver_util, 'ReleaseLock') |
| 54 | self.mox.StubOutWithMock(tempfile, 'mkdtemp') |
| 55 | |
| 56 | artifacts = [] |
| 57 | |
| 58 | for index in range(5): |
Chris Masone | 3fe44db | 2012-05-02 10:50:21 -0700 | [diff] [blame^] | 59 | artifact = self.mox.CreateMock(artifact_download.DownloadableArtifact) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 60 | # Make every other artifact synchronous. |
| 61 | if index % 2 == 0: |
| 62 | artifact.Synchronous = lambda: True |
| 63 | else: |
| 64 | artifact.Synchronous = lambda: False |
| 65 | |
| 66 | artifacts.append(artifact) |
| 67 | |
| 68 | devserver_util.AcquireLock( |
| 69 | static_dir=self._work_dir, |
| 70 | tag='/'.join([board, self.build])).AndReturn(self._work_dir) |
| 71 | |
| 72 | tempfile.mkdtemp(suffix=mox.IgnoreArg()).AndReturn(self._work_dir) |
| 73 | devserver_util.GatherArtifactDownloads( |
| 74 | self._work_dir, self.archive_url_prefix, self.build, |
Chris Masone | 3fe44db | 2012-05-02 10:50:21 -0700 | [diff] [blame^] | 75 | self._work_dir).AndReturn(artifacts) |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 76 | |
| 77 | for index, artifact in enumerate(artifacts): |
| 78 | if index % 2 == 0: |
| 79 | artifact.Download() |
| 80 | artifact.Stage() |
| 81 | |
| 82 | return artifacts |
| 83 | |
| 84 | def testDownloaderSerially(self): |
| 85 | """Runs through the standard downloader workflow with no backgrounding.""" |
| 86 | artifacts = self._CommonDownloaderSetup() |
| 87 | |
| 88 | # Downloads non-synchronous artifacts second. |
| 89 | for index, artifact in enumerate(artifacts): |
| 90 | if index % 2 != 0: |
| 91 | artifact.Download() |
| 92 | artifact.Stage() |
| 93 | |
| 94 | self.mox.ReplayAll() |
| 95 | self.assertEqual(downloader.Downloader(self._work_dir).Download( |
| 96 | self.archive_url_prefix, background=False), 'Success') |
| 97 | self.mox.VerifyAll() |
| 98 | |
| 99 | def testDownloaderInBackground(self): |
| 100 | """Runs through the standard downloader workflow with backgrounding.""" |
| 101 | artifacts = self._CommonDownloaderSetup() |
| 102 | |
| 103 | # Downloads non-synchronous artifacts second. |
| 104 | for index, artifact in enumerate(artifacts): |
| 105 | if index % 2 != 0: |
| 106 | artifact.Download() |
| 107 | artifact.Stage() |
| 108 | |
| 109 | self.mox.ReplayAll() |
| 110 | d = downloader.Downloader(self._work_dir) |
| 111 | d.Download(self.archive_url_prefix, background=True) |
| 112 | self.assertEqual(d.GetStatusOfBackgroundDownloads(), 'Success') |
| 113 | self.mox.VerifyAll() |
| 114 | |
| 115 | def testInteractionWithDevserver(self): |
Chris Sosa | 9164ca3 | 2012-03-28 11:04:50 -0700 | [diff] [blame] | 116 | """Tests interaction between the downloader and devserver methods.""" |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 117 | artifacts = self._CommonDownloaderSetup() |
| 118 | class FakeUpdater(): |
| 119 | static_dir = self._work_dir |
| 120 | |
| 121 | devserver.updater = FakeUpdater() |
| 122 | |
| 123 | # Downloads non-synchronous artifacts second. |
| 124 | for index, artifact in enumerate(artifacts): |
| 125 | if index % 2 != 0: |
| 126 | artifact.Download() |
| 127 | artifact.Stage() |
| 128 | |
| 129 | self.mox.ReplayAll() |
| 130 | dev = devserver.DevServerRoot() |
| 131 | status = dev.download(archive_url=self.archive_url_prefix) |
| 132 | self.assertTrue(status, 'Success') |
| 133 | status = dev.wait_for_status(archive_url=self.archive_url_prefix) |
| 134 | self.assertTrue(status, 'Success') |
| 135 | self.mox.VerifyAll() |
| 136 | |
Chris Sosa | 9164ca3 | 2012-03-28 11:04:50 -0700 | [diff] [blame] | 137 | def testBuildStaged(self): |
| 138 | """Test whether we can correctly check if a build is previously staged.""" |
| 139 | archive_url = 'x86-awesome-release/R99-1234.0-r1' |
| 140 | archive_url_non_staged = 'x86-awesome-release/R99-1234.0-r2' |
| 141 | # Create the directory to reflect staging. |
| 142 | os.makedirs(os.path.join(self._work_dir, archive_url)) |
| 143 | |
| 144 | self.assertTrue(downloader.Downloader.BuildStaged(archive_url, |
| 145 | self._work_dir)) |
| 146 | self.assertFalse(downloader.Downloader.BuildStaged(archive_url_non_staged, |
| 147 | self._work_dir)) |
| 148 | |
Chris Sosa | 47a7d4e | 2012-03-28 11:26:55 -0700 | [diff] [blame] | 149 | |
| 150 | if __name__ == '__main__': |
| 151 | unittest.main() |