moto/tests/test_cloudwatch/test_cloudwatch_boto3.py

206 lines
6.8 KiB
Python
Raw Normal View History

2017-09-22 15:38:20 +00:00
from __future__ import unicode_literals
import boto3
from botocore.exceptions import ClientError
from datetime import datetime, timedelta
import pytz
2019-10-31 15:44:26 +00:00
import sure # noqa
2017-09-22 15:38:20 +00:00
from moto import mock_cloudwatch
@mock_cloudwatch
def test_put_list_dashboard():
2019-10-31 15:44:26 +00:00
client = boto3.client("cloudwatch", region_name="eu-central-1")
2017-09-22 15:38:20 +00:00
widget = '{"widgets": [{"type": "text", "x": 0, "y": 7, "width": 3, "height": 3, "properties": {"markdown": "Hello world"}}]}'
2019-10-31 15:44:26 +00:00
client.put_dashboard(DashboardName="test1", DashboardBody=widget)
2017-09-22 15:38:20 +00:00
resp = client.list_dashboards()
2019-10-31 15:44:26 +00:00
len(resp["DashboardEntries"]).should.equal(1)
2017-09-22 15:38:20 +00:00
@mock_cloudwatch
def test_put_list_prefix_nomatch_dashboard():
2019-10-31 15:44:26 +00:00
client = boto3.client("cloudwatch", region_name="eu-central-1")
2017-09-22 15:38:20 +00:00
widget = '{"widgets": [{"type": "text", "x": 0, "y": 7, "width": 3, "height": 3, "properties": {"markdown": "Hello world"}}]}'
2019-10-31 15:44:26 +00:00
client.put_dashboard(DashboardName="test1", DashboardBody=widget)
resp = client.list_dashboards(DashboardNamePrefix="nomatch")
2017-09-22 15:38:20 +00:00
2019-10-31 15:44:26 +00:00
len(resp["DashboardEntries"]).should.equal(0)
2017-09-22 15:38:20 +00:00
@mock_cloudwatch
def test_delete_dashboard():
2019-10-31 15:44:26 +00:00
client = boto3.client("cloudwatch", region_name="eu-central-1")
2017-09-22 15:38:20 +00:00
widget = '{"widgets": [{"type": "text", "x": 0, "y": 7, "width": 3, "height": 3, "properties": {"markdown": "Hello world"}}]}'
2019-10-31 15:44:26 +00:00
client.put_dashboard(DashboardName="test1", DashboardBody=widget)
client.put_dashboard(DashboardName="test2", DashboardBody=widget)
client.put_dashboard(DashboardName="test3", DashboardBody=widget)
client.delete_dashboards(DashboardNames=["test2", "test1"])
2017-09-22 15:38:20 +00:00
2019-10-31 15:44:26 +00:00
resp = client.list_dashboards(DashboardNamePrefix="test3")
len(resp["DashboardEntries"]).should.equal(1)
2017-09-22 15:38:20 +00:00
@mock_cloudwatch
def test_delete_dashboard_fail():
2019-10-31 15:44:26 +00:00
client = boto3.client("cloudwatch", region_name="eu-central-1")
2017-09-22 15:38:20 +00:00
widget = '{"widgets": [{"type": "text", "x": 0, "y": 7, "width": 3, "height": 3, "properties": {"markdown": "Hello world"}}]}'
2019-10-31 15:44:26 +00:00
client.put_dashboard(DashboardName="test1", DashboardBody=widget)
client.put_dashboard(DashboardName="test2", DashboardBody=widget)
client.put_dashboard(DashboardName="test3", DashboardBody=widget)
2017-09-22 15:38:20 +00:00
# Doesnt delete anything if all dashboards to be deleted do not exist
try:
2019-10-31 15:44:26 +00:00
client.delete_dashboards(DashboardNames=["test2", "test1", "test_no_match"])
2017-09-22 15:38:20 +00:00
except ClientError as err:
2019-10-31 15:44:26 +00:00
err.response["Error"]["Code"].should.equal("ResourceNotFound")
2017-09-22 15:38:20 +00:00
else:
2019-10-31 15:44:26 +00:00
raise RuntimeError("Should of raised error")
2017-09-22 15:38:20 +00:00
resp = client.list_dashboards()
2019-10-31 15:44:26 +00:00
len(resp["DashboardEntries"]).should.equal(3)
2017-09-22 15:38:20 +00:00
@mock_cloudwatch
def test_get_dashboard():
2019-10-31 15:44:26 +00:00
client = boto3.client("cloudwatch", region_name="eu-central-1")
2017-09-22 15:38:20 +00:00
widget = '{"widgets": [{"type": "text", "x": 0, "y": 7, "width": 3, "height": 3, "properties": {"markdown": "Hello world"}}]}'
2019-10-31 15:44:26 +00:00
client.put_dashboard(DashboardName="test1", DashboardBody=widget)
2017-09-22 15:38:20 +00:00
2019-10-31 15:44:26 +00:00
resp = client.get_dashboard(DashboardName="test1")
resp.should.contain("DashboardArn")
resp.should.contain("DashboardBody")
resp["DashboardName"].should.equal("test1")
2017-09-22 15:38:20 +00:00
@mock_cloudwatch
def test_get_dashboard_fail():
2019-10-31 15:44:26 +00:00
client = boto3.client("cloudwatch", region_name="eu-central-1")
2017-09-22 15:38:20 +00:00
try:
2019-10-31 15:44:26 +00:00
client.get_dashboard(DashboardName="test1")
2017-09-22 15:38:20 +00:00
except ClientError as err:
2019-10-31 15:44:26 +00:00
err.response["Error"]["Code"].should.equal("ResourceNotFound")
2017-09-22 15:38:20 +00:00
else:
2019-10-31 15:44:26 +00:00
raise RuntimeError("Should of raised error")
2017-09-22 15:38:20 +00:00
@mock_cloudwatch
def test_alarm_state():
2019-10-31 15:44:26 +00:00
client = boto3.client("cloudwatch", region_name="eu-central-1")
client.put_metric_alarm(
2019-10-31 15:44:26 +00:00
AlarmName="testalarm1",
MetricName="cpu",
Namespace="blah",
Period=10,
EvaluationPeriods=5,
2019-10-31 15:44:26 +00:00
Statistic="Average",
Threshold=2,
2019-10-31 15:44:26 +00:00
ComparisonOperator="GreaterThanThreshold",
)
client.put_metric_alarm(
2019-10-31 15:44:26 +00:00
AlarmName="testalarm2",
MetricName="cpu",
Namespace="blah",
Period=10,
EvaluationPeriods=5,
2019-10-31 15:44:26 +00:00
Statistic="Average",
Threshold=2,
2019-10-31 15:44:26 +00:00
ComparisonOperator="GreaterThanThreshold",
)
# This is tested implicitly as if it doesnt work the rest will die
client.set_alarm_state(
2019-10-31 15:44:26 +00:00
AlarmName="testalarm1",
StateValue="ALARM",
StateReason="testreason",
StateReasonData='{"some": "json_data"}',
)
2019-10-31 15:44:26 +00:00
resp = client.describe_alarms(StateValue="ALARM")
len(resp["MetricAlarms"]).should.equal(1)
resp["MetricAlarms"][0]["AlarmName"].should.equal("testalarm1")
resp["MetricAlarms"][0]["StateValue"].should.equal("ALARM")
2019-10-31 15:44:26 +00:00
resp = client.describe_alarms(StateValue="OK")
len(resp["MetricAlarms"]).should.equal(1)
resp["MetricAlarms"][0]["AlarmName"].should.equal("testalarm2")
resp["MetricAlarms"][0]["StateValue"].should.equal("OK")
# Just for sanity
resp = client.describe_alarms()
2019-10-31 15:44:26 +00:00
len(resp["MetricAlarms"]).should.equal(2)
2017-09-22 15:38:20 +00:00
@mock_cloudwatch
def test_put_metric_data_no_dimensions():
2019-10-31 15:44:26 +00:00
conn = boto3.client("cloudwatch", region_name="us-east-1")
conn.put_metric_data(
2019-10-31 15:44:26 +00:00
Namespace="tester", MetricData=[dict(MetricName="metric", Value=1.5)]
)
2017-09-22 15:38:20 +00:00
2019-10-31 15:44:26 +00:00
metrics = conn.list_metrics()["Metrics"]
metrics.should.have.length_of(1)
metric = metrics[0]
2019-10-31 15:44:26 +00:00
metric["Namespace"].should.equal("tester")
metric["MetricName"].should.equal("metric")
2017-09-22 15:38:20 +00:00
@mock_cloudwatch
def test_put_metric_data_with_statistics():
2019-10-31 15:44:26 +00:00
conn = boto3.client("cloudwatch", region_name="us-east-1")
conn.put_metric_data(
2019-10-31 15:44:26 +00:00
Namespace="tester",
MetricData=[
dict(
2019-10-31 15:44:26 +00:00
MetricName="statmetric",
Timestamp=datetime(2015, 1, 1),
# no Value to test https://github.com/spulec/moto/issues/1615
StatisticValues=dict(
2019-10-31 15:44:26 +00:00
SampleCount=123.0, Sum=123.0, Minimum=123.0, Maximum=123.0
),
2019-10-31 15:44:26 +00:00
Unit="Milliseconds",
StorageResolution=123,
)
2019-10-31 15:44:26 +00:00
],
)
2019-10-31 15:44:26 +00:00
metrics = conn.list_metrics()["Metrics"]
metrics.should.have.length_of(1)
metric = metrics[0]
2019-10-31 15:44:26 +00:00
metric["Namespace"].should.equal("tester")
metric["MetricName"].should.equal("statmetric")
# TODO: test statistics - https://github.com/spulec/moto/issues/1615
2019-10-31 15:44:26 +00:00
@mock_cloudwatch
def test_get_metric_statistics():
2019-10-31 15:44:26 +00:00
conn = boto3.client("cloudwatch", region_name="us-east-1")
utc_now = datetime.now(tz=pytz.utc)
conn.put_metric_data(
2019-10-31 15:44:26 +00:00
Namespace="tester",
MetricData=[dict(MetricName="metric", Value=1.5, Timestamp=utc_now)],
)
stats = conn.get_metric_statistics(
2019-10-31 15:44:26 +00:00
Namespace="tester",
MetricName="metric",
2018-05-05 19:22:29 +00:00
StartTime=utc_now - timedelta(seconds=60),
EndTime=utc_now + timedelta(seconds=60),
Period=60,
2019-10-31 15:44:26 +00:00
Statistics=["SampleCount", "Sum"],
)
2019-10-31 15:44:26 +00:00
stats["Datapoints"].should.have.length_of(1)
datapoint = stats["Datapoints"][0]
datapoint["SampleCount"].should.equal(1.0)
datapoint["Sum"].should.equal(1.5)