blob: 0813be7c888dda1ef40843a523b784e158de7465 [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
9import mox
10import subprocess
11import unittest
12
13import gsutil_util
14
15
16class GSUtilUtilTest(mox.MoxTestBase):
17
18 def setUp(self):
19 mox.MoxTestBase.setUp(self)
20
21 self._good_mock_process = self.mox.CreateMock(subprocess.Popen)
22 self._good_mock_process.returncode = 0
23 self._bad_mock_process = self.mox.CreateMock(subprocess.Popen)
24 self._bad_mock_process.returncode = 1
25
26 def _CallRunGS(self, str_should_contain, attempts=1):
27 """Helper that wraps a RunGS for tests."""
28 for attempt in range(attempts):
29 if attempt == gsutil_util.GSUTIL_ATTEMPTS:
30 # We can't mock more than we can attempt.
31 return
32
33 # Return 1's for all but last attempt.
34 if attempt != attempts - 1:
35 mock_process = self._bad_mock_process
36 else:
37 mock_process = self._good_mock_process
38
39 subprocess.Popen(mox.StrContains(str_should_contain), shell=True,
40 stdout=subprocess.PIPE).AndReturn(mock_process)
41 mock_process.communicate().AndReturn(('Does not matter', None))
42
43 def testDownloadFromGS(self):
44 """Tests that we can run download build from gs with one error."""
45 self.mox.StubOutWithMock(subprocess, 'Popen', use_mock_anything=True)
46
47 # Make sure we our retry works.
48 self._CallRunGS('from to', attempts=2)
49 self.mox.ReplayAll()
50 gsutil_util.DownloadFromGS('from', 'to')
51 self.mox.VerifyAll()
52
53 def testDownloadFromGSButGSDown(self):
54 """Tests that we fail correctly if we can't reach GS."""
55 self.mox.StubOutWithMock(subprocess, 'Popen', use_mock_anything=True)
56 self._CallRunGS('from to', attempts=gsutil_util.GSUTIL_ATTEMPTS + 1)
57
58 self.mox.ReplayAll()
59 self.assertRaises(
60 gsutil_util.GSUtilError,
61 gsutil_util.DownloadFromGS,
62 'from', 'to')
63 self.mox.VerifyAll()
64
65
66if __name__ == '__main__':
67 unittest.main()