def testGetIfHashChanged(self, path_mock, unused_lock_mock, get_mock, calc_hash_mock): path_mock.exists.side_effect = [False, True, True] calc_hash_mock.return_value = 'hash' # The file at |local_path| doesn't exist. We should download file from cs. ret = cloud_storage.GetIfHashChanged('remote_path', 'local_path', 'cs_bucket', 'hash') self.assertTrue(ret) get_mock.assert_called_once_with('cs_bucket', 'remote_path', 'local_path') get_mock.reset_mock() self.assertFalse(calc_hash_mock.call_args) calc_hash_mock.reset_mock() # A local file exists at |local_path| but has the wrong hash. # We should download file from cs. ret = cloud_storage.GetIfHashChanged('remote_path', 'local_path', 'cs_bucket', 'new_hash') self.assertTrue(ret) get_mock.assert_called_once_with('cs_bucket', 'remote_path', 'local_path') get_mock.reset_mock() calc_hash_mock.assert_called_once_with('local_path') calc_hash_mock.reset_mock() # Downloaded file exists locally and has the right hash. Don't download. ret = cloud_storage.GetIfHashChanged('remote_path', 'local_path', 'cs_bucket', 'hash') self.assertFalse(get_mock.call_args) self.assertFalse(ret) calc_hash_mock.reset_mock() get_mock.reset_mock()
def testDisableCloudStorageIo(self, unused_lock_mock): os.environ['DISABLE_CLOUD_STORAGE_IO'] = '1' dir_path = 'real_dir_path' self.fs.CreateDirectory(dir_path) file_path = os.path.join(dir_path, 'file1') file_path_sha = file_path + '.sha1' def CleanTimeStampFile(): os.remove(file_path + '.fetchts') self.CreateFiles([file_path, file_path_sha]) with open(file_path_sha, 'w') as f: f.write('hash1234') with self.assertRaises(cloud_storage.CloudStorageIODisabled): cloud_storage.Copy('bucket1', 'bucket2', 'remote_path1', 'remote_path2') with self.assertRaises(cloud_storage.CloudStorageIODisabled): cloud_storage.Get('bucket', 'foo', file_path) with self.assertRaises(cloud_storage.CloudStorageIODisabled): cloud_storage.GetIfChanged(file_path, 'foo') with self.assertRaises(cloud_storage.CloudStorageIODisabled): cloud_storage.GetIfHashChanged('bar', file_path, 'bucket', 'hash1234') with self.assertRaises(cloud_storage.CloudStorageIODisabled): cloud_storage.Insert('bucket', 'foo', file_path) CleanTimeStampFile() with self.assertRaises(cloud_storage.CloudStorageIODisabled): cloud_storage.GetFilesInDirectoryIfChanged(dir_path, 'bucket')
def GetRemotePath(self): """Gets the path to a downloaded version of the dependency. May not download the file if it has already been downloaded. Will unzip the downloaded file if a non-empty archive_info was passed in at init. Returns: A path to an executable that was stored in cloud_storage, or None if not found. Raises: CredentialsError: If cloud_storage credentials aren't configured. PermissionError: If cloud_storage credentials are configured, but not with an account that has permission to download the needed file. NotFoundError: If the needed file does not exist where expected in cloud_storage or the downloaded zip file. ServerError: If an internal server error is hit while downloading the needed file. CloudStorageError: If another error occured while downloading the remote path. FileNotFoundError: If the download was otherwise unsuccessful. """ if not self._has_minimum_data: return None download_dir = os.path.dirname(self._download_path) if not os.path.exists(download_dir): try: os.makedirs(download_dir) except OSError as e: # The logic above is racy, and os.makedirs will raise an OSError if # the directory exists. if e.errno != errno.EEXIST: raise dependency_path = self._download_path cloud_storage.GetIfHashChanged(self._cs_remote_path, self._download_path, self._cs_bucket, self._cs_hash) if not os.path.exists(dependency_path): raise exceptions.FileNotFoundError(dependency_path) if self.has_archive_info: dependency_path = self._archive_info.GetUnzippedPath() else: mode = os.stat(dependency_path).st_mode os.chmod(dependency_path, mode | stat.S_IX) return os.path.abspath(dependency_path)