blob: 0822e5dc1a36307535a3ad01483d2404a979bf5c [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
18class GSUtilUtilTest(mox.MoxTestBase):
19
20 def setUp(self):
21 mox.MoxTestBase.setUp(self)
22
23 self._good_mock_process = self.mox.CreateMock(subprocess.Popen)
24 self._good_mock_process.returncode = 0
25 self._bad_mock_process = self.mox.CreateMock(subprocess.Popen)
26 self._bad_mock_process.returncode = 1
Chris Sosa101fd862012-06-12 17:44:53 -070027 self.mox.StubOutWithMock(time, 'sleep')
28 time.sleep(mox.IgnoreArg()).MultipleTimes()
Chris Sosa47a7d4e2012-03-28 11:26:55 -070029
30 def _CallRunGS(self, str_should_contain, attempts=1):
31 """Helper that wraps a RunGS for tests."""
32 for attempt in range(attempts):
33 if attempt == gsutil_util.GSUTIL_ATTEMPTS:
34 # We can't mock more than we can attempt.
35 return
36
37 # Return 1's for all but last attempt.
38 if attempt != attempts - 1:
39 mock_process = self._bad_mock_process
40 else:
41 mock_process = self._good_mock_process
42
43 subprocess.Popen(mox.StrContains(str_should_contain), shell=True,
44 stdout=subprocess.PIPE).AndReturn(mock_process)
45 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."""
49 self.mox.StubOutWithMock(subprocess, 'Popen', use_mock_anything=True)
50
51 # Make sure we our retry works.
52 self._CallRunGS('from to', attempts=2)
53 self.mox.ReplayAll()
54 gsutil_util.DownloadFromGS('from', 'to')
55 self.mox.VerifyAll()
56
57 def testDownloadFromGSButGSDown(self):
58 """Tests that we fail correctly if we can't reach GS."""
59 self.mox.StubOutWithMock(subprocess, 'Popen', use_mock_anything=True)
60 self._CallRunGS('from to', attempts=gsutil_util.GSUTIL_ATTEMPTS + 1)
61
62 self.mox.ReplayAll()
63 self.assertRaises(
64 gsutil_util.GSUtilError,
65 gsutil_util.DownloadFromGS,
66 'from', 'to')
67 self.mox.VerifyAll()
68
69
70if __name__ == '__main__':
71 unittest.main()