Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(41)

Side by Side Diff: tests/gsutil_test.py

Issue 870093003: Hook sys.stdio directly to the gsutil subprocess for the gsutil call (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/depot_tools
Patch Set: Fix tests Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « gsutil.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 base64
11 import hashlib 11 import hashlib
12 import zipfile
13 import shutil
14 import sys
15 import base64
16 import tempfile
17 import json 12 import json
18 import os 13 import os
14 import shutil
15 import subprocess
16 import sys
17 import tempfile
18 import unittest
19 import urllib2 19 import urllib2
20 import zipfile
20 21
21 22
22 # Add depot_tools to path 23 # Add depot_tools to path
23 THIS_DIR = os.path.dirname(os.path.abspath(__file__)) 24 THIS_DIR = os.path.dirname(os.path.abspath(__file__))
24 DEPOT_TOOLS_DIR = os.path.dirname(THIS_DIR) 25 DEPOT_TOOLS_DIR = os.path.dirname(THIS_DIR)
25 sys.path.append(DEPOT_TOOLS_DIR) 26 sys.path.append(DEPOT_TOOLS_DIR)
26 27
27 import gsutil 28 import gsutil
28 29
29 30
(...skipping 25 matching lines...) Expand all
55 self.expectations.append((args, kwargs, returns)) 56 self.expectations.append((args, kwargs, returns))
56 57
57 def __call__(self, *args, **kwargs): 58 def __call__(self, *args, **kwargs):
58 if not self.expectations: 59 if not self.expectations:
59 raise TestError('Got unexpected\n%s\n%s' % (args, kwargs)) 60 raise TestError('Got unexpected\n%s\n%s' % (args, kwargs))
60 exp_args, exp_kwargs, exp_returns = self.expectations.pop(0) 61 exp_args, exp_kwargs, exp_returns = self.expectations.pop(0)
61 if args != exp_args or kwargs != exp_kwargs: 62 if args != exp_args or kwargs != exp_kwargs:
62 message = 'Expected:\n args: %s\n kwargs: %s\n' % (exp_args, exp_kwargs) 63 message = 'Expected:\n args: %s\n kwargs: %s\n' % (exp_args, exp_kwargs)
63 message += 'Got:\n args: %s\n kwargs: %s\n' % (args, kwargs) 64 message += 'Got:\n args: %s\n kwargs: %s\n' % (args, kwargs)
64 raise TestError(message) 65 raise TestError(message)
65 if isinstance(exp_returns, Exception):
66 raise exp_returns
67 return exp_returns 66 return exp_returns
68 67
69 68
70 class GsutilUnitTests(unittest.TestCase): 69 class GsutilUnitTests(unittest.TestCase):
71 def setUp(self): 70 def setUp(self):
72 self.fake = FakeCall() 71 self.fake = FakeCall()
73 self.tempdir = tempfile.mkdtemp() 72 self.tempdir = tempfile.mkdtemp()
74 self.old_urlopen = getattr(urllib2, 'urlopen') 73 self.old_urlopen = getattr(urllib2, 'urlopen')
75 self.old_call = getattr(gsutil, 'call') 74 self.old_call = getattr(subprocess, 'call')
76 setattr(urllib2, 'urlopen', self.fake) 75 setattr(urllib2, 'urlopen', self.fake)
77 setattr(gsutil, 'call', self.fake) 76 setattr(subprocess, 'call', self.fake)
78 77
79 def tearDown(self): 78 def tearDown(self):
80 self.assertEqual(self.fake.expectations, []) 79 self.assertEqual(self.fake.expectations, [])
81 shutil.rmtree(self.tempdir) 80 shutil.rmtree(self.tempdir)
82 setattr(urllib2, 'urlopen', self.old_urlopen) 81 setattr(urllib2, 'urlopen', self.old_urlopen)
83 setattr(gsutil, 'call', self.old_call) 82 setattr(subprocess, 'call', self.old_call)
84 83
85 def test_download_gsutil(self): 84 def test_download_gsutil(self):
86 version = '4.2' 85 version = '4.2'
87 filename = 'gsutil_%s.zip' % version 86 filename = 'gsutil_%s.zip' % version
88 full_filename = os.path.join(self.tempdir, filename) 87 full_filename = os.path.join(self.tempdir, filename)
89 fake_file = 'This is gsutil.zip' 88 fake_file = 'This is gsutil.zip'
90 fake_file2 = 'This is other gsutil.zip' 89 fake_file2 = 'This is other gsutil.zip'
91 url = '%s%s' % (gsutil.GSUTIL_URL, filename) 90 url = '%s%s' % (gsutil.GSUTIL_URL, filename)
92 self.fake.add_expectation(url, _returns=Buffer(fake_file)) 91 self.fake.add_expectation(url, _returns=Buffer(fake_file))
93 92
(...skipping 25 matching lines...) Expand all
119 self.assertEquals(fake_file2, f.read()) 118 self.assertEquals(fake_file2, f.read())
120 self.assertEquals(self.fake.expectations, []) 119 self.assertEquals(self.fake.expectations, [])
121 120
122 def test_ensure_gsutil_full(self): 121 def test_ensure_gsutil_full(self):
123 version = '4.2' 122 version = '4.2'
124 gsutil_dir = os.path.join(self.tempdir, 'gsutil_%s' % version, 'gsutil') 123 gsutil_dir = os.path.join(self.tempdir, 'gsutil_%s' % version, 'gsutil')
125 gsutil_bin = os.path.join(gsutil_dir, 'gsutil') 124 gsutil_bin = os.path.join(gsutil_dir, 'gsutil')
126 os.makedirs(gsutil_dir) 125 os.makedirs(gsutil_dir)
127 126
128 self.fake.add_expectation( 127 self.fake.add_expectation(
129 [sys.executable, gsutil_bin, 'version'], verbose=False, 128 [sys.executable, gsutil_bin, 'version'], stdout=subprocess.PIPE,
130 _returns=gsutil.SubprocessError()) 129 stderr=subprocess.STDOUT, _returns=1)
131 130
132 with open(gsutil_bin, 'w') as f: 131 with open(gsutil_bin, 'w') as f:
133 f.write('Foobar') 132 f.write('Foobar')
134 zip_filename = 'gsutil_%s.zip' % version 133 zip_filename = 'gsutil_%s.zip' % version
135 url = '%s%s' % (gsutil.GSUTIL_URL, zip_filename) 134 url = '%s%s' % (gsutil.GSUTIL_URL, zip_filename)
136 _, tempzip = tempfile.mkstemp() 135 _, tempzip = tempfile.mkstemp()
137 fake_gsutil = 'Fake gsutil' 136 fake_gsutil = 'Fake gsutil'
138 with zipfile.ZipFile(tempzip, 'w') as zf: 137 with zipfile.ZipFile(tempzip, 'w') as zf:
139 zf.writestr('gsutil/gsutil', fake_gsutil) 138 zf.writestr('gsutil/gsutil', fake_gsutil)
140 with open(tempzip, 'rb') as f: 139 with open(tempzip, 'rb') as f:
141 self.fake.add_expectation(url, _returns=Buffer(f.read())) 140 self.fake.add_expectation(url, _returns=Buffer(f.read()))
142 self.fake.add_expectation( 141 self.fake.add_expectation(
143 [sys.executable, gsutil_bin, 'version'], verbose=False, 142 [sys.executable, gsutil_bin, 'version'], stdout=subprocess.PIPE,
144 _returns=gsutil.SubprocessError()) 143 stderr=subprocess.STDOUT, _returns=1)
145 144
146 # This should delete the old bin and rewrite it with 'Fake gsutil' 145 # This should delete the old bin and rewrite it with 'Fake gsutil'
147 self.assertRaises( 146 self.assertRaises(
148 gsutil.InvalidGsutilError, gsutil.ensure_gsutil, version, self.tempdir) 147 gsutil.InvalidGsutilError, gsutil.ensure_gsutil, version, self.tempdir)
149 self.assertTrue(os.path.isdir(os.path.join(self.tempdir, '.cache_dir'))) 148 self.assertTrue(os.path.isdir(os.path.join(self.tempdir, '.cache_dir')))
150 self.assertTrue(os.path.exists(gsutil_bin)) 149 self.assertTrue(os.path.exists(gsutil_bin))
151 with open(gsutil_bin, 'r') as f: 150 with open(gsutil_bin, 'r') as f:
152 self.assertEquals(f.read(), fake_gsutil) 151 self.assertEquals(f.read(), fake_gsutil)
153 self.assertEquals(self.fake.expectations, []) 152 self.assertEquals(self.fake.expectations, [])
154 153
155 def test_ensure_gsutil_short(self): 154 def test_ensure_gsutil_short(self):
156 version = '4.2' 155 version = '4.2'
157 gsutil_dir = os.path.join(self.tempdir, 'gsutil_%s' % version, 'gsutil') 156 gsutil_dir = os.path.join(self.tempdir, 'gsutil_%s' % version, 'gsutil')
158 gsutil_bin = os.path.join(gsutil_dir, 'gsutil') 157 gsutil_bin = os.path.join(gsutil_dir, 'gsutil')
159 os.makedirs(gsutil_dir) 158 os.makedirs(gsutil_dir)
160 159
161 # Mock out call(). 160 # Mock out call().
162 self.fake.add_expectation( 161 self.fake.add_expectation(
163 [sys.executable, gsutil_bin, 'version'], verbose=False, _returns=True) 162 [sys.executable, gsutil_bin, 'version'],
163 stdout=subprocess.PIPE, stderr=subprocess.STDOUT, _returns=0)
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()
OLDNEW
« no previous file with comments | « gsutil.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698