def test_delete_bucket_raises(mock_client): mock_client.get_bucket.side_effect = NotFound('error') gs = GCS(bucket='foo', gc_credentials_file='foo/bar') with pytest.raises(GCSDestinationError): gs.delete_bucket()
def test_create_bucket_raises(mock_client): mock_client.create_bucket.side_effect = Conflict('error') gs = GCS(bucket='foo', gc_credentials_file='foo/bar') with pytest.raises(GCSDestinationError): gs.create_bucket()
def test_delete_bucket(mock_client, force): mock_bucket = mock.Mock() mock_client.get_bucket.return_value = mock_bucket gs = GCS(bucket='foo', gc_credentials_file='foo/bar') gs.delete_bucket(force=force) mock_bucket.delete.assert_called_once_with(force=force)
def test_delete_bucket_raises(mock_client): mock_client.get_bucket.side_effect = NotFound('error') gs = GCS( bucket='foo', gc_credentials_file='foo/bar' ) with pytest.raises(GCSDestinationError): gs.delete_bucket()
def test_create_bucket(mock_client): gs = GCS( bucket='foo', gc_credentials_file='foo/bar' ) gs.create_bucket() mock_client.create_bucket.assert_called_once_with( bucket_name='foo' )
def test_create_bucket_raises(mock_client): mock_client.create_bucket.side_effect = Conflict('error') gs = GCS( bucket='foo', gc_credentials_file='foo/bar' ) with pytest.raises(GCSDestinationError): gs.create_bucket()
def test_get_stream_calls_popen(mock_os, mock_process): mock_os.pipe.return_value = (100, 200) gs = GCS(gc_credentials_file='foo', bucket='bar') mock_copy = mock.Mock() mock_copy.key = 'foo-key' with gs.get_stream(mock_copy): pass mock_process.assert_called_once_with(target=gs._download_to_pipe, args=('foo-key', 100, 200))
def test_delete_raises_not_found(mock_bucket): # Actually list_blobs() returns HTTPIterator. For test simplicity we'll # use empty iterator. mock_bucket.list_blobs.return_value = iter([]) gs = GCS(bucket='bar', gc_credentials_file='xyz') with pytest.raises(FileNotFound): gs.delete('foo') mock_bucket.list_blobs.assert_called_once_with(prefix='foo')
def test_deletes_chunks(mock_bucket): mock_blob1 = mock.Mock() mock_blob1.name = 'foo/part-0000000000000000' mock_blob2 = mock.Mock() mock_blob2.name = 'foo/part-0000000000000001' mock_bucket.list_blobs.return_value = iter([mock_blob1, mock_blob2]) gs = GCS(bucket='somebucket', gc_credentials_file='xyz') gs.delete('foo') mock_blob1.delete.assert_called_once_with() mock_blob2.delete.assert_called_once_with()
def test_deletes_only_fully_matching(mock_bucket): mock_blob1 = mock.Mock() mock_blob1.name = 'foo' mock_blob2 = mock.Mock() mock_blob2.name = 'foobar' mock_bucket.list_blobs.return_value = iter([mock_blob1, mock_blob2]) gs = GCS(bucket='somebucket', gc_credentials_file='xyz') gs.delete('foo') mock_blob1.delete.assert_called_once_with() mock_blob2.delete.assert_not_called()
def test_delete_bucket(mock_client, force): mock_bucket = mock.Mock() mock_client.get_bucket.return_value = mock_bucket gs = GCS( bucket='foo', gc_credentials_file='foo/bar' ) gs.delete_bucket(force=force) mock_bucket.delete.assert_called_once_with( force=force )
def test_delete_raises_not_found(mock_bucket): # Actually list_blobs() returns HTTPIterator. For test simplicity we'll # use empty iterator. mock_bucket.list_blobs.return_value = iter([]) gs = GCS(bucket='bar', gc_credentials_file='xyz') with pytest.raises(FileNotFound): gs.delete('foo') mock_bucket.list_blobs.assert_called_once_with( prefix='foo' )
def test_deletes_chunks(mock_bucket): mock_blob1 = mock.Mock() mock_blob1.name = 'foo/part-0000000000000000' mock_blob2 = mock.Mock() mock_blob2.name = 'foo/part-0000000000000001' mock_bucket.list_blobs.return_value = iter( [ mock_blob1, mock_blob2 ] ) gs = GCS(bucket='somebucket', gc_credentials_file='xyz') gs.delete('foo') mock_blob1.delete.assert_called_once_with() mock_blob2.delete.assert_called_once_with()
def test_get_stream_calls_popen(mock_os, mock_process): mock_os.pipe.return_value = (100, 200) gs = GCS( gc_credentials_file='foo', bucket='bar' ) mock_copy = mock.Mock() mock_copy.key = 'foo-key' with gs.get_stream(mock_copy): pass mock_process.assert_called_once_with( target=gs._download_to_pipe, args=('foo-key', 100, 200) )
def test_deletes_only_fully_matching(mock_bucket): mock_blob1 = mock.Mock() mock_blob1.name = 'foo' mock_blob2 = mock.Mock() mock_blob2.name = 'foobar' mock_bucket.list_blobs.return_value = iter( [ mock_blob1, mock_blob2 ] ) gs = GCS(bucket='somebucket', gc_credentials_file='xyz') gs.delete('foo') mock_blob1.delete.assert_called_once_with() mock_blob2.delete.assert_not_called()
def test_init_set_env(): # make sure GOOGLE_APPLICATION_CREDENTIALS is not set if 'GOOGLE_APPLICATION_CREDENTIALS' in environ: del environ['GOOGLE_APPLICATION_CREDENTIALS'] with pytest.raises(KeyError): assert environ['GOOGLE_APPLICATION_CREDENTIALS'] GCS(bucket='bar', gc_credentials_file='foo') assert environ['GOOGLE_APPLICATION_CREDENTIALS'] == 'foo'
def destination(self, backup_source=socket.gethostname()): """ :param backup_source: Hostname of the host where backup is taken from. :type backup_source: str :return: Backup destination instance :rtype: BaseDestination """ try: backup_destination = self.__cfg.get("destination", "backup_destination") if backup_destination == "ssh": return Ssh( self.ssh.path, hostname=backup_source, ssh_host=self.ssh.host, ssh_port=self.ssh.port, ssh_user=self.ssh.user, ssh_key=self.ssh.key, ) elif backup_destination == "s3": return S3( bucket=self.s3.bucket, aws_access_key_id=self.s3.aws_access_key_id, aws_secret_access_key=self.s3.aws_secret_access_key, aws_default_region=self.s3.aws_default_region, hostname=backup_source, ) elif backup_destination == "gcs": return GCS( bucket=self.gcs.bucket, gc_credentials_file=self.gcs.gc_credentials_file, gc_encryption_key=self.gcs.gc_encryption_key, hostname=backup_source, ) else: raise ConfigurationError("Unsupported destination '%s'" % backup_destination) except NoSectionError as err: raise ConfigurationError( "%s is missing required section 'destination'" % self._config_file) from err
def test_create_bucket(mock_client): gs = GCS(bucket='foo', gc_credentials_file='foo/bar') gs.create_bucket() mock_client.create_bucket.assert_called_once_with(bucket_name='foo')
def test_init_set_bucket(mock_base): mock_base.return_value = None gs = GCS(gc_credentials_file='foo', bucket='bar') assert gs.bucket == 'bar' mock_base.assert_called_once_with('bar')
def test_init_raises_if_no_file(): with pytest.raises(GCSDestinationError): GCS(bucket='foo')
def gs(): return GCS(bucket='test-bucket', gc_credentials_file='foo')
def test_init_set_bucket(): gs = GCS(gc_credentials_file='foo', bucket='bar') assert gs.bucket == 'bar'
def gs(creds_file): return GCS(gc_credentials_file=creds_file, bucket='twindb-backups')