blob: e8634858808ec21f3e37c3a1a17b098d15763d80 [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
7"""Unit tests for devserver_util module."""
8
9import mox
10import os
11import shutil
12import tempfile
13import unittest
14
15import artifact_download
16import devserver
17import devserver_util
18import downloader
19
20
21# Fake Dev Server Layout:
22TEST_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
29class 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):
59 artifact = self.mox.CreateMock(artifact_download.DownloadableArtifact)
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,
75 self._work_dir).AndReturn(artifacts)
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):
116 artifacts = self._CommonDownloaderSetup()
117 class FakeUpdater():
118 static_dir = self._work_dir
119
120 devserver.updater = FakeUpdater()
121
122 # Downloads non-synchronous artifacts second.
123 for index, artifact in enumerate(artifacts):
124 if index % 2 != 0:
125 artifact.Download()
126 artifact.Stage()
127
128 self.mox.ReplayAll()
129 dev = devserver.DevServerRoot()
130 status = dev.download(archive_url=self.archive_url_prefix)
131 self.assertTrue(status, 'Success')
132 status = dev.wait_for_status(archive_url=self.archive_url_prefix)
133 self.assertTrue(status, 'Success')
134 self.mox.VerifyAll()
135
136
137if __name__ == '__main__':
138 unittest.main()