def setup_class(self):
        self.patchers.append(create_start_patcher(
            patch_function='_get_device_of_volume', patch_object=BaseClient, side_effect=get_device_of_volume))
        self.patchers.append(create_start_patcher(
            patch_function='delete_snapshot', patch_object=BaseClient, return_value=True))
        self.patchers.append(create_start_patcher(
            patch_function='delete_volume', patch_object=BaseClient, return_value=True))
        self.patchers.append(create_start_patcher(
            patch_function='glob', patch_object=glob, side_effect=mockglob))
        self.patchers.append(create_start_patcher(patch_function='generate_name_by_prefix',
                                                  patch_object=BaseClient, side_effect=generate_name_by_prefix))
        self.patchers.append(create_start_patcher(
            patch_function='shell', patch_object=BaseClient, side_effect=shell))
        self.patchers.append(create_start_patcher(
            patch_function='last_operation', patch_object=BaseClient))
        self.patchers.append(create_start_patcher(
            patch_function='google.oauth2.service_account.Credentials.from_service_account_info'))
        self.patchers.append(create_start_patcher(
            patch_function='googleapiclient.discovery.build', return_value=ComputeClient()))
        self.patchers.append(create_start_patcher(
            patch_function='google.cloud.storage.Client', return_value=StorageClient()))
        self.patchers.append(create_start_patcher(
            patch_function='google.cloud.storage.Blob.upload_from_string'))
        self.patchers.append(create_start_patcher(
            patch_function='google.cloud.storage.Blob.delete'))
        self.patchers.append(create_start_patcher(
            patch_function='upload_from_filename', patch_object=Blob, side_effect=upload_from_filename))
        self.patchers.append(create_start_patcher(
            patch_function='download_to_filename', patch_object=Blob, side_effect=download_to_filename))

        os.environ['SF_BACKUP_RESTORE_LOG_DIRECTORY'] = log_dir
        os.environ['SF_BACKUP_RESTORE_LAST_OPERATION_DIRECTORY'] = log_dir

        self.gcpClient = GcpClient(operation_name, configuration, directory_persistent, directory_work_list,
                                   poll_delay_time, poll_maximum_time)
 def test_gcp_client_raises_availability_zone_exception(self):
     mock_blob_upload_patcher = create_start_patcher(
         patch_function='google.cloud.storage.Blob.upload_from_string')
     mock_blob_delete_patcher = create_start_patcher(
         patch_function='google.cloud.storage.Blob.delete')
     configuration['instance_id'] = invalid_vm_id
     with pytest.raises(Exception, message='Could not retrieve the availability zone of the instance.'):
         GcpClient(operation_name, configuration, directory_persistent, directory_work_list,
                   poll_delay_time, poll_maximum_time)
     mock_blob_upload_patcher.stop()
     mock_blob_delete_patcher.stop()
 def test_gcp_client_creation_with_credhub(self, mock_get, mock_post):
     credhub_config = {
         'type': 'online',
         'backup_guid': 'backup-guid',
         'instance_id': 'vm-id',
         'secret': 'xyz',
         'job_name': 'service-job-name',
         'container': valid_container,
         'projectId': project_id,
         'credhub_url': '/credhub',
         'credhub_uaa_url': '/oauth',
         'credhub_key': '/test',
         'credhub_client_id': '1',
         'credhub_client_secret': 'secret',
         'credhub_username': '******',
         'credhub_user_password': '******'
     }
     auth_response = {'access_token': 'auth-token-response'}
     mock_post.return_value = Mock(ok=True)
     mock_post.return_value.json.return_value = auth_response
     mock_get.return_value = Mock(ok=True)
     mock_get.return_value.json.return_value = {
         'data': [{
             'value': {
                 'credentials': {
                     'type': 'service_account',
                     'project_id': 'gcp-dev',
                     'private_key_id': '2222',
                     'private_key':
                     '-----BEGIN PRIVATE KEY-----\\nMIIEFatI0=\\n-----END PRIVATE KEY-----\\n',
                     'client_email': '*****@*****.**',
                     'client_id': '6666',
                     'auth_uri': 'auth_uri',
                     'token_uri': 'token_uri',
                     'auth_provider_x509_cert_url': 'cert_url',
                     'client_x509_cert_url': 'cert_url'
                 },
                 'projectId': 'gcp-dev'
             }
         }]
     }
     gcpClient = GcpClient(operation_name, credhub_config,
                           directory_persistent, directory_work_list,
                           poll_delay_time, poll_maximum_time)
     assert gcpClient.project_id == project_id
     assert gcpClient.project_id == project_id
     assert gcpClient.compute_client is not None
     assert gcpClient.storage_client is not None
     assert gcpClient.container == bucket
     assert gcpClient.availability_zone == availability_zone
 def setup_class(cls):
     cls.mock_lastop_patcher = patch.object(BaseClient, 'last_operation')
     cls.mock_lastop = cls.mock_lastop_patcher.start()
     cls.mock_sevice_account_patcher = patch('google.oauth2.service_account.Credentials.from_service_account_info')
     cls.mock_sevice_account = cls.mock_sevice_account_patcher.start()
     cls.mock_compute_client_patcher = patch('googleapiclient.discovery.build')
     cls.mock_compute_client = cls.mock_compute_client_patcher.start()
     cls.mock_storage_client_patcher = patch('google.cloud.storage.Client')
     cls.mock_storage_client = cls.mock_storage_client_patcher.start()
     cls.mock_blob_upload_patcher = patch('google.cloud.storage.Blob.upload_from_string')
     cls.mock_blob_upload = cls.mock_blob_upload_patcher.start()
     cls.mock_blob_delete_patcher = patch('google.cloud.storage.Blob.delete')
     cls.mock_blob_delete = cls.mock_blob_delete_patcher.start()
     os.environ['SF_BACKUP_RESTORE_LOG_DIRECTORY'] = log_dir
     os.environ['SF_BACKUP_RESTORE_LAST_OPERATION_DIRECTORY'] = log_dir
     cls.mock_compute_client.return_value = ComputeClient()
     cls.mock_storage_client.return_value = StorageClient()
     data = 'random data'
     blob = Blob()
     cls.mock_blob_upload.return_value = blob.upload_from_string(data)
     cls.mock_blob_delete.return_value = blob.delete()
     cls.gcpClient = GcpClient(operation_name, configuration, directory_persistent, directory_work_list,
         poll_delay_time, poll_maximum_time)
 def test_gcp_client_raises_container_not_found_exception(self):
     with pytest.raises(Exception, message='Could not find or access the given container.'):
         GcpClient(operation_name, configuration, directory_persistent, directory_work_list,
                   poll_delay_time, poll_maximum_time)