class TestProducerByConsumerErrorCountWithError(KnownMetricsBase,
                                                unittest2.TestCase):
    SUBJECT = _KNOWN.PRODUCER_BY_CONSUMER_ERROR_COUNT
    GIVEN_INFO = report_request.Info(response_code=401)
    WANTED_ADDED_METRICS = messages.MetricValueSet(
        metricName=SUBJECT.metric_name,
        metricValues=[metric_value.create(int64Value=1)])
class TestCredentialIdWithAuthIssuer(KnownLabelsBase, unittest2.TestCase):
    SUBJECT = _KNOWN.CREDENTIAL_ID
    GIVEN_INFO = report_request.Info(auth_issuer='dummy_issuer',
                                     auth_audience='dummy_audience')
    WANTED_VALUE = 'jwtAuth:issuer=' + base64.urlsafe_b64encode('dummy_issuer')
    WANTED_VALUE += '&audience=' + base64.urlsafe_b64encode('dummy_audience')
    WANTED_LABEL_DICT = {SUBJECT.label_name: WANTED_VALUE}
Пример #3
0
def _make_dummy_report_request(project_id, service_name):
    rules = report_request.ReportingRules()
    info = report_request.Info(consumer_project_id=project_id,
                               operation_id='an_op_id',
                               operation_name='an_op_name',
                               method='GET',
                               referer='a_referer',
                               service_name=service_name)
    return info.as_report_request(rules)
def _given_info(wanted_size, test_api_key, api_key_valid=True):
    return report_request.Info(
        request_size=wanted_size,
        response_size=wanted_size,
        request_time=datetime.timedelta(seconds=3),
        backend_time=datetime.timedelta(seconds=2),
        overhead_time=datetime.timedelta(seconds=1),
        api_key=test_api_key,
        api_key_valid=api_key_valid,
    )
class TestProducerErrorCountWithErrors(KnownMetricsBase, unittest2.TestCase):
    SUBJECT = _KNOWN.PRODUCER_ERROR_COUNT
    GIVEN_INFO = report_request.Info(
        response_code=401,
        api_key=KnownMetricsBase.TEST_API_KEY,
        api_key_valid=True,
    )
    WANTED_ADDED_METRICS = messages.MetricValueSet(
        metricName=SUBJECT.metric_name,
        metricValues=[metric_value.create(int64Value=1)])
class KnownMetricsBase(object):
    SUBJECT = None
    WANTED_ADDED_METRICS = []
    WANTED_SIZE = 7426  # arbitrary
    GIVEN_INFO = report_request.Info(
        request_size=WANTED_SIZE,
        response_size=WANTED_SIZE,
        request_time=datetime.timedelta(seconds=3),
        backend_time=datetime.timedelta(seconds=2),
        overhead_time=datetime.timedelta(seconds=1))

    def _base_operation(self):
        return messages.Operation(consumerId='project:project_id',
                                  operationId='an_op_id',
                                  operationName='an_op_name')

    def _wanted_operation(self):
        op = self._base_operation()
        if self.WANTED_ADDED_METRICS:
            op.metricValueSets.append(self.WANTED_ADDED_METRICS)
        return op

    def _matching_descriptor(self):
        return messages.MetricDescriptor(name=self.SUBJECT.metric_name,
                                         metricKind=self.SUBJECT.kind,
                                         valueType=self.SUBJECT.value_type)

    def _not_matched(self):
        d = self._matching_descriptor()
        d.metricKind = MetricKind.METRIC_KIND_UNSPECIFIED
        return d

    def test_should_be_supported(self):
        expect(_KNOWN.is_supported(self._matching_descriptor())).to(be_true)
        expect(_KNOWN.is_supported(self._not_matched())).not_to(be_true)

    def test_should_be_matched_correctly(self):
        expect(self.SUBJECT.matches(self._matching_descriptor())).to(be_true)
        expect(self.SUBJECT.matches(self._not_matched())).not_to(be_true)

    def test_should_update_request_info(self):
        an_op = self._base_operation()
        wanted_op = self._wanted_operation()
        self.SUBJECT.do_operation_update(self.GIVEN_INFO, an_op)
        expect(an_op).to(equal(wanted_op))
class KnownLabelsBase(object):
    SUBJECT = None
    GIVEN_INFO = report_request.Info(
        api_method = 'dummy_method',
        api_version = 'dummy_version',
        location = 'dummy_location',
        referer = 'dummy_referer'
    )
    WANTED_LABEL_DICT = {}

    def _matching_descriptor(self, hide_default=False):
        res = messages.LabelDescriptor(
            key=self.SUBJECT.label_name,
            valueType=self.SUBJECT.value_type)
        if res.valueType == ValueType.STRING and hide_default:
            res.valueType = None
        return res

    def _not_matched(self):
        d = self._matching_descriptor()
        d.valueType = ValueType.INT64  # no known labels have this type
        return d

    def test_should_be_supported(self):
        expect(_KNOWN.is_supported(self._matching_descriptor())).to(be_true)
        expect(_KNOWN.is_supported(
            self._matching_descriptor(hide_default=True))).to(be_true)
        expect(_KNOWN.is_supported(self._not_matched())).not_to(be_true)

    def test_should_be_matched_correctly(self):
        expect(self.SUBJECT.matches(self._matching_descriptor())).to(be_true)
        expect(self.SUBJECT.matches(
            self._matching_descriptor(hide_default=True))).to(be_true)
        expect(self.SUBJECT.matches(self._not_matched())).not_to(be_true)

    def test_should_update_request_info(self):
        given_dict = {}
        self.SUBJECT.do_labels_update(self.GIVEN_INFO, given_dict)
        expect(given_dict).to(equal(self.WANTED_LABEL_DICT))
 def test_should_fail_as_report_request_on_incomplete_info(self):
     timer = _DateTimeTimer()
     incomplete = report_request.Info()  # has no service_name
     rules = report_request.ReportingRules()
     testf = lambda: incomplete.as_report_request(rules, timer=timer)
     expect(testf).to(raise_error(ValueError))
 def test_should_raise_if_constructed_with_a_bad_error_cause(self):
     testf = lambda: report_request.Info(error_cause=object())
     expect(testf).to(raise_error(ValueError))
 def test_should_raise_if_constructed_with_a_bad_protocol(self):
     testf = lambda: report_request.Info(protocol=object())
     # not a report_request.ReportedProtocols
     expect(testf).to(raise_error(ValueError))
 def test_should_construct_with_no_args(self):
     expect(report_request.Info()).not_to(be_none)
            'error_cause': 'internal',
        }),
    timestamp=_START_OF_EPOCH_TIMESTAMP)

_WANTED_USER_AGENT = label_descriptor.USER_AGENT
_WANTED_SERVICE_AGENT = label_descriptor.SERVICE_AGENT
_WANTED_PLATFORM = 'Unknown'

_EXPECTED_OK_METRIC = metric_descriptor.KnownMetrics.CONSUMER_REQUEST_COUNT
_EXPECTED_NOK_METRIC = metric_descriptor.KnownMetrics.CONSUMER_ERROR_COUNT
_ADD_LOG_TESTS = [
    (report_request.Info(operation_id='an_op_id',
                         operation_name='an_op_name',
                         method='GET',
                         referer='a_referer',
                         backend_time=_TEST_LATENCY,
                         overhead_time=_TEST_LATENCY,
                         request_time=_TEST_LATENCY,
                         request_size=_TEST_SIZE,
                         response_size=_TEST_SIZE,
                         service_name=_TEST_SERVICE_NAME),
     messages.Operation(
         importance=messages.Operation.ImportanceValueValuesEnum.LOW,
         logEntries=[_EXPECTED_OK_LOG_ENTRY],
         operationId='an_op_id',
         operationName='an_op_name',
         startTime=_START_OF_EPOCH_TIMESTAMP,
         endTime=_START_OF_EPOCH_TIMESTAMP)),
    (report_request.Info(response_code=404,
                         operation_id='an_op_id',
                         operation_name='an_op_name',
                         method='GET',
class TestCredentialIdWithApiKey(KnownLabelsBase, unittest2.TestCase):
    SUBJECT = _KNOWN.CREDENTIAL_ID
    GIVEN_INFO = report_request.Info(
        api_key = 'dummy_api_key',
    )
    WANTED_LABEL_DICT = {SUBJECT.label_name: 'apiKey:dummy_api_key'}
class StatusCodeWithUnknownStatus(KnownLabelsBase, unittest2.TestCase):
    SUBJECT = _KNOWN.STATUS_CODE
    GIVEN_INFO = report_request.Info(
        response_code = 777,
    )
    WANTED_LABEL_DICT = {SUBJECT.label_name: '2'}