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(['wanted_log'])))
        expect(rules.metrics).to(equal(self.WANTED_METRICS))
        expect(rules.labels).to(equal(self.WANTED_LABELS))


_TEST_CONSUMER_ID = 'testConsumerID'
_TEST_OP1_NAME = 'testOp1'
_TEST_OP2_NAME = '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 = 'a_service_name'
_TEST_SIZE = 1
_TEST_LATENCY = datetime.timedelta(seconds=7)
_EXPECTED_OK_LOG_ENTRY = messages.LogEntry(
    name='endpoints-log',
    severity=messages.LogEntry.SeverityValueValuesEnum.INFO,
    structPayload=encoding.PyValueToMessage(
        messages.LogEntry.StructPayloadValue, {
            'http_response_code': 200,
            'http_method': 'GET',
            'request_latency_in_ms': 7000.0,
            'timestamp': time.mktime(_START_OF_EPOCH.timetuple()),
            'response_size': 1,
            'request_size': 1,
            'referer': 'a_referer',
_TEST_OP_NAME = 'testOperationName'


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
_START_OF_EPOCH = timestamp.to_rfc3339(datetime.datetime(1970, 1, 1, 0, 0, 0))
_TEST_SERVICE_NAME = 'a_service_name'
_INFO_TESTS = [
    (check_request.Info(operation_id='an_op_id',
                        operation_name='an_op_name',
                        referer='a_referer',
                        service_name=_TEST_SERVICE_NAME),
     messages.Operation(
         importance=messages.Operation.ImportanceValueValuesEnum.LOW,
         labels=encoding.PyValueToMessage(
             messages.Operation.LabelsValue, {
                 'servicecontrol.googleapis.com/user_agent':
                 _WANTED_USER_AGENT,
                 'servicecontrol.googleapis.com/referer': 'a_referer'
             }),
         operationId='an_op_id',
示例#3
0
# distributed under the License is distributed on an "AS IS" BASIS,
# 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

import datetime
import unittest2
from expects import be_none, expect, equal, raise_error

from google.api.control import messages, metric_value, operation, timestamp
from google.api.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 = {
    'key1': 'value1',
    'key2': 'value2',
}

# in tests, the description field is not currently used, but should be filled
_TESTS = [{
    'description':
    'update the start time to that of the earliest',
    'kinds':
    None,
            logs=logs,
            label_names=label_names,
            metric_names=metric_names
        )
        expect(rules).not_to(be_none)
        expect(rules.logs).to(equal(set(['wanted_log'])))
        expect(rules.metrics).to(equal(self.WANTED_METRICS))
        expect(rules.labels).to(equal(self.WANTED_LABELS))


_TEST_CONSUMER_ID = 'testConsumerID'
_TEST_OP1_NAME = 'testOp1'
_TEST_OP2_NAME = '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 = 'a_service_name'
_TEST_SIZE=1
_TEST_LATENCY=datetime.timedelta(seconds=7)
_EXPECTED_OK_LOG_ENTRY = messages.LogEntry(
    name = 'endpoints-log',
    severity = messages.LogEntry.SeverityValueValuesEnum.INFO,
    structPayload=encoding.PyValueToMessage(
        messages.LogEntry.StructPayloadValue, {
            'http_response_code': 200,
            'http_method': 'GET',
            'request_latency_in_ms': 7000.0,
            'timestamp': time.mktime(_START_OF_EPOCH.timetuple()),
            'response_size': 1,
            'request_size': 1,
            'referer': 'a_referer',
示例#5
0
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 = {
        'key1': 'value1',
        'key2': '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='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='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))
# distributed under the License is distributed on an "AS IS" BASIS,
# 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

import datetime
import unittest2
from expects import be_none, expect, equal, raise_error

from google.api.control import messages, metric_value, operation, timestamp
from google.api.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 = {
    'key1': 'value1',
    'key2': 'value2',
}

# in tests, the description field is not currently used, but should be filled
_TESTS = [
    {
        'description': 'update the start time to that of the earliest',
        'kinds': None,
        'initial': messages.Operation(
示例#7
0
 def test_should_fail_on_invalid_input(self):
     testf = lambda: timestamp.to_rfc3339('this will not work')
     expect(testf).to(raise_error(ValueError))
示例#8
0
 def test_should_converts_correctly(self):
     for t in self.TESTS:
         expect(timestamp.to_rfc3339(t[0])).to(equal(t[1]))
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
_START_OF_EPOCH = timestamp.to_rfc3339(datetime.datetime(1970, 1, 1, 0, 0, 0))
_TEST_SERVICE_NAME = 'a_service_name'
_INFO_TESTS = [
    (check_request.Info(
        operation_id='an_op_id',
        operation_name='an_op_name',
        referer='a_referer',
        service_name=_TEST_SERVICE_NAME),
     messages.Operation(
         importance=messages.Operation.ImportanceValueValuesEnum.LOW,
         labels = encoding.PyValueToMessage(
             messages.Operation.LabelsValue, {
                 'servicecontrol.googleapis.com/user_agent': _WANTED_USER_AGENT,
                 'servicecontrol.googleapis.com/referer': 'a_referer'
             }),
         operationId='an_op_id',
示例#10
0
 def test_should_fail_on_invalid_input(self):
     testf = lambda: timestamp.to_rfc3339('this will not work')
     expect(testf).to(raise_error(ValueError))
示例#11
0
 def test_should_converts_correctly(self):
     for t in self.TESTS:
         expect(timestamp.to_rfc3339(t[0])).to(equal(t[1]))