def _make_test_request(service_name, importance=None): if importance is None: importance = messages.Operation.ImportanceValueValuesEnum.LOW op = messages.Operation(consumerId=_TEST_CONSUMER_ID, operationName=_TEST_OP_NAME, importance=importance) check_request = messages.CheckRequest(operation=op) return messages.ServicecontrolServicesCheckRequest( serviceName=service_name, checkRequest=check_request) _WANTED_USER_AGENT = label_descriptor.USER_AGENT _WANTED_SERVICE_AGENT = label_descriptor.SERVICE_AGENT _START_OF_EPOCH = timestamp.to_rfc3339(datetime.datetime(1970, 1, 1, 0, 0, 0)) _TEST_SERVICE_NAME = u'a_service_name' _INFO_TESTS = [ (check_request.Info(operation_id=u'an_op_id', operation_name=u'an_op_name', referer=u'a_referer', service_name=_TEST_SERVICE_NAME), messages.Operation( importance=messages.Operation.ImportanceValueValuesEnum.LOW, labels=encoding.PyValueToMessage( messages.Operation.LabelsValue, { u'servicecontrol.googleapis.com/user_agent': _WANTED_USER_AGENT, u'servicecontrol.googleapis.com/referer': u'a_referer', u'servicecontrol.googleapis.com/service_agent':
metric_names = [x.metric_name for x in self.WANTED_METRICS] rules = self.subject_cls.from_known_inputs(logs=logs, label_names=label_names, metric_names=metric_names) expect(rules).not_to(be_none) expect(rules.logs).to(equal(set([u'wanted_log']))) expect(rules.metrics).to(equal(self.WANTED_METRICS)) expect(rules.labels).to(equal(self.WANTED_LABELS)) _TEST_CONSUMER_ID = u'testConsumerID' _TEST_OP1_NAME = u'testOp1' _TEST_OP2_NAME = u'testOp2' _WANTED_USER_AGENT = label_descriptor.USER_AGENT _START_OF_EPOCH = datetime.datetime.utcfromtimestamp(0) _START_OF_EPOCH_TIMESTAMP = timestamp.to_rfc3339(_START_OF_EPOCH) _TEST_SERVICE_NAME = u'a_service_name' _TEST_SIZE = 1 _TEST_LATENCY = datetime.timedelta(seconds=7) _EXPECTED_OK_LOG_ENTRY = messages.LogEntry( name=u'endpoints-log', severity=messages.LogEntry.SeverityValueValuesEnum.INFO, structPayload=encoding.PyValueToMessage( messages.LogEntry.StructPayloadValue, { u'http_response_code': 200, u'http_method': u'GET', u'request_latency_in_ms': 7000.0, u'timestamp': time.mktime(_START_OF_EPOCH.timetuple()), u'response_size': 1, u'request_size': 1, u'referer': u'a_referer',
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from __future__ import absolute_import from builtins import object import datetime import unittest2 from expects import be_none, expect, equal, raise_error from endpoints_management.control import messages, metric_value, operation, timestamp from endpoints_management.control import MetricKind _A_FLOAT_VALUE = 1.1 _REALLY_EARLY = timestamp.to_rfc3339(datetime.datetime(1970, 1, 1, 0, 0, 0)) _EARLY = timestamp.to_rfc3339(datetime.datetime(1980, 1, 1, 10, 0, 0)) _LATER = timestamp.to_rfc3339(datetime.datetime(1980, 2, 2, 10, 0, 0)) _LATER_STILL = timestamp.to_rfc3339(datetime.datetime(1981, 2, 2, 10, 0, 0)) _TEST_LABELS = { u'key1': u'value1', u'key2': u'value2', } # in tests, the description field is not currently used, but should be filled _TESTS = [ { u'description': u'update the start time to that of the earliest', u'kinds': None, u'initial': messages.Operation(
logs=logs, label_names=label_names, metric_names=metric_names ) expect(rules).not_to(be_none) expect(rules.logs).to(equal(set([u'wanted_log']))) expect(rules.metrics).to(equal(self.WANTED_METRICS)) expect(rules.labels).to(equal(self.WANTED_LABELS)) _TEST_CONSUMER_ID = u'testConsumerID' _TEST_OP1_NAME = u'testOp1' _TEST_OP2_NAME = u'testOp2' _WANTED_USER_AGENT = label_descriptor.USER_AGENT _START_OF_EPOCH = datetime.datetime.utcfromtimestamp(0) _START_OF_EPOCH_TIMESTAMP = timestamp.to_rfc3339(_START_OF_EPOCH) _TEST_SERVICE_NAME = u'a_service_name' _TEST_SIZE=1 _TEST_LATENCY=datetime.timedelta(seconds=7) _EXPECTED_OK_LOG_ENTRY = messages.LogEntry( name = u'endpoints-log', severity = messages.LogEntry.SeverityValueValuesEnum.INFO, structPayload=encoding.PyValueToMessage( messages.LogEntry.StructPayloadValue, { u'http_response_code': 200, u'http_method': u'GET', u'request_latency_in_ms': 7000.0, u'timestamp': time.mktime(_START_OF_EPOCH.timetuple()), u'response_size': 1, u'request_size': 1, u'referer': u'a_referer',
if importance is None: importance = sc_messages.Operation.ImportanceValueValuesEnum.LOW op = sc_messages.QuotaOperation( consumerId=_TEST_CONSUMER_ID, methodName=_TEST_OP_NAME, operationId=operation_id, ) quota_request = sc_messages.AllocateQuotaRequest(allocateOperation=op) return sc_messages.ServicecontrolServicesAllocateQuotaRequest( serviceName=service_name, allocateQuotaRequest=quota_request) _WANTED_USER_AGENT = label_descriptor.USER_AGENT _WANTED_SERVICE_AGENT = label_descriptor.SERVICE_AGENT _START_OF_EPOCH = timestamp.to_rfc3339(datetime.datetime(1970, 1, 1, 0, 0, 0)) _TEST_SERVICE_NAME = u'a_service_name' _INFO_TESTS = [ (quota_request.Info( operation_id=u'an_op_id', operation_name=u'an_op_name', referer=u'a_referer', service_name=_TEST_SERVICE_NAME), sc_messages.QuotaOperation( labels = encoding.PyValueToMessage( sc_messages.QuotaOperation.LabelsValue, { u'servicecontrol.googleapis.com/referer': u'a_referer', }), operationId=u'an_op_id', methodName=u'an_op_name', quotaMode=sc_messages.QuotaOperation.QuotaModeValueValuesEnum.BEST_EFFORT)),
class TestMerge(unittest2.TestCase): A_FLOAT_VALUE = 1.0 EARLY = timestamp.to_rfc3339(datetime.datetime(1970, 1, 1, 10, 0, 0)) LATER = timestamp.to_rfc3339(datetime.datetime(1990, 1, 1, 10, 0, 0)) TEST_LABELS = { u'key1': u'value1', u'key2': u'value2', } def setUp(self): self.test_value = metric_value.create(labels=self.TEST_LABELS, doubleValue=self.A_FLOAT_VALUE) self.early_ending = metric_value.create(labels=self.TEST_LABELS, doubleValue=self.A_FLOAT_VALUE, endTime=self.EARLY) self.late_ending = metric_value.create(labels=self.TEST_LABELS, doubleValue=self.A_FLOAT_VALUE, endTime=self.LATER) self.test_value_with_money = metric_value.create( labels=self.TEST_LABELS, moneyValue=messages.Money(currencyCode=u'JPY', units=100, nanos=0)) def test_should_fail_for_metric_values_with_different_types(self): changed = metric_value.create(labels=self.TEST_LABELS, int64Value=1) for kind in (MetricKind.GAUGE, MetricKind.CUMULATIVE, MetricKind.DELTA): testf = lambda: metric_value.merge(kind, self.test_value, changed) expect(testf).to(raise_error(ValueError)) def test_should_fail_for_uninitialized_metric_values(self): no_init = metric_value.create() for kind in (MetricKind.GAUGE, MetricKind.CUMULATIVE, MetricKind.DELTA): testf = lambda: metric_value.merge(kind, no_init, no_init) expect(testf).to(raise_error(ValueError)) def test_should_fail_for_delta_metrics_with_unmergable_types(self): no_init = metric_value.create() unmergeables = [ metric_value.create(stringValue=u'a test string'), metric_value.create(boolValue=False), ] for mv in unmergeables: testf = lambda: metric_value.merge(MetricKind.DELTA, mv, mv) expect(testf).to(raise_error(ValueError)) def test_should_succeed_for_delta_metrics_with_the_money_type(self): v = self.test_value_with_money want = 2 * v.moneyValue.units got = metric_value.merge(MetricKind.DELTA, v, v) expect(got.moneyValue.units).to(equal(want)) def test_should_succeed_for_delta_metrics_with_the_double_type(self): v = self.test_value want = 2 * v.doubleValue got = metric_value.merge(MetricKind.DELTA, v, v) expect(got.doubleValue).to(equal(want)) def test_should_succeed_for_delta_metrics_with_the_int64_type(self): test_int = 4 v = metric_value.create(labels=self.TEST_LABELS, int64Value=test_int) want = 2 * test_int got = metric_value.merge(MetricKind.DELTA, v, v) expect(got.int64Value).to(equal(want)) def test_should_succeed_for_delta_metrics_with_the_distribution_type(self): test_distribution = distribution.create_explicit([0.1, 0.3, 0.5]) distribution.add_sample(0.4, test_distribution) v = metric_value.create(labels=self.TEST_LABELS, distributionValue=test_distribution) want = 2 * test_distribution.count got = metric_value.merge(MetricKind.DELTA, v, v) expect(got.distributionValue.count).to(equal(want)) def test_should_return_metric_value_with_latest_end_time_for_non_deltas( self): for kind in (MetricKind.GAUGE, MetricKind.CUMULATIVE): got = metric_value.merge(kind, self.early_ending, self.late_ending) expect(got).to(equal(self.late_ending)) got = metric_value.merge(kind, self.late_ending, self.early_ending) expect(got).to(equal(self.late_ending)) def test_should_use_the_latest_end_time_delta_merges(self): got = metric_value.merge(MetricKind.DELTA, self.early_ending, self.late_ending) expect(got.endTime).to(equal(self.late_ending.endTime)) got = metric_value.merge(MetricKind.DELTA, self.late_ending, self.early_ending) expect(got.endTime).to(equal(self.late_ending.endTime)) def test_should_use_the_earliest_start_time_in_delta_merges(self): early_starting = metric_value.create(labels=self.TEST_LABELS, doubleValue=self.A_FLOAT_VALUE, startTime=self.EARLY) late_starting = metric_value.create(labels=self.TEST_LABELS, doubleValue=self.A_FLOAT_VALUE, startTime=self.LATER) got = metric_value.merge(MetricKind.DELTA, early_starting, late_starting) expect(got.startTime).to(equal(early_starting.startTime)) got = metric_value.merge(MetricKind.DELTA, late_starting, early_starting) expect(got.startTime).to(equal(early_starting.startTime))
def test_should_fail_on_invalid_input(self): testf = lambda: timestamp.to_rfc3339(u'this will not work') expect(testf).to(raise_error(ValueError))
def test_should_converts_correctly(self): for t in self.TESTS: expect(timestamp.to_rfc3339(t[0])).to(equal(t[1]))