class TestCredentialIdWithAuthIssuer(KnownLabelsBase, unittest2.TestCase): SUBJECT = _KNOWN.CREDENTIAL_ID GIVEN_INFO = report_request.Info( auth_issuer = u'dummy_issuer', auth_audience = u'dummy_audience') WANTED_VALUE = b'jwtAuth:issuer=' + base64.urlsafe_b64encode(b'dummy_issuer') WANTED_VALUE += b'&audience=' + base64.urlsafe_b64encode(b'dummy_audience') WANTED_LABEL_DICT = {SUBJECT.label_name: WANTED_VALUE}
def _make_dummy_report_request(project_id, service_name): rules = report_request.ReportingRules() info = report_request.Info(consumer_project_id=project_id, operation_id=u'an_op_id', operation_name=u'an_op_name', method=u'GET', referer=u'a_referer', service_name=service_name) return info.as_report_request(rules)
class TestProducerByConsumerErrorCountWithErrors(KnownMetricsBase, unittest2.TestCase): SUBJECT = _KNOWN.PRODUCER_BY_CONSUMER_ERROR_COUNT GIVEN_INFO = report_request.Info(response_code=401, api_key=KnownMetricsBase.TEST_API_KEY, api_key_valid=True, consumer_project_number=1234) WANTED_ADDED_METRICS = sc_messages.MetricValueSet( metricName=SUBJECT.metric_name, metricValues=[metric_value.create(int64Value=1)])
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 KnownLabelsBase(object): SUBJECT = None GIVEN_INFO = report_request.Info( api_method = u'dummy_method', api_version = u'dummy_version', location = u'dummy_location', referer = u'dummy_referer', consumer_project_number=1234) WANTED_LABEL_DICT = {} def _matching_descriptor(self, hide_default=False): res = sm_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)
u'error_cause': u'internal', }), timestamp=_START_OF_EPOCH_TIMESTAMP) _WANTED_USER_AGENT = label_descriptor.USER_AGENT _WANTED_SERVICE_AGENT = label_descriptor.SERVICE_AGENT _WANTED_PLATFORM = u'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=u'an_op_id', operation_name=u'an_op_name', method=u'GET', referer=u'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=u'an_op_id', operationName=u'an_op_name', startTime=_START_OF_EPOCH_TIMESTAMP, endTime=_START_OF_EPOCH_TIMESTAMP)), (report_request.Info(response_code=404, operation_id=u'an_op_id', operation_name=u'an_op_name', method=u'GET',
class TestCredentialIdWithApiKey(KnownLabelsBase, unittest2.TestCase): SUBJECT = _KNOWN.CREDENTIAL_ID GIVEN_INFO = report_request.Info( api_key = u'dummy_api_key', ) WANTED_LABEL_DICT = {SUBJECT.label_name: b'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: u'2'}