def run_api_tests(self, record=False): """Run several API calls.""" if record: os.environ[replay.RECORD_ENVIRONMENT_VAR] = self.record_file os.environ[replay.REPLAY_ENVIRONMENT_VAR] = '' mock_responses = [] mock_responses.append(({'status': '200'}, fake_compute.GET_PROJECT_RESPONSE)) for page in fake_compute.LIST_NETWORKS_RESPONSES: mock_responses.append(({'status': '200'}, page)) mock_responses.append(( {'status': '403', 'content-type': 'application/json'}, fake_compute.ACCESS_DENIED)) http_mocks.mock_http_response_sequence(mock_responses) else: os.environ[replay.RECORD_ENVIRONMENT_VAR] = '' os.environ[replay.REPLAY_ENVIRONMENT_VAR] = self.record_file gce_api_client = None mock_creds = mock.Mock(spec_set=credentials.Credentials) with mock.patch.object(google.auth, 'default', return_value=(mock_creds, 'test-project')): gce_api_client = compute.ComputeClient( global_configs=self.fake_global_configs) responses = [] responses.append(gce_api_client.get_project(self.project_id)) responses.append(gce_api_client.get_networks(self.project_id)) try: gce_api_client.get_instances(self.project_id) except api_errors.ApiExecutionError as e: responses.append(str(e)) return responses
def setUpClass(cls, mock_google_credential): """Set up.""" fake_global_configs = { 'compute': {'max_calls': 18, 'period': 1}} cls.gce_api_client = compute.ComputeClient( global_configs=fake_global_configs) cls.project_id = fake_compute.FAKE_PROJECT_ID
def compute_client(self): """A thread local instance of compute.ComputeClient. Returns: compute.ComputeClient: A Compute API client instance. """ if not hasattr(self._local, 'compute_client'): self._local.compute_client = compute.ComputeClient( self.global_configs, dry_run=self._dry_run) return self._local.compute_client
def __init__(self, project_id, global_configs=None, compute_client=None, dry_run=False, project_sema=None, max_running_operations=0): """Initialize. Args: project_id (str): The project id for the project to enforce. global_configs (dict): Global configurations. compute_client (ComputeClient): A Compute API client. If not provided, one will be created using the default credentials. dry_run (bool): Set to true to ensure no actual changes are made to the project. EnforcePolicy will still return a ProjectResult proto showing the changes that would have been made. project_sema (threading.BoundedSemaphore): An optional semaphore object, used to limit the number of concurrent projects getting written to. max_running_operations (int): [DEPRECATED] Used to limit the number of concurrent running operations on an API. """ self.project_id = project_id if not compute_client: compute_client = compute.ComputeClient(global_configs, dry_run=dry_run) self.compute_client = compute_client self.result = enforcer_log_pb2.ProjectResult() self.result.status = STATUS_UNSPECIFIED self.result.project_id = self.project_id self.result.timestamp_sec = date_time.get_utc_now_microtimestamp() self._dry_run = dry_run self._project_sema = project_sema if max_running_operations: LOGGER.warning( 'Max running operations is deprecated. Argument ignored.') self._operation_sema = None
def test_no_quota(self, mock_google_credential): """Verify no rate limiter is used if the configuration is missing.""" gce_api_client = compute.ComputeClient(global_configs={}) self.assertEqual(None, gce_api_client.repository._rate_limiter)
def setUpClass(cls, mock_google_credential): """Set up.""" fake_global_configs = {"compute": {"max_calls": 18, "period": 1}} cls.gce_api_client = compute.ComputeClient( global_configs=fake_global_configs, dry_run=True)