Techdebt: Replace sure with regular assertions in CloudWatch (#6493)
This commit is contained in:
parent
89f2a3c538
commit
0cad3b54f5
@ -1,5 +1,4 @@
|
|||||||
import boto3
|
import boto3
|
||||||
import sure # noqa # pylint: disable=unused-import
|
|
||||||
|
|
||||||
from moto import mock_cloudwatch
|
from moto import mock_cloudwatch
|
||||||
from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID
|
from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID
|
||||||
@ -29,29 +28,25 @@ def test_create_alarm():
|
|||||||
)
|
)
|
||||||
|
|
||||||
alarms = cloudwatch.describe_alarms()["MetricAlarms"]
|
alarms = cloudwatch.describe_alarms()["MetricAlarms"]
|
||||||
alarms.should.have.length_of(1)
|
assert len(alarms) == 1
|
||||||
alarm = alarms[0]
|
alarm = alarms[0]
|
||||||
alarm.should.have.key("AlarmName").equal("tester")
|
assert alarm["AlarmName"] == "tester"
|
||||||
alarm.should.have.key("Namespace").equal("tester_namespace")
|
assert alarm["Namespace"] == "tester_namespace"
|
||||||
alarm.should.have.key("MetricName").equal("tester_metric")
|
assert alarm["MetricName"] == "tester_metric"
|
||||||
alarm.should.have.key("ComparisonOperator").equal("GreaterThanOrEqualToThreshold")
|
assert alarm["ComparisonOperator"] == "GreaterThanOrEqualToThreshold"
|
||||||
alarm.should.have.key("Threshold").equal(2.0)
|
assert alarm["Threshold"] == 2.0
|
||||||
alarm.should.have.key("Period").equal(60)
|
assert alarm["Period"] == 60
|
||||||
alarm.should.have.key("EvaluationPeriods").equal(5)
|
assert alarm["EvaluationPeriods"] == 5
|
||||||
alarm.should.have.key("Statistic").should.equal("Average")
|
assert alarm["Statistic"] == "Average"
|
||||||
alarm.should.have.key("AlarmDescription").equal("A test")
|
assert alarm["AlarmDescription"] == "A test"
|
||||||
alarm.should.have.key("Dimensions").equal(
|
assert alarm["Dimensions"] == [{"Name": "InstanceId", "Value": "i-0123457"}]
|
||||||
[{"Name": "InstanceId", "Value": "i-0123457"}]
|
assert alarm["AlarmActions"] == ["arn:alarm"]
|
||||||
)
|
assert alarm["OKActions"] == ["arn:ok"]
|
||||||
alarm.should.have.key("AlarmActions").equal(["arn:alarm"])
|
assert alarm["InsufficientDataActions"] == ["arn:insufficient"]
|
||||||
alarm.should.have.key("OKActions").equal(["arn:ok"])
|
assert alarm["Unit"] == "Seconds"
|
||||||
alarm.should.have.key("InsufficientDataActions").equal(["arn:insufficient"])
|
assert alarm["AlarmArn"] == f"arn:aws:cloudwatch:{region}:{ACCOUNT_ID}:alarm:{name}"
|
||||||
alarm.should.have.key("Unit").equal("Seconds")
|
|
||||||
alarm.should.have.key("AlarmArn").equal(
|
|
||||||
f"arn:aws:cloudwatch:{region}:{ACCOUNT_ID}:alarm:{name}"
|
|
||||||
)
|
|
||||||
# default value should be True
|
# default value should be True
|
||||||
alarm.should.have.key("ActionsEnabled").equal(True)
|
assert alarm["ActionsEnabled"] is True
|
||||||
|
|
||||||
|
|
||||||
@mock_cloudwatch
|
@mock_cloudwatch
|
||||||
@ -59,7 +54,7 @@ def test_delete_alarm():
|
|||||||
cloudwatch = boto3.client("cloudwatch", region_name="eu-central-1")
|
cloudwatch = boto3.client("cloudwatch", region_name="eu-central-1")
|
||||||
|
|
||||||
alarms = cloudwatch.describe_alarms()["MetricAlarms"]
|
alarms = cloudwatch.describe_alarms()["MetricAlarms"]
|
||||||
alarms.should.have.length_of(0)
|
assert len(alarms) == 0
|
||||||
|
|
||||||
name = "tester"
|
name = "tester"
|
||||||
cloudwatch.put_metric_alarm(
|
cloudwatch.put_metric_alarm(
|
||||||
@ -80,12 +75,12 @@ def test_delete_alarm():
|
|||||||
)
|
)
|
||||||
|
|
||||||
alarms = cloudwatch.describe_alarms()["MetricAlarms"]
|
alarms = cloudwatch.describe_alarms()["MetricAlarms"]
|
||||||
alarms.should.have.length_of(1)
|
assert len(alarms) == 1
|
||||||
|
|
||||||
cloudwatch.delete_alarms(AlarmNames=[name])
|
cloudwatch.delete_alarms(AlarmNames=[name])
|
||||||
|
|
||||||
alarms = cloudwatch.describe_alarms()["MetricAlarms"]
|
alarms = cloudwatch.describe_alarms()["MetricAlarms"]
|
||||||
alarms.should.have.length_of(0)
|
assert len(alarms) == 0
|
||||||
|
|
||||||
|
|
||||||
@mock_cloudwatch
|
@mock_cloudwatch
|
||||||
@ -112,10 +107,10 @@ def test_describe_alarms_for_metric():
|
|||||||
ActionsEnabled=True,
|
ActionsEnabled=True,
|
||||||
)
|
)
|
||||||
alarms = conn.describe_alarms_for_metric(MetricName="cpu", Namespace="blah")
|
alarms = conn.describe_alarms_for_metric(MetricName="cpu", Namespace="blah")
|
||||||
alarms.get("MetricAlarms").should.have.length_of(1)
|
assert len(alarms.get("MetricAlarms")) == 1
|
||||||
alarm = alarms.get("MetricAlarms")[0]
|
alarm = alarms.get("MetricAlarms")[0]
|
||||||
assert "testalarm1" in alarm.get("AlarmArn")
|
assert "testalarm1" in alarm.get("AlarmArn")
|
||||||
alarm.should.have.key("ActionsEnabled").equal(True)
|
assert alarm["ActionsEnabled"] is True
|
||||||
|
|
||||||
|
|
||||||
@mock_cloudwatch
|
@mock_cloudwatch
|
||||||
@ -177,7 +172,7 @@ def test_describe_alarms():
|
|||||||
)
|
)
|
||||||
alarms = conn.describe_alarms()
|
alarms = conn.describe_alarms()
|
||||||
metric_alarms = alarms.get("MetricAlarms")
|
metric_alarms = alarms.get("MetricAlarms")
|
||||||
metric_alarms.should.have.length_of(2)
|
assert len(metric_alarms) == 2
|
||||||
single_metric_alarm = [
|
single_metric_alarm = [
|
||||||
alarm for alarm in metric_alarms if alarm["AlarmName"] == "testalarm1"
|
alarm for alarm in metric_alarms if alarm["AlarmName"] == "testalarm1"
|
||||||
][0]
|
][0]
|
||||||
@ -185,23 +180,23 @@ def test_describe_alarms():
|
|||||||
alarm for alarm in metric_alarms if alarm["AlarmName"] == "testalarm2"
|
alarm for alarm in metric_alarms if alarm["AlarmName"] == "testalarm2"
|
||||||
][0]
|
][0]
|
||||||
|
|
||||||
single_metric_alarm["MetricName"].should.equal("cpu")
|
assert single_metric_alarm["MetricName"] == "cpu"
|
||||||
single_metric_alarm.shouldnt.have.property("Metrics")
|
assert "Metrics" not in single_metric_alarm
|
||||||
single_metric_alarm["Namespace"].should.equal("blah")
|
assert single_metric_alarm["Namespace"] == "blah"
|
||||||
single_metric_alarm["Period"].should.equal(10)
|
assert single_metric_alarm["Period"] == 10
|
||||||
single_metric_alarm["EvaluationPeriods"].should.equal(5)
|
assert single_metric_alarm["EvaluationPeriods"] == 5
|
||||||
single_metric_alarm["Statistic"].should.equal("Average")
|
assert single_metric_alarm["Statistic"] == "Average"
|
||||||
single_metric_alarm["ComparisonOperator"].should.equal("GreaterThanThreshold")
|
assert single_metric_alarm["ComparisonOperator"] == "GreaterThanThreshold"
|
||||||
single_metric_alarm["Threshold"].should.equal(2)
|
assert single_metric_alarm["Threshold"] == 2
|
||||||
single_metric_alarm["ActionsEnabled"].should.equal(False)
|
assert single_metric_alarm["ActionsEnabled"] is False
|
||||||
|
|
||||||
multiple_metric_alarm.shouldnt.have.property("MetricName")
|
assert "MetricName" not in multiple_metric_alarm
|
||||||
multiple_metric_alarm["EvaluationPeriods"].should.equal(1)
|
assert multiple_metric_alarm["EvaluationPeriods"] == 1
|
||||||
multiple_metric_alarm["DatapointsToAlarm"].should.equal(1)
|
assert multiple_metric_alarm["DatapointsToAlarm"] == 1
|
||||||
multiple_metric_alarm["Metrics"].should.equal(metric_data_queries)
|
assert multiple_metric_alarm["Metrics"] == metric_data_queries
|
||||||
multiple_metric_alarm["ComparisonOperator"].should.equal("GreaterThanThreshold")
|
assert multiple_metric_alarm["ComparisonOperator"] == "GreaterThanThreshold"
|
||||||
multiple_metric_alarm["Threshold"].should.equal(1.0)
|
assert multiple_metric_alarm["Threshold"] == 1.0
|
||||||
multiple_metric_alarm["ActionsEnabled"].should.equal(True)
|
assert multiple_metric_alarm["ActionsEnabled"] is True
|
||||||
|
|
||||||
|
|
||||||
@mock_cloudwatch
|
@mock_cloudwatch
|
||||||
@ -239,17 +234,17 @@ def test_alarm_state():
|
|||||||
)
|
)
|
||||||
|
|
||||||
resp = client.describe_alarms(StateValue="ALARM")
|
resp = client.describe_alarms(StateValue="ALARM")
|
||||||
len(resp["MetricAlarms"]).should.equal(1)
|
assert len(resp["MetricAlarms"]) == 1
|
||||||
resp["MetricAlarms"][0]["AlarmName"].should.equal("testalarm1")
|
assert resp["MetricAlarms"][0]["AlarmName"] == "testalarm1"
|
||||||
resp["MetricAlarms"][0]["StateValue"].should.equal("ALARM")
|
assert resp["MetricAlarms"][0]["StateValue"] == "ALARM"
|
||||||
resp["MetricAlarms"][0]["ActionsEnabled"].should.equal(True)
|
assert resp["MetricAlarms"][0]["ActionsEnabled"] is True
|
||||||
|
|
||||||
resp = client.describe_alarms(StateValue="OK")
|
resp = client.describe_alarms(StateValue="OK")
|
||||||
len(resp["MetricAlarms"]).should.equal(1)
|
assert len(resp["MetricAlarms"]) == 1
|
||||||
resp["MetricAlarms"][0]["AlarmName"].should.equal("testalarm2")
|
assert resp["MetricAlarms"][0]["AlarmName"] == "testalarm2"
|
||||||
resp["MetricAlarms"][0]["StateValue"].should.equal("OK")
|
assert resp["MetricAlarms"][0]["StateValue"] == "OK"
|
||||||
resp["MetricAlarms"][0]["ActionsEnabled"].should.equal(True)
|
assert resp["MetricAlarms"][0]["ActionsEnabled"] is True
|
||||||
|
|
||||||
# Just for sanity
|
# Just for sanity
|
||||||
resp = client.describe_alarms()
|
resp = client.describe_alarms()
|
||||||
len(resp["MetricAlarms"]).should.equal(2)
|
assert len(resp["MetricAlarms"]) == 2
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -1,6 +1,6 @@
|
|||||||
import boto3
|
import boto3
|
||||||
|
import pytest
|
||||||
from botocore.exceptions import ClientError
|
from botocore.exceptions import ClientError
|
||||||
import sure # noqa # pylint: disable=unused-import
|
|
||||||
|
|
||||||
from moto import mock_cloudwatch
|
from moto import mock_cloudwatch
|
||||||
|
|
||||||
@ -13,7 +13,7 @@ def test_put_list_dashboard():
|
|||||||
client.put_dashboard(DashboardName="test1", DashboardBody=widget)
|
client.put_dashboard(DashboardName="test1", DashboardBody=widget)
|
||||||
resp = client.list_dashboards()
|
resp = client.list_dashboards()
|
||||||
|
|
||||||
len(resp["DashboardEntries"]).should.equal(1)
|
assert len(resp["DashboardEntries"]) == 1
|
||||||
|
|
||||||
|
|
||||||
@mock_cloudwatch
|
@mock_cloudwatch
|
||||||
@ -24,7 +24,7 @@ def test_put_list_prefix_nomatch_dashboard():
|
|||||||
client.put_dashboard(DashboardName="test1", DashboardBody=widget)
|
client.put_dashboard(DashboardName="test1", DashboardBody=widget)
|
||||||
resp = client.list_dashboards(DashboardNamePrefix="nomatch")
|
resp = client.list_dashboards(DashboardNamePrefix="nomatch")
|
||||||
|
|
||||||
len(resp["DashboardEntries"]).should.equal(0)
|
assert len(resp["DashboardEntries"]) == 0
|
||||||
|
|
||||||
|
|
||||||
@mock_cloudwatch
|
@mock_cloudwatch
|
||||||
@ -38,7 +38,7 @@ def test_delete_dashboard():
|
|||||||
client.delete_dashboards(DashboardNames=["test2", "test1"])
|
client.delete_dashboards(DashboardNames=["test2", "test1"])
|
||||||
|
|
||||||
resp = client.list_dashboards(DashboardNamePrefix="test3")
|
resp = client.list_dashboards(DashboardNamePrefix="test3")
|
||||||
len(resp["DashboardEntries"]).should.equal(1)
|
assert len(resp["DashboardEntries"]) == 1
|
||||||
|
|
||||||
|
|
||||||
@mock_cloudwatch
|
@mock_cloudwatch
|
||||||
@ -49,16 +49,13 @@ def test_delete_dashboard_fail():
|
|||||||
client.put_dashboard(DashboardName="test1", DashboardBody=widget)
|
client.put_dashboard(DashboardName="test1", DashboardBody=widget)
|
||||||
client.put_dashboard(DashboardName="test2", DashboardBody=widget)
|
client.put_dashboard(DashboardName="test2", DashboardBody=widget)
|
||||||
client.put_dashboard(DashboardName="test3", DashboardBody=widget)
|
client.put_dashboard(DashboardName="test3", DashboardBody=widget)
|
||||||
# Doesnt delete anything if all dashboards to be deleted do not exist
|
# Doesn't delete anything if some dashboards to be deleted do not exist
|
||||||
try:
|
with pytest.raises(ClientError) as exc:
|
||||||
client.delete_dashboards(DashboardNames=["test2", "test1", "test_no_match"])
|
client.delete_dashboards(DashboardNames=["test2", "test1", "test_no_match"])
|
||||||
except ClientError as err:
|
assert exc.value.response["Error"]["Code"] == "ResourceNotFound"
|
||||||
err.response["Error"]["Code"].should.equal("ResourceNotFound")
|
|
||||||
else:
|
|
||||||
raise RuntimeError("Should have raised error")
|
|
||||||
|
|
||||||
resp = client.list_dashboards()
|
resp = client.list_dashboards()
|
||||||
len(resp["DashboardEntries"]).should.equal(3)
|
assert len(resp["DashboardEntries"]) == 3
|
||||||
|
|
||||||
|
|
||||||
@mock_cloudwatch
|
@mock_cloudwatch
|
||||||
@ -68,18 +65,15 @@ def test_get_dashboard():
|
|||||||
client.put_dashboard(DashboardName="test1", DashboardBody=widget)
|
client.put_dashboard(DashboardName="test1", DashboardBody=widget)
|
||||||
|
|
||||||
resp = client.get_dashboard(DashboardName="test1")
|
resp = client.get_dashboard(DashboardName="test1")
|
||||||
resp.should.contain("DashboardArn")
|
assert "DashboardArn" in resp
|
||||||
resp.should.contain("DashboardBody")
|
assert "DashboardBody" in resp
|
||||||
resp["DashboardName"].should.equal("test1")
|
assert resp["DashboardName"] == "test1"
|
||||||
|
|
||||||
|
|
||||||
@mock_cloudwatch
|
@mock_cloudwatch
|
||||||
def test_get_dashboard_fail():
|
def test_get_dashboard_fail():
|
||||||
client = boto3.client("cloudwatch", region_name="eu-central-1")
|
client = boto3.client("cloudwatch", region_name="eu-central-1")
|
||||||
|
|
||||||
try:
|
with pytest.raises(ClientError) as exc:
|
||||||
client.get_dashboard(DashboardName="test1")
|
client.get_dashboard(DashboardName="test1")
|
||||||
except ClientError as err:
|
assert exc.value.response["Error"]["Code"] == "ResourceNotFound"
|
||||||
err.response["Error"]["Code"].should.equal("ResourceNotFound")
|
|
||||||
else:
|
|
||||||
raise RuntimeError("Should have raised error")
|
|
||||||
|
@ -3,7 +3,6 @@ from operator import itemgetter
|
|||||||
import boto3
|
import boto3
|
||||||
from botocore.exceptions import ClientError
|
from botocore.exceptions import ClientError
|
||||||
import pytest
|
import pytest
|
||||||
import sure # noqa # pylint: disable=unused-import
|
|
||||||
|
|
||||||
from moto import mock_cloudwatch
|
from moto import mock_cloudwatch
|
||||||
from moto.cloudwatch.utils import make_arn_for_alarm
|
from moto.cloudwatch.utils import make_arn_for_alarm
|
||||||
@ -40,7 +39,7 @@ def test_list_tags_for_resource():
|
|||||||
response = client.list_tags_for_resource(ResourceARN=arn)
|
response = client.list_tags_for_resource(ResourceARN=arn)
|
||||||
|
|
||||||
# then
|
# then
|
||||||
response["Tags"].should.equal([{"Key": "key-1", "Value": "value-1"}])
|
assert response["Tags"] == [{"Key": "key-1", "Value": "value-1"}]
|
||||||
|
|
||||||
|
|
||||||
@mock_cloudwatch
|
@mock_cloudwatch
|
||||||
@ -57,7 +56,7 @@ def test_list_tags_for_resource_with_unknown_resource():
|
|||||||
)
|
)
|
||||||
|
|
||||||
# then
|
# then
|
||||||
response["Tags"].should.equal([])
|
assert response["Tags"] == []
|
||||||
|
|
||||||
|
|
||||||
@mock_cloudwatch
|
@mock_cloudwatch
|
||||||
@ -91,15 +90,10 @@ def test_tag_resource():
|
|||||||
|
|
||||||
# then
|
# then
|
||||||
response = client.list_tags_for_resource(ResourceARN=arn)
|
response = client.list_tags_for_resource(ResourceARN=arn)
|
||||||
sorted(response["Tags"], key=itemgetter("Key")).should.equal(
|
assert sorted(response["Tags"], key=itemgetter("Key")) == [
|
||||||
sorted(
|
{"Key": "key-1", "Value": "value-1"},
|
||||||
[
|
{"Key": "key-2", "Value": "value-2"},
|
||||||
{"Key": "key-1", "Value": "value-1"},
|
]
|
||||||
{"Key": "key-2", "Value": "value-2"},
|
|
||||||
],
|
|
||||||
key=itemgetter("Key"),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@mock_cloudwatch
|
@mock_cloudwatch
|
||||||
@ -117,7 +111,7 @@ def test_tag_resource_on_resource_without_tags():
|
|||||||
alarm_arn = alarms["MetricAlarms"][0]["AlarmArn"]
|
alarm_arn = alarms["MetricAlarms"][0]["AlarmArn"]
|
||||||
|
|
||||||
# List 0 tags - none have been added
|
# List 0 tags - none have been added
|
||||||
cw.list_tags_for_resource(ResourceARN=alarm_arn)["Tags"].should.equal([])
|
assert cw.list_tags_for_resource(ResourceARN=alarm_arn)["Tags"] == []
|
||||||
|
|
||||||
# Tag the Alarm for the first time
|
# Tag the Alarm for the first time
|
||||||
cw.tag_resource(ResourceARN=alarm_arn, Tags=[{"Key": "tk", "Value": "tv"}])
|
cw.tag_resource(ResourceARN=alarm_arn, Tags=[{"Key": "tk", "Value": "tv"}])
|
||||||
@ -141,10 +135,10 @@ def test_tag_resource_error_not_exists():
|
|||||||
|
|
||||||
# then
|
# then
|
||||||
ex = e.value
|
ex = e.value
|
||||||
ex.operation_name.should.equal("TagResource")
|
assert ex.operation_name == "TagResource"
|
||||||
ex.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(404)
|
assert ex.response["ResponseMetadata"]["HTTPStatusCode"] == 404
|
||||||
ex.response["Error"]["Code"].should.contain("ResourceNotFoundException")
|
assert ex.response["Error"]["Code"] == "ResourceNotFoundException"
|
||||||
ex.response["Error"]["Message"].should.equal("Unknown")
|
assert ex.response["Error"]["Message"] == "Unknown"
|
||||||
|
|
||||||
|
|
||||||
@mock_cloudwatch
|
@mock_cloudwatch
|
||||||
@ -181,7 +175,7 @@ def test_untag_resource():
|
|||||||
|
|
||||||
# then
|
# then
|
||||||
response = client.list_tags_for_resource(ResourceARN=arn)
|
response = client.list_tags_for_resource(ResourceARN=arn)
|
||||||
response["Tags"].should.equal([{"Key": "key-1", "Value": "value-1"}])
|
assert response["Tags"] == [{"Key": "key-1", "Value": "value-1"}]
|
||||||
|
|
||||||
|
|
||||||
@mock_cloudwatch
|
@mock_cloudwatch
|
||||||
@ -201,7 +195,7 @@ def test_untag_resource_error_not_exists():
|
|||||||
|
|
||||||
# then
|
# then
|
||||||
ex = e.value
|
ex = e.value
|
||||||
ex.operation_name.should.equal("UntagResource")
|
assert ex.operation_name == "UntagResource"
|
||||||
ex.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(404)
|
assert ex.response["ResponseMetadata"]["HTTPStatusCode"] == 404
|
||||||
ex.response["Error"]["Code"].should.contain("ResourceNotFoundException")
|
assert ex.response["Error"]["Code"] == "ResourceNotFoundException"
|
||||||
ex.response["Error"]["Message"].should.equal("Unknown")
|
assert ex.response["Error"]["Message"] == "Unknown"
|
||||||
|
Loading…
Reference in New Issue
Block a user