OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2014 The Chromium Authors. All rights reserved. | 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Test gsutil.py.""" | 6 """Test gsutil.py.""" |
7 | 7 |
8 | 8 |
9 import __builtin__ | 9 import __builtin__ |
10 import unittest | 10 import unittest |
11 import hashlib | 11 import hashlib |
12 import zipfile | 12 import zipfile |
13 import shutil | 13 import shutil |
14 import sys | 14 import sys |
15 import base64 | 15 import base64 |
16 import tempfile | 16 import tempfile |
17 import json | 17 import json |
18 import os | 18 import os |
19 import urllib | 19 import urllib2 |
20 | 20 |
21 | 21 |
22 # Add depot_tools to path | 22 # Add depot_tools to path |
23 THIS_DIR = os.path.dirname(os.path.abspath(__file__)) | 23 THIS_DIR = os.path.dirname(os.path.abspath(__file__)) |
24 DEPOT_TOOLS_DIR = os.path.dirname(THIS_DIR) | 24 DEPOT_TOOLS_DIR = os.path.dirname(THIS_DIR) |
25 sys.path.append(DEPOT_TOOLS_DIR) | 25 sys.path.append(DEPOT_TOOLS_DIR) |
26 | 26 |
27 import gsutil | 27 import gsutil |
28 | 28 |
29 | 29 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 raise TestError(message) | 64 raise TestError(message) |
65 if isinstance(exp_returns, Exception): | 65 if isinstance(exp_returns, Exception): |
66 raise exp_returns | 66 raise exp_returns |
67 return exp_returns | 67 return exp_returns |
68 | 68 |
69 | 69 |
70 class GsutilUnitTests(unittest.TestCase): | 70 class GsutilUnitTests(unittest.TestCase): |
71 def setUp(self): | 71 def setUp(self): |
72 self.fake = FakeCall() | 72 self.fake = FakeCall() |
73 self.tempdir = tempfile.mkdtemp() | 73 self.tempdir = tempfile.mkdtemp() |
74 self.old_urlopen = getattr(urllib, 'urlopen') | 74 self.old_urlopen = getattr(urllib2, 'urlopen') |
75 self.old_call = getattr(gsutil, 'call') | 75 self.old_call = getattr(gsutil, 'call') |
76 setattr(urllib, 'urlopen', self.fake) | 76 setattr(urllib2, 'urlopen', self.fake) |
77 setattr(gsutil, 'call', self.fake) | 77 setattr(gsutil, 'call', self.fake) |
78 | 78 |
79 def tearDown(self): | 79 def tearDown(self): |
80 self.assertEqual(self.fake.expectations, []) | 80 self.assertEqual(self.fake.expectations, []) |
81 shutil.rmtree(self.tempdir) | 81 shutil.rmtree(self.tempdir) |
82 setattr(urllib, 'urlopen', self.old_urlopen) | 82 setattr(urllib2, 'urlopen', self.old_urlopen) |
83 setattr(gsutil, 'call', self.old_call) | 83 setattr(gsutil, 'call', self.old_call) |
84 | 84 |
85 def test_download_gsutil(self): | 85 def test_download_gsutil(self): |
86 version = '4.2' | 86 version = '4.2' |
87 filename = 'gsutil_%s.zip' % version | 87 filename = 'gsutil_%s.zip' % version |
88 full_filename = os.path.join(self.tempdir, filename) | 88 full_filename = os.path.join(self.tempdir, filename) |
89 fake_file = 'This is gsutil.zip' | 89 fake_file = 'This is gsutil.zip' |
90 fake_file2 = 'This is other gsutil.zip' | 90 fake_file2 = 'This is other gsutil.zip' |
91 url = '%s%s' % (gsutil.GSUTIL_URL, filename) | 91 url = '%s%s' % (gsutil.GSUTIL_URL, filename) |
92 self.fake.add_expectation(url, _returns=Buffer(fake_file)) | 92 self.fake.add_expectation(url, _returns=Buffer(fake_file)) |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 self.fake.add_expectation( | 162 self.fake.add_expectation( |
163 [sys.executable, gsutil_bin, 'version'], verbose=False, _returns=True) | 163 [sys.executable, gsutil_bin, 'version'], verbose=False, _returns=True) |
164 | 164 |
165 with open(gsutil_bin, 'w') as f: | 165 with open(gsutil_bin, 'w') as f: |
166 f.write('Foobar') | 166 f.write('Foobar') |
167 self.assertEquals( | 167 self.assertEquals( |
168 gsutil.ensure_gsutil(version, self.tempdir), gsutil_bin) | 168 gsutil.ensure_gsutil(version, self.tempdir), gsutil_bin) |
169 | 169 |
170 if __name__ == '__main__': | 170 if __name__ == '__main__': |
171 unittest.main() | 171 unittest.main() |
OLD | NEW |