| OLD | NEW |
| 1 # -*- coding: utf-8 -*- |
| 1 # Copyright 2013 Google Inc. All Rights Reserved. | 2 # Copyright 2013 Google Inc. All Rights Reserved. |
| 2 # | 3 # |
| 3 # Permission is hereby granted, free of charge, to any person obtaining a | 4 # Permission is hereby granted, free of charge, to any person obtaining a |
| 4 # copy of this software and associated documentation files (the | 5 # copy of this software and associated documentation files (the |
| 5 # "Software"), to deal in the Software without restriction, including | 6 # "Software"), to deal in the Software without restriction, including |
| 6 # without limitation the rights to use, copy, modify, merge, publish, dis- | 7 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 7 # tribute, sublicense, and/or sell copies of the Software, and to permit | 8 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 # persons to whom the Software is furnished to do so, subject to the fol- | 9 # persons to whom the Software is furnished to do so, subject to the fol- |
| 9 # lowing conditions: | 10 # lowing conditions: |
| 10 # | 11 # |
| 11 # The above copyright notice and this permission notice shall be included | 12 # The above copyright notice and this permission notice shall be included |
| 12 # in all copies or substantial portions of the Software. | 13 # in all copies or substantial portions of the Software. |
| 13 # | 14 # |
| 14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- | 16 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 16 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT | 17 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 17 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | 18 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 18 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | 19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | 20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 20 # IN THE SOFTWARE. | 21 # IN THE SOFTWARE. |
| 22 """Tests for the update command.""" |
| 21 | 23 |
| 22 """Tests for the update command.""" | 24 from __future__ import absolute_import |
| 23 | 25 |
| 24 import os.path | 26 import os.path |
| 25 import shutil | 27 import shutil |
| 26 import subprocess | 28 import subprocess |
| 27 import sys | 29 import sys |
| 28 import tarfile | 30 import tarfile |
| 29 | 31 |
| 30 import gslib | 32 import gslib |
| 31 import gslib.tests.testcase as testcase | 33 import gslib.tests.testcase as testcase |
| 32 from gslib.tests.util import ObjectToURI as suri | 34 from gslib.tests.util import ObjectToURI as suri |
| 33 from gslib.tests.util import unittest | 35 from gslib.tests.util import unittest |
| 34 from gslib.util import BOTO_IS_SECURE | 36 from gslib.util import CERTIFICATE_VALIDATION_ENABLED |
| 35 | 37 |
| 36 | 38 |
| 37 TESTS_DIR = os.path.abspath(os.path.dirname(__file__)) | 39 TESTS_DIR = os.path.abspath(os.path.dirname(__file__)) |
| 38 GSUTIL_DIR = os.path.join(TESTS_DIR, '..', '..') | 40 GSUTIL_DIR = os.path.join(TESTS_DIR, '..', '..') |
| 39 | 41 |
| 40 | 42 |
| 41 class UpdateTest(testcase.GsUtilIntegrationTestCase): | 43 class UpdateTest(testcase.GsUtilIntegrationTestCase): |
| 42 """Update command test suite.""" | 44 """Update command test suite.""" |
| 43 | 45 |
| 44 @unittest.skipUnless(BOTO_IS_SECURE[0], | 46 @unittest.skipUnless(CERTIFICATE_VALIDATION_ENABLED, |
| 45 'Test requires boto secure connection.') | 47 'Test requires https certificate validation enabled.') |
| 46 def test_update(self): | 48 def test_update(self): |
| 47 """Tests that the update command works or throws proper exceptions.""" | 49 """Tests that the update command works or raises proper exceptions.""" |
| 48 | 50 |
| 49 if gslib.IS_PACKAGE_INSTALL: | 51 if gslib.IS_PACKAGE_INSTALL: |
| 50 # The update command is not present when installed via package manager. | 52 # The update command is not present when installed via package manager. |
| 51 stderr = self.RunGsUtil(['update'], return_stderr=True, expected_status=1) | 53 stderr = self.RunGsUtil(['update'], return_stderr=True, expected_status=1) |
| 52 self.assertIn('Invalid command', stderr) | 54 self.assertIn('Invalid command', stderr) |
| 53 return | 55 return |
| 54 | 56 |
| 55 # Create two temp directories, one of which we will run 'gsutil update' in | 57 # Create two temp directories, one of which we will run 'gsutil update' in |
| 56 # to pull the changes from the other. | 58 # to pull the changes from the other. |
| 57 tmpdir_src = self.CreateTempDir() | 59 tmpdir_src = self.CreateTempDir() |
| 58 tmpdir_dst = self.CreateTempDir() | 60 tmpdir_dst = self.CreateTempDir() |
| 59 | 61 |
| 60 # Copy gsutil to both source and destination directories. | 62 # Copy gsutil to both source and destination directories. |
| 61 gsutil_src = os.path.join(tmpdir_src, 'gsutil') | 63 gsutil_src = os.path.join(tmpdir_src, 'gsutil') |
| 62 gsutil_dst = os.path.join(tmpdir_dst, 'gsutil') | 64 gsutil_dst = os.path.join(tmpdir_dst, 'gsutil') |
| 65 # Path when executing from tmpdir (Windows doesn't support in-place rename) |
| 66 gsutil_relative_dst = os.path.join('gsutil', 'gsutil') |
| 67 |
| 63 shutil.copytree(GSUTIL_DIR, gsutil_src) | 68 shutil.copytree(GSUTIL_DIR, gsutil_src) |
| 64 # Copy specific files rather than all of GSUTIL_DIR so we don't pick up temp | 69 # Copy specific files rather than all of GSUTIL_DIR so we don't pick up temp |
| 65 # working files left in top-level directory by gsutil developers (like tags, | 70 # working files left in top-level directory by gsutil developers (like tags, |
| 66 # .git*, etc.) | 71 # .git*, etc.) |
| 67 os.makedirs(gsutil_dst) | 72 os.makedirs(gsutil_dst) |
| 68 for comp in ('CHANGES.md', 'CHECKSUM', 'COPYING', 'gslib', 'gsutil', | 73 for comp in ('CHANGES.md', 'CHECKSUM', 'COPYING', 'gslib', 'gsutil', |
| 69 'gsutil.py', 'LICENSE.third_party', 'MANIFEST.in', 'README.md', | 74 'gsutil.py', 'MANIFEST.in', 'README.md', 'setup.py', |
| 70 'scripts', 'setup.py', 'third_party', 'VERSION'): | 75 'third_party', 'VERSION'): |
| 71 if os.path.isdir(os.path.join(GSUTIL_DIR, comp)): | 76 if os.path.isdir(os.path.join(GSUTIL_DIR, comp)): |
| 72 func = shutil.copytree | 77 func = shutil.copytree |
| 73 else: | 78 else: |
| 74 func = shutil.copyfile | 79 func = shutil.copyfile |
| 75 func(os.path.join(GSUTIL_DIR, comp), os.path.join(gsutil_dst, comp)) | 80 func(os.path.join(GSUTIL_DIR, comp), os.path.join(gsutil_dst, comp)) |
| 76 | 81 |
| 77 # Create a fake version number in the source so we can verify it in the | 82 # Create a fake version number in the source so we can verify it in the |
| 78 # destination. | 83 # destination. |
| 79 expected_version = '17.25' | 84 expected_version = '17.25' |
| 80 src_version_file = os.path.join(gsutil_src, 'VERSION') | 85 src_version_file = os.path.join(gsutil_src, 'VERSION') |
| (...skipping 25 matching lines...) Expand all Loading... |
| 106 (_, stderr) = p.communicate() | 111 (_, stderr) = p.communicate() |
| 107 self.assertEqual(p.returncode, 1) | 112 self.assertEqual(p.returncode, 1) |
| 108 self.assertIn('update command only works with tar.gz', stderr) | 113 self.assertIn('update command only works with tar.gz', stderr) |
| 109 | 114 |
| 110 # Run with non-existent gs:// URI. | 115 # Run with non-existent gs:// URI. |
| 111 p = subprocess.Popen( | 116 p = subprocess.Popen( |
| 112 prefix + ['gsutil', 'update', 'gs://pub/Jdjh38)(;.tar.gz'], | 117 prefix + ['gsutil', 'update', 'gs://pub/Jdjh38)(;.tar.gz'], |
| 113 cwd=gsutil_dst, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | 118 cwd=gsutil_dst, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 114 (_, stderr) = p.communicate() | 119 (_, stderr) = p.communicate() |
| 115 self.assertEqual(p.returncode, 1) | 120 self.assertEqual(p.returncode, 1) |
| 116 self.assertIn('non-existent object', stderr) | 121 self.assertIn('NotFoundException', stderr) |
| 117 | 122 |
| 118 # Run with file:// URI wihout -f option. | 123 # Run with file:// URI wihout -f option. |
| 119 p = subprocess.Popen(prefix + ['gsutil', 'update', suri(src_tarball)], | 124 p = subprocess.Popen(prefix + ['gsutil', 'update', suri(src_tarball)], |
| 120 cwd=gsutil_dst, stdout=subprocess.PIPE, | 125 cwd=gsutil_dst, stdout=subprocess.PIPE, |
| 121 stderr=subprocess.PIPE) | 126 stderr=subprocess.PIPE) |
| 122 (_, stderr) = p.communicate() | 127 (_, stderr) = p.communicate() |
| 123 self.assertEqual(p.returncode, 1) | 128 self.assertEqual(p.returncode, 1) |
| 124 self.assertIn('command does not support', stderr) | 129 self.assertIn('command does not support', stderr) |
| 125 | 130 |
| 126 # Run with a file present that was not distributed with gsutil. | 131 # Run with a file present that was not distributed with gsutil. |
| 127 with open(os.path.join(gsutil_dst, 'userdata.txt'), 'w') as fp: | 132 with open(os.path.join(gsutil_dst, 'userdata.txt'), 'w') as fp: |
| 128 fp.write('important data\n') | 133 fp.write('important data\n') |
| 129 p = subprocess.Popen(prefix + ['gsutil', 'update', '-f', suri(src_tarball)], | 134 p = subprocess.Popen(prefix + ['gsutil', 'update', '-f', suri(src_tarball)], |
| 130 cwd=gsutil_dst, stdout=subprocess.PIPE, | 135 cwd=gsutil_dst, stdout=subprocess.PIPE, |
| 131 stderr=subprocess.PIPE, stdin=subprocess.PIPE) | 136 stderr=subprocess.PIPE, stdin=subprocess.PIPE) |
| 132 (_, stderr) = p.communicate() | 137 (_, stderr) = p.communicate() |
| 133 # Clean up before next test, and before assertions so failure doesn't leave | 138 # Clean up before next test, and before assertions so failure doesn't leave |
| 134 # this file around. | 139 # this file around. |
| 135 os.unlink(os.path.join(gsutil_dst, 'userdata.txt')) | 140 os.unlink(os.path.join(gsutil_dst, 'userdata.txt')) |
| 136 self.assertEqual(p.returncode, 1) | 141 self.assertEqual(p.returncode, 1) |
| 137 self.assertIn( | 142 self.assertIn( |
| 138 'The update command cannot run with user data in the gsutil directory', | 143 'The update command cannot run with user data in the gsutil directory', |
| 139 stderr.replace('\n', ' ')) | 144 stderr.replace(os.linesep, ' ')) |
| 140 | 145 |
| 141 # Now do the real update, which should succeed. | 146 # Now do the real update, which should succeed. |
| 142 p = subprocess.Popen(prefix + ['gsutil', 'update', '-f', suri(src_tarball)], | 147 p = subprocess.Popen(prefix + [gsutil_relative_dst, 'update', '-f', |
| 143 cwd=gsutil_dst, stdout=subprocess.PIPE, | 148 suri(src_tarball)], |
| 149 cwd=tmpdir_dst, stdout=subprocess.PIPE, |
| 144 stderr=subprocess.PIPE, stdin=subprocess.PIPE) | 150 stderr=subprocess.PIPE, stdin=subprocess.PIPE) |
| 145 (_, stderr) = p.communicate(input='y\r\n') | 151 (_, stderr) = p.communicate(input='y\r\n') |
| 146 self.assertEqual(p.returncode, 0, msg=( | 152 self.assertEqual(p.returncode, 0, msg=( |
| 147 'Non-zero return code (%d) from gsutil update. stderr = \n%s' % | 153 'Non-zero return code (%d) from gsutil update. stderr = \n%s' % |
| 148 (p.returncode, stderr))) | 154 (p.returncode, stderr))) |
| 149 | 155 |
| 150 # Verify that version file was updated. | 156 # Verify that version file was updated. |
| 151 dst_version_file = os.path.join(tmpdir_dst, 'gsutil', 'VERSION') | 157 dst_version_file = os.path.join(tmpdir_dst, 'gsutil', 'VERSION') |
| 152 with open(dst_version_file, 'r') as f: | 158 with open(dst_version_file, 'r') as f: |
| 153 self.assertEqual(f.read(), expected_version) | 159 self.assertEqual(f.read(), expected_version) |
| OLD | NEW |