def test_get_gce_compute_driver(mock_service_account, mock_discovery): creds = Mock() mock_service_account.Credentials.from_service_account_info.return_value = creds get_gce_compute_driver({'some': 'creds'}) mock_discovery.build.assert_called_once_with('compute', 'v1', credentials=creds, cache_discovery=False)
def run_job(self): """ Deprecate image in all accounts. """ self.status = SUCCESS if self.old_cloud_image_name is None: # There is no old image that needs deprecate for the job. return self.request_credentials([self.account]) credential = self.credentials[self.account] self.cloud_image_name = self.status_msg['cloud_image_name'] try: project = credential.get('project_id') compute_driver = get_gce_compute_driver(credential, version='alpha') deprecate_gce_image(compute_driver, project, self.old_cloud_image_name, self.cloud_image_name, self.months_to_deletion) self.log_callback.info('Deprecated image {0}.'.format( self.old_cloud_image_name)) except Exception as error: msg = 'There was an error deprecating image in {0}: {1}'.format( self.account, error) self.add_error_msg(msg) self.log_callback.error(msg) self.status = FAILED
def cleanup_image(self): credentials = self.credentials[self.account] project = credentials.get('project_id') object_name = self.status_msg['object_name'] compute_driver = get_gce_compute_driver(credentials) storage_driver = get_gce_storage_driver(credentials) self.log_callback.info('Cleaning up image: {0} in region: {1}.'.format( self.cloud_image_name, self.region)) try: delete_gce_image(compute_driver, project, self.cloud_image_name) delete_image_tarball(storage_driver, object_name, self.bucket) except Exception as error: msg = 'Failed to cleanup image: {0}'.format(error) self.log_callback.warning(msg) self.add_error_msg(msg)
def run_job(self): self.status = SUCCESS self.log_callback.info('Creating image.') self.cloud_image_name = self.status_msg['cloud_image_name'] object_name = self.status_msg['object_name'] timestamp = re.findall(r'\d{8}', self.cloud_image_name)[0] self.cloud_image_description = format_string_with_date( self.base_cloud_image_description, timestamp=timestamp) self.request_credentials([self.account]) credentials = self.credentials[self.account] project = credentials.get('project_id') compute_driver = get_gce_compute_driver(credentials, version='alpha') uri = ''.join([ 'https://www.googleapis.com/storage/v1/b/', self.bucket, '/o/', object_name ]) if get_gce_image(compute_driver, project, self.cloud_image_name): self.log_callback.info( 'Replacing existing image with the same name.') delete_gce_image(compute_driver, project, self.cloud_image_name) rollout = create_gce_rollout(compute_driver, project) create_gce_image(compute_driver, project, self.cloud_image_name, self.cloud_image_description, uri, family=self.family, guest_os_features=self.guest_os_features, rollout=rollout) self.log_callback.info('Created image has ID: {0}'.format( self.cloud_image_name))
def run_job(self): """ Tests image with img-proof and update status and results. """ self.status = SUCCESS accounts = [self.account] if self.testing_account: # Get both sets of credentials in case cleanup method is run. accounts.append(self.testing_account) self.request_credentials(accounts) credentials = self.credentials[self.testing_account or self.account] project = credentials.get('project_id') compute_driver = get_gce_compute_driver(credentials) if self.test_fallback_regions == []: # fallback testing explicitly disabled fallback_regions = set() elif self.test_fallback_regions is None: fallback_regions = get_region_list(compute_driver, project) else: fallback_regions = set(self.test_fallback_regions) fallback_regions.add(self.region) self.cloud_image_name = self.status_msg['cloud_image_name'] with create_json_file(credentials) as auth_file: for firmware in self.boot_firmware: self.log_callback.info( 'Running img-proof tests against image with ' 'type: {inst_type}. Using boot firmware setting: ' '{firmware}.'.format(inst_type=self.instance_type, firmware=firmware)) if self.test_gvnic_with == firmware: test_gvnic = True else: test_gvnic = False retry_region = self.region while fallback_regions: try: result = img_proof_test( cloud=self.cloud, description=self.description, distro=self.distro, image_id=self.cloud_image_name, instance_type=self.instance_type, img_proof_timeout=self.img_proof_timeout, region=retry_region, service_account_file=auth_file, ssh_private_key_file=self.ssh_private_key_file, ssh_user=self.ssh_user, tests=self.tests, boot_firmware=firmware, image_project=self.image_project, log_callback=self.log_callback, sev_capable=self.sev_capable, use_gvnic=test_gvnic) except IpaRetryableError as error: result = {'status': EXCEPTION, 'msg': str(error)} fallback_regions.remove(retry_region) if fallback_regions: retry_region = random.choice(fallback_regions) except Exception as error: self.add_error_msg(str(error)) result = { 'status': EXCEPTION, 'msg': str(traceback.format_exc()) } break else: break self.status = process_test_result(result, self.log_callback, self.region, self.status_msg) if self.status != SUCCESS: self.add_error_msg( 'Image failed img-proof test suite. ' 'See "mash job test-results --job-id {GUID} -v" ' 'for details on the failing tests.') break if self.cleanup_images or \ (self.status != SUCCESS and self.cleanup_images is not False): self.cleanup_image()