Edward Lemur | 83aafc9 | 2019-11-25 23:25:05 +0000 | [diff] [blame^] | 1 | #!/usr/bin/env vpython3 |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 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 |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Test gsutil.py.""" |
| 7 | |
Edward Lemur | 83aafc9 | 2019-11-25 23:25:05 +0000 | [diff] [blame^] | 8 | from __future__ import unicode_literals |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 9 | |
Edward Lemur | 83aafc9 | 2019-11-25 23:25:05 +0000 | [diff] [blame^] | 10 | |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 11 | import base64 |
hinoka@chromium.org | 5e879a4 | 2015-01-24 00:55:46 +0000 | [diff] [blame] | 12 | import hashlib |
Edward Lemur | 83aafc9 | 2019-11-25 23:25:05 +0000 | [diff] [blame^] | 13 | import io |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 14 | import json |
| 15 | import os |
hinoka@chromium.org | 5e879a4 | 2015-01-24 00:55:46 +0000 | [diff] [blame] | 16 | import shutil |
| 17 | import subprocess |
| 18 | import sys |
| 19 | import tempfile |
| 20 | import unittest |
hinoka@chromium.org | 5e879a4 | 2015-01-24 00:55:46 +0000 | [diff] [blame] | 21 | import zipfile |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 22 | |
Edward Lemur | 83aafc9 | 2019-11-25 23:25:05 +0000 | [diff] [blame^] | 23 | try: |
| 24 | import urllib2 as urllib |
| 25 | except ImportError: # For Py3 compatibility |
| 26 | import urllib.request as urllib |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 27 | |
| 28 | # Add depot_tools to path |
| 29 | THIS_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 30 | DEPOT_TOOLS_DIR = os.path.dirname(THIS_DIR) |
| 31 | sys.path.append(DEPOT_TOOLS_DIR) |
| 32 | |
| 33 | import gsutil |
| 34 | |
| 35 | |
| 36 | class TestError(Exception): |
| 37 | pass |
| 38 | |
| 39 | |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 40 | class FakeCall(object): |
| 41 | def __init__(self): |
| 42 | self.expectations = [] |
| 43 | |
| 44 | def add_expectation(self, *args, **kwargs): |
| 45 | returns = kwargs.pop('_returns', None) |
| 46 | self.expectations.append((args, kwargs, returns)) |
| 47 | |
| 48 | def __call__(self, *args, **kwargs): |
| 49 | if not self.expectations: |
| 50 | raise TestError('Got unexpected\n%s\n%s' % (args, kwargs)) |
| 51 | exp_args, exp_kwargs, exp_returns = self.expectations.pop(0) |
| 52 | if args != exp_args or kwargs != exp_kwargs: |
| 53 | message = 'Expected:\n args: %s\n kwargs: %s\n' % (exp_args, exp_kwargs) |
| 54 | message += 'Got:\n args: %s\n kwargs: %s\n' % (args, kwargs) |
| 55 | raise TestError(message) |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 56 | return exp_returns |
| 57 | |
| 58 | |
| 59 | class GsutilUnitTests(unittest.TestCase): |
| 60 | def setUp(self): |
| 61 | self.fake = FakeCall() |
| 62 | self.tempdir = tempfile.mkdtemp() |
Edward Lemur | 83aafc9 | 2019-11-25 23:25:05 +0000 | [diff] [blame^] | 63 | self.old_urlopen = getattr(urllib, 'urlopen') |
hinoka@chromium.org | 5e879a4 | 2015-01-24 00:55:46 +0000 | [diff] [blame] | 64 | self.old_call = getattr(subprocess, 'call') |
Edward Lemur | 83aafc9 | 2019-11-25 23:25:05 +0000 | [diff] [blame^] | 65 | setattr(urllib, 'urlopen', self.fake) |
hinoka@chromium.org | 5e879a4 | 2015-01-24 00:55:46 +0000 | [diff] [blame] | 66 | setattr(subprocess, 'call', self.fake) |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 67 | |
| 68 | def tearDown(self): |
| 69 | self.assertEqual(self.fake.expectations, []) |
| 70 | shutil.rmtree(self.tempdir) |
Edward Lemur | 83aafc9 | 2019-11-25 23:25:05 +0000 | [diff] [blame^] | 71 | setattr(urllib, 'urlopen', self.old_urlopen) |
hinoka@chromium.org | 5e879a4 | 2015-01-24 00:55:46 +0000 | [diff] [blame] | 72 | setattr(subprocess, 'call', self.old_call) |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 73 | |
| 74 | def test_download_gsutil(self): |
| 75 | version = '4.2' |
| 76 | filename = 'gsutil_%s.zip' % version |
| 77 | full_filename = os.path.join(self.tempdir, filename) |
Edward Lemur | 83aafc9 | 2019-11-25 23:25:05 +0000 | [diff] [blame^] | 78 | fake_file = b'This is gsutil.zip' |
| 79 | fake_file2 = b'This is other gsutil.zip' |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 80 | url = '%s%s' % (gsutil.GSUTIL_URL, filename) |
Edward Lemur | 83aafc9 | 2019-11-25 23:25:05 +0000 | [diff] [blame^] | 81 | self.fake.add_expectation(url, _returns=io.BytesIO(fake_file)) |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 82 | |
Raphael Kubo da Costa | e9342a7 | 2019-10-14 17:49:39 +0000 | [diff] [blame] | 83 | self.assertEqual( |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 84 | gsutil.download_gsutil(version, self.tempdir), full_filename) |
Edward Lemur | 83aafc9 | 2019-11-25 23:25:05 +0000 | [diff] [blame^] | 85 | with open(full_filename, 'rb') as f: |
Raphael Kubo da Costa | e9342a7 | 2019-10-14 17:49:39 +0000 | [diff] [blame] | 86 | self.assertEqual(fake_file, f.read()) |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 87 | |
| 88 | metadata_url = gsutil.API_URL + filename |
| 89 | md5_calc = hashlib.md5() |
| 90 | md5_calc.update(fake_file) |
Edward Lemur | 83aafc9 | 2019-11-25 23:25:05 +0000 | [diff] [blame^] | 91 | b64_md5 = base64.b64encode(md5_calc.hexdigest().encode('utf-8')) |
| 92 | self.fake.add_expectation( |
| 93 | metadata_url, |
| 94 | _returns=io.BytesIO( |
| 95 | json.dumps({'md5Hash': b64_md5.decode('utf-8')}).encode('utf-8'))) |
Raphael Kubo da Costa | e9342a7 | 2019-10-14 17:49:39 +0000 | [diff] [blame] | 96 | self.assertEqual( |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 97 | gsutil.download_gsutil(version, self.tempdir), full_filename) |
Edward Lemur | 83aafc9 | 2019-11-25 23:25:05 +0000 | [diff] [blame^] | 98 | with open(full_filename, 'rb') as f: |
Raphael Kubo da Costa | e9342a7 | 2019-10-14 17:49:39 +0000 | [diff] [blame] | 99 | self.assertEqual(fake_file, f.read()) |
| 100 | self.assertEqual(self.fake.expectations, []) |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 101 | |
Edward Lemur | 83aafc9 | 2019-11-25 23:25:05 +0000 | [diff] [blame^] | 102 | self.fake.add_expectation( |
| 103 | metadata_url, |
| 104 | _returns=io.BytesIO( |
| 105 | json.dumps({ |
| 106 | 'md5Hash': base64.b64encode(b'aaaaaaa').decode('utf-8') # Bad MD5 |
| 107 | }).encode('utf-8'))) |
| 108 | self.fake.add_expectation(url, _returns=io.BytesIO(fake_file2)) |
Raphael Kubo da Costa | e9342a7 | 2019-10-14 17:49:39 +0000 | [diff] [blame] | 109 | self.assertEqual( |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 110 | gsutil.download_gsutil(version, self.tempdir), full_filename) |
Edward Lemur | 83aafc9 | 2019-11-25 23:25:05 +0000 | [diff] [blame^] | 111 | with open(full_filename, 'rb') as f: |
Raphael Kubo da Costa | e9342a7 | 2019-10-14 17:49:39 +0000 | [diff] [blame] | 112 | self.assertEqual(fake_file2, f.read()) |
| 113 | self.assertEqual(self.fake.expectations, []) |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 114 | |
| 115 | def test_ensure_gsutil_full(self): |
| 116 | version = '4.2' |
| 117 | gsutil_dir = os.path.join(self.tempdir, 'gsutil_%s' % version, 'gsutil') |
| 118 | gsutil_bin = os.path.join(gsutil_dir, 'gsutil') |
Ryan Tseng | 83fd81f | 2017-10-23 11:13:48 -0700 | [diff] [blame] | 119 | gsutil_flag = os.path.join(gsutil_dir, 'install.flag') |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 120 | os.makedirs(gsutil_dir) |
| 121 | |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 122 | zip_filename = 'gsutil_%s.zip' % version |
| 123 | url = '%s%s' % (gsutil.GSUTIL_URL, zip_filename) |
| 124 | _, tempzip = tempfile.mkstemp() |
| 125 | fake_gsutil = 'Fake gsutil' |
| 126 | with zipfile.ZipFile(tempzip, 'w') as zf: |
| 127 | zf.writestr('gsutil/gsutil', fake_gsutil) |
| 128 | with open(tempzip, 'rb') as f: |
Edward Lemur | 83aafc9 | 2019-11-25 23:25:05 +0000 | [diff] [blame^] | 129 | self.fake.add_expectation(url, _returns=io.BytesIO(f.read())) |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 130 | |
Ryan Tseng | 83fd81f | 2017-10-23 11:13:48 -0700 | [diff] [blame] | 131 | # This should write the gsutil_bin with 'Fake gsutil' |
| 132 | gsutil.ensure_gsutil(version, self.tempdir, False) |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 133 | self.assertTrue(os.path.exists(gsutil_bin)) |
| 134 | with open(gsutil_bin, 'r') as f: |
Raphael Kubo da Costa | e9342a7 | 2019-10-14 17:49:39 +0000 | [diff] [blame] | 135 | self.assertEqual(f.read(), fake_gsutil) |
Ryan Tseng | 83fd81f | 2017-10-23 11:13:48 -0700 | [diff] [blame] | 136 | self.assertTrue(os.path.exists(gsutil_flag)) |
Raphael Kubo da Costa | e9342a7 | 2019-10-14 17:49:39 +0000 | [diff] [blame] | 137 | self.assertEqual(self.fake.expectations, []) |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 138 | |
| 139 | def test_ensure_gsutil_short(self): |
| 140 | version = '4.2' |
| 141 | gsutil_dir = os.path.join(self.tempdir, 'gsutil_%s' % version, 'gsutil') |
| 142 | gsutil_bin = os.path.join(gsutil_dir, 'gsutil') |
Ryan Tseng | 83fd81f | 2017-10-23 11:13:48 -0700 | [diff] [blame] | 143 | gsutil_flag = os.path.join(gsutil_dir, 'install.flag') |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 144 | os.makedirs(gsutil_dir) |
| 145 | |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 146 | with open(gsutil_bin, 'w') as f: |
| 147 | f.write('Foobar') |
Ryan Tseng | 83fd81f | 2017-10-23 11:13:48 -0700 | [diff] [blame] | 148 | with open(gsutil_flag, 'w') as f: |
| 149 | f.write('Barbaz') |
Raphael Kubo da Costa | e9342a7 | 2019-10-14 17:49:39 +0000 | [diff] [blame] | 150 | self.assertEqual( |
dnj@chromium.org | 605d81d | 2015-09-18 22:33:53 +0000 | [diff] [blame] | 151 | gsutil.ensure_gsutil(version, self.tempdir, False), gsutil_bin) |
hinoka@chromium.org | 7a79054 | 2014-12-10 02:04:39 +0000 | [diff] [blame] | 152 | |
| 153 | if __name__ == '__main__': |
| 154 | unittest.main() |