Exemplo n.º 1
0
    def test_backup_cmp_shafiles(self):
        volume_id = '1a99ac67-c534-4fe3-b472-0000001785e2'

        container_name = self.temp_dir.replace(tempfile.gettempdir() + '/', '',
                                               1)
        backup = self._create_backup_db_entry(volume_id=volume_id,
                                              container=container_name)
        service1 = google_dr.GoogleBackupDriver(self.ctxt)
        self.volume_file.seek(0)
        service1.backup(backup, self.volume_file)
        self.assertEqual(container_name, backup.container)

        # Create incremental backup with no change to contents
        deltabackup = self._create_backup_db_entry(volume_id=volume_id,
                                                   container=container_name,
                                                   parent_id=backup.id)
        service2 = google_dr.GoogleBackupDriver(self.ctxt)
        self.volume_file.seek(0)
        service2.backup(deltabackup, self.volume_file)
        self.assertEqual(container_name, deltabackup.container)

        # Compare shas from both files
        content1 = service1._read_sha256file(backup)
        content2 = service2._read_sha256file(deltabackup)

        self.assertEqual(len(content1['sha256s']), len(content2['sha256s']))
        self.assertEqual(set(content1['sha256s']), set(content2['sha256s']))
Exemplo n.º 2
0
    def test_backup_delta_two_blocks_in_object_change(self):
        volume_id = 'b943e84f-aa67-4331-9ab2-000000cf19ba'

        self.flags(backup_gcs_object_size=8 * units.Ki)
        self.flags(backup_gcs_block_size=units.Ki)

        container_name = self.temp_dir.replace(tempfile.gettempdir() + '/', '',
                                               1)
        backup = self._create_backup_db_entry(volume_id=volume_id,
                                              container=container_name)

        service1 = google_dr.GoogleBackupDriver(self.ctxt)
        self.volume_file.seek(0)
        service1.backup(backup, self.volume_file)
        self.assertEqual(container_name, backup.container)

        # Create incremental backup with no change to contents
        self.volume_file.seek(16 * units.Ki)
        self.volume_file.write(os.urandom(units.Ki))
        self.volume_file.seek(20 * units.Ki)
        self.volume_file.write(os.urandom(units.Ki))

        deltabackup = self._create_backup_db_entry(volume_id=volume_id,
                                                   container=container_name,
                                                   parent_id=backup.id)
        service2 = google_dr.GoogleBackupDriver(self.ctxt)
        self.volume_file.seek(0)
        service2.backup(deltabackup, self.volume_file)
        self.assertEqual(container_name, deltabackup.container)

        # Verify that two shas are changed at index 16 and 20
        content1 = service1._read_sha256file(backup)
        content2 = service2._read_sha256file(deltabackup)
        self.assertNotEqual(content1['sha256s'][16], content2['sha256s'][16])
        self.assertNotEqual(content1['sha256s'][20], content2['sha256s'][20])
Exemplo n.º 3
0
    def test_restore_delta(self):
        volume_id = '04d83506-bcf7-4ff5-9c65-00000051bd2e'
        self.flags(backup_gcs_object_size=8 * units.Ki)
        self.flags(backup_gcs_block_size=units.Ki)
        container_name = self.temp_dir.replace(tempfile.gettempdir() + '/', '',
                                               1)
        backup = self._create_backup_db_entry(volume_id=volume_id,
                                              container=container_name)
        service1 = google_dr.GoogleBackupDriver(self.ctxt)
        self.volume_file.seek(0)
        service1.backup(backup, self.volume_file)

        # Create incremental backup with no change to contents
        self.volume_file.seek(16 * units.Ki)
        self.volume_file.write(os.urandom(units.Ki))
        self.volume_file.seek(20 * units.Ki)
        self.volume_file.write(os.urandom(units.Ki))

        deltabackup = self._create_backup_db_entry(
            volume_id=volume_id,
            status=objects.fields.BackupStatus.RESTORING,
            container=container_name,
            parent_id=backup.id)
        self.volume_file.seek(0)
        service2 = google_dr.GoogleBackupDriver(self.ctxt)
        service2.backup(deltabackup, self.volume_file, True)

        with tempfile.NamedTemporaryFile() as restored_file:
            service2.restore(deltabackup, volume_id, restored_file)
            self.assertTrue(
                filecmp.cmp(self.volume_file.name, restored_file.name))
Exemplo n.º 4
0
    def test_backup_delta_two_objects_change(self):
        volume_id = '30dab288-265a-4583-9abe-000000d42c67'

        self.flags(backup_gcs_object_size=8 * units.Ki)
        self.flags(backup_gcs_block_size=units.Ki)

        container_name = self.temp_dir.replace(tempfile.gettempdir() + '/', '',
                                               1)
        backup = self._create_backup_db_entry(volume_id=volume_id,
                                              container=container_name)
        service1 = google_dr.GoogleBackupDriver(self.ctxt)
        self.volume_file.seek(0)
        service1.backup(backup, self.volume_file)
        self.assertEqual(container_name, backup.container)

        # Create incremental backup with no change to contents
        self.volume_file.seek(2 * 8 * units.Ki)
        self.volume_file.write(os.urandom(units.Ki))
        self.volume_file.seek(4 * 8 * units.Ki)
        self.volume_file.write(os.urandom(units.Ki))

        deltabackup = self._create_backup_db_entry(volume_id=volume_id,
                                                   container=container_name,
                                                   parent_id=backup.id)
        service2 = google_dr.GoogleBackupDriver(self.ctxt)
        self.volume_file.seek(0)
        service2.backup(deltabackup, self.volume_file)
        self.assertEqual(container_name, deltabackup.container)

        content1 = service1._read_sha256file(backup)
        content2 = service2._read_sha256file(deltabackup)

        # Verify that two shas are changed at index 16 and 32
        self.assertNotEqual(content1['sha256s'][16], content2['sha256s'][16])
        self.assertNotEqual(content1['sha256s'][32], content2['sha256s'][32])
Exemplo n.º 5
0
 def test_backup_zlib(self):
     volume_id = '5cea0535-b6fb-4531-9a38-000000bea094'
     backup = self._create_backup_db_entry(volume_id=volume_id)
     self.flags(backup_compression_algorithm='zlib')
     service = google_dr.GoogleBackupDriver(self.ctxt)
     self._write_effective_compression_file(self.size_volume_file)
     service.backup(backup, self.volume_file)
Exemplo n.º 6
0
 def test_backup_bz2(self):
     volume_id = 'dc0fee35-b44e-4f13-80d6-000000e1b50c'
     backup = self._create_backup_db_entry(volume_id=volume_id)
     self.flags(backup_compression_algorithm='bz2')
     service = google_dr.GoogleBackupDriver(self.ctxt)
     self._write_effective_compression_file(self.size_volume_file)
     service.backup(backup, self.volume_file)
Exemplo n.º 7
0
 def test_backup_uncompressed(self):
     volume_id = '2b9f10a3-42b4-4fdf-b316-000000ceb039'
     backup = self._create_backup_db_entry(volume_id=volume_id)
     self.flags(backup_compression_algorithm='none')
     service = google_dr.GoogleBackupDriver(self.ctxt)
     self.volume_file.seek(0)
     service.backup(backup, self.volume_file)
Exemplo n.º 8
0
 def test_backup_zstd(self):
     volume_id = '471910a0-a197-4259-9c50-0fc3d6a07dbc'
     backup = self._create_backup_db_entry(volume_id=volume_id)
     self.flags(backup_compression_algorithm='zstd')
     service = google_dr.GoogleBackupDriver(self.ctxt)
     self._write_effective_compression_file(self.size_volume_file)
     service.backup(backup, self.volume_file)
Exemplo n.º 9
0
 def test_backup_proxy_configured(self, mock_proxy_info):
     # Configuration overwrites enviromental variable
     proxy_cfg = "http://myproxy.example.com"
     os.environ['http_proxy'] = proxy_cfg + '_fake'
     google_dr.CONF.set_override("backup_gcs_proxy_url", proxy_cfg)
     google_dr.GoogleBackupDriver(self.ctxt)
     self.assertEqual(proxy_cfg, os.environ.get('http_proxy'))
Exemplo n.º 10
0
 def test_delete(self):
     volume_id = '9ab256c8-3175-4ad8-baa1-0000007f9d31'
     object_prefix = 'test_prefix'
     backup = self._create_backup_db_entry(volume_id=volume_id,
                                           service_metadata=object_prefix)
     service = google_dr.GoogleBackupDriver(self.ctxt)
     service.delete_backup(backup)
Exemplo n.º 11
0
 def test_backup_default_container(self):
     volume_id = '9552017f-c8b9-4e4e-a876-00000053349c'
     backup = self._create_backup_db_entry(volume_id=volume_id,
                                           container=None)
     service = google_dr.GoogleBackupDriver(self.ctxt)
     self.volume_file.seek(0)
     service.backup(backup, self.volume_file)
     self.assertEqual('gcscinderbucket', backup.container)
Exemplo n.º 12
0
    def test_restore(self):
        volume_id = 'c2a81f09-f480-4325-8424-00000071685b'
        backup = self._create_backup_db_entry(
            volume_id=volume_id, status=objects.fields.BackupStatus.RESTORING)
        service = google_dr.GoogleBackupDriver(self.ctxt)

        with tempfile.NamedTemporaryFile() as volume_file:
            service.restore(backup, volume_id, volume_file)
Exemplo n.º 13
0
 def test_create_backup_fail2(self):
     volume_id = 'b09b1ad4-5f0e-4d3f-8b9e-0000004f5ec4'
     container_name = 'gcs_oauth2_failure'
     backup = self._create_backup_db_entry(volume_id=volume_id,
                                           container=container_name)
     service = google_dr.GoogleBackupDriver(self.ctxt)
     self.volume_file.seek(0)
     self.assertRaises(google_dr.GCSOAuth2Failure, service.backup, backup,
                       self.volume_file)
Exemplo n.º 14
0
 def test_backup(self):
     volume_id = 'b09b1ad4-5f0e-4d3f-8b9e-0000004f5ec2'
     container_name = 'test-bucket'
     backup = self._create_backup_db_entry(volume_id=volume_id,
                                           container=container_name)
     service = google_dr.GoogleBackupDriver(self.ctxt)
     self.volume_file.seek(0)
     result = service.backup(backup, self.volume_file)
     self.assertIsNone(result)
Exemplo n.º 15
0
 def test_backup_custom_container(self):
     volume_id = '1da9859e-77e5-4731-bd58-000000ca119e'
     container_name = 'fake99'
     backup = self._create_backup_db_entry(volume_id=volume_id,
                                           container=container_name)
     service = google_dr.GoogleBackupDriver(self.ctxt)
     self.volume_file.seek(0)
     service.backup(backup, self.volume_file)
     self.assertEqual(container_name, backup.container)
Exemplo n.º 16
0
    def test_restore_fail(self):
        volume_id = 'c2a81f09-f480-4325-8424-00000071685b'
        container_name = 'gcs_connection_failure'
        backup = self._create_backup_db_entry(volume_id=volume_id,
                                              container=container_name)
        service = google_dr.GoogleBackupDriver(self.ctxt)

        with tempfile.NamedTemporaryFile() as volume_file:
            self.assertRaises(google_dr.GCSConnectionFailure, service.restore,
                              backup, volume_id, volume_file)
Exemplo n.º 17
0
    def test_prepare_output_data_no_compression(self):
        self.flags(backup_compression_algorithm='none')
        service = google_dr.GoogleBackupDriver(self.ctxt)
        # Set up buffer of 128 zeroed bytes
        fake_data = b'\0' * 128

        result = service._prepare_output_data(fake_data)

        self.assertEqual('none', result[0])
        self.assertEqual(fake_data, result[1])
Exemplo n.º 18
0
    def test_no_httplib2_auth(self, build, from_stream):
        # Google api client requires google-auth-httplib2 if not present we
        # use legacy credentials
        google_dr.CONF.set_override('backup_gcs_credential_file',
                                    'credentials_file')

        google_dr.GoogleBackupDriver(self.ctxt)

        from_stream.assert_called_once_with('credentials_file')
        build.assert_called_once_with('storage', 'v1', cache_discovery=False,
                                      credentials=from_stream.return_value)
Exemplo n.º 19
0
    def test_prepare_output_data_ineffective_compression(self):
        service = google_dr.GoogleBackupDriver(self.ctxt)
        # Set up buffer of 128 zeroed bytes
        fake_data = b'\0' * 128
        # Pre-compress so that compression in the driver will be ineffective.
        already_compressed_data = service.compressor.compress(fake_data)

        result = service._prepare_output_data(already_compressed_data)

        self.assertEqual('none', result[0])
        self.assertEqual(already_compressed_data, result[1])
Exemplo n.º 20
0
 def test_get_compressor(self):
     service = google_dr.GoogleBackupDriver(self.ctxt)
     compressor = service._get_compressor('None')
     self.assertIsNone(compressor)
     compressor = service._get_compressor('zlib')
     self.assertEqual(zlib, compressor)
     self.assertIsInstance(compressor, tpool.Proxy)
     compressor = service._get_compressor('bz2')
     self.assertEqual(bz2, compressor)
     self.assertIsInstance(compressor, tpool.Proxy)
     self.assertRaises(ValueError, service._get_compressor, 'fake')
Exemplo n.º 21
0
    def test_non_google_auth_version(self, account, build, from_stream):
        # Prior to v1.6.0 Google api client doesn't support google-auth library
        google_dr.CONF.set_override('backup_gcs_credential_file',
                                    'credentials_file')

        google_dr.GoogleBackupDriver(self.ctxt)

        from_stream.assert_called_once_with('credentials_file')
        account.Credentials.from_service_account_file.assert_not_called()
        build.assert_called_once_with('storage', 'v1', cache_discovery=False,
                                      credentials=from_stream.return_value)
Exemplo n.º 22
0
    def test_backup_default_container_notify(self, _send_progress,
                                             _send_progress_end):
        volume_id = '87dd0eed-2598-4ebd-8ebb-000000ac578a'
        backup = self._create_backup_db_entry(volume_id=volume_id,
                                              container=None)
        # If the backup_object_number_per_notification is set to 1,
        # the _send_progress method will be called for sure.
        google_dr.CONF.set_override("backup_object_number_per_notification", 1)
        google_dr.CONF.set_override("backup_gcs_enable_progress_timer", False)
        service = google_dr.GoogleBackupDriver(self.ctxt)
        self.volume_file.seek(0)
        service.backup(backup, self.volume_file)
        self.assertTrue(_send_progress.called)
        self.assertTrue(_send_progress_end.called)

        # If the backup_object_number_per_notification is increased to
        # another value, the _send_progress method will not be called.
        _send_progress.reset_mock()
        _send_progress_end.reset_mock()
        google_dr.CONF.set_override("backup_object_number_per_notification",
                                    10)
        service = google_dr.GoogleBackupDriver(self.ctxt)
        self.volume_file.seek(0)
        service.backup(backup, self.volume_file)
        self.assertFalse(_send_progress.called)
        self.assertTrue(_send_progress_end.called)

        # If the timer is enabled, the _send_progress will be called,
        # since the timer can trigger the progress notification.
        _send_progress.reset_mock()
        _send_progress_end.reset_mock()
        google_dr.CONF.set_override("backup_object_number_per_notification",
                                    10)
        google_dr.CONF.set_override("backup_gcs_enable_progress_timer", True)
        service = google_dr.GoogleBackupDriver(self.ctxt)
        self.volume_file.seek(0)
        service.backup(backup, self.volume_file)
        self.assertTrue(_send_progress.called)
        self.assertTrue(_send_progress_end.called)
Exemplo n.º 23
0
    def test_google_auth_used(self, account, build, from_stream,
                              get_dist_version):
        # Google api client requires google-auth-httplib2 if not present we
        # use legacy credentials
        get_dist_version.return_value = '1.6.6'
        google_dr.CONF.set_override('backup_gcs_credential_file',
                                    'credentials_file')

        google_dr.GoogleBackupDriver(self.ctxt)

        get_dist_version.assert_called_once_with('google-api-python-client')
        from_stream.assert_not_called()
        create_creds = account.Credentials.from_service_account_file
        create_creds.assert_called_once_with('credentials_file')
        build.assert_called_once_with('storage', 'v1', cache_discovery=False,
                                      credentials=create_creds.return_value)
Exemplo n.º 24
0
    def test_backup_shafile(self):
        volume_id = '6465dad4-22af-48f7-8a1a-000000218907'

        container_name = self.temp_dir.replace(tempfile.gettempdir() + '/', '',
                                               1)
        backup = self._create_backup_db_entry(volume_id=volume_id,
                                              container=container_name)
        service = google_dr.GoogleBackupDriver(self.ctxt)
        self.volume_file.seek(0)
        service.backup(backup, self.volume_file)
        self.assertEqual(container_name, backup.container)

        # Verify sha contents
        content1 = service._read_sha256file(backup)
        self.assertEqual(64 * units.Ki / content1['chunk_size'],
                         len(content1['sha256s']))
Exemplo n.º 25
0
    def test_backup_backup_metadata_fail(self):
        """Test of when an exception occurs in backup().

        In backup(), after an exception occurs in
        self._backup_metadata(), we want to check the process of an
        exception handler.
        """
        volume_id = '020d9142-339c-4876-a445-000000f1520c'

        backup = self._create_backup_db_entry(volume_id=volume_id)
        self.flags(backup_compression_algorithm='none')
        service = google_dr.GoogleBackupDriver(self.ctxt)
        self.volume_file.seek(0)
        # We expect that an exception be notified directly.
        self.assertRaises(exception.BackupDriverException, service.backup,
                          backup, self.volume_file)
Exemplo n.º 26
0
    def test_backup_backup_metadata_fail2(self):
        """Test of when an exception occurs in an exception handler.

        In backup(), after an exception occurs in
        self._backup_metadata(), we want to check the process when the
        second exception occurs in self.delete_backup().
        """
        volume_id = '2164421d-f181-4db7-b9bd-000000eeb628'

        backup = self._create_backup_db_entry(volume_id=volume_id)
        self.flags(backup_compression_algorithm='none')
        service = google_dr.GoogleBackupDriver(self.ctxt)
        self.volume_file.seek(0)
        # We expect that the second exception is notified.
        self.assertRaises(exception.BackupOperationError, service.backup,
                          backup, self.volume_file)
Exemplo n.º 27
0
    def test_prepare_output_data_effective_compression(self):
        """Test compression works on a native thread."""
        # Use dictionary to share data between threads
        thread_dict = {}
        original_compress = zlib.compress

        def my_compress(data):
            thread_dict['compress'] = threading.current_thread()
            return original_compress(data)

        self.mock_object(zlib, 'compress', side_effect=my_compress)

        service = google_dr.GoogleBackupDriver(self.ctxt)
        # Set up buffer of 128 zeroed bytes
        fake_data = b'\0' * 128

        result = service._prepare_output_data(fake_data)

        self.assertEqual('zlib', result[0])
        self.assertGreater(len(fake_data), len(result[1]))
        self.assertNotEqual(threading.current_thread(),
                            thread_dict['compress'])
Exemplo n.º 28
0
 def test_delete_without_object_prefix(self):
     volume_id = 'ee30d649-72a6-49a5-b78d-000000edb6b1'
     backup = self._create_backup_db_entry(volume_id=volume_id)
     service = google_dr.GoogleBackupDriver(self.ctxt)
     service.delete_backup(backup)