Beispiel #1
0
  def test_inspect_not_found_ok(self):
    context = ExecutionContext()
    response = Mock()
    response.status = 404
    response.reason = 'Not Found'
    default_variables = {'project': 'PROJECT'}
    service = MyFakeGcpService([HttpError(response, 'Not Found')])
    agent = TestGcpAgent.make_test_agent(
        service=service, default_variables=default_variables)


    contract_builder = gt.GcpContractBuilder(agent)

    c1 = contract_builder.new_clause_builder('TITLE')
    verifier = c1.inspect_resource(
        'regions', resource_id='us-central1-f', no_resource_ok=True)
    verifier.contains_path_value('field', 'value')
    self.assertTrue(isinstance(verifier, jc.ValueObservationVerifierBuilder))

    contract = contract_builder.build()
    verification_result = contract.verify(context)
    self.assertTrue(verification_result,
                    JsonSnapshotHelper.ValueToEncodedJson(verification_result))
    self.assertEquals({'project': 'PROJECT', 'region': 'us-central1-f'},
                      agent.service.last_get_args)
Beispiel #2
0
    def test_inspect_indirect(self):
        context = ExecutionContext(test_id='TESTID', test_project='PROJECT')
        default_variables = {
            'project': lambda x: x.get('test_project', 'UNKNOWN')
        }
        service = MyFakeGcpService(['Hello, World'])
        agent = TestGcpAgent.make_test_agent(
            service=service, default_variables=default_variables)

        contract_builder = gt.GcpContractBuilder(agent)

        c1 = contract_builder.new_clause_builder('TITLE')
        verifier = c1.inspect_resource('regions',
                                       resource_id=lambda x: x['test_id'],
                                       no_resource_ok=True)
        verifier.contains_path_value('', 'Hello, World')
        self.assertTrue(
            isinstance(verifier, jc.ValueObservationVerifierBuilder))

        contract = contract_builder.build()
        verification_result = contract.verify(context)
        self.assertTrue(
            verification_result,
            JsonSnapshotHelper.ValueToEncodedJson(verification_result))
        self.assertEquals({
            'project': 'PROJECT',
            'region': 'TESTID'
        }, agent.service.last_get_args)
Beispiel #3
0
  def test_list(self):
    context = ExecutionContext()
    default_variables = {'project': 'PROJECT'}
    service = MyFakeGcpService([{'items': [1, 2, 3]}])
    agent = TestGcpAgent.make_test_agent(
        service=service, default_variables=default_variables)

    contract_builder = gt.GcpContractBuilder(agent)
    c1 = contract_builder.new_clause_builder('TITLE')
    verifier = c1.list_resource('regions')
    verifier.contains_path_value(jp.DONT_ENUMERATE_TERMINAL, [1, 2, 3])

    # 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))
    self.assertEquals({'project': 'PROJECT'}, agent.service.last_list_args)
Beispiel #4
0
  def test_inspect_indirect(self):
    context = ExecutionContext(test_id='TESTID', test_project='PROJECT')
    default_variables = {'project': lambda x: x.get('test_project', 'UNKNOWN')}
    service = MyFakeGcpService(['Hello, World'])
    agent = TestGcpAgent.make_test_agent(
        service=service, default_variables=default_variables)

    contract_builder = gt.GcpContractBuilder(agent)

    c1 = contract_builder.new_clause_builder('TITLE')
    verifier = c1.inspect_resource(
        'regions', resource_id=lambda x: x['test_id'],
        no_resource_ok=True)
    verifier.contains_path_value('', 'Hello, World')
    self.assertTrue(isinstance(verifier, jc.ValueObservationVerifierBuilder))

    contract = contract_builder.build()
    verification_result = contract.verify(context)
    self.assertTrue(verification_result,
                    JsonSnapshotHelper.ValueToEncodedJson(verification_result))
    self.assertEquals({'project': 'PROJECT', 'region': 'TESTID'},
                      agent.service.last_get_args)
Beispiel #5
0
 def testing_agent(self):
   return TestGcpAgent.make_test_agent()