OLD | NEW |
(Empty) | |
| 1 # Copyright 2016 Google Inc. All Rights Reserved. |
| 2 # |
| 3 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 # you may not use this file except in compliance with the License. |
| 5 # You may obtain a copy of the License at |
| 6 # |
| 7 # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 # |
| 9 # Unless required by applicable law or agreed to in writing, software |
| 10 # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 # See the License for the specific language governing permissions and |
| 13 # limitations under the License. |
| 14 |
| 15 from __future__ import absolute_import |
| 16 |
| 17 import datetime |
| 18 import unittest2 |
| 19 from expects import be_none, be_true, expect, equal, raise_error |
| 20 |
| 21 import google.api.gen.servicecontrol_v1_messages as messages |
| 22 from google.api.control import (distribution, metric_descriptor, timestamp, Metr
icKind, |
| 23 ValueType) |
| 24 from google.api.control import metric_value, operation, report_request |
| 25 |
| 26 |
| 27 _KNOWN = metric_descriptor.KnownMetrics |
| 28 |
| 29 |
| 30 def _wanted_distribution_with_sample(value, *args): |
| 31 d = distribution.create_exponential(*args) |
| 32 distribution.add_sample(value, d) |
| 33 return d |
| 34 |
| 35 |
| 36 def _given_info(wanted_size, test_api_key, api_key_valid=True): |
| 37 return report_request.Info( |
| 38 request_size=wanted_size, |
| 39 response_size=wanted_size, |
| 40 request_time=datetime.timedelta(seconds=3), |
| 41 backend_time=datetime.timedelta(seconds=2), |
| 42 overhead_time=datetime.timedelta(seconds=1), |
| 43 api_key=test_api_key, |
| 44 api_key_valid=api_key_valid, |
| 45 ) |
| 46 |
| 47 |
| 48 class KnownMetricsBase(object): |
| 49 SUBJECT = None |
| 50 WANTED_ADDED_METRICS = [] |
| 51 WANTED_SIZE = 7426 # arbitrary |
| 52 TEST_API_KEY = 'test_key' |
| 53 GIVEN_INFO = _given_info(WANTED_SIZE, TEST_API_KEY, api_key_valid=True) |
| 54 |
| 55 def _base_operation(self): |
| 56 return messages.Operation( |
| 57 consumerId='project:project_id', |
| 58 operationId='an_op_id', |
| 59 operationName='an_op_name') |
| 60 |
| 61 def _wanted_operation(self): |
| 62 op = self._base_operation() |
| 63 if self.WANTED_ADDED_METRICS: |
| 64 op.metricValueSets.append(self.WANTED_ADDED_METRICS) |
| 65 return op |
| 66 |
| 67 def _matching_descriptor(self): |
| 68 return messages.MetricDescriptor( |
| 69 name=self.SUBJECT.metric_name, |
| 70 metricKind=self.SUBJECT.kind, |
| 71 valueType=self.SUBJECT.value_type) |
| 72 |
| 73 def _not_matched(self): |
| 74 d = self._matching_descriptor() |
| 75 d.metricKind = MetricKind.METRIC_KIND_UNSPECIFIED |
| 76 return d |
| 77 |
| 78 def test_should_be_supported(self): |
| 79 expect(_KNOWN.is_supported(self._matching_descriptor())).to(be_true) |
| 80 expect(_KNOWN.is_supported(self._not_matched())).not_to(be_true) |
| 81 |
| 82 def test_should_be_matched_correctly(self): |
| 83 expect(self.SUBJECT.matches(self._matching_descriptor())).to(be_true) |
| 84 expect(self.SUBJECT.matches(self._not_matched())).not_to(be_true) |
| 85 |
| 86 def test_should_update_request_info(self): |
| 87 an_op = self._base_operation() |
| 88 wanted_op = self._wanted_operation() |
| 89 self.SUBJECT.do_operation_update(self.GIVEN_INFO, an_op) |
| 90 expect(an_op).to(equal(wanted_op)) |
| 91 |
| 92 |
| 93 class KnownMetricsInvalidApiKey(KnownMetricsBase): |
| 94 GIVEN_INFO = _given_info(KnownMetricsBase.WANTED_SIZE, |
| 95 KnownMetricsBase.TEST_API_KEY, |
| 96 api_key_valid=False) |
| 97 |
| 98 |
| 99 class TestConsumerRequestCount(KnownMetricsBase, unittest2.TestCase): |
| 100 SUBJECT = _KNOWN.CONSUMER_REQUEST_COUNT |
| 101 WANTED_ADDED_METRICS = messages.MetricValueSet( |
| 102 metricName=SUBJECT.metric_name, |
| 103 metricValues=[metric_value.create(int64Value=1)]) |
| 104 |
| 105 |
| 106 class TestConsumerRequestCountInvalidApiKey(KnownMetricsInvalidApiKey, |
| 107 unittest2.TestCase): |
| 108 SUBJECT = _KNOWN.CONSUMER_REQUEST_COUNT |
| 109 |
| 110 |
| 111 class TestProducerRequestCount(KnownMetricsBase, unittest2.TestCase): |
| 112 SUBJECT = _KNOWN.PRODUCER_REQUEST_COUNT |
| 113 WANTED_ADDED_METRICS = messages.MetricValueSet( |
| 114 metricName=SUBJECT.metric_name, |
| 115 metricValues=[metric_value.create(int64Value=1)]) |
| 116 |
| 117 |
| 118 class TestConsumerRequestSizes(KnownMetricsBase, unittest2.TestCase): |
| 119 SUBJECT = _KNOWN.CONSUMER_REQUEST_SIZES |
| 120 WANTED_DISTRIBUTION = _wanted_distribution_with_sample( |
| 121 KnownMetricsBase.WANTED_SIZE, |
| 122 *metric_descriptor._SIZE_DISTRIBUTION_ARGS) |
| 123 WANTED_ADDED_METRICS = messages.MetricValueSet( |
| 124 metricName=SUBJECT.metric_name, |
| 125 metricValues=[metric_value.create(distributionValue=WANTED_DISTRIBUTION)
]) |
| 126 |
| 127 |
| 128 class TestConsumerRequestSizesInvalidApiKey(KnownMetricsInvalidApiKey, |
| 129 unittest2.TestCase): |
| 130 SUBJECT = _KNOWN.CONSUMER_REQUEST_SIZES |
| 131 |
| 132 |
| 133 class TestProducerRequestSizes(KnownMetricsBase, unittest2.TestCase): |
| 134 SUBJECT = _KNOWN.PRODUCER_REQUEST_SIZES |
| 135 WANTED_DISTRIBUTION = _wanted_distribution_with_sample( |
| 136 KnownMetricsBase.WANTED_SIZE, |
| 137 *metric_descriptor._SIZE_DISTRIBUTION_ARGS) |
| 138 WANTED_ADDED_METRICS = messages.MetricValueSet( |
| 139 metricName=SUBJECT.metric_name, |
| 140 metricValues=[metric_value.create(distributionValue=WANTED_DISTRIBUTION)
]) |
| 141 |
| 142 |
| 143 class TestConsumerResponseSizes(KnownMetricsBase, unittest2.TestCase): |
| 144 SUBJECT = _KNOWN.CONSUMER_RESPONSE_SIZES |
| 145 WANTED_DISTRIBUTION = _wanted_distribution_with_sample( |
| 146 KnownMetricsBase.WANTED_SIZE, |
| 147 *metric_descriptor._SIZE_DISTRIBUTION_ARGS) |
| 148 WANTED_ADDED_METRICS = messages.MetricValueSet( |
| 149 metricName=SUBJECT.metric_name, |
| 150 metricValues=[metric_value.create(distributionValue=WANTED_DISTRIBUTION)
]) |
| 151 |
| 152 |
| 153 class TestConsumerResponseSizesInvalidApiKey(KnownMetricsInvalidApiKey, |
| 154 unittest2.TestCase): |
| 155 SUBJECT = _KNOWN.CONSUMER_RESPONSE_SIZES |
| 156 |
| 157 |
| 158 class TestProducerResponseSizes(KnownMetricsBase, unittest2.TestCase): |
| 159 SUBJECT = _KNOWN.PRODUCER_RESPONSE_SIZES |
| 160 WANTED_DISTRIBUTION = _wanted_distribution_with_sample( |
| 161 KnownMetricsBase.WANTED_SIZE, |
| 162 *metric_descriptor._SIZE_DISTRIBUTION_ARGS) |
| 163 WANTED_ADDED_METRICS = messages.MetricValueSet( |
| 164 metricName=SUBJECT.metric_name, |
| 165 metricValues=[metric_value.create(distributionValue=WANTED_DISTRIBUTION)
]) |
| 166 |
| 167 |
| 168 class TestConsumerErrorCountNoError(KnownMetricsBase, unittest2.TestCase): |
| 169 SUBJECT = _KNOWN.CONSUMER_ERROR_COUNT |
| 170 |
| 171 |
| 172 class TestConsumerErrorCountWithErrors(KnownMetricsBase, unittest2.TestCase): |
| 173 SUBJECT = _KNOWN.CONSUMER_ERROR_COUNT |
| 174 GIVEN_INFO = report_request.Info( |
| 175 response_code=401, |
| 176 api_key=KnownMetricsBase.TEST_API_KEY, |
| 177 api_key_valid=True, |
| 178 ) |
| 179 WANTED_ADDED_METRICS = messages.MetricValueSet( |
| 180 metricName=SUBJECT.metric_name, |
| 181 metricValues=[metric_value.create(int64Value=1)]) |
| 182 |
| 183 |
| 184 class TestConsumerErrorCountInvalidApiKey(KnownMetricsInvalidApiKey, |
| 185 unittest2.TestCase): |
| 186 SUBJECT = _KNOWN.CONSUMER_ERROR_COUNT |
| 187 |
| 188 |
| 189 class TestProducerErrorCountNoError(KnownMetricsBase, unittest2.TestCase): |
| 190 SUBJECT = _KNOWN.PRODUCER_ERROR_COUNT |
| 191 |
| 192 |
| 193 class TestProducerErrorCountWithErrors(KnownMetricsBase, unittest2.TestCase): |
| 194 SUBJECT = _KNOWN.PRODUCER_ERROR_COUNT |
| 195 GIVEN_INFO = report_request.Info( |
| 196 response_code=401, |
| 197 api_key=KnownMetricsBase.TEST_API_KEY, |
| 198 api_key_valid=True, |
| 199 ) |
| 200 WANTED_ADDED_METRICS = messages.MetricValueSet( |
| 201 metricName=SUBJECT.metric_name, |
| 202 metricValues=[metric_value.create(int64Value=1)]) |
| 203 |
| 204 |
| 205 class TestConsumerTotalLatencies(KnownMetricsBase, unittest2.TestCase): |
| 206 SUBJECT = _KNOWN.CONSUMER_TOTAL_LATENCIES |
| 207 WANTED_DISTRIBUTION = _wanted_distribution_with_sample( |
| 208 KnownMetricsBase.GIVEN_INFO.request_time.seconds, |
| 209 *metric_descriptor._TIME_DISTRIBUTION_ARGS) |
| 210 WANTED_ADDED_METRICS = messages.MetricValueSet( |
| 211 metricName=SUBJECT.metric_name, |
| 212 metricValues=[metric_value.create(distributionValue=WANTED_DISTRIBUTION)
]) |
| 213 |
| 214 |
| 215 class TestConsumerTotalLatenciesInvalidApiKey(KnownMetricsInvalidApiKey, |
| 216 unittest2.TestCase): |
| 217 SUBJECT = _KNOWN.CONSUMER_TOTAL_LATENCIES |
| 218 |
| 219 |
| 220 class TestProducerTotalLatencies(KnownMetricsBase, unittest2.TestCase): |
| 221 SUBJECT = _KNOWN.PRODUCER_TOTAL_LATENCIES |
| 222 WANTED_DISTRIBUTION = _wanted_distribution_with_sample( |
| 223 KnownMetricsBase.GIVEN_INFO.request_time.seconds, |
| 224 *metric_descriptor._TIME_DISTRIBUTION_ARGS) |
| 225 WANTED_ADDED_METRICS = messages.MetricValueSet( |
| 226 metricName=SUBJECT.metric_name, |
| 227 metricValues=[metric_value.create(distributionValue=WANTED_DISTRIBUTION)
]) |
| 228 |
| 229 |
| 230 class TestConsumerRequestOverheadLatencies(KnownMetricsBase, unittest2.TestCase)
: |
| 231 SUBJECT = _KNOWN.CONSUMER_REQUEST_OVERHEAD_LATENCIES |
| 232 WANTED_DISTRIBUTION = _wanted_distribution_with_sample( |
| 233 KnownMetricsBase.GIVEN_INFO.overhead_time.seconds, |
| 234 *metric_descriptor._TIME_DISTRIBUTION_ARGS) |
| 235 WANTED_ADDED_METRICS = messages.MetricValueSet( |
| 236 metricName=SUBJECT.metric_name, |
| 237 metricValues=[metric_value.create(distributionValue=WANTED_DISTRIBUTION)
]) |
| 238 |
| 239 |
| 240 class TestConsumerRequestOverheadLatenciesInvalidApiKey( |
| 241 KnownMetricsInvalidApiKey, unittest2.TestCase): |
| 242 SUBJECT = _KNOWN.CONSUMER_REQUEST_OVERHEAD_LATENCIES |
| 243 |
| 244 |
| 245 class TestProducerRequestOverheadLatencies(KnownMetricsBase, unittest2.TestCase)
: |
| 246 SUBJECT = _KNOWN.PRODUCER_REQUEST_OVERHEAD_LATENCIES |
| 247 WANTED_DISTRIBUTION = _wanted_distribution_with_sample( |
| 248 KnownMetricsBase.GIVEN_INFO.overhead_time.seconds, |
| 249 *metric_descriptor._TIME_DISTRIBUTION_ARGS) |
| 250 WANTED_ADDED_METRICS = messages.MetricValueSet( |
| 251 metricName=SUBJECT.metric_name, |
| 252 metricValues=[metric_value.create(distributionValue=WANTED_DISTRIBUTION)
]) |
| 253 |
| 254 |
| 255 class TestConsumerBackendLatencies(KnownMetricsBase, unittest2.TestCase): |
| 256 SUBJECT = _KNOWN.CONSUMER_BACKEND_LATENCIES |
| 257 WANTED_DISTRIBUTION = _wanted_distribution_with_sample( |
| 258 KnownMetricsBase.GIVEN_INFO.backend_time.seconds, |
| 259 *metric_descriptor._TIME_DISTRIBUTION_ARGS) |
| 260 WANTED_ADDED_METRICS = messages.MetricValueSet( |
| 261 metricName=SUBJECT.metric_name, |
| 262 metricValues=[metric_value.create(distributionValue=WANTED_DISTRIBUTION)
]) |
| 263 |
| 264 |
| 265 class TestConsumerBackendLatenciesInvalidApiKey(KnownMetricsInvalidApiKey, |
| 266 unittest2.TestCase): |
| 267 SUBJECT = _KNOWN.CONSUMER_BACKEND_LATENCIES |
| 268 |
| 269 |
| 270 class TestProducerBackendLatencies(KnownMetricsBase, unittest2.TestCase): |
| 271 SUBJECT = _KNOWN.PRODUCER_BACKEND_LATENCIES |
| 272 WANTED_DISTRIBUTION = _wanted_distribution_with_sample( |
| 273 KnownMetricsBase.GIVEN_INFO.backend_time.seconds, |
| 274 *metric_descriptor._TIME_DISTRIBUTION_ARGS) |
| 275 WANTED_ADDED_METRICS = messages.MetricValueSet( |
| 276 metricName=SUBJECT.metric_name, |
| 277 metricValues=[metric_value.create(distributionValue=WANTED_DISTRIBUTION)
]) |
OLD | NEW |