Exemplo n.º 1
0
def test_publish_duration_metric(mock_session):
    fake_datetime = datetime(2019, 1, 1)
    proxy = MetricsPublisherProxy(ACCOUNT_ID, RESOURCE_TYPE)
    proxy.add_metrics_publisher(mock_session)
    proxy.publish_duration_metric(fake_datetime, Action.CREATE, 100)

    expected_calls = [
        call.client("cloudwatch"),
        call.client().put_metric_data(
            Namespace="AWS/CloudFormation/123412341234/Aa/Bb/Cc",
            MetricData=[{
                "MetricName":
                MetricTypes.HandlerInvocationDuration.name,
                "Dimensions": [
                    {
                        "Name": "DimensionKeyActionType",
                        "Value": "CREATE"
                    },
                    {
                        "Name": "DimensionKeyResourceType",
                        "Value": "Aa::Bb::Cc"
                    },
                ],
                "Unit":
                StandardUnit.Milliseconds.name,
                "Timestamp":
                str(fake_datetime),
                "Value":
                100,
            }],
        ),
    ]
    assert mock_session.mock_calls == expected_calls
Exemplo n.º 2
0
def test_publish_log_delivery_exception_metric(mock_session):
    fake_datetime = datetime(2019, 1, 1)
    proxy = MetricsPublisherProxy(ACCOUNT_ID, RESOURCE_TYPE)
    proxy.add_metrics_publisher(mock_session)
    proxy.publish_log_delivery_exception_metric(fake_datetime,
                                                TypeError("test"))

    expected_calls = [
        call.client("cloudwatch"),
        call.client().put_metric_data(
            Namespace="AWS/CloudFormation/123412341234/Aa/Bb/Cc",
            MetricData=[{
                "MetricName":
                MetricTypes.HandlerException.name,
                "Dimensions": [
                    {
                        "Name": "DimensionKeyActionType",
                        "Value": "ProviderLogDelivery",
                    },
                    {
                        "Name": "DimensionKeyExceptionType",
                        "Value": "<class 'TypeError'>",
                    },
                    {
                        "Name": "DimensionKeyResourceType",
                        "Value": "Aa::Bb::Cc"
                    },
                ],
                "Unit":
                StandardUnit.Count.name,
                "Timestamp":
                str(fake_datetime),
                "Value":
                1.0,
            }],
        ),
    ]
    assert mock_session.mock_calls == expected_calls
Exemplo n.º 3
0
def test_publish_exception_metric(mock_session):
    fake_datetime = datetime(2019, 1, 1)
    proxy = MetricsPublisherProxy(RESOURCE_TYPE)
    proxy.add_metrics_publisher(mock_session)
    proxy.publish_exception_metric(fake_datetime, Action.CREATE,
                                   Exception("fake-err"))
    expected_calls = [
        call.client("cloudwatch"),
        call.client().put_metric_data(
            Namespace="AWS/CloudFormation/Aa/Bb/Cc",
            MetricData=[{
                "MetricName":
                MetricTypes.HandlerException.name,
                "Dimensions": [
                    {
                        "Name": "DimensionKeyActionType",
                        "Value": "CREATE"
                    },
                    {
                        "Name": "DimensionKeyExceptionType",
                        "Value": "<class 'Exception'>",
                    },
                    {
                        "Name": "DimensionKeyResourceType",
                        "Value": "Aa::Bb::Cc"
                    },
                ],
                "Unit":
                StandardUnit.Count.name,
                "Timestamp":
                str(fake_datetime),
                "Value":
                1.0,
            }],
        ),
    ]
    assert mock_session.mock_calls == expected_calls
Exemplo n.º 4
0
def test_metrics_publisher_proxy_add_metrics_publisher_none_safe():
    proxy = MetricsPublisherProxy(ACCOUNT_ID, RESOURCE_TYPE)
    proxy.add_metrics_publisher(None)
    assert proxy._publishers == []  # pylint: disable=protected-access
Exemplo n.º 5
0
from unittest.mock import Mock, call, patch

import boto3
import pytest
from cloudformation_cli_python_lib.interface import Action, MetricTypes, StandardUnit
from cloudformation_cli_python_lib.metrics import (
    MetricPublisher,
    MetricsPublisherProxy,
    format_dimensions,
)

from botocore.stub import Stubber

ACCOUNT_ID = "123412341234"
RESOURCE_TYPE = "Aa::Bb::Cc"
NAMESPACE = MetricsPublisherProxy._make_namespace(  # pylint: disable=protected-access
    ACCOUNT_ID, RESOURCE_TYPE)


@pytest.fixture
def mock_session():
    return Mock(spec_set=["client"])


def test_format_dimensions():
    dimensions = {"MyDimensionKey": "val_1", "MyDimensionKey2": "val_2"}
    result = format_dimensions(dimensions)
    assert result == [
        {
            "Name": "MyDimensionKey",
            "Value": "val_1"
        },
def test_metrics_publisher_proxy_add_metrics_publisher_none_safe():
    proxy = MetricsPublisherProxy()
    proxy.add_metrics_publisher(None, None)
    assert proxy._publishers == []  # pylint: disable=protected-access