def test_canonical_querystring_is_directly_created_by_querystring_builder(self): self.builder._init_timestamps() querystring_builder = QuerystringBuilder("10") metric = MetricDataStatistic("test_metric", statistic_values=MetricDataStatistic.Statistics(20)) original_querystring = querystring_builder.build_querystring([metric], self.builder._get_request_map()) generated_querystring = self.builder._create_canonical_querystring([metric]) self.assertEquals(original_querystring, generated_querystring)
def test_client_raise_exception_on_credentials_error(self): metric = MetricDataStatistic( "metric_name", statistic_values=MetricDataStatistic.Statistics(20), namespace="namespace") self.server.set_expected_response("Client Error: Forbidden", 403) self.assert_no_retry_on_error_request("namespace", [metric])
def test_create_signed_request_with_iam_role_has_token_parameter(self): self.builder = RequestBuilder( AWSCredentials("access_key", "secret_key", "token"), self.region) metric = MetricDataStatistic( "test_metric", statistic_values=MetricDataStatistic.Statistics(20)) request = self.builder.create_signed_request(self.namespace, [metric]) self.assertTrue("X-Amz-Security-Token" in request)
def test_client_raise_exception_on_request_throttling(self): metric = MetricDataStatistic( "metric_name", statistic_values=MetricDataStatistic.Statistics(20), namespace="namespace") self.server.set_expected_response("Request Throttled", 400) self.assert_no_retry_on_error_request("namespace", [metric])
def test_build_querystring_with_one_metric(self): dimensions = { "Dimension1": 20, "Dimension2": 30, "Host": "localhost" } metric = MetricDataStatistic("test_metric", statistic_values=MetricDataStatistic.Statistics(20), dimensions=dimensions) expected_map = get_canonical_map() expected_map.update(self.builder._build_metric_map([metric])) querystring = self.builder.build_querystring([metric], get_canonical_map()) assert_querystring(expected_map, querystring)
def test_build_metric_map_for_single_metric_with_dimensions(self): dimensions = {"Dimension1": 20, "Dimension2": 30, "Host": "localhost"} metric = MetricDataStatistic( "test_metric", statistic_values=MetricDataStatistic.Statistics(20), dimensions=dimensions) metric_map = self.builder._build_metric_map([metric]) assert_metric_map([metric], metric_map)
def test_build_metric_map_for_multiple_metrics_without_dimensions(self): metric1 = MetricDataStatistic( "Metric1", statistic_values=MetricDataStatistic.Statistics(20)) metric2 = MetricDataStatistic( "Metric2", statistic_values=MetricDataStatistic.Statistics(30)) metric_list = [metric1, metric2] metric_map = self.builder._build_metric_map(metric_list) assert_metric_map(metric_list, metric_map)
def test_put_metric_data_with_timeout(self): self.server.set_timeout_delay(PutClient._DEFAULT_RESPONSE_TIMEOUT * (PutClient._TOTAL_RETRIES + 1)) metric_name = "test_metric" metric = MetricDataStatistic( metric_name, statistic_values=MetricDataStatistic.Statistics(20)) self.client.put_metric_data(MetricDataStatistic.NAMESPACE, [metric]) self.assertTrue(self.logger.warning.called)
def test_server_received_user_agent_information(self): metric = MetricDataStatistic( metric_name="test_metric", statistic_values=MetricDataStatistic.Statistics(20), namespace="testing_namespace") self.client.put_metric_data("testing_namespace", [metric]) received_request = self.server_get_received_request() self.assertTrue(PutClientTest.USER_AGENT in received_request)
def test_build_metric_map_for_multiple_metrics_with_dimensions(self): dimensions1 = { "Dimension1": 20, "Dimension2": 30, "Host": "localhost" } metric1 = MetricDataStatistic("test_metric", statistic_values=MetricDataStatistic.Statistics(20), dimensions=dimensions1) dimensions2 = { "Dimension1": 20, "Dimension2": 30, "Host": "localhost" } metric2 = MetricDataStatistic("test_metric2", statistic_values=MetricDataStatistic.Statistics(400), dimensions=dimensions2) metric_list = [metric1, metric2] metric_map = self.builder._build_metric_map(metric_list) assert_metric_map(metric_list, metric_map)
def test_build_querystring_with_storage_resolution(self): self.builder = QuerystringBuilder(True) dimensions1 = {"Dimension1": 20, "Dimension2": 30, "Host": "localhost"} metric1 = MetricDataStatistic( "test_metric", statistic_values=MetricDataStatistic.Statistics(20), dimensions=dimensions1) querystring = self.builder.build_querystring([metric1], get_canonical_map()) self.assertTrue(self.builder._STORAGE_RESOLUTION in querystring)
def test_put_metric_data_with_retry(self): self.server.set_timeout_delay(PutClient._DEFAULT_RESPONSE_TIMEOUT * PutClient._TOTAL_RETRIES) metric_name = "test_metric" metric = MetricDataStatistic( metric_name, statistic_values=MetricDataStatistic.Statistics(20)) self.client.put_metric_data(MetricDataStatistic.NAMESPACE, [metric]) received_request = self.server_get_received_request() self.assertTrue("MetricData.member.1.MetricName=" + metric_name in received_request)
def test_metric_data_creates_statistic_on_add_value(self): metric = MetricDataStatistic() self.assertFalse(metric.statistics) metric.add_value(10) self.assertTrue(metric.statistics) assert_statistics(metric.statistics, min=10, max=10, sum=10, sample_count=1)
def test_create_signed_request_generates_all_required_parameters(self): metric = MetricDataStatistic("test_metric", statistic_values=MetricDataStatistic.Statistics(20)) request = self.builder.create_signed_request(self.namespace, [metric]) self.assertTrue("Action" in request) self.assertTrue("MetricData.member.1." in request) self.assertTrue("Namespace" in request) self.assertTrue("Version" in request) self.assertTrue("X-Amz-Algorithm" in request) self.assertTrue("X-Amz-Credential" in request) self.assertTrue("X-Amz-Date" in request) self.assertTrue("X-Amz-SignedHeaders" in request)
def test_put_metric_data_with_inconsistent_namespaces(self): metric1 = MetricDataStatistic( "metric_name1", statistic_values=MetricDataStatistic.Statistics(20), namespace="namespace1") metric2 = MetricDataStatistic( "metric_name2", statistic_values=MetricDataStatistic.Statistics(20)) with self.assertRaises(ValueError): self.client.put_metric_data(MetricDataStatistic.NAMESPACE, [metric1, metric2])
def test_put_metric_data_with_iam_role_creds(self): metric_name = "test_metric" namespace = "testing_namespace" metric = MetricDataStatistic( metric_name, statistic_values=MetricDataStatistic.Statistics(20), namespace=namespace) self.config_helper.credentials = AWSCredentials( "access", "secret", "IAM_ROLE_TOKEN") self.client = PutClient(self.config_helper) self.client.put_metric_data(namespace, [metric]) received_request = self.server_get_received_request() self.assertTrue( "X-Amz-Security-Token=IAM_ROLE_TOKEN" in received_request)
def test_credentials_are_updated_in_the_put_client(self): metric = MetricDataStatistic( metric_name="test_metric", statistic_values=MetricDataStatistic.Statistics(20), namespace="testing_namespace") self.client.put_metric_data("testing_namespace", [metric]) received_request = self.server_get_received_request() self.assertTrue("access" in received_request) self.config_helper.credentials = AWSCredentials( "NEW_ACCESS_KEY", "NEW_SECRET_KEY") self.client = PutClient(self.config_helper) self.client.put_metric_data("testing_namespace", [metric]) received_request = self.server_get_received_request() self.assertTrue("NEW_ACCESS_KEY" in received_request)
def test_build_map_without_statistics(self): dimensions = { "Dimension1": 20, "Dimension2": 30, "Host": "localhost" } metric = MetricDataStatistic("test_metric", dimensions=dimensions) map = {} prefix = "metric_prefix" with self.assertRaises(ValueError): self.builder._add_values(metric, map, prefix)
def test_create_statistic_object_with_value(self): value = 400 statistics = MetricDataStatistic.Statistics(value) assert_statistics(statistics, min=value, max=value, sum=value, sample_count=1)
def test_metric_data_calculates_statistic_on_add_value(self): metric = MetricDataStatistic() metric.add_value(10) metric.add_value(-20) metric.add_value(30) assert_statistics(metric.statistics, min=-20, max=30, sum=20, sample_count=3)
def test_empty_metric_object(self): metric = MetricDataStatistic() assert_metric_data(metric, metric_name="", unit="", dimensions={}, statistics=None, timestamp=None, namespace=MetricDataStatistic.NAMESPACE)
def test_build_querystring_with_statistics(self): dimensions = { "Dimension1": 20, "Dimension2": 30, "Host": "localhost" } metric = MetricDataStatistic("test_metric", dimensions=dimensions) metric.add_value(10) metric.add_value(-20) metric.add_value(50) expected_map = get_canonical_map() expected_map.update(self.builder._build_metric_map([metric])) querystring = self.builder.build_querystring([metric], get_canonical_map()) self.assertTrue(QuerystringBuilder._STAT_MAX +"=50" in querystring) self.assertTrue(QuerystringBuilder._STAT_MIN +"=-20" in querystring) self.assertTrue(QuerystringBuilder._STAT_SUM +"=40" in querystring) self.assertTrue(QuerystringBuilder._STAT_SAMPLE +"=3" in querystring)
def test_put_metric_data(self): metric_name = "test_metric" namespace = "testing_namespace" metric = MetricDataStatistic( metric_name, statistic_values=MetricDataStatistic.Statistics(20), namespace=namespace) self.client.put_metric_data(namespace, [metric]) received_request = self.server_get_received_request() self.assertTrue("MetricData.member.1.MetricName=" + metric_name in received_request) self.assertTrue("MetricData.member.1.Timestamp=" + metric.timestamp in received_request) self.assertTrue( "MetricData.member.1.StatisticValues." in received_request) self.assertTrue("Namespace=" + namespace in received_request) self.assertTrue("Action=PutMetricData" in received_request) self.assertTrue("Version" in received_request) self.assertTrue("X-Amz-Algorithm" in received_request) self.assertTrue("X-Amz-Credential" in received_request) self.assertTrue("X-Amz-Date" in received_request) self.assertTrue("X-Amz-SignedHeaders" in received_request)
def test_metric_object_with_custom_value(self): new_metric_name = "Plugin1-Metric1" new_timestamp = awsutils.get_aws_timestamp() new_unit = "Seconds" new_dimensions = {'Host': 'InstanceID'} new_namespace = "test_namespace" metric = MetricDataStatistic(metric_name=new_metric_name, timestamp=new_timestamp, unit=new_unit, dimensions=new_dimensions, namespace=new_namespace) assert_metric_data(metric, new_metric_name, new_timestamp, new_unit, new_dimensions, namespace=new_namespace)
def test_build_map_with_statistics(self): dimensions1 = { "Dimension1": 20, "Dimension2": 30, "Host": "localhost" } metric = MetricDataStatistic("test_metric", dimensions=dimensions1) metric.add_value(10) metric.add_value(20) metric.add_value(40) map = {} prefix = "metric_prefix" self.builder._add_values(metric, map, prefix) self.assertEquals(10, map[prefix + QuerystringBuilder._STAT_MIN]) self.assertEquals(40, map[prefix + QuerystringBuilder._STAT_MAX]) self.assertEquals(70, map[prefix + QuerystringBuilder._STAT_SUM]) self.assertEquals(3, map[prefix + QuerystringBuilder._STAT_SAMPLE])
def test_client_raise_exception_on_service_unavailable_error(self): metric = MetricDataStatistic("metric_name", namespace="namespace") metric.add_value(10) self.server.set_expected_response("Service Unavailable", 503) self.assert_no_retry_on_error_request("namespace", [metric])
def test_statistic_values_add_value3(self): statistics = MetricDataStatistic.Statistics(30) statistics._add_value(-10) statistics._add_value(20) assert_statistics(statistics, min=-10, max=30, sum=40, sample_count=3)
def test_metric_data_default_constructor(self): metric = MetricDataStatistic() assert_metric_data(metric) self.assertTrue(metric.timestamp)
def test_metric_data_gets_current_timestamp(self): metric1 = MetricDataStatistic("metric_name", 20) sleep(1) metric2 = MetricDataStatistic("metric_name", 30) self.assertFalse(metric1.timestamp is metric2.timestamp)
def test_build_metric_map_for_single_metric_without_dimensions(self): metric = MetricDataStatistic("test_metric", statistic_values=MetricDataStatistic.Statistics(20)) metric_map = self.builder._build_metric_map([metric]) assert_metric_map([metric], metric_map)