blob: 8225a7550995bd68252043a821f30f8e0b3e0cfb [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 gsutil_util module."""
8
Chris Sosa47a7d4e2012-03-28 11:26:55 -07009import subprocess
Chris Sosa101fd862012-06-12 17:44:53 -070010import time
Chris Sosa47a7d4e2012-03-28 11:26:55 -070011import unittest
12
Gilad Arnoldabb352e2012-09-23 01:24:27 -070013import mox
14
Chris Sosa47a7d4e2012-03-28 11:26:55 -070015import gsutil_util
16
17
Chris Sosa76e44b92013-01-31 12:11:38 -080018# pylint: disable=W0212
Chris Sosa47a7d4e2012-03-28 11:26:55 -070019class GSUtilUtilTest(mox.MoxTestBase):
20
21 def setUp(self):
22 mox.MoxTestBase.setUp(self)
23
24 self._good_mock_process = self.mox.CreateMock(subprocess.Popen)
25 self._good_mock_process.returncode = 0
26 self._bad_mock_process = self.mox.CreateMock(subprocess.Popen)
27 self._bad_mock_process.returncode = 1
Chris Sosa47a7d4e2012-03-28 11:26:55 -070028
29 def _CallRunGS(self, str_should_contain, attempts=1):
30 """Helper that wraps a RunGS for tests."""
31 for attempt in range(attempts):
32 if attempt == gsutil_util.GSUTIL_ATTEMPTS:
33 # We can't mock more than we can attempt.
34 return
35
36 # Return 1's for all but last attempt.
37 if attempt != attempts - 1:
38 mock_process = self._bad_mock_process
39 else:
40 mock_process = self._good_mock_process
41
Chris Sosa6de29302013-03-14 15:27:36 -070042 subprocess.Popen(mox.StrContains(str_should_contain),
43 shell=True, stdout=subprocess.PIPE,
44 stderr=subprocess.PIPE).AndReturn(mock_process)
Chris Sosa47a7d4e2012-03-28 11:26:55 -070045 mock_process.communicate().AndReturn(('Does not matter', None))
46
47 def testDownloadFromGS(self):
48 """Tests that we can run download build from gs with one error."""
Chris Sosa76e44b92013-01-31 12:11:38 -080049 self.mox.StubOutWithMock(time, 'sleep')
50 time.sleep(mox.IgnoreArg()).MultipleTimes()
Chris Sosa47a7d4e2012-03-28 11:26:55 -070051 self.mox.StubOutWithMock(subprocess, 'Popen', use_mock_anything=True)
52
53 # Make sure we our retry works.
54 self._CallRunGS('from to', attempts=2)
55 self.mox.ReplayAll()
56 gsutil_util.DownloadFromGS('from', 'to')
57 self.mox.VerifyAll()
58
59 def testDownloadFromGSButGSDown(self):
60 """Tests that we fail correctly if we can't reach GS."""
Chris Sosa76e44b92013-01-31 12:11:38 -080061 self.mox.StubOutWithMock(time, 'sleep')
62 time.sleep(mox.IgnoreArg()).MultipleTimes()
Chris Sosa47a7d4e2012-03-28 11:26:55 -070063 self.mox.StubOutWithMock(subprocess, 'Popen', use_mock_anything=True)
64 self._CallRunGS('from to', attempts=gsutil_util.GSUTIL_ATTEMPTS + 1)
65
66 self.mox.ReplayAll()
67 self.assertRaises(
68 gsutil_util.GSUtilError,
69 gsutil_util.DownloadFromGS,
70 'from', 'to')
71 self.mox.VerifyAll()
72
Chris Sosa76e44b92013-01-31 12:11:38 -080073 def testGSNamesFromList(self):
74 """Test that we can detect whether the target artifacts are available."""
75 # Test when the all target files are available
76 pattern = '.*_full_.*'
77 uploaded_list = ['chromeos_R17-1413.0.0-a1_x86-mario_full_dev.bin',
78 'debug.tgz',
79 'autotest.tar.bz2']
80
81 names = gsutil_util._GetGSNamesFromList(uploaded_list, pattern)
82 self.assertEqual(names[0],
83 'chromeos_R17-1413.0.0-a1_x86-mario_full_dev.bin')
84
85 bad_pattern = '_delta_'
86 # Test when some target files are missing
87 names = gsutil_util._GetGSNamesFromList(uploaded_list, bad_pattern)
88 self.assertEqual(names, [])
89
90 def testGetGSNamesWithWait(self):
91 """Test that we get the target artifact that is available."""
92 archive_url = ('gs://chromeos-image-archive/x86-mario-release/'
93 'R17-1413.0.0-a1-b1346')
94 name = 'chromeos_R17-1413.0.0-a1_x86-mario_full_dev.bin'
95 pattern = '_full_'
96 mock_data = 'mock data\nmock_data\nmock_data'
97 msg = 'UNIT TEST'
98
99 self.mox.StubOutWithMock(gsutil_util, 'GSUtilRun')
100 self.mox.StubOutWithMock(gsutil_util, '_GetGSNamesFromList')
101
102 # GSUtil cat gs://archive_url_prefix/UPLOADED.
103 gsutil_util.GSUtilRun(mox.StrContains(gsutil_util.UPLOADED_LIST),
104 mox.IgnoreArg()).AndReturn(mock_data)
105 gsutil_util._GetGSNamesFromList(mock_data.split('\n'),
106 pattern).AndReturn([name])
107
108 self.mox.ReplayAll()
Chris Sosa6de29302013-03-14 15:27:36 -0700109 # Timeout explicitly set to 0 to test that we always run at least once.
Chris Sosa76e44b92013-01-31 12:11:38 -0800110 returned_names = gsutil_util.GetGSNamesWithWait(
Chris Sosa6de29302013-03-14 15:27:36 -0700111 pattern, archive_url, msg, delay=1, timeout=0)
Chris Sosa76e44b92013-01-31 12:11:38 -0800112 self.assertEqual([name], returned_names)
113 self.mox.VerifyAll()
114
115 def testGetGSNamesWithWaitWithRetry(self):
116 """Test that we can poll until all target artifacts are available."""
117 archive_url = ('gs://chromeos-image-archive/x86-mario-release/'
118 'R17-1413.0.0-a1-b1346')
119 name = 'chromeos_R17-1413.0.0-a1_x86-mario_full_dev.bin'
120 pattern = '_full_'
121 mock_data = 'mock data\nmock_data\nmock_data'
122 msg = 'UNIT TEST'
123
124 self.mox.StubOutWithMock(gsutil_util, 'GSUtilRun')
125 self.mox.StubOutWithMock(gsutil_util, '_GetGSNamesFromList')
126
127 # GSUtil cat gs://archive_url_prefix/UPLOADED.
128 gsutil_util.GSUtilRun(mox.StrContains(gsutil_util.UPLOADED_LIST),
129 mox.IgnoreArg()).AndReturn(mock_data)
130 gsutil_util._GetGSNamesFromList(mock_data.split('\n'),
131 pattern).AndReturn(None)
132
133 gsutil_util.GSUtilRun(mox.StrContains(gsutil_util.UPLOADED_LIST),
134 mox.IgnoreArg()).AndReturn(mock_data)
135 gsutil_util._GetGSNamesFromList(mox.IgnoreArg(),
136 mox.IgnoreArg()).AndReturn([name])
137
138 self.mox.ReplayAll()
139 returned_names = gsutil_util.GetGSNamesWithWait(
Chris Sosa6de29302013-03-14 15:27:36 -0700140 pattern, archive_url, msg, delay=1, timeout=3)
Chris Sosa76e44b92013-01-31 12:11:38 -0800141 self.assertEqual(name, returned_names[0])
142 self.mox.VerifyAll()
143
144 def testGetGSNamesWithWaitTimeout(self):
145 """Test that we wait for the target artifacts until timeout occurs."""
146 archive_url = ('gs://chromeos-image-archive/x86-mario-release/'
147 'R17-1413.0.0-a1-b1346')
148 pattern = '_full_'
149 mock_data = 'mock data\nmock_data\nmock_data'
150 msg = 'UNIT TEST'
151
152 self.mox.StubOutWithMock(gsutil_util, 'GSUtilRun')
153 self.mox.StubOutWithMock(gsutil_util, '_GetGSNamesFromList')
154
155 # GSUtil cat gs://archive_url_prefix/UPLOADED.
156 gsutil_util.GSUtilRun(mox.StrContains(gsutil_util.UPLOADED_LIST),
157 mox.IgnoreArg()).AndReturn(mock_data)
158 gsutil_util._GetGSNamesFromList(mock_data.split('\n'),
159 pattern).AndReturn(None)
160
161 self.mox.ReplayAll()
162 returned_name = gsutil_util.GetGSNamesWithWait(
163 pattern, archive_url, msg, delay=2, timeout=1)
164 self.assertEqual(returned_name, None)
165 self.mox.VerifyAll()
166
Chris Sosa47a7d4e2012-03-28 11:26:55 -0700167
168if __name__ == '__main__':
169 unittest.main()