def test_inspect_not_found_ok(self): # Return a 404 Not found # The string we return just needs to end with " was not found", # which is what gcloud currently returns (subject to change) # and all we test for. error_response = st.CliResponseType(- 1, '', 'ERROR: (gcloud.preview.managed-instance-groups.describe)' ' The thing you requested was not found') gcloud = fake_gcloud_agent.FakeGCloudAgent( 'PROJECT', 'ZONE', default_response=error_response) contract_builder = gt.GceContractBuilder(gcloud) extra_args=['arg1', 'arg2', 'arg3'] c1 = contract_builder.new_clause_builder('TITLE') verifier = c1.inspect_resource( 'instances', 'test_name', extra_args=extra_args, no_resource_ok=True) verifier.contains_path_value('field', 'value') self.assertTrue(isinstance(verifier, jc.ValueObservationVerifierBuilder)) contract = contract_builder.build() verification_result = contract.verify() self.assertTrue(verification_result, JsonSnapshotHelper.ValueToEncodedJson(verification_result)) command = gcloud.build_gcloud_command_args( 'instances', ['describe', 'test_name'] + extra_args, project='PROJECT', zone='ZONE') self.assertEquals(command, gcloud.last_run_params)
def test_list(self): # Return a list of two objects -- a dictionary and an array. context = ExecutionContext() default_response = st.CliResponseType(0, '[{"field":"value"}, [1,2,3]]', '') gcloud = fake_gcloud_agent.FakeGCloudAgent( 'PROJECT', 'ZONE', default_response=default_response) contract_builder = gt.GCloudContractBuilder(gcloud) c1 = contract_builder.new_clause_builder('TITLE') extra_args = ['arg1', 'arg2', 'arg3'] verifier = c1.list_resources('instances', extra_args=extra_args) verifier.contains_path_value('field', 'value') self.assertTrue( isinstance(verifier, jc.ValueObservationVerifierBuilder)) # When we build and run the contract, it is going to call the observer. # The clause has no constraints so it will succeed. We do this so that # we can verify the contract will call the clause which in turn will # call the agent with the expected parameters we test for below. contract = contract_builder.build() self.assertTrue(contract.verify(context)) command = gcloud.build_gcloud_command_args('instances', ['list'] + extra_args, project='PROJECT') self.assertEquals(command, gcloud.last_run_params)
def test_empty_builder(self): default_response = st.CliResponseType(0, '', '') gcloud = fake_gcloud_agent.FakeGCloudAgent( 'PROJECT', 'ZONE', default_response=default_response) contract_builder = gt.GceContractBuilder(gcloud) contract = contract_builder.build() results = contract.verify() self.assertTrue(results)