|
|
|
#!/usr/bin/env vpython3
|
|
|
|
# Copyright 2014 The Chromium Authors. All rights reserved.
|
|
|
|
# Use of this source code is governed by a BSD-style license that can be
|
|
|
|
# found in the LICENSE file.
|
|
|
|
|
|
|
|
"""Test gsutil.py."""
|
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
|
|
|
|
import base64
|
|
|
|
import hashlib
|
|
|
|
import io
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import tempfile
|
|
|
|
import unittest
|
|
|
|
import zipfile
|
|
|
|
|
|
|
|
try:
|
|
|
|
import urllib2 as urllib
|
|
|
|
except ImportError: # For Py3 compatibility
|
|
|
|
import urllib.request as urllib
|
|
|
|
|
|
|
|
# Add depot_tools to path
|
|
|
|
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
DEPOT_TOOLS_DIR = os.path.dirname(THIS_DIR)
|
|
|
|
sys.path.append(DEPOT_TOOLS_DIR)
|
|
|
|
|
|
|
|
import gsutil
|
|
|
|
|
|
|
|
|
|
|
|
class TestError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class FakeCall(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.expectations = []
|
|
|
|
|
|
|
|
def add_expectation(self, *args, **kwargs):
|
|
|
|
returns = kwargs.pop('_returns', None)
|
|
|
|
self.expectations.append((args, kwargs, returns))
|
|
|
|
|
|
|
|
def __call__(self, *args, **kwargs):
|
|
|
|
if not self.expectations:
|
|
|
|
raise TestError('Got unexpected\n%s\n%s' % (args, kwargs))
|
|
|
|
exp_args, exp_kwargs, exp_returns = self.expectations.pop(0)
|
|
|
|
if args != exp_args or kwargs != exp_kwargs:
|
|
|
|
message = 'Expected:\n args: %s\n kwargs: %s\n' % (exp_args, exp_kwargs)
|
|
|
|
message += 'Got:\n args: %s\n kwargs: %s\n' % (args, kwargs)
|
|
|
|
raise TestError(message)
|
|
|
|
return exp_returns
|
|
|
|
|
|
|
|
|
|
|
|
class GsutilUnitTests(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.fake = FakeCall()
|
|
|
|
self.tempdir = tempfile.mkdtemp()
|
|
|
|
self.old_urlopen = getattr(urllib, 'urlopen')
|
|
|
|
self.old_call = getattr(subprocess, 'call')
|
|
|
|
setattr(urllib, 'urlopen', self.fake)
|
|
|
|
setattr(subprocess, 'call', self.fake)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.assertEqual(self.fake.expectations, [])
|
|
|
|
shutil.rmtree(self.tempdir)
|
|
|
|
setattr(urllib, 'urlopen', self.old_urlopen)
|
|
|
|
setattr(subprocess, 'call', self.old_call)
|
|
|
|
|
|
|
|
def test_download_gsutil(self):
|
Revert "Update gsutil to use gsutil version 4.66, python3"
This reverts commit e8ef6259b967f7d76d96fe3039dc7c9614a52d6d.
Reason for revert: https://crbug.com/1240673
High rate of bot_update failures, lots of gsutil defunct
processes.
Original change's description:
> Update gsutil to use gsutil version 4.66, python3
>
> This is a reland of 457736028de6b719c0f1907a54321385cf9a9eb9
> with following changes:
> * bump version from 4.61 to 4.66, which contains several bugfixes
>
> Original change's description:
> > Reland "Reland "Update gsutil to use gsutil version 4.61, python3.""
> >
> > This is a reland of e53a593956446795a68b5314459e86de38caa761
> >
> > Additional bug fixes:
> > * handle race condition in gsutil when creating its directory
> > * limit to one gsutil process on darwin due to bug in python3
> >
> > Original change's description:
> > > Reland "Update gsutil to use gsutil version 4.61, python3."
> > >
> > > This reverts commit af121aeec9a2e4436c7609a39fcb61131a815b83.
> > >
> > > Reason for revert: re-landing with a switch back to vpython to get the compiled C extension version of crcmod for performance.
> > >
> > > Original change's description:
> > > > Revert "Update gsutil to use gsutil version 4.61, python3."
> > > >
> > > > This reverts commit f059ec936899adfd83f63fff46fdb61ae3a39537.
> > > >
> > > > Reason for revert: Reverting because we probably need to be using vpython and a compiled crcmod instead. See, e.g.,. b/188591640.
> > > >
> > > > Original change's description:
> > > > > Update gsutil to use gsutil version 4.61, python3.
> > > > >
> > > > > This CL updates the gsutil.py wrapper to download and use
> > > > > v4.61 of GCP's gsutil, which is Python3-compatible.
> > > > >
> > > > > v4.61 appears to be fully self-contained and have all of the
> > > > > packages it needs vendored into it. So, there's no reason to
> > > > > use vpython anymore, and this CL removes that.
> > > > >
> > > > > Also, this CL removes the 'fallback' option to gsutil and
> > > > > the ability to force a version switch, as this should no
> > > > > longer be necessary (it was added for a migration back in 2014
> > > > > but apparently this code was never removed afterwards).
> > > > >
> > > > > This CL also updates download_from_google_storage.py and
> > > > > upload_to_google_storage.py to similarly not have the version flags
> > > > > and to just use regular python3, not vpython3.
> > > > >
> > > > > Bug: 1184108
> > > > > Change-Id: I0d1a8351dba2d3ad1f927afa333fb10959f19443
> > > > > Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2898439
> > > > > Reviewed-by: Mike Frysinger <vapier@chromium.org>
> > > > > Reviewed-by: Josip Sokcevic <sokcevic@google.com>
> > > > > Reviewed-by: Robbie Iannucci <iannucci@chromium.org>
> > > > > Commit-Queue: Dirk Pranke <dpranke@google.com>
> > > >
> > > > Bug: 1184108
> > > > Change-Id: I8e21a9a40d81e4e185642f866855b6838f80f1c2
> > > > Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2905904
> > > > Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
> > > > Commit-Queue: Dirk Pranke <dpranke@google.com>
> > >
> > > Bug: 1184108
> > > Change-Id: I5d6d6d06842e08517488471c144972818fcf02ff
> > > Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2907155
> > > Reviewed-by: Mike Frysinger <vapier@chromium.org>
> > > Reviewed-by: Josip Sokcevic <sokcevic@google.com>
> > > Commit-Queue: Dirk Pranke <dpranke@google.com>
> >
> > Bug: 1184108
> > Change-Id: Ibb5d886fd22e3553521ff8ad6e2b4435844ef972
> > Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2988716
> > Reviewed-by: Dirk Pranke <dpranke@google.com>
> > Reviewed-by: Mike Frysinger <vapier@chromium.org>
> > Reviewed-by: Robbie Iannucci <iannucci@chromium.org>
> > Commit-Queue: Josip Sokcevic <sokcevic@google.com>
>
> Bug: 1184108
> Change-Id: I33787dc75f6e45d6b462706e934d7a2a37703fa7
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/3088085
> Reviewed-by: Dirk Pranke <dpranke@google.com>
> Commit-Queue: Josip Sokcevic <sokcevic@google.com>
Bug: 1184108, 1240673
Change-Id: I74c3243ea29b99476e09b6ddb49cb052812a1e3e
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/3100348
Auto-Submit: Josip Sokcevic <sokcevic@google.com>
Commit-Queue: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
4 years ago
|
|
|
version = '4.2'
|
|
|
|
filename = 'gsutil_%s.zip' % version
|
|
|
|
full_filename = os.path.join(self.tempdir, filename)
|
|
|
|
fake_file = b'This is gsutil.zip'
|
|
|
|
fake_file2 = b'This is other gsutil.zip'
|
|
|
|
url = '%s%s' % (gsutil.GSUTIL_URL, filename)
|
|
|
|
self.fake.add_expectation(url, _returns=io.BytesIO(fake_file))
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
gsutil.download_gsutil(version, self.tempdir), full_filename)
|
|
|
|
with open(full_filename, 'rb') as f:
|
|
|
|
self.assertEqual(fake_file, f.read())
|
|
|
|
|
|
|
|
metadata_url = gsutil.API_URL + filename
|
|
|
|
md5_calc = hashlib.md5()
|
|
|
|
md5_calc.update(fake_file)
|
|
|
|
b64_md5 = base64.b64encode(md5_calc.hexdigest().encode('utf-8'))
|
|
|
|
self.fake.add_expectation(
|
|
|
|
metadata_url,
|
|
|
|
_returns=io.BytesIO(
|
|
|
|
json.dumps({'md5Hash': b64_md5.decode('utf-8')}).encode('utf-8')))
|
|
|
|
self.assertEqual(
|
|
|
|
gsutil.download_gsutil(version, self.tempdir), full_filename)
|
|
|
|
with open(full_filename, 'rb') as f:
|
|
|
|
self.assertEqual(fake_file, f.read())
|
|
|
|
self.assertEqual(self.fake.expectations, [])
|
|
|
|
|
|
|
|
self.fake.add_expectation(
|
|
|
|
metadata_url,
|
|
|
|
_returns=io.BytesIO(
|
|
|
|
json.dumps({
|
|
|
|
'md5Hash': base64.b64encode(b'aaaaaaa').decode('utf-8') # Bad MD5
|
|
|
|
}).encode('utf-8')))
|
|
|
|
self.fake.add_expectation(url, _returns=io.BytesIO(fake_file2))
|
|
|
|
self.assertEqual(
|
|
|
|
gsutil.download_gsutil(version, self.tempdir), full_filename)
|
|
|
|
with open(full_filename, 'rb') as f:
|
|
|
|
self.assertEqual(fake_file2, f.read())
|
|
|
|
self.assertEqual(self.fake.expectations, [])
|
|
|
|
|
|
|
|
def test_ensure_gsutil_full(self):
|
Revert "Update gsutil to use gsutil version 4.66, python3"
This reverts commit e8ef6259b967f7d76d96fe3039dc7c9614a52d6d.
Reason for revert: https://crbug.com/1240673
High rate of bot_update failures, lots of gsutil defunct
processes.
Original change's description:
> Update gsutil to use gsutil version 4.66, python3
>
> This is a reland of 457736028de6b719c0f1907a54321385cf9a9eb9
> with following changes:
> * bump version from 4.61 to 4.66, which contains several bugfixes
>
> Original change's description:
> > Reland "Reland "Update gsutil to use gsutil version 4.61, python3.""
> >
> > This is a reland of e53a593956446795a68b5314459e86de38caa761
> >
> > Additional bug fixes:
> > * handle race condition in gsutil when creating its directory
> > * limit to one gsutil process on darwin due to bug in python3
> >
> > Original change's description:
> > > Reland "Update gsutil to use gsutil version 4.61, python3."
> > >
> > > This reverts commit af121aeec9a2e4436c7609a39fcb61131a815b83.
> > >
> > > Reason for revert: re-landing with a switch back to vpython to get the compiled C extension version of crcmod for performance.
> > >
> > > Original change's description:
> > > > Revert "Update gsutil to use gsutil version 4.61, python3."
> > > >
> > > > This reverts commit f059ec936899adfd83f63fff46fdb61ae3a39537.
> > > >
> > > > Reason for revert: Reverting because we probably need to be using vpython and a compiled crcmod instead. See, e.g.,. b/188591640.
> > > >
> > > > Original change's description:
> > > > > Update gsutil to use gsutil version 4.61, python3.
> > > > >
> > > > > This CL updates the gsutil.py wrapper to download and use
> > > > > v4.61 of GCP's gsutil, which is Python3-compatible.
> > > > >
> > > > > v4.61 appears to be fully self-contained and have all of the
> > > > > packages it needs vendored into it. So, there's no reason to
> > > > > use vpython anymore, and this CL removes that.
> > > > >
> > > > > Also, this CL removes the 'fallback' option to gsutil and
> > > > > the ability to force a version switch, as this should no
> > > > > longer be necessary (it was added for a migration back in 2014
> > > > > but apparently this code was never removed afterwards).
> > > > >
> > > > > This CL also updates download_from_google_storage.py and
> > > > > upload_to_google_storage.py to similarly not have the version flags
> > > > > and to just use regular python3, not vpython3.
> > > > >
> > > > > Bug: 1184108
> > > > > Change-Id: I0d1a8351dba2d3ad1f927afa333fb10959f19443
> > > > > Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2898439
> > > > > Reviewed-by: Mike Frysinger <vapier@chromium.org>
> > > > > Reviewed-by: Josip Sokcevic <sokcevic@google.com>
> > > > > Reviewed-by: Robbie Iannucci <iannucci@chromium.org>
> > > > > Commit-Queue: Dirk Pranke <dpranke@google.com>
> > > >
> > > > Bug: 1184108
> > > > Change-Id: I8e21a9a40d81e4e185642f866855b6838f80f1c2
> > > > Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2905904
> > > > Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
> > > > Commit-Queue: Dirk Pranke <dpranke@google.com>
> > >
> > > Bug: 1184108
> > > Change-Id: I5d6d6d06842e08517488471c144972818fcf02ff
> > > Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2907155
> > > Reviewed-by: Mike Frysinger <vapier@chromium.org>
> > > Reviewed-by: Josip Sokcevic <sokcevic@google.com>
> > > Commit-Queue: Dirk Pranke <dpranke@google.com>
> >
> > Bug: 1184108
> > Change-Id: Ibb5d886fd22e3553521ff8ad6e2b4435844ef972
> > Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2988716
> > Reviewed-by: Dirk Pranke <dpranke@google.com>
> > Reviewed-by: Mike Frysinger <vapier@chromium.org>
> > Reviewed-by: Robbie Iannucci <iannucci@chromium.org>
> > Commit-Queue: Josip Sokcevic <sokcevic@google.com>
>
> Bug: 1184108
> Change-Id: I33787dc75f6e45d6b462706e934d7a2a37703fa7
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/3088085
> Reviewed-by: Dirk Pranke <dpranke@google.com>
> Commit-Queue: Josip Sokcevic <sokcevic@google.com>
Bug: 1184108, 1240673
Change-Id: I74c3243ea29b99476e09b6ddb49cb052812a1e3e
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/3100348
Auto-Submit: Josip Sokcevic <sokcevic@google.com>
Commit-Queue: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
4 years ago
|
|
|
version = '4.2'
|
|
|
|
gsutil_dir = os.path.join(self.tempdir, 'gsutil_%s' % version, 'gsutil')
|
|
|
|
gsutil_bin = os.path.join(gsutil_dir, 'gsutil')
|
|
|
|
gsutil_flag = os.path.join(gsutil_dir, 'install.flag')
|
|
|
|
os.makedirs(gsutil_dir)
|
|
|
|
|
|
|
|
zip_filename = 'gsutil_%s.zip' % version
|
|
|
|
url = '%s%s' % (gsutil.GSUTIL_URL, zip_filename)
|
|
|
|
_, tempzip = tempfile.mkstemp()
|
|
|
|
fake_gsutil = 'Fake gsutil'
|
|
|
|
with zipfile.ZipFile(tempzip, 'w') as zf:
|
|
|
|
zf.writestr('gsutil/gsutil', fake_gsutil)
|
|
|
|
with open(tempzip, 'rb') as f:
|
|
|
|
self.fake.add_expectation(url, _returns=io.BytesIO(f.read()))
|
|
|
|
|
|
|
|
# This should write the gsutil_bin with 'Fake gsutil'
|
|
|
|
gsutil.ensure_gsutil(version, self.tempdir, False)
|
|
|
|
self.assertTrue(os.path.exists(gsutil_bin))
|
|
|
|
with open(gsutil_bin, 'r') as f:
|
|
|
|
self.assertEqual(f.read(), fake_gsutil)
|
|
|
|
self.assertTrue(os.path.exists(gsutil_flag))
|
|
|
|
self.assertEqual(self.fake.expectations, [])
|
|
|
|
|
|
|
|
def test_ensure_gsutil_short(self):
|
Revert "Update gsutil to use gsutil version 4.66, python3"
This reverts commit e8ef6259b967f7d76d96fe3039dc7c9614a52d6d.
Reason for revert: https://crbug.com/1240673
High rate of bot_update failures, lots of gsutil defunct
processes.
Original change's description:
> Update gsutil to use gsutil version 4.66, python3
>
> This is a reland of 457736028de6b719c0f1907a54321385cf9a9eb9
> with following changes:
> * bump version from 4.61 to 4.66, which contains several bugfixes
>
> Original change's description:
> > Reland "Reland "Update gsutil to use gsutil version 4.61, python3.""
> >
> > This is a reland of e53a593956446795a68b5314459e86de38caa761
> >
> > Additional bug fixes:
> > * handle race condition in gsutil when creating its directory
> > * limit to one gsutil process on darwin due to bug in python3
> >
> > Original change's description:
> > > Reland "Update gsutil to use gsutil version 4.61, python3."
> > >
> > > This reverts commit af121aeec9a2e4436c7609a39fcb61131a815b83.
> > >
> > > Reason for revert: re-landing with a switch back to vpython to get the compiled C extension version of crcmod for performance.
> > >
> > > Original change's description:
> > > > Revert "Update gsutil to use gsutil version 4.61, python3."
> > > >
> > > > This reverts commit f059ec936899adfd83f63fff46fdb61ae3a39537.
> > > >
> > > > Reason for revert: Reverting because we probably need to be using vpython and a compiled crcmod instead. See, e.g.,. b/188591640.
> > > >
> > > > Original change's description:
> > > > > Update gsutil to use gsutil version 4.61, python3.
> > > > >
> > > > > This CL updates the gsutil.py wrapper to download and use
> > > > > v4.61 of GCP's gsutil, which is Python3-compatible.
> > > > >
> > > > > v4.61 appears to be fully self-contained and have all of the
> > > > > packages it needs vendored into it. So, there's no reason to
> > > > > use vpython anymore, and this CL removes that.
> > > > >
> > > > > Also, this CL removes the 'fallback' option to gsutil and
> > > > > the ability to force a version switch, as this should no
> > > > > longer be necessary (it was added for a migration back in 2014
> > > > > but apparently this code was never removed afterwards).
> > > > >
> > > > > This CL also updates download_from_google_storage.py and
> > > > > upload_to_google_storage.py to similarly not have the version flags
> > > > > and to just use regular python3, not vpython3.
> > > > >
> > > > > Bug: 1184108
> > > > > Change-Id: I0d1a8351dba2d3ad1f927afa333fb10959f19443
> > > > > Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2898439
> > > > > Reviewed-by: Mike Frysinger <vapier@chromium.org>
> > > > > Reviewed-by: Josip Sokcevic <sokcevic@google.com>
> > > > > Reviewed-by: Robbie Iannucci <iannucci@chromium.org>
> > > > > Commit-Queue: Dirk Pranke <dpranke@google.com>
> > > >
> > > > Bug: 1184108
> > > > Change-Id: I8e21a9a40d81e4e185642f866855b6838f80f1c2
> > > > Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2905904
> > > > Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
> > > > Commit-Queue: Dirk Pranke <dpranke@google.com>
> > >
> > > Bug: 1184108
> > > Change-Id: I5d6d6d06842e08517488471c144972818fcf02ff
> > > Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2907155
> > > Reviewed-by: Mike Frysinger <vapier@chromium.org>
> > > Reviewed-by: Josip Sokcevic <sokcevic@google.com>
> > > Commit-Queue: Dirk Pranke <dpranke@google.com>
> >
> > Bug: 1184108
> > Change-Id: Ibb5d886fd22e3553521ff8ad6e2b4435844ef972
> > Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2988716
> > Reviewed-by: Dirk Pranke <dpranke@google.com>
> > Reviewed-by: Mike Frysinger <vapier@chromium.org>
> > Reviewed-by: Robbie Iannucci <iannucci@chromium.org>
> > Commit-Queue: Josip Sokcevic <sokcevic@google.com>
>
> Bug: 1184108
> Change-Id: I33787dc75f6e45d6b462706e934d7a2a37703fa7
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/3088085
> Reviewed-by: Dirk Pranke <dpranke@google.com>
> Commit-Queue: Josip Sokcevic <sokcevic@google.com>
Bug: 1184108, 1240673
Change-Id: I74c3243ea29b99476e09b6ddb49cb052812a1e3e
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/3100348
Auto-Submit: Josip Sokcevic <sokcevic@google.com>
Commit-Queue: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
4 years ago
|
|
|
version = '4.2'
|
|
|
|
gsutil_dir = os.path.join(self.tempdir, 'gsutil_%s' % version, 'gsutil')
|
|
|
|
gsutil_bin = os.path.join(gsutil_dir, 'gsutil')
|
|
|
|
gsutil_flag = os.path.join(gsutil_dir, 'install.flag')
|
|
|
|
os.makedirs(gsutil_dir)
|
|
|
|
|
|
|
|
with open(gsutil_bin, 'w') as f:
|
|
|
|
f.write('Foobar')
|
|
|
|
with open(gsutil_flag, 'w') as f:
|
|
|
|
f.write('Barbaz')
|
|
|
|
self.assertEqual(
|
|
|
|
gsutil.ensure_gsutil(version, self.tempdir, False), gsutil_bin)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|