From 6b0d02292527befe6bd6867a1758d61a6e55536e Mon Sep 17 00:00:00 2001 From: Bert Blommers Date: Mon, 24 Jul 2023 12:50:35 +0000 Subject: [PATCH] Techdebt: Replace sure with regular assertions in ELBv2 (#6558) --- tests/test_elbv2/test_elbv2.py | 606 +++++++++--------- tests/test_elbv2/test_elbv2_cloudformation.py | 89 ++- tests/test_elbv2/test_elbv2_integration.py | 2 +- .../test_elbv2_listener_rule_tags.py | 28 +- tests/test_elbv2/test_elbv2_listener_rules.py | 87 ++- tests/test_elbv2/test_elbv2_listener_tags.py | 32 +- tests/test_elbv2/test_elbv2_set_subnets.py | 9 +- tests/test_elbv2/test_elbv2_target_groups.py | 252 ++++---- tests/test_elbv2/test_server.py | 4 +- 9 files changed, 527 insertions(+), 582 deletions(-) diff --git a/tests/test_elbv2/test_elbv2.py b/tests/test_elbv2/test_elbv2.py index 637117227..978476249 100644 --- a/tests/test_elbv2/test_elbv2.py +++ b/tests/test_elbv2/test_elbv2.py @@ -3,7 +3,6 @@ import os import boto3 from botocore.exceptions import ClientError import pytest -import sure # noqa # pylint: disable=unused-import from moto import mock_elbv2, mock_ec2, mock_acm from moto.elbv2 import elbv2_backends @@ -16,27 +15,26 @@ from tests import EXAMPLE_AMI_ID def test_create_load_balancer(): response, _, security_group, subnet1, subnet2, conn = create_load_balancer() - lb = response.get("LoadBalancers")[0] - lb.get("DNSName").should.equal("my-lb-1.us-east-1.elb.amazonaws.com") - lb.get("LoadBalancerArn").should.equal( - f"arn:aws:elasticloadbalancing:us-east-1:{ACCOUNT_ID}:loadbalancer/app/my-lb/50dc6c495c0c9188" + lb = response["LoadBalancers"][0] + assert lb["DNSName"] == "my-lb-1.us-east-1.elb.amazonaws.com" + assert ( + lb["LoadBalancerArn"] + == f"arn:aws:elasticloadbalancing:us-east-1:{ACCOUNT_ID}:loadbalancer/app/my-lb/50dc6c495c0c9188" ) - lb.get("SecurityGroups").should.equal([security_group.id]) - lb.get("AvailabilityZones").should.equal( - [ - {"SubnetId": subnet1.id, "ZoneName": "us-east-1a"}, - {"SubnetId": subnet2.id, "ZoneName": "us-east-1b"}, - ] - ) - lb.get("CreatedTime").tzinfo.should_not.be.none - lb.get("State").get("Code").should.equal("provisioning") - lb_arn = lb.get("LoadBalancerArn") + assert lb["SecurityGroups"] == [security_group.id] + assert lb["AvailabilityZones"] == [ + {"SubnetId": subnet1.id, "ZoneName": "us-east-1a"}, + {"SubnetId": subnet2.id, "ZoneName": "us-east-1b"}, + ] + assert lb["CreatedTime"].tzinfo is not None + assert lb["State"]["Code"] == "provisioning" + lb_arn = lb["LoadBalancerArn"] # Ensure the tags persisted tag_desc = conn.describe_tags(ResourceArns=[lb_arn])["TagDescriptions"][0] - tag_desc.should.have.key("ResourceArn").equals(lb_arn) + assert tag_desc["ResourceArn"] == lb_arn tags = {d["Key"]: d["Value"] for d in tag_desc["Tags"]} - tags.should.equal({"key_name": "a_value"}) + assert tags == {"key_name": "a_value"} def create_load_balancer(): @@ -91,13 +89,9 @@ def test_create_elb_using_subnetmapping(): ) lb = conn.describe_load_balancers()["LoadBalancers"][0] - lb.should.have.key("AvailabilityZones").length_of(2) - lb["AvailabilityZones"].should.contain( - {"ZoneName": "us-west-1a", "SubnetId": subnet1.id} - ) - lb["AvailabilityZones"].should.contain( - {"ZoneName": "us-west-1b", "SubnetId": subnet2.id} - ) + assert len(lb["AvailabilityZones"]) == 2 + assert {"ZoneName": "us-west-1a", "SubnetId": subnet1.id} in lb["AvailabilityZones"] + assert {"ZoneName": "us-west-1b", "SubnetId": subnet2.id} in lb["AvailabilityZones"] @mock_elbv2 @@ -107,18 +101,16 @@ def test_describe_load_balancers(): response = conn.describe_load_balancers() - response.get("LoadBalancers").should.have.length_of(1) - lb = response.get("LoadBalancers")[0] - lb.get("LoadBalancerName").should.equal("my-lb") - lb.get("State").get("Code").should.equal("active") + assert len(response["LoadBalancers"]) == 1 + lb = response["LoadBalancers"][0] + assert lb["LoadBalancerName"] == "my-lb" + assert lb["State"]["Code"] == "active" - response = conn.describe_load_balancers( - LoadBalancerArns=[lb.get("LoadBalancerArn")] - ) - response.get("LoadBalancers")[0].get("LoadBalancerName").should.equal("my-lb") + response = conn.describe_load_balancers(LoadBalancerArns=[lb["LoadBalancerArn"]]) + assert response["LoadBalancers"][0]["LoadBalancerName"] == "my-lb" response = conn.describe_load_balancers(Names=["my-lb"]) - response.get("LoadBalancers")[0].get("LoadBalancerName").should.equal("my-lb") + assert response["LoadBalancers"][0]["LoadBalancerName"] == "my-lb" with pytest.raises(ClientError): conn.describe_load_balancers(LoadBalancerArns=["not-a/real/arn"]) @@ -134,9 +126,9 @@ def test_describe_listeners(): with pytest.raises(ClientError) as exc: conn.describe_listeners() err = exc.value.response["Error"] - err["Code"].should.equal("ValidationError") - err["Message"].should.equal( - "You must specify either listener ARNs or a load balancer ARN" + assert err["Code"] == "ValidationError" + assert ( + err["Message"] == "You must specify either listener ARNs or a load balancer ARN" ) @@ -146,26 +138,26 @@ def test_add_remove_tags(): _, _, _, _, _, conn = create_load_balancer() lbs = conn.describe_load_balancers()["LoadBalancers"] - lbs.should.have.length_of(1) + assert len(lbs) == 1 lb = lbs[0] with pytest.raises(ClientError): conn.add_tags(ResourceArns=["missing-arn"], Tags=[{"Key": "a", "Value": "b"}]) conn.add_tags( - ResourceArns=[lb.get("LoadBalancerArn")], Tags=[{"Key": "a", "Value": "b"}] + ResourceArns=[lb["LoadBalancerArn"]], Tags=[{"Key": "a", "Value": "b"}] ) tags = { d["Key"]: d["Value"] - for d in conn.describe_tags(ResourceArns=[lb.get("LoadBalancerArn")])[ + for d in conn.describe_tags(ResourceArns=[lb["LoadBalancerArn"]])[ "TagDescriptions" ][0]["Tags"] } - tags.should.have.key("a").which.should.equal("b") + assert tags["a"] == "b" conn.add_tags( - ResourceArns=[lb.get("LoadBalancerArn")], + ResourceArns=[lb["LoadBalancerArn"]], Tags=[ {"Key": "a", "Value": "b"}, {"Key": "b", "Value": "b"}, @@ -181,51 +173,51 @@ def test_add_remove_tags(): with pytest.raises(ClientError) as exc: conn.add_tags( - ResourceArns=[lb.get("LoadBalancerArn")], Tags=[{"Key": "k", "Value": "b"}] + ResourceArns=[lb["LoadBalancerArn"]], Tags=[{"Key": "k", "Value": "b"}] ) err = exc.value.response["Error"] - err["Code"].should.equal("TooManyTagsError") + assert err["Code"] == "TooManyTagsError" conn.add_tags( - ResourceArns=[lb.get("LoadBalancerArn")], Tags=[{"Key": "j", "Value": "c"}] + ResourceArns=[lb["LoadBalancerArn"]], Tags=[{"Key": "j", "Value": "c"}] ) tags = { d["Key"]: d["Value"] - for d in conn.describe_tags(ResourceArns=[lb.get("LoadBalancerArn")])[ + for d in conn.describe_tags(ResourceArns=[lb["LoadBalancerArn"]])[ "TagDescriptions" ][0]["Tags"] } - tags.should.have.key("a").which.should.equal("b") - tags.should.have.key("b").which.should.equal("b") - tags.should.have.key("c").which.should.equal("b") - tags.should.have.key("d").which.should.equal("b") - tags.should.have.key("e").which.should.equal("b") - tags.should.have.key("f").which.should.equal("b") - tags.should.have.key("g").which.should.equal("b") - tags.should.have.key("h").which.should.equal("b") - tags.should.have.key("j").which.should.equal("c") - tags.shouldnt.have.key("k") + assert tags["a"] == "b" + assert tags["b"] == "b" + assert tags["c"] == "b" + assert tags["d"] == "b" + assert tags["e"] == "b" + assert tags["f"] == "b" + assert tags["g"] == "b" + assert tags["h"] == "b" + assert tags["j"] == "c" + assert "k" not in tags - conn.remove_tags(ResourceArns=[lb.get("LoadBalancerArn")], TagKeys=["a"]) + conn.remove_tags(ResourceArns=[lb["LoadBalancerArn"]], TagKeys=["a"]) tags = { d["Key"]: d["Value"] - for d in conn.describe_tags(ResourceArns=[lb.get("LoadBalancerArn")])[ + for d in conn.describe_tags(ResourceArns=[lb["LoadBalancerArn"]])[ "TagDescriptions" ][0]["Tags"] } - tags.shouldnt.have.key("a") - tags.should.have.key("b").which.should.equal("b") - tags.should.have.key("c").which.should.equal("b") - tags.should.have.key("d").which.should.equal("b") - tags.should.have.key("e").which.should.equal("b") - tags.should.have.key("f").which.should.equal("b") - tags.should.have.key("g").which.should.equal("b") - tags.should.have.key("h").which.should.equal("b") - tags.should.have.key("j").which.should.equal("c") + assert "a" not in tags + assert tags["b"] == "b" + assert tags["c"] == "b" + assert tags["d"] == "b" + assert tags["e"] == "b" + assert tags["f"] == "b" + assert tags["g"] == "b" + assert tags["h"] == "b" + assert tags["j"] == "c" @mock_elbv2 @@ -254,23 +246,17 @@ def test_create_elb_in_multiple_region(): Tags=[{"Key": "key_name", "Value": "a_value"}], ) - list( - boto3.client("elbv2", region_name="us-west-1") - .describe_load_balancers() - .get("LoadBalancers") - ).should.have.length_of(1) - list( - boto3.client("elbv2", region_name="us-west-2") - .describe_load_balancers() - .get("LoadBalancers") - ).should.have.length_of(1) + west_1_lbs = boto3.client("elbv2", "us-west-1").describe_load_balancers() + assert len(west_1_lbs["LoadBalancers"]) == 1 + west_2_lbs = boto3.client("elbv2", "us-west-2").describe_load_balancers() + assert len(west_2_lbs["LoadBalancers"]) == 1 @mock_elbv2 @mock_ec2 def test_create_listeners_without_port(): response, vpc, _, _, _, conn = create_load_balancer() - load_balancer_arn = response.get("LoadBalancers")[0].get("LoadBalancerArn") + load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"] response = conn.create_target_group( Name="a-target", Protocol="HTTP", @@ -285,7 +271,7 @@ def test_create_listeners_without_port(): UnhealthyThresholdCount=2, Matcher={"HttpCode": "200"}, ) - target_group = response.get("TargetGroups")[0] + target_group = response["TargetGroups"][0] target_group_arn = target_group["TargetGroupArn"] response = conn.create_listener( LoadBalancerArn=load_balancer_arn, @@ -293,12 +279,12 @@ def test_create_listeners_without_port(): DefaultActions=[{"Type": "forward", "TargetGroupArn": target_group_arn}], ) - listener = response.get("Listeners")[0] - listener.get("Port").should.equal(None) - listener.get("Protocol").should.equal("HTTP") - listener.get("DefaultActions").should.equal( - [{"TargetGroupArn": target_group_arn, "Type": "forward"}] - ) + listener = response["Listeners"][0] + assert listener.get("Port") is None + assert listener["Protocol"] == "HTTP" + assert listener["DefaultActions"] == [ + {"TargetGroupArn": target_group_arn, "Type": "forward"} + ] @mock_ec2 @@ -309,12 +295,12 @@ def test_create_rule_forward_config_as_second_arg(): # Actions with type=forward had to be the first action specified response, vpc, _, _, _, elbv2 = create_load_balancer() - load_balancer_arn = response.get("LoadBalancers")[0].get("LoadBalancerArn") + load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"] response = elbv2.create_listener( LoadBalancerArn=load_balancer_arn, Protocol="HTTP", Port=80, DefaultActions=[] ) - http_listener_arn = response.get("Listeners")[0]["ListenerArn"] + http_listener_arn = response["Listeners"][0]["ListenerArn"] priority = 100 @@ -328,10 +314,10 @@ def test_create_rule_forward_config_as_second_arg(): HealthCheckPath="/", Matcher={"HttpCode": "200"}, ) - target_group = response.get("TargetGroups")[0] + target_group = response["TargetGroups"][0] # No targets registered yet - target_group_arn = target_group.get("TargetGroupArn") + target_group_arn = target_group["TargetGroupArn"] elbv2.create_rule( ListenerArn=http_listener_arn, Conditions=[ @@ -368,16 +354,14 @@ def test_create_rule_forward_config_as_second_arg(): our_rule = all_rules[0] actions = our_rule["Actions"] forward_action = [a for a in actions if "ForwardConfig" in a.keys()][0] - forward_action.should.equal( - { - "ForwardConfig": { - "TargetGroups": [{"TargetGroupArn": target_group_arn, "Weight": 1}], - "TargetGroupStickinessConfig": {"Enabled": False}, - }, - "Type": "forward", - "Order": 2, - } - ) + assert forward_action == { + "ForwardConfig": { + "TargetGroups": [{"TargetGroupArn": target_group_arn, "Weight": 1}], + "TargetGroupStickinessConfig": {"Enabled": False}, + }, + "Type": "forward", + "Order": 2, + } @mock_elbv2 @@ -407,10 +391,10 @@ def test_describe_paginated_balancers(): ) resp = conn.describe_load_balancers() - resp["LoadBalancers"].should.have.length_of(50) - resp["NextMarker"].should.equal(resp["LoadBalancers"][-1]["LoadBalancerName"]) + assert len(resp["LoadBalancers"]) == 50 + assert resp["NextMarker"] == resp["LoadBalancers"][-1]["LoadBalancerName"] resp2 = conn.describe_load_balancers(Marker=resp["NextMarker"]) - resp2["LoadBalancers"].should.have.length_of(1) + assert len(resp2["LoadBalancers"]) == 1 assert "NextToken" not in resp2.keys() @@ -419,12 +403,12 @@ def test_describe_paginated_balancers(): def test_delete_load_balancer(): response, _, _, _, _, conn = create_load_balancer() - response.get("LoadBalancers").should.have.length_of(1) - lb = response.get("LoadBalancers")[0] + assert len(response["LoadBalancers"]) == 1 + lb = response["LoadBalancers"][0] - conn.delete_load_balancer(LoadBalancerArn=lb.get("LoadBalancerArn")) - balancers = conn.describe_load_balancers().get("LoadBalancers") - balancers.should.have.length_of(0) + conn.delete_load_balancer(LoadBalancerArn=lb["LoadBalancerArn"]) + balancers = conn.describe_load_balancers()["LoadBalancers"] + assert len(balancers) == 0 @mock_ec2 @@ -466,20 +450,20 @@ def test_register_targets(): UnhealthyThresholdCount=2, Matcher={"HttpCode": "200"}, ) - target_group = response.get("TargetGroups")[0] + target_group = response["TargetGroups"][0] # No targets registered yet response = conn.describe_target_health( - TargetGroupArn=target_group.get("TargetGroupArn") + TargetGroupArn=target_group["TargetGroupArn"] ) - response.get("TargetHealthDescriptions").should.have.length_of(0) + assert len(response["TargetHealthDescriptions"]) == 0 response = ec2.create_instances(ImageId=EXAMPLE_AMI_ID, MinCount=2, MaxCount=2) instance_id1 = response[0].id instance_id2 = response[1].id response = conn.register_targets( - TargetGroupArn=target_group.get("TargetGroupArn"), + TargetGroupArn=target_group["TargetGroupArn"], Targets=[ {"Id": instance_id1, "Port": 5060}, {"Id": instance_id2, "Port": 4030}, @@ -487,48 +471,48 @@ def test_register_targets(): ) response = conn.describe_target_health( - TargetGroupArn=target_group.get("TargetGroupArn") + TargetGroupArn=target_group["TargetGroupArn"] ) - response.get("TargetHealthDescriptions").should.have.length_of(2) + assert len(response["TargetHealthDescriptions"]) == 2 response = conn.deregister_targets( - TargetGroupArn=target_group.get("TargetGroupArn"), + TargetGroupArn=target_group["TargetGroupArn"], Targets=[{"Id": instance_id2}], ) response = conn.describe_target_health( - TargetGroupArn=target_group.get("TargetGroupArn") + TargetGroupArn=target_group["TargetGroupArn"] ) - response.get("TargetHealthDescriptions").should.have.length_of(1) + assert len(response["TargetHealthDescriptions"]) == 1 def get_target_by_instance_id(instance_id): - for target in response.get("TargetHealthDescriptions"): - if target.get("Target").get("Id") == instance_id: + for target in response["TargetHealthDescriptions"]: + if target["Target"]["Id"] == instance_id: return target return None def assert_target_not_registered(target): - assert target.get("TargetHealth").get("State") == "unavailable" - assert target.get("TargetHealth").get("Reason") == "Target.NotRegistered" + assert target["TargetHealth"]["State"] == "unavailable" + assert target["TargetHealth"]["Reason"] == "Target.NotRegistered" response = conn.describe_target_health( - TargetGroupArn=target_group.get("TargetGroupArn"), + TargetGroupArn=target_group["TargetGroupArn"], Targets=[{"Id": instance_id2}], ) - response.get("TargetHealthDescriptions").should.have.length_of(1) + assert len(response["TargetHealthDescriptions"]) == 1 target_default_port = get_target_by_instance_id(instance_id2) assert target_default_port is not None - assert target_default_port.get("Target").get("Port") == 8080 + assert target_default_port["Target"]["Port"] == 8080 assert_target_not_registered(target_default_port) response = conn.describe_target_health( - TargetGroupArn=target_group.get("TargetGroupArn"), + TargetGroupArn=target_group["TargetGroupArn"], Targets=[{"Id": instance_id2, "Port": 4030}], ) - response.get("TargetHealthDescriptions").should.have.length_of(1) + assert len(response["TargetHealthDescriptions"]) == 1 target_custom_port = get_target_by_instance_id(instance_id2) assert target_custom_port is not None - assert target_custom_port.get("Target").get("Port") == 4030 + assert target_custom_port["Target"]["Port"] == 4030 assert_target_not_registered(target_custom_port) @@ -572,13 +556,13 @@ def test_stopped_instance_target(): UnhealthyThresholdCount=2, Matcher={"HttpCode": "200"}, ) - target_group = response.get("TargetGroups")[0] + target_group = response["TargetGroups"][0] # No targets registered yet response = conn.describe_target_health( - TargetGroupArn=target_group.get("TargetGroupArn") + TargetGroupArn=target_group["TargetGroupArn"] ) - response.get("TargetHealthDescriptions").should.have.length_of(0) + assert len(response["TargetHealthDescriptions"]) == 0 response = ec2.create_instances(ImageId=EXAMPLE_AMI_ID, MinCount=1, MaxCount=1) instance = response[0] @@ -586,35 +570,33 @@ def test_stopped_instance_target(): target_dict = {"Id": instance.id, "Port": 500} response = conn.register_targets( - TargetGroupArn=target_group.get("TargetGroupArn"), Targets=[target_dict] + TargetGroupArn=target_group["TargetGroupArn"], Targets=[target_dict] ) response = conn.describe_target_health( - TargetGroupArn=target_group.get("TargetGroupArn") + TargetGroupArn=target_group["TargetGroupArn"] ) - response.get("TargetHealthDescriptions").should.have.length_of(1) - target_health_description = response.get("TargetHealthDescriptions")[0] + assert len(response["TargetHealthDescriptions"]) == 1 + target_health_description = response["TargetHealthDescriptions"][0] - target_health_description["Target"].should.equal(target_dict) - target_health_description["HealthCheckPort"].should.equal(str(target_group_port)) - target_health_description["TargetHealth"].should.equal({"State": "healthy"}) + assert target_health_description["Target"] == target_dict + assert target_health_description["HealthCheckPort"] == str(target_group_port) + assert target_health_description["TargetHealth"] == {"State": "healthy"} instance.stop() response = conn.describe_target_health( - TargetGroupArn=target_group.get("TargetGroupArn") - ) - response.get("TargetHealthDescriptions").should.have.length_of(1) - target_health_description = response.get("TargetHealthDescriptions")[0] - target_health_description["Target"].should.equal(target_dict) - target_health_description["HealthCheckPort"].should.equal(str(target_group_port)) - target_health_description["TargetHealth"].should.equal( - { - "State": "unused", - "Reason": "Target.InvalidState", - "Description": "Target is in the stopped state", - } + TargetGroupArn=target_group["TargetGroupArn"] ) + assert len(response["TargetHealthDescriptions"]) == 1 + target_health_description = response["TargetHealthDescriptions"][0] + assert target_health_description["Target"] == target_dict + assert target_health_description["HealthCheckPort"] == str(target_group_port) + assert target_health_description["TargetHealth"] == { + "State": "unused", + "Reason": "Target.InvalidState", + "Description": "Target is in the stopped state", + } @mock_ec2 @@ -657,13 +639,13 @@ def test_terminated_instance_target(): UnhealthyThresholdCount=2, Matcher={"HttpCode": "200"}, ) - target_group = response.get("TargetGroups")[0] + target_group = response["TargetGroups"][0] # No targets registered yet response = conn.describe_target_health( - TargetGroupArn=target_group.get("TargetGroupArn") + TargetGroupArn=target_group["TargetGroupArn"] ) - response.get("TargetHealthDescriptions").should.have.length_of(0) + assert len(response["TargetHealthDescriptions"]) == 0 response = ec2.create_instances(ImageId=EXAMPLE_AMI_ID, MinCount=1, MaxCount=1) instance = response[0] @@ -671,25 +653,25 @@ def test_terminated_instance_target(): target_dict = {"Id": instance.id, "Port": 500} response = conn.register_targets( - TargetGroupArn=target_group.get("TargetGroupArn"), Targets=[target_dict] + TargetGroupArn=target_group["TargetGroupArn"], Targets=[target_dict] ) response = conn.describe_target_health( - TargetGroupArn=target_group.get("TargetGroupArn") + TargetGroupArn=target_group["TargetGroupArn"] ) - response.get("TargetHealthDescriptions").should.have.length_of(1) - target_health_description = response.get("TargetHealthDescriptions")[0] + assert len(response["TargetHealthDescriptions"]) == 1 + target_health_description = response["TargetHealthDescriptions"][0] - target_health_description["Target"].should.equal(target_dict) - target_health_description["HealthCheckPort"].should.equal(str(target_group_port)) - target_health_description["TargetHealth"].should.equal({"State": "healthy"}) + assert target_health_description["Target"] == target_dict + assert target_health_description["HealthCheckPort"] == str(target_group_port) + assert target_health_description["TargetHealth"] == {"State": "healthy"} instance.terminate() response = conn.describe_target_health( - TargetGroupArn=target_group.get("TargetGroupArn") + TargetGroupArn=target_group["TargetGroupArn"] ) - response.get("TargetHealthDescriptions").should.have.length_of(0) + assert len(response["TargetHealthDescriptions"]) == 0 @mock_elbv2 @@ -697,12 +679,12 @@ def test_terminated_instance_target(): def test_create_rule_priority_in_use(): response, _, _, _, _, elbv2 = create_load_balancer() - load_balancer_arn = response.get("LoadBalancers")[0].get("LoadBalancerArn") + load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"] response = elbv2.create_listener( LoadBalancerArn=load_balancer_arn, Protocol="HTTP", Port=80, DefaultActions=[] ) - http_listener_arn = response.get("Listeners")[0]["ListenerArn"] + http_listener_arn = response["Listeners"][0]["ListenerArn"] priority = 100 elbv2.create_rule( @@ -715,15 +697,15 @@ def test_create_rule_priority_in_use(): ListenerArn=http_listener_arn, Priority=priority, Conditions=[], Actions=[] ) err = ex.value.response["Error"] - err["Code"].should.equal("PriorityInUse") - err["Message"].should.equal("The specified priority is in use.") + assert err["Code"] == "PriorityInUse" + assert err["Message"] == "The specified priority is in use." @mock_elbv2 @mock_ec2 def test_modify_rule_conditions(): response, _, _, _, _, elbv2 = create_load_balancer() - load_balancer_arn = response.get("LoadBalancers")[0].get("LoadBalancerArn") + load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"] action = { "Type": "redirect", @@ -741,7 +723,7 @@ def test_modify_rule_conditions(): Port=80, DefaultActions=[action], ) - http_listener_arn = response.get("Listeners")[0]["ListenerArn"] + http_listener_arn = response["Listeners"][0]["ListenerArn"] response = elbv2.create_rule( ListenerArn=http_listener_arn, Priority=100, Conditions=[], Actions=[] @@ -779,7 +761,7 @@ def test_modify_rule_conditions(): def test_handle_listener_rules(): response, vpc, _, _, _, conn = create_load_balancer() - load_balancer_arn = response.get("LoadBalancers")[0].get("LoadBalancerArn") + load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"] response = conn.create_target_group( Name="a-target", @@ -795,7 +777,7 @@ def test_handle_listener_rules(): UnhealthyThresholdCount=2, Matcher={"HttpCode": "200"}, ) - target_group = response.get("TargetGroups")[0] + target_group = response["TargetGroups"][0] # Plain HTTP listener response = conn.create_listener( @@ -803,16 +785,16 @@ def test_handle_listener_rules(): Protocol="HTTP", Port=80, DefaultActions=[ - {"Type": "forward", "TargetGroupArn": target_group.get("TargetGroupArn")} + {"Type": "forward", "TargetGroupArn": target_group["TargetGroupArn"]} ], ) - listener = response.get("Listeners")[0] - listener.get("Port").should.equal(80) - listener.get("Protocol").should.equal("HTTP") - listener.get("DefaultActions").should.equal( - [{"TargetGroupArn": target_group.get("TargetGroupArn"), "Type": "forward"}] - ) - http_listener_arn = listener.get("ListenerArn") + listener = response["Listeners"][0] + assert listener["Port"] == 80 + assert listener["Protocol"] == "HTTP" + assert listener["DefaultActions"] == [ + {"TargetGroupArn": target_group["TargetGroupArn"], "Type": "forward"} + ] + http_listener_arn = listener["ListenerArn"] # create first rule priority = 100 @@ -830,12 +812,10 @@ def test_handle_listener_rules(): "PathPatternConfig": {"Values": [pathpatternconfig_pattern]}, }, ], - Actions=[ - {"TargetGroupArn": target_group.get("TargetGroupArn"), "Type": "forward"} - ], + Actions=[{"TargetGroupArn": target_group["TargetGroupArn"], "Type": "forward"}], ) - rule = created_rule.get("Rules")[0] - rule["Priority"].should.equal("100") + rule = created_rule["Rules"][0] + assert rule["Priority"] == "100" # check if rules is sorted by priority priority = 500 @@ -852,9 +832,7 @@ def test_handle_listener_rules(): "PathPatternConfig": {"Values": [pathpatternconfig_pattern]}, }, ], - Actions=[ - {"TargetGroupArn": target_group.get("TargetGroupArn"), "Type": "forward"} - ], + Actions=[{"TargetGroupArn": target_group["TargetGroupArn"], "Type": "forward"}], ) # add rule that uses forward_config @@ -878,11 +856,11 @@ def test_handle_listener_rules(): "ForwardConfig": { "TargetGroups": [ { - "TargetGroupArn": target_group.get("TargetGroupArn"), + "TargetGroupArn": target_group["TargetGroupArn"], "Weight": 1, }, { - "TargetGroupArn": target_group.get("TargetGroupArn"), + "TargetGroupArn": target_group["TargetGroupArn"], "Weight": 2, }, ] @@ -906,7 +884,7 @@ def test_handle_listener_rules(): ], Actions=[ { - "TargetGroupArn": target_group.get("TargetGroupArn"), + "TargetGroupArn": target_group["TargetGroupArn"], "Type": "forward", } ], @@ -914,31 +892,30 @@ def test_handle_listener_rules(): # test for describe listeners obtained_rules = conn.describe_rules(ListenerArn=http_listener_arn) - obtained_rules["Rules"].should.have.length_of(4) + assert len(obtained_rules["Rules"]) == 4 priorities = [rule["Priority"] for rule in obtained_rules["Rules"]] - priorities.should.equal(["100", "500", "550", "default"]) + assert priorities == ["100", "500", "550", "default"] first_rule = obtained_rules["Rules"][0] second_rule = obtained_rules["Rules"][1] third_rule = obtained_rules["Rules"][2] default_rule = obtained_rules["Rules"][3] - first_rule["IsDefault"].should.equal(False) - default_rule["IsDefault"].should.equal(True) + assert first_rule["IsDefault"] is False + assert default_rule["IsDefault"] is True obtained_rules = conn.describe_rules(RuleArns=[first_rule["RuleArn"]]) - obtained_rules["Rules"].should.equal([first_rule]) + assert obtained_rules["Rules"] == [first_rule] # test for pagination obtained_rules = conn.describe_rules(ListenerArn=http_listener_arn, PageSize=1) - len(obtained_rules["Rules"]).should.equal(1) - obtained_rules.should.have.key("NextMarker") + assert len(obtained_rules["Rules"]) == 1 next_marker = obtained_rules["NextMarker"] following_rules = conn.describe_rules( ListenerArn=http_listener_arn, PageSize=1, Marker=next_marker ) - len(following_rules["Rules"]).should.equal(1) - following_rules["Rules"][0]["RuleArn"].should_not.equal( - obtained_rules["Rules"][0]["RuleArn"] + assert len(following_rules["Rules"]) == 1 + assert ( + following_rules["Rules"][0]["RuleArn"] != obtained_rules["Rules"][0]["RuleArn"] ) # test for invalid describe rule request @@ -969,13 +946,14 @@ def test_handle_listener_rules(): rules = conn.describe_rules(ListenerArn=http_listener_arn) obtained_rule = rules["Rules"][0] - obtained_rule["Conditions"][0]["Values"][0].should.equal(new_host) - obtained_rule["Conditions"][1]["Values"][0].should.equal(new_path_pattern) - obtained_rule["Conditions"][2]["PathPatternConfig"]["Values"][0].should.equal( - new_pathpatternconfig_pattern + assert obtained_rule["Conditions"][0]["Values"][0] == new_host + assert obtained_rule["Conditions"][1]["Values"][0] == new_path_pattern + assert ( + obtained_rule["Conditions"][2]["PathPatternConfig"]["Values"][0] + == new_pathpatternconfig_pattern ) - obtained_rule["Actions"][0]["TargetGroupArn"].should.equal( - target_group.get("TargetGroupArn") + assert ( + obtained_rule["Actions"][0]["TargetGroupArn"] == target_group["TargetGroupArn"] ) # modify priority @@ -990,11 +968,11 @@ def test_handle_listener_rules(): ) # assert response of SetRulePriorities operation - updated_rule["Rules"].should.have.length_of(1) - updated_rule["Rules"][0]["RuleArn"].should.equal(first_rule["RuleArn"]) - updated_rule["Rules"][0]["Priority"].should.equal(str(new_priority)) - updated_rule["Rules"][0]["Conditions"].should.have.length_of(3) - updated_rule["Rules"][0]["Actions"].should.have.length_of(1) + assert len(updated_rule["Rules"]) == 1 + assert updated_rule["Rules"][0]["RuleArn"] == first_rule["RuleArn"] + assert updated_rule["Rules"][0]["Priority"] == str(new_priority) + assert len(updated_rule["Rules"][0]["Conditions"]) == 3 + assert len(updated_rule["Rules"][0]["Actions"]) == 1 # modify forward_config rule partially rule new_host_2 = "new.examplewebsite.com" @@ -1010,20 +988,19 @@ def test_handle_listener_rules(): "PathPatternConfig": {"Values": [new_pathpatternconfig_pattern_2]}, }, ], - Actions=[ - {"TargetGroupArn": target_group.get("TargetGroupArn"), "Type": "forward"} - ], + Actions=[{"TargetGroupArn": target_group["TargetGroupArn"], "Type": "forward"}], ) rules = conn.describe_rules(ListenerArn=http_listener_arn) obtained_rule = rules["Rules"][2] - obtained_rule["Conditions"][0]["Values"][0].should.equal(new_host_2) - obtained_rule["Conditions"][1]["Values"][0].should.equal(new_path_pattern_2) - obtained_rule["Conditions"][2]["PathPatternConfig"]["Values"][0].should.equal( - new_pathpatternconfig_pattern_2 + assert obtained_rule["Conditions"][0]["Values"][0] == new_host_2 + assert obtained_rule["Conditions"][1]["Values"][0] == new_path_pattern_2 + assert ( + obtained_rule["Conditions"][2]["PathPatternConfig"]["Values"][0] + == new_pathpatternconfig_pattern_2 ) - obtained_rule["Actions"][0]["TargetGroupArn"].should.equal( - target_group.get("TargetGroupArn") + assert ( + obtained_rule["Actions"][0]["TargetGroupArn"] == target_group["TargetGroupArn"] ) # modify priority @@ -1049,7 +1026,7 @@ def test_handle_listener_rules(): arn = first_rule["RuleArn"] conn.delete_rule(RuleArn=arn) rules = conn.describe_rules(ListenerArn=http_listener_arn)["Rules"] - len(rules).should.equal(3) + assert len(rules) == 3 # test for invalid action type safe_priority = 2 @@ -1063,7 +1040,7 @@ def test_handle_listener_rules(): ], Actions=[ { - "TargetGroupArn": target_group.get("TargetGroupArn"), + "TargetGroupArn": target_group["TargetGroupArn"], "Type": "forward2", } ], @@ -1071,7 +1048,7 @@ def test_handle_listener_rules(): # test for invalid action type safe_priority = 2 - invalid_target_group_arn = target_group.get("TargetGroupArn") + "x" + invalid_target_group_arn = target_group["TargetGroupArn"] + "x" with pytest.raises(ClientError): conn.create_rule( ListenerArn=http_listener_arn, @@ -1092,7 +1069,7 @@ def test_handle_listener_rules(): Conditions=[{"Field": "xxxxxxx", "Values": [host]}], Actions=[ { - "TargetGroupArn": target_group.get("TargetGroupArn"), + "TargetGroupArn": target_group["TargetGroupArn"], "Type": "forward", } ], @@ -1107,7 +1084,7 @@ def test_handle_listener_rules(): Conditions=[{"Field": "host-header", "Values": []}], Actions=[ { - "TargetGroupArn": target_group.get("TargetGroupArn"), + "TargetGroupArn": target_group["TargetGroupArn"], "Type": "forward", } ], @@ -1122,7 +1099,7 @@ def test_handle_listener_rules(): Conditions=[{"Field": "host-header", "Values": [host, host]}], Actions=[ { - "TargetGroupArn": target_group.get("TargetGroupArn"), + "TargetGroupArn": target_group["TargetGroupArn"], "Type": "forward", } ], @@ -1134,8 +1111,8 @@ def test_describe_account_limits(): client = boto3.client("elbv2", region_name="eu-central-1") resp = client.describe_account_limits() - resp["Limits"][0].should.contain("Name") - resp["Limits"][0].should.contain("Max") + assert "Name" in resp["Limits"][0] + assert "Max" in resp["Limits"][0] @mock_elbv2 @@ -1143,12 +1120,12 @@ def test_describe_ssl_policies(): client = boto3.client("elbv2", region_name="eu-central-1") resp = client.describe_ssl_policies() - len(resp["SslPolicies"]).should.equal(6) + assert len(resp["SslPolicies"]) == 6 resp = client.describe_ssl_policies( Names=["ELBSecurityPolicy-TLS-1-2-2017-01", "ELBSecurityPolicy-2016-08"] ) - len(resp["SslPolicies"]).should.equal(2) + assert len(resp["SslPolicies"]) == 2 resp = client.describe_ssl_policies( Names=[ @@ -1157,7 +1134,7 @@ def test_describe_ssl_policies(): "ELBSecurityPolicy-2016-08", ] ) - len(resp["SslPolicies"]).should.equal(2) + assert len(resp["SslPolicies"]) == 2 @mock_elbv2 @@ -1217,7 +1194,7 @@ def test_set_security_groups(): ) resp = client.describe_load_balancers(LoadBalancerArns=[arn]) - len(resp["LoadBalancers"][0]["SecurityGroups"]).should.equal(2) + assert len(resp["LoadBalancers"][0]["SecurityGroups"]) == 2 with pytest.raises(ClientError): client.set_security_groups(LoadBalancerArn=arn, SecurityGroups=["non_existent"]) @@ -1242,7 +1219,7 @@ def test_modify_load_balancer_attributes_idle_timeout(): response["Attributes"], ) )[0] - idle_timeout["Value"].should.equal("600") + assert idle_timeout["Value"] == "600" @mock_elbv2 @@ -1262,7 +1239,7 @@ def test_modify_load_balancer_attributes_routing_http2_enabled(): lambda item: item["Key"] == "routing.http2.enabled", response["Attributes"] ) )[0] - routing_http2_enabled["Value"].should.equal("false") + assert routing_http2_enabled["Value"] == "false" @mock_elbv2 @@ -1280,8 +1257,8 @@ def test_modify_load_balancer_attributes_crosszone_enabled(): ) attrs = client.describe_load_balancer_attributes(LoadBalancerArn=arn)["Attributes"] - attrs.should.contain({"Key": "deletion_protection.enabled", "Value": "false"}) - attrs.should.contain({"Key": "load_balancing.cross_zone.enabled", "Value": "false"}) + assert {"Key": "deletion_protection.enabled", "Value": "false"} in attrs + assert {"Key": "load_balancing.cross_zone.enabled", "Value": "false"} in attrs @mock_elbv2 @@ -1305,7 +1282,7 @@ def test_modify_load_balancer_attributes_routing_http_drop_invalid_header_fields response["Attributes"], ) )[0] - routing_http_drop_invalid_header_fields_enabled["Value"].should.equal("false") + assert routing_http_drop_invalid_header_fields_enabled["Value"] == "false" @mock_elbv2 @@ -1335,7 +1312,7 @@ def test_modify_listener_http_to_https(): Tags=[{"Key": "key_name", "Value": "a_value"}], ) - load_balancer_arn = response.get("LoadBalancers")[0].get("LoadBalancerArn") + load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"] response = client.create_target_group( Name="a-target", @@ -1351,7 +1328,7 @@ def test_modify_listener_http_to_https(): UnhealthyThresholdCount=2, Matcher={"HttpCode": "200"}, ) - target_group = response.get("TargetGroups")[0] + target_group = response["TargetGroups"][0] target_group_arn = target_group["TargetGroupArn"] # Plain HTTP listener @@ -1374,9 +1351,10 @@ def test_modify_listener_http_to_https(): DefaultActions=[{"Type": "forward", "TargetGroupArn": target_group_arn}], ) err = ex.value.response["Error"] - err["Code"].should.equal("CertificateWereNotPassed") - err["Message"].should.equal( - "You must provide a list containing exactly one certificate if the listener protocol is HTTPS." + assert err["Code"] == "CertificateWereNotPassed" + assert ( + err["Message"] + == "You must provide a list containing exactly one certificate if the listener protocol is HTTPS." ) acm.request_certificate( @@ -1397,12 +1375,10 @@ def test_modify_listener_http_to_https(): Certificates=[{"CertificateArn": yahoo_arn}], DefaultActions=[{"Type": "forward", "TargetGroupArn": target_group_arn}], ) - response["Listeners"][0]["Port"].should.equal(443) - response["Listeners"][0]["Protocol"].should.equal("HTTPS") - response["Listeners"][0]["SslPolicy"].should.equal( - "ELBSecurityPolicy-TLS-1-2-2017-01" - ) - len(response["Listeners"][0]["Certificates"]).should.equal(1) + assert response["Listeners"][0]["Port"] == 443 + assert response["Listeners"][0]["Protocol"] == "HTTPS" + assert response["Listeners"][0]["SslPolicy"] == "ELBSecurityPolicy-TLS-1-2-2017-01" + assert len(response["Listeners"][0]["Certificates"]) == 1 # Check default cert, can't do this in server mode if os.environ.get("TEST_SERVER_MODE", "false").lower() == "false": @@ -1411,7 +1387,7 @@ def test_modify_listener_http_to_https(): .load_balancers[load_balancer_arn] .listeners[listener_arn] ) - listener.certificate.should.equal(yahoo_arn) + assert listener.certificate == yahoo_arn # Bad cert with pytest.raises(ClientError) as exc: @@ -1424,7 +1400,7 @@ def test_modify_listener_http_to_https(): DefaultActions=[{"Type": "forward", "TargetGroupArn": target_group_arn}], ) err = exc.value.response["Error"] - err["Message"].should.equal("Certificate lalala not found") + assert err["Message"] == "Certificate lalala not found" # Unknown protocol with pytest.raises(ClientError) as exc: @@ -1437,7 +1413,7 @@ def test_modify_listener_http_to_https(): DefaultActions=[{"Type": "forward", "TargetGroupArn": target_group_arn}], ) err = exc.value.response["Error"] - err["Message"].should.equal("Protocol HTP is not supported") + assert err["Message"] == "Protocol HTP is not supported" @mock_acm @@ -1465,12 +1441,12 @@ def test_modify_listener_of_https_target_group(): Tags=[{"Key": "key_name", "Value": "a_value"}], ) - load_balancer_arn = response.get("LoadBalancers")[0].get("LoadBalancerArn") + load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"] response = client.create_target_group( Name="a-target", Protocol="HTTPS", Port=8443, VpcId=vpc.id ) - target_group = response.get("TargetGroups")[0] + target_group = response["TargetGroups"][0] target_group_arn = target_group["TargetGroupArn"] # HTTPS listener @@ -1498,10 +1474,10 @@ def test_modify_listener_of_https_target_group(): Certificates=[{"CertificateArn": yahoo_arn}], DefaultActions=[{"Type": "forward", "TargetGroupArn": target_group_arn}], )["Listeners"][0] - listener["Certificates"].should.equal([{"CertificateArn": yahoo_arn}]) + assert listener["Certificates"] == [{"CertificateArn": yahoo_arn}] listener = client.describe_listeners(ListenerArns=[listener_arn])["Listeners"][0] - listener["Certificates"].should.equal([{"CertificateArn": yahoo_arn}]) + assert listener["Certificates"] == [{"CertificateArn": yahoo_arn}] @mock_elbv2 @@ -1512,7 +1488,7 @@ def test_add_unknown_listener_certificate(): ListenerArn="unknown", Certificates=[{"CertificateArn": "google_arn"}] ) err = exc.value.response["Error"] - err["Code"].should.equal("ListenerNotFound") + assert err["Code"] == "ListenerNotFound" @mock_elbv2 @@ -1521,7 +1497,7 @@ def test_describe_unknown_listener_certificate(): with pytest.raises(ClientError) as exc: client.describe_listener_certificates(ListenerArn="unknown") err = exc.value.response["Error"] - err["Code"].should.equal("ListenerNotFound") + assert err["Code"] == "ListenerNotFound" @mock_acm @@ -1545,10 +1521,10 @@ def test_add_listener_certificate(): Tags=[{"Key": "key_name", "Value": "a_value"}], ) - load_balancer_arn = response.get("LoadBalancers")[0].get("LoadBalancerArn") + load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"] response = client.create_target_group(Name="a-target", Protocol="HTTPS", Port=8443) - target_group_arn = response.get("TargetGroups")[0]["TargetGroupArn"] + target_group_arn = response["TargetGroups"][0]["TargetGroupArn"] # HTTPS listener response = acm.request_certificate( @@ -1566,14 +1542,14 @@ def test_add_listener_certificate(): certs = client.add_listener_certificates( ListenerArn=listener_arn, Certificates=[{"CertificateArn": google_arn}] )["Certificates"] - certs.should.have.length_of(1) - certs[0].should.have.key("CertificateArn").equals(google_arn) + assert len(certs) == 1 + assert certs[0]["CertificateArn"] == google_arn certs = client.describe_listener_certificates(ListenerArn=listener_arn)[ "Certificates" ] - certs.should.have.length_of(1) - certs[0].should.have.key("CertificateArn").equals(google_arn) + assert len(certs) == 1 + assert certs[0]["CertificateArn"] == google_arn client.remove_listener_certificates( ListenerArn=listener_arn, Certificates=[{"CertificateArn": google_arn}] @@ -1582,17 +1558,17 @@ def test_add_listener_certificate(): certs = client.describe_listener_certificates(ListenerArn=listener_arn)[ "Certificates" ] - certs.should.have.length_of(0) + assert len(certs) == 0 @mock_elbv2 @mock_ec2 def test_forward_config_action(): response, _, _, _, _, conn = create_load_balancer() - load_balancer_arn = response.get("LoadBalancers")[0].get("LoadBalancerArn") + load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"] response = conn.create_target_group(Name="a-target", Protocol="HTTPS", Port=8443) - target_group_arn = response.get("TargetGroups")[0]["TargetGroupArn"] + target_group_arn = response["TargetGroups"][0]["TargetGroupArn"] action = { "Type": "forward", @@ -1610,25 +1586,25 @@ def test_forward_config_action(): DefaultActions=[action], ) - listener = response.get("Listeners")[0] - listener.get("DefaultActions").should.equal([expected_action]) - listener_arn = listener.get("ListenerArn") + listener = response["Listeners"][0] + assert listener["DefaultActions"] == [expected_action] + listener_arn = listener["ListenerArn"] describe_listener_response = conn.describe_listeners(ListenerArns=[listener_arn]) describe_listener_actions = describe_listener_response["Listeners"][0][ "DefaultActions" ] - describe_listener_actions.should.equal([expected_action]) + assert describe_listener_actions == [expected_action] @mock_elbv2 @mock_ec2 def test_forward_config_action__with_stickiness(): response, _, _, _, _, conn = create_load_balancer() - load_balancer_arn = response.get("LoadBalancers")[0].get("LoadBalancerArn") + load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"] response = conn.create_target_group(Name="a-target", Protocol="HTTPS", Port=8443) - target_group_arn = response.get("TargetGroups")[0]["TargetGroupArn"] + target_group_arn = response["TargetGroups"][0]["TargetGroupArn"] action = { "Type": "forward", @@ -1645,22 +1621,22 @@ def test_forward_config_action__with_stickiness(): DefaultActions=[action], ) - listener = response.get("Listeners")[0] - listener.get("DefaultActions").should.equal([action]) - listener_arn = listener.get("ListenerArn") + listener = response["Listeners"][0] + assert listener["DefaultActions"] == [action] + listener_arn = listener["ListenerArn"] describe_listener_response = conn.describe_listeners(ListenerArns=[listener_arn]) describe_listener_actions = describe_listener_response["Listeners"][0][ "DefaultActions" ] - describe_listener_actions.should.equal([action]) + assert describe_listener_actions == [action] @mock_elbv2 @mock_ec2 def test_redirect_action_listener_rule(): response, _, _, _, _, conn = create_load_balancer() - load_balancer_arn = response.get("LoadBalancers")[0].get("LoadBalancerArn") + load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"] action = { "Type": "redirect", @@ -1682,9 +1658,9 @@ def test_redirect_action_listener_rule(): DefaultActions=[action], ) - listener = response.get("Listeners")[0] - listener.get("DefaultActions").should.equal([action]) - listener_arn = listener.get("ListenerArn") + listener = response["Listeners"][0] + assert listener["DefaultActions"] == [action] + listener_arn = listener["ListenerArn"] conn.create_rule( ListenerArn=listener_arn, @@ -1693,24 +1669,24 @@ def test_redirect_action_listener_rule(): Actions=[action], ) describe_rules_response = conn.describe_rules(ListenerArn=listener_arn) - describe_rules_response["Rules"][0]["Actions"].should.equal([action]) + assert describe_rules_response["Rules"][0]["Actions"] == [action] describe_listener_response = conn.describe_listeners(ListenerArns=[listener_arn]) describe_listener_actions = describe_listener_response["Listeners"][0][ "DefaultActions" ] - describe_listener_actions.should.equal([action]) + assert describe_listener_actions == [action] modify_listener_response = conn.modify_listener(ListenerArn=listener_arn, Port=81) modify_listener_actions = modify_listener_response["Listeners"][0]["DefaultActions"] - modify_listener_actions.should.equal([action]) + assert modify_listener_actions == [action] @mock_elbv2 @mock_ec2 def test_cognito_action_listener_rule(): response, _, _, _, _, conn = create_load_balancer() - load_balancer_arn = response.get("LoadBalancers")[0].get("LoadBalancerArn") + load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"] action = { "Type": "authenticate-cognito", @@ -1728,9 +1704,9 @@ def test_cognito_action_listener_rule(): DefaultActions=[action], ) - listener = response.get("Listeners")[0] - listener.get("DefaultActions")[0].should.equal(action) - listener_arn = listener.get("ListenerArn") + listener = response["Listeners"][0] + assert listener["DefaultActions"][0] == action + listener_arn = listener["ListenerArn"] conn.create_rule( ListenerArn=listener_arn, @@ -1739,20 +1715,20 @@ def test_cognito_action_listener_rule(): Actions=[action], ) describe_rules_response = conn.describe_rules(ListenerArn=listener_arn) - describe_rules_response["Rules"][0]["Actions"][0].should.equal(action) + assert describe_rules_response["Rules"][0]["Actions"][0] == action describe_listener_response = conn.describe_listeners(ListenerArns=[listener_arn]) describe_listener_actions = describe_listener_response["Listeners"][0][ "DefaultActions" ][0] - describe_listener_actions.should.equal(action) + assert describe_listener_actions == action @mock_elbv2 @mock_ec2 def test_oidc_action_listener__simple(): response, _, _, _, _, conn = create_load_balancer() - load_balancer_arn = response.get("LoadBalancers")[0].get("LoadBalancerArn") + load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"] action = { "Type": "authenticate-oidc", @@ -1771,9 +1747,9 @@ def test_oidc_action_listener__simple(): DefaultActions=[action], ) - listener = response.get("Listeners")[0] - listener.get("DefaultActions")[0].should.equal(action) - listener_arn = listener.get("ListenerArn") + listener = response["Listeners"][0] + assert listener["DefaultActions"][0] == action + listener_arn = listener["ListenerArn"] conn.create_rule( ListenerArn=listener_arn, @@ -1782,13 +1758,13 @@ def test_oidc_action_listener__simple(): Actions=[action], ) describe_rules_response = conn.describe_rules(ListenerArn=listener_arn) - describe_rules_response["Rules"][0]["Actions"][0].should.equal(action) + assert describe_rules_response["Rules"][0]["Actions"][0] == action describe_listener_response = conn.describe_listeners(ListenerArns=[listener_arn]) describe_listener_actions = describe_listener_response["Listeners"][0][ "DefaultActions" ][0] - describe_listener_actions.should.equal(action) + assert describe_listener_actions == action @mock_elbv2 @@ -1796,7 +1772,7 @@ def test_oidc_action_listener__simple(): @pytest.mark.parametrize("use_secret", [True, False]) def test_oidc_action_listener(use_secret): response, _, _, _, _, conn = create_load_balancer() - load_balancer_arn = response.get("LoadBalancers")[0].get("LoadBalancerArn") + load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"] action = { "Type": "authenticate-oidc", @@ -1822,9 +1798,9 @@ def test_oidc_action_listener(use_secret): DefaultActions=[action], ) - listener = response.get("Listeners")[0] - listener.get("DefaultActions")[0].should.equal(action) - listener_arn = listener.get("ListenerArn") + listener = response["Listeners"][0] + assert listener["DefaultActions"][0] == action + listener_arn = listener["ListenerArn"] conn.create_rule( ListenerArn=listener_arn, @@ -1833,20 +1809,20 @@ def test_oidc_action_listener(use_secret): Actions=[action], ) describe_rules_response = conn.describe_rules(ListenerArn=listener_arn) - describe_rules_response["Rules"][0]["Actions"][0].should.equal(action) + assert describe_rules_response["Rules"][0]["Actions"][0] == action describe_listener_response = conn.describe_listeners(ListenerArns=[listener_arn]) describe_listener_actions = describe_listener_response["Listeners"][0][ "DefaultActions" ][0] - describe_listener_actions.should.equal(action) + assert describe_listener_actions == action @mock_elbv2 @mock_ec2 def test_fixed_response_action_listener_rule(): response, _, _, _, _, conn = create_load_balancer() - load_balancer_arn = response.get("LoadBalancers")[0].get("LoadBalancerArn") + load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"] action = { "Type": "fixed-response", @@ -1863,9 +1839,9 @@ def test_fixed_response_action_listener_rule(): DefaultActions=[action], ) - listener = response.get("Listeners")[0] - listener.get("DefaultActions")[0].should.equal(action) - listener_arn = listener.get("ListenerArn") + listener = response["Listeners"][0] + assert listener["DefaultActions"][0] == action + listener_arn = listener["ListenerArn"] conn.create_rule( ListenerArn=listener_arn, @@ -1874,20 +1850,20 @@ def test_fixed_response_action_listener_rule(): Actions=[action], ) describe_rules_response = conn.describe_rules(ListenerArn=listener_arn) - describe_rules_response["Rules"][0]["Actions"][0].should.equal(action) + assert describe_rules_response["Rules"][0]["Actions"][0] == action describe_listener_response = conn.describe_listeners(ListenerArns=[listener_arn]) describe_listener_actions = describe_listener_response["Listeners"][0][ "DefaultActions" ][0] - describe_listener_actions.should.equal(action) + assert describe_listener_actions == action @mock_elbv2 @mock_ec2 def test_fixed_response_action_listener_rule_validates_status_code(): response, _, _, _, _, conn = create_load_balancer() - load_balancer_arn = response.get("LoadBalancers")[0].get("LoadBalancerArn") + load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"] invalid_status_code_action = { "Type": "fixed-response", @@ -1898,7 +1874,7 @@ def test_fixed_response_action_listener_rule_validates_status_code(): }, } - with pytest.raises(ClientError) as invalid_status_code_exception: + with pytest.raises(ClientError) as exc: conn.create_listener( LoadBalancerArn=load_balancer_arn, Protocol="HTTP", @@ -1906,16 +1882,14 @@ def test_fixed_response_action_listener_rule_validates_status_code(): DefaultActions=[invalid_status_code_action], ) - invalid_status_code_exception.value.response["Error"]["Code"].should.equal( - "ValidationError" - ) + assert exc.value.response["Error"]["Code"] == "ValidationError" @mock_elbv2 @mock_ec2 def test_fixed_response_action_listener_rule_validates_content_type(): response, _, _, _, _, conn = create_load_balancer() - load_balancer_arn = response.get("LoadBalancers")[0].get("LoadBalancerArn") + load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"] invalid_content_type_action = { "Type": "fixed-response", @@ -1925,23 +1899,21 @@ def test_fixed_response_action_listener_rule_validates_content_type(): "StatusCode": "200", }, } - with pytest.raises(ClientError) as invalid_content_type_exception: + with pytest.raises(ClientError) as exc: conn.create_listener( LoadBalancerArn=load_balancer_arn, Protocol="HTTP", Port=80, DefaultActions=[invalid_content_type_action], ) - invalid_content_type_exception.value.response["Error"]["Code"].should.equal( - "InvalidLoadBalancerAction" - ) + assert exc.value.response["Error"]["Code"] == "InvalidLoadBalancerAction" @mock_elbv2 @mock_ec2 def test_create_listener_with_alpn_policy(): response, _, _, _, _, conn = create_load_balancer() - load_balancer_arn = response.get("LoadBalancers")[0].get("LoadBalancerArn") + load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"] response = conn.create_listener( LoadBalancerArn=load_balancer_arn, @@ -1951,9 +1923,9 @@ def test_create_listener_with_alpn_policy(): AlpnPolicy=["pol1", "pol2"], ) - listener = response.get("Listeners")[0] + listener = response["Listeners"][0] listener_arn = listener["ListenerArn"] - listener.get("AlpnPolicy").should.equal(["pol1", "pol2"]) + assert listener["AlpnPolicy"] == ["pol1", "pol2"] describe = conn.describe_listeners(ListenerArns=[listener_arn])["Listeners"][0] - describe.should.have.key("AlpnPolicy").should.equal(["pol1", "pol2"]) + assert describe["AlpnPolicy"] == ["pol1", "pol2"] diff --git a/tests/test_elbv2/test_elbv2_cloudformation.py b/tests/test_elbv2/test_elbv2_cloudformation.py index e04a544d5..3ae7da1ac 100644 --- a/tests/test_elbv2/test_elbv2_cloudformation.py +++ b/tests/test_elbv2/test_elbv2_cloudformation.py @@ -1,6 +1,5 @@ import boto3 import json -import sure # noqa # pylint: disable=unused-import from moto import mock_elbv2, mock_ec2, mock_cloudformation from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID @@ -68,32 +67,24 @@ def test_redirect_action_listener_rule_cloudformation(): template_json = json.dumps(template) cnf_conn.create_stack(StackName="test-stack", TemplateBody=template_json) - describe_load_balancers_response = elbv2_client.describe_load_balancers( - Names=["my-lb"] - ) - describe_load_balancers_response["LoadBalancers"].should.have.length_of(1) - load_balancer_arn = describe_load_balancers_response["LoadBalancers"][0][ - "LoadBalancerArn" + resp = elbv2_client.describe_load_balancers(Names=["my-lb"]) + assert len(resp["LoadBalancers"]) == 1 + load_balancer_arn = resp["LoadBalancers"][0]["LoadBalancerArn"] + + listeners = elbv2_client.describe_listeners(LoadBalancerArn=load_balancer_arn) + + assert len(listeners["Listeners"]) == 1 + assert listeners["Listeners"][0]["DefaultActions"] == [ + { + "Type": "redirect", + "RedirectConfig": { + "Port": "443", + "Protocol": "HTTPS", + "StatusCode": "HTTP_301", + }, + } ] - describe_listeners_response = elbv2_client.describe_listeners( - LoadBalancerArn=load_balancer_arn - ) - - describe_listeners_response["Listeners"].should.have.length_of(1) - describe_listeners_response["Listeners"][0]["DefaultActions"].should.equal( - [ - { - "Type": "redirect", - "RedirectConfig": { - "Port": "443", - "Protocol": "HTTPS", - "StatusCode": "HTTP_301", - }, - } - ] - ) - @mock_elbv2 @mock_cloudformation @@ -167,19 +158,17 @@ def test_cognito_action_listener_rule_cloudformation(): LoadBalancerArn=load_balancer_arn ) - describe_listeners_response["Listeners"].should.have.length_of(1) - describe_listeners_response["Listeners"][0]["DefaultActions"].should.equal( - [ - { - "Type": "authenticate-cognito", - "AuthenticateCognitoConfig": { - "UserPoolArn": f"arn:aws:cognito-idp:us-east-1:{ACCOUNT_ID}:userpool/us-east-1_ABCD1234", - "UserPoolClientId": "abcd1234abcd", - "UserPoolDomain": "testpool", - }, - } - ] - ) + assert len(describe_listeners_response["Listeners"]) == 1 + assert describe_listeners_response["Listeners"][0]["DefaultActions"] == [ + { + "Type": "authenticate-cognito", + "AuthenticateCognitoConfig": { + "UserPoolArn": f"arn:aws:cognito-idp:us-east-1:{ACCOUNT_ID}:userpool/us-east-1_ABCD1234", + "UserPoolClientId": "abcd1234abcd", + "UserPoolDomain": "testpool", + }, + } + ] @mock_ec2 @@ -330,16 +319,14 @@ def test_fixed_response_action_listener_rule_cloudformation(): LoadBalancerArn=load_balancer_arn ) - describe_listeners_response["Listeners"].should.have.length_of(1) - describe_listeners_response["Listeners"][0]["DefaultActions"].should.equal( - [ - { - "Type": "fixed-response", - "FixedResponseConfig": { - "ContentType": "text/plain", - "MessageBody": "This page does not exist", - "StatusCode": "404", - }, - } - ] - ) + assert len(describe_listeners_response["Listeners"]) == 1 + assert describe_listeners_response["Listeners"][0]["DefaultActions"] == [ + { + "Type": "fixed-response", + "FixedResponseConfig": { + "ContentType": "text/plain", + "MessageBody": "This page does not exist", + "StatusCode": "404", + }, + } + ] diff --git a/tests/test_elbv2/test_elbv2_integration.py b/tests/test_elbv2/test_elbv2_integration.py index 0dc597aa2..578794b54 100644 --- a/tests/test_elbv2/test_elbv2_integration.py +++ b/tests/test_elbv2/test_elbv2_integration.py @@ -65,4 +65,4 @@ def test_modify_listener_using_iam_certificate(): Certificates=[{"CertificateArn": iam_arn}], DefaultActions=[{"Type": "forward", "TargetGroupArn": target_group_arn}], )["Listeners"][0] - listener["Certificates"].should.equal([{"CertificateArn": iam_arn}]) + assert listener["Certificates"] == [{"CertificateArn": iam_arn}] diff --git a/tests/test_elbv2/test_elbv2_listener_rule_tags.py b/tests/test_elbv2/test_elbv2_listener_rule_tags.py index 53039bc53..c7953bbd4 100644 --- a/tests/test_elbv2/test_elbv2_listener_rule_tags.py +++ b/tests/test_elbv2/test_elbv2_listener_rule_tags.py @@ -1,5 +1,3 @@ -import sure # noqa # pylint: disable=unused-import - from moto import mock_elbv2, mock_ec2 from .test_elbv2 import create_load_balancer @@ -35,7 +33,7 @@ def test_create_listener_rule_with_tags(): # Ensure the tags persisted response = elbv2.describe_tags(ResourceArns=[rule_arn]) tags = {d["Key"]: d["Value"] for d in response["TagDescriptions"][0]["Tags"]} - tags.should.equal({"k1": "v1"}) + assert tags == {"k1": "v1"} @mock_elbv2 @@ -62,7 +60,7 @@ def test_listener_rule_add_remove_tags(): elbv2.add_tags(ResourceArns=[rule_arn], Tags=[{"Key": "a", "Value": "b"}]) tags = elbv2.describe_tags(ResourceArns=[rule_arn])["TagDescriptions"][0]["Tags"] - tags.should.equal([{"Key": "a", "Value": "b"}]) + assert tags == [{"Key": "a", "Value": "b"}] elbv2.add_tags( ResourceArns=[rule_arn], @@ -74,20 +72,16 @@ def test_listener_rule_add_remove_tags(): ) tags = elbv2.describe_tags(ResourceArns=[rule_arn])["TagDescriptions"][0]["Tags"] - tags.should.equal( - [ - {"Key": "a", "Value": "b"}, - {"Key": "b", "Value": "b"}, - {"Key": "c", "Value": "b"}, - ] - ) + assert tags == [ + {"Key": "a", "Value": "b"}, + {"Key": "b", "Value": "b"}, + {"Key": "c", "Value": "b"}, + ] elbv2.remove_tags(ResourceArns=[rule_arn], TagKeys=["a"]) tags = elbv2.describe_tags(ResourceArns=[rule_arn])["TagDescriptions"][0]["Tags"] - tags.should.equal( - [ - {"Key": "b", "Value": "b"}, - {"Key": "c", "Value": "b"}, - ] - ) + assert tags == [ + {"Key": "b", "Value": "b"}, + {"Key": "c", "Value": "b"}, + ] diff --git a/tests/test_elbv2/test_elbv2_listener_rules.py b/tests/test_elbv2/test_elbv2_listener_rules.py index ee25d15c1..56555d1f0 100644 --- a/tests/test_elbv2/test_elbv2_listener_rules.py +++ b/tests/test_elbv2/test_elbv2_listener_rules.py @@ -1,7 +1,6 @@ import boto3 from botocore.exceptions import ClientError import pytest -import sure # noqa # pylint: disable=unused-import from moto import mock_elbv2, mock_ec2 @@ -117,24 +116,24 @@ def test_create_rule_condition(condition): ) # assert create_rule response - response["Rules"].should.have.length_of(1) + assert len(response["Rules"]) == 1 rule = response.get("Rules")[0] - rule["Priority"].should.equal("100") - rule["Conditions"].should.equal([condition]) + assert rule["Priority"] == "100" + assert rule["Conditions"] == [condition] # assert describe_rules response response = conn.describe_rules(ListenerArn=http_listener_arn) - response["Rules"].should.have.length_of(2) # including the default rule + assert len(response["Rules"]) == 2 # including the default rule # assert describe_rules with arn filter response rule = response["Rules"][0] - rule["Conditions"].should.equal([condition]) + assert rule["Conditions"] == [condition] response = conn.describe_rules(RuleArns=[rule["RuleArn"]]) - response["Rules"].should.equal([rule]) + assert response["Rules"] == [rule] # assert describe_tags response response = conn.describe_tags(ResourceArns=[rule["RuleArn"]]) - response["TagDescriptions"].should.have.length_of(1) + assert len(response["TagDescriptions"]) == 1 @mock_elbv2 @@ -188,9 +187,9 @@ def test_modify_rule_condition(create_condition, modify_condition): # modify_rule response = conn.modify_rule(RuleArn=rule["RuleArn"], Conditions=[modify_condition]) - response["Rules"].should.have.length_of(1) + assert len(response["Rules"]) == 1 modified_rule = response.get("Rules")[0] - modified_rule["Conditions"].should.equal([modify_condition]) + assert modified_rule["Conditions"] == [modify_condition] @mock_elbv2 @@ -309,8 +308,8 @@ def test_create_rule_validate_condition(condition, expected_message): ) err = ex.value.response["Error"] - err["Code"].should.equal("ValidationError") - err["Message"].should.equal(expected_message) + assert err["Code"] == "ValidationError" + assert err["Message"] == expected_message @mock_elbv2 @@ -321,8 +320,8 @@ def test_describe_unknown_rule(): with pytest.raises(ClientError) as exc: conn.describe_rules(RuleArns=["unknown_arn"]) err = exc.value.response["Error"] - err["Code"].should.equal("RuleNotFound") - err["Message"].should.equal("One or more rules not found") + assert err["Code"] == "RuleNotFound" + assert err["Message"] == "One or more rules not found" @mock_elbv2 @@ -403,17 +402,17 @@ def test_create_rule_action(action): ) # assert create_rule response - response["Rules"].should.have.length_of(1) + assert len(response["Rules"]) == 1 rule = response.get("Rules")[0] - rule["Priority"].should.equal("100") - rule["Conditions"].should.equal([]) - rule["Actions"].should.equal([action]) + assert rule["Priority"] == "100" + assert rule["Conditions"] == [] + assert rule["Actions"] == [action] # assert describe_rules response response = conn.describe_rules(ListenerArn=http_listener_arn) - response["Rules"].should.have.length_of(2) # including the default rule + assert len(response["Rules"]) == 2 # including the default rule rule = response.get("Rules")[0] - rule["Actions"][0].should.equal(action) + assert rule["Actions"][0] == action # assert set_rule_priorities response rule_arn = response.get("Rules")[0]["RuleArn"] @@ -421,11 +420,11 @@ def test_create_rule_action(action): RulePriorities=[{"RuleArn": rule_arn, "Priority": 99}] ) - response["Rules"].should.have.length_of(1) + assert len(response["Rules"]) == 1 rule = response.get("Rules")[0] - rule["Priority"].should.equal("99") - rule["Conditions"].should.equal([]) - rule["Actions"][0].should.equal(action) + assert rule["Priority"] == "99" + assert rule["Conditions"] == [] + assert rule["Actions"][0] == action @mock_elbv2 @@ -451,17 +450,17 @@ def test_create_rule_action_forward_config(): ) # assert create_rule response - response["Rules"].should.have.length_of(1) + assert len(response["Rules"]) == 1 rule = response.get("Rules")[0] - rule["Priority"].should.equal("100") - rule["Conditions"].should.equal([]) - rule["Actions"][0].should.equal(action) + assert rule["Priority"] == "100" + assert rule["Conditions"] == [] + assert rule["Actions"][0] == action # assert describe_rules response response = conn.describe_rules(ListenerArn=http_listener_arn) - response["Rules"].should.have.length_of(2) # including the default rule + assert len(response["Rules"]) == 2 # including the default rule rule = response.get("Rules")[0] - rule["Actions"][0].should.equal(action) + assert rule["Actions"][0] == action # assert set_rule_priorities response rule_arn = response.get("Rules")[0]["RuleArn"] @@ -469,11 +468,11 @@ def test_create_rule_action_forward_config(): RulePriorities=[{"RuleArn": rule_arn, "Priority": 99}] ) - response["Rules"].should.have.length_of(1) + assert len(response["Rules"]) == 1 rule = response.get("Rules")[0] - rule["Priority"].should.equal("99") - rule["Conditions"].should.equal([]) - rule["Actions"][0].should.equal(action) + assert rule["Priority"] == "99" + assert rule["Conditions"] == [] + assert rule["Actions"][0] == action @mock_elbv2 @@ -495,17 +494,17 @@ def test_create_rule_action_forward_target_group(): ) # assert create_rule response - response["Rules"].should.have.length_of(1) + assert len(response["Rules"]) == 1 rule = response.get("Rules")[0] - rule["Priority"].should.equal("100") - rule["Conditions"].should.equal([]) - rule["Actions"][0].should.equal(action) + assert rule["Priority"] == "100" + assert rule["Conditions"] == [] + assert rule["Actions"][0] == action # assert describe_rules response response = conn.describe_rules(ListenerArn=http_listener_arn) - response["Rules"].should.have.length_of(2) # including the default rule + assert len(response["Rules"]) == 2 # including the default rule rule = response.get("Rules")[0] - rule["Actions"][0].should.equal(action) + assert rule["Actions"][0] == action # assert set_rule_priorities rule_arn = response.get("Rules")[0]["RuleArn"] @@ -514,8 +513,8 @@ def test_create_rule_action_forward_target_group(): ) # assert set_rule_priorities response - response["Rules"].should.have.length_of(1) + assert len(response["Rules"]) == 1 rule = response.get("Rules")[0] - rule["Priority"].should.equal("99") - rule["Conditions"].should.equal([]) - rule["Actions"][0].should.equal(action) + assert rule["Priority"] == "99" + assert rule["Conditions"] == [] + assert rule["Actions"][0] == action diff --git a/tests/test_elbv2/test_elbv2_listener_tags.py b/tests/test_elbv2/test_elbv2_listener_tags.py index 59965f98e..7912f0163 100644 --- a/tests/test_elbv2/test_elbv2_listener_tags.py +++ b/tests/test_elbv2/test_elbv2_listener_tags.py @@ -1,5 +1,3 @@ -import sure # noqa # pylint: disable=unused-import - from moto import mock_elbv2, mock_ec2 from .test_elbv2 import create_load_balancer @@ -23,7 +21,7 @@ def test_create_listener_with_tags(): # Ensure the tags persisted response = elbv2.describe_tags(ResourceArns=[listener_arn]) tags = {d["Key"]: d["Value"] for d in response["TagDescriptions"][0]["Tags"]} - tags.should.equal({"k1": "v1"}) + assert tags == {"k1": "v1"} @mock_elbv2 @@ -46,7 +44,7 @@ def test_listener_add_remove_tags(): tags = elbv2.describe_tags(ResourceArns=[listener_arn])["TagDescriptions"][0][ "Tags" ] - tags.should.equal([{"Key": "k1", "Value": "v1"}, {"Key": "a", "Value": "b"}]) + assert tags == [{"Key": "k1", "Value": "v1"}, {"Key": "a", "Value": "b"}] elbv2.add_tags( ResourceArns=[listener_arn], @@ -60,24 +58,20 @@ def test_listener_add_remove_tags(): tags = elbv2.describe_tags(ResourceArns=[listener_arn])["TagDescriptions"][0][ "Tags" ] - tags.should.equal( - [ - {"Key": "k1", "Value": "v1"}, - {"Key": "a", "Value": "b"}, - {"Key": "b", "Value": "b"}, - {"Key": "c", "Value": "b"}, - ] - ) + assert tags == [ + {"Key": "k1", "Value": "v1"}, + {"Key": "a", "Value": "b"}, + {"Key": "b", "Value": "b"}, + {"Key": "c", "Value": "b"}, + ] elbv2.remove_tags(ResourceArns=[listener_arn], TagKeys=["a"]) tags = elbv2.describe_tags(ResourceArns=[listener_arn])["TagDescriptions"][0][ "Tags" ] - tags.should.equal( - [ - {"Key": "k1", "Value": "v1"}, - {"Key": "b", "Value": "b"}, - {"Key": "c", "Value": "b"}, - ] - ) + assert tags == [ + {"Key": "k1", "Value": "v1"}, + {"Key": "b", "Value": "b"}, + {"Key": "c", "Value": "b"}, + ] diff --git a/tests/test_elbv2/test_elbv2_set_subnets.py b/tests/test_elbv2/test_elbv2_set_subnets.py index cb2deceea..6b4679430 100644 --- a/tests/test_elbv2/test_elbv2_set_subnets.py +++ b/tests/test_elbv2/test_elbv2_set_subnets.py @@ -1,5 +1,4 @@ import boto3 -import sure # noqa # pylint: disable=unused-import from moto import mock_elbv2, mock_ec2 @@ -38,7 +37,7 @@ def test_set_subnets_errors(): ) resp = client.describe_load_balancers(LoadBalancerArns=[arn]) - len(resp["LoadBalancers"][0]["AvailabilityZones"]).should.equal(3) + assert len(resp["LoadBalancers"][0]["AvailabilityZones"]) == 3 @mock_elbv2 @@ -74,6 +73,6 @@ def test_set_subnets__mapping(): resp = client.describe_load_balancers(LoadBalancerArns=[arn]) a_zones = resp["LoadBalancers"][0]["AvailabilityZones"] - a_zones.should.have.length_of(2) - a_zones.should.contain({"ZoneName": "us-east-1a", "SubnetId": subnet1.id}) - a_zones.should.contain({"ZoneName": "us-east-1b", "SubnetId": subnet2.id}) + assert len(a_zones) == 2 + assert {"ZoneName": "us-east-1a", "SubnetId": subnet1.id} in a_zones + assert {"ZoneName": "us-east-1b", "SubnetId": subnet2.id} in a_zones diff --git a/tests/test_elbv2/test_elbv2_target_groups.py b/tests/test_elbv2/test_elbv2_target_groups.py index 47f91b698..d9de48a7c 100644 --- a/tests/test_elbv2/test_elbv2_target_groups.py +++ b/tests/test_elbv2/test_elbv2_target_groups.py @@ -1,7 +1,6 @@ import boto3 from botocore.exceptions import ClientError import pytest -import sure # noqa # pylint: disable=unused-import from moto import mock_elbv2, mock_ec2 from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID @@ -30,9 +29,10 @@ def test_create_target_group_with_invalid_healthcheck_protocol(): Matcher={"HttpCode": "200"}, ) err = exc.value.response["Error"] - err["Code"].should.equal("ValidationError") - err["Message"].should.equal( - "Value /HTTP at 'healthCheckProtocol' failed to satisfy constraint: Member must satisfy enum value set: ['HTTPS', 'HTTP', 'TCP', 'TLS', 'UDP', 'TCP_UDP', 'GENEVE']" + assert err["Code"] == "ValidationError" + assert ( + err["Message"] + == "Value /HTTP at 'healthCheckProtocol' failed to satisfy constraint: Member must satisfy enum value set: ['HTTPS', 'HTTP', 'TCP', 'TLS', 'UDP', 'TCP_UDP', 'GENEVE']" ) @@ -56,7 +56,7 @@ def test_create_target_group_with_tags(): Matcher={"HttpCode": "200"}, Tags=[{"Key": "key1", "Value": "val1"}], ) - target_group = response.get("TargetGroups")[0] + target_group = response["TargetGroups"][0] target_group_arn = target_group["TargetGroupArn"] # Add tags to the target group @@ -64,15 +64,17 @@ def test_create_target_group_with_tags(): ResourceArns=[target_group_arn], Tags=[{"Key": "key2", "Value": "val2"}], ) - conn.describe_tags(ResourceArns=[target_group_arn])["TagDescriptions"][0][ + tags = conn.describe_tags(ResourceArns=[target_group_arn])["TagDescriptions"][0][ "Tags" - ].should.equal([{"Key": "key1", "Value": "val1"}, {"Key": "key2", "Value": "val2"}]) + ] + assert tags == [{"Key": "key1", "Value": "val1"}, {"Key": "key2", "Value": "val2"}] # Verify they can be removed conn.remove_tags(ResourceArns=[target_group_arn], TagKeys=["key1"]) - conn.describe_tags(ResourceArns=[target_group_arn])["TagDescriptions"][0][ + tags = conn.describe_tags(ResourceArns=[target_group_arn])["TagDescriptions"][0][ "Tags" - ].should.equal([{"Key": "key2", "Value": "val2"}]) + ] + assert tags == [{"Key": "key2", "Value": "val2"}] @mock_elbv2 @@ -80,7 +82,7 @@ def test_create_target_group_with_tags(): def test_create_target_group_and_listeners(): response, vpc, _, _, _, conn = create_load_balancer() - load_balancer_arn = response.get("LoadBalancers")[0].get("LoadBalancerArn") + load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"] response = conn.create_target_group( Name="a-target", @@ -96,13 +98,13 @@ def test_create_target_group_and_listeners(): UnhealthyThresholdCount=2, Matcher={"HttpCode": "200"}, ) - target_group = response.get("TargetGroups")[0] - target_group_arn = target_group.get("TargetGroupArn") - target_group.get("HealthCheckProtocol").should.equal("HTTP") + target_group = response["TargetGroups"][0] + target_group_arn = target_group["TargetGroupArn"] + assert target_group["HealthCheckProtocol"] == "HTTP" # Check it's in the describe_target_groups response response = conn.describe_target_groups() - response.get("TargetGroups").should.have.length_of(1) + assert len(response["TargetGroups"]) == 1 # Plain HTTP listener response = conn.create_listener( @@ -111,18 +113,18 @@ def test_create_target_group_and_listeners(): Port=80, DefaultActions=[{"Type": "forward", "TargetGroupArn": target_group_arn}], ) - listener = response.get("Listeners")[0] - listener.get("Port").should.equal(80) - listener.get("Protocol").should.equal("HTTP") - listener.get("DefaultActions").should.equal( - [{"TargetGroupArn": target_group_arn, "Type": "forward"}] - ) - http_listener_arn = listener.get("ListenerArn") + listener = response["Listeners"][0] + assert listener["Port"] == 80 + assert listener["Protocol"] == "HTTP" + assert listener["DefaultActions"] == [ + {"TargetGroupArn": target_group_arn, "Type": "forward"} + ] + http_listener_arn = listener["ListenerArn"] response = conn.describe_target_groups( LoadBalancerArn=load_balancer_arn, Names=["a-target"] ) - response.get("TargetGroups").should.have.length_of(1) + assert len(response["TargetGroups"]) == 1 # And another with SSL actions = {"Type": "forward", "TargetGroupArn": target_group_arn} @@ -135,30 +137,30 @@ def test_create_target_group_and_listeners(): ], DefaultActions=[actions], ) - listener = response.get("Listeners")[0] - listener.get("Port").should.equal(443) - listener.get("Protocol").should.equal("HTTPS") - listener.get("Certificates").should.equal( - [{"CertificateArn": f"arn:aws:iam:{ACCOUNT_ID}:server-certificate/test-cert"}] - ) - listener.get("DefaultActions").should.equal( - [{"TargetGroupArn": target_group_arn, "Type": "forward"}] - ) + listener = response["Listeners"][0] + assert listener["Port"] == 443 + assert listener["Protocol"] == "HTTPS" + assert listener["Certificates"] == [ + {"CertificateArn": f"arn:aws:iam:{ACCOUNT_ID}:server-certificate/test-cert"} + ] + assert listener["DefaultActions"] == [ + {"TargetGroupArn": target_group_arn, "Type": "forward"} + ] - https_listener_arn = listener.get("ListenerArn") + https_listener_arn = listener["ListenerArn"] response = conn.describe_listeners(LoadBalancerArn=load_balancer_arn) - response.get("Listeners").should.have.length_of(2) + assert len(response["Listeners"]) == 2 response = conn.describe_listeners(ListenerArns=[https_listener_arn]) - response.get("Listeners").should.have.length_of(1) - listener = response.get("Listeners")[0] - listener.get("Port").should.equal(443) - listener.get("Protocol").should.equal("HTTPS") + assert len(response["Listeners"]) == 1 + listener = response["Listeners"][0] + assert listener["Port"] == 443 + assert listener["Protocol"] == "HTTPS" response = conn.describe_listeners( ListenerArns=[http_listener_arn, https_listener_arn] ) - response.get("Listeners").should.have.length_of(2) + assert len(response["Listeners"]) == 2 conn.create_rule( ListenerArn=http_listener_arn, @@ -170,40 +172,39 @@ def test_create_target_group_and_listeners(): # listener referencing it with pytest.raises(ClientError) as e: conn.delete_target_group(TargetGroupArn=target_group_arn) - e.value.operation_name.should.equal("DeleteTargetGroup") - e.value.args.should.equal( - ( - f"An error occurred (ResourceInUse) when calling the DeleteTargetGroup operation: The target group 'arn:aws:elasticloadbalancing:us-east-1:{ACCOUNT_ID}:targetgroup/a-target/50dc6c495c0c9188' is currently in use by a listener or a rule", - ) - ) # NOQA + assert e.value.operation_name == "DeleteTargetGroup" + assert ( + e.value.response["Error"]["Message"] + == f"The target group 'arn:aws:elasticloadbalancing:us-east-1:{ACCOUNT_ID}:targetgroup/a-target/50dc6c495c0c9188' is currently in use by a listener or a rule" + ) # Delete one listener response = conn.describe_listeners(LoadBalancerArn=load_balancer_arn) - response.get("Listeners").should.have.length_of(2) + assert len(response["Listeners"]) == 2 conn.delete_listener(ListenerArn=http_listener_arn) response = conn.describe_listeners(LoadBalancerArn=load_balancer_arn) - response.get("Listeners").should.have.length_of(1) + assert len(response["Listeners"]) == 1 # Then delete the load balancer conn.delete_load_balancer(LoadBalancerArn=load_balancer_arn) # It's gone response = conn.describe_load_balancers() - response.get("LoadBalancers").should.have.length_of(0) + assert len(response["LoadBalancers"]) == 0 # And it deleted the remaining listener with pytest.raises(ClientError) as e: conn.describe_listeners(ListenerArns=[http_listener_arn, https_listener_arn]) - e.value.response["Error"]["Code"].should.equal("ListenerNotFound") + assert e.value.response["Error"]["Code"] == "ListenerNotFound" # But not the target groups response = conn.describe_target_groups() - response.get("TargetGroups").should.have.length_of(1) + assert len(response["TargetGroups"]) == 1 # Which we'll now delete conn.delete_target_group(TargetGroupArn=target_group_arn) response = conn.describe_target_groups() - response.get("TargetGroups").should.have.length_of(0) + assert len(response["TargetGroups"]) == 0 @mock_elbv2 @@ -221,7 +222,7 @@ def test_create_target_group_without_non_required_parameters(): HealthCheckProtocol="HTTP", HealthCheckPort="8080", ) - response.get("TargetGroups", []).should.have.length_of(1) + assert len(response.get("TargetGroups", [])) == 1 @mock_elbv2 @@ -250,9 +251,10 @@ def test_create_invalid_target_group_long_name(): Matcher={"HttpCode": "200"}, ) err = exc.value.response["Error"] - err["Code"].should.equal("ValidationError") - err["Message"].should.equal( - "Target group name 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' cannot be longer than '32' characters" + assert err["Code"] == "ValidationError" + assert ( + err["Message"] + == "Target group name 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' cannot be longer than '32' characters" ) @@ -281,9 +283,10 @@ def test_create_invalid_target_group_invalid_characters(name): Matcher={"HttpCode": "200"}, ) err = exc.value.response["Error"] - err["Code"].should.equal("ValidationError") - err["Message"].should.equal( - f"1 validation error detected: Value '{name}' at 'targetGroup.targetGroupArn.targetGroupName' failed to satisfy constraint: Member must satisfy regular expression pattern: (?!.*--)(?!^-)(?!.*-$)^[A-Za-z0-9-]+$" + assert err["Code"] == "ValidationError" + assert ( + err["Message"] + == f"1 validation error detected: Value '{name}' at 'targetGroup.targetGroupArn.targetGroupName' failed to satisfy constraint: Member must satisfy regular expression pattern: (?!.*--)(?!^-)(?!.*-$)^[A-Za-z0-9-]+$" ) @@ -312,9 +315,10 @@ def test_create_invalid_target_group_alphanumeric_characters(name): Matcher={"HttpCode": "200"}, ) err = exc.value.response["Error"] - err["Code"].should.equal("ValidationError") - err["Message"].should.equal( - f"Target group name '{name}' can only contain characters that are alphanumeric characters or hyphens(-)" + assert err["Code"] == "ValidationError" + assert ( + err["Message"] + == f"Target group name '{name}' can only contain characters that are alphanumeric characters or hyphens(-)" ) @@ -362,26 +366,26 @@ def test_target_group_attributes(): UnhealthyThresholdCount=2, Matcher={"HttpCode": "200"}, ) - target_group = response.get("TargetGroups")[0] + target_group = response["TargetGroups"][0] # Check it's in the describe_target_groups response response = conn.describe_target_groups() - response.get("TargetGroups").should.have.length_of(1) + assert len(response["TargetGroups"]) == 1 target_group_arn = target_group["TargetGroupArn"] # check if Names filter works response = conn.describe_target_groups(Names=[]) response = conn.describe_target_groups(Names=["a-target"]) - response.get("TargetGroups").should.have.length_of(1) + assert len(response["TargetGroups"]) == 1 target_group_arn = target_group["TargetGroupArn"] # The attributes should start with the two defaults response = conn.describe_target_group_attributes(TargetGroupArn=target_group_arn) - response["Attributes"].should.have.length_of(5) + assert len(response["Attributes"]) == 5 attributes = {attr["Key"]: attr["Value"] for attr in response["Attributes"]} - attributes["deregistration_delay.timeout_seconds"].should.equal("300") - attributes["stickiness.enabled"].should.equal("false") - attributes["waf.fail_open.enabled"].should.equal("false") + assert attributes["deregistration_delay.timeout_seconds"] == "300" + assert attributes["stickiness.enabled"] == "false" + assert attributes["waf.fail_open.enabled"] == "false" # Add cookie stickiness response = conn.modify_target_group_attributes( @@ -393,17 +397,17 @@ def test_target_group_attributes(): ) # The response should have only the keys updated - response["Attributes"].should.have.length_of(2) + assert len(response["Attributes"]) == 2 attributes = {attr["Key"]: attr["Value"] for attr in response["Attributes"]} - attributes["stickiness.type"].should.equal("lb_cookie") - attributes["stickiness.enabled"].should.equal("true") + assert attributes["stickiness.type"] == "lb_cookie" + assert attributes["stickiness.enabled"] == "true" # These new values should be in the full attribute list response = conn.describe_target_group_attributes(TargetGroupArn=target_group_arn) - response["Attributes"].should.have.length_of(6) + assert len(response["Attributes"]) == 6 attributes = {attr["Key"]: attr["Value"] for attr in response["Attributes"]} - attributes["stickiness.type"].should.equal("lb_cookie") - attributes["stickiness.enabled"].should.equal("true") + assert attributes["stickiness.type"] == "lb_cookie" + assert attributes["stickiness.enabled"] == "true" @mock_elbv2 @@ -431,9 +435,10 @@ def test_create_target_group_invalid_protocol(): Matcher={"HttpCode": "200"}, ) err = ex.value.response["Error"] - err["Code"].should.equal("ValidationError") - err["Message"].should.contain( + assert err["Code"] == "ValidationError" + assert ( "Value /HTTP at 'healthCheckProtocol' failed to satisfy constraint" + in err["Message"] ) @@ -445,8 +450,8 @@ def test_describe_invalid_target_group(): with pytest.raises(ClientError) as exc: conn.describe_target_groups(Names=["invalid"]) err = exc.value.response["Error"] - err["Code"].should.equal("TargetGroupNotFound") - err["Message"].should.equal("The specified target group does not exist.") + assert err["Code"] == "TargetGroupNotFound" + assert err["Message"] == "The specified target group does not exist." @mock_elbv2 @@ -454,7 +459,7 @@ def test_describe_invalid_target_group(): def test_describe_target_groups_no_arguments(): response, vpc, _, _, _, conn = create_load_balancer() - response.get("LoadBalancers")[0].get("LoadBalancerArn") + assert "LoadBalancerArn" in response["LoadBalancers"][0] conn.create_target_group( Name="a-target", @@ -472,8 +477,8 @@ def test_describe_target_groups_no_arguments(): ) groups = conn.describe_target_groups()["TargetGroups"] - groups.should.have.length_of(1) - groups[0].should.have.key("Matcher").equals({"HttpCode": "201"}) + assert len(groups) == 1 + assert groups[0]["Matcher"] == {"HttpCode": "201"} @mock_elbv2 @@ -498,7 +503,7 @@ def test_modify_target_group(): UnhealthyThresholdCount=2, Matcher={"HttpCode": "200"}, ) - arn = response.get("TargetGroups")[0]["TargetGroupArn"] + arn = response["TargetGroups"][0]["TargetGroupArn"] client.modify_target_group( TargetGroupArn=arn, @@ -513,16 +518,16 @@ def test_modify_target_group(): ) response = client.describe_target_groups(TargetGroupArns=[arn]) - response["TargetGroups"][0]["Matcher"]["HttpCode"].should.equal("200-399") - response["TargetGroups"][0]["HealthCheckIntervalSeconds"].should.equal(10) - response["TargetGroups"][0]["HealthCheckPath"].should.equal("/status") - response["TargetGroups"][0]["HealthCheckPort"].should.equal("8081") - response["TargetGroups"][0]["HealthCheckProtocol"].should.equal("HTTPS") - response["TargetGroups"][0]["HealthCheckTimeoutSeconds"].should.equal(10) - response["TargetGroups"][0]["HealthyThresholdCount"].should.equal(10) - response["TargetGroups"][0].should.have.key("Protocol").equals("HTTP") - response["TargetGroups"][0].should.have.key("ProtocolVersion").equals("HTTP1") - response["TargetGroups"][0]["UnhealthyThresholdCount"].should.equal(4) + assert response["TargetGroups"][0]["Matcher"]["HttpCode"] == "200-399" + assert response["TargetGroups"][0]["HealthCheckIntervalSeconds"] == 10 + assert response["TargetGroups"][0]["HealthCheckPath"] == "/status" + assert response["TargetGroups"][0]["HealthCheckPort"] == "8081" + assert response["TargetGroups"][0]["HealthCheckProtocol"] == "HTTPS" + assert response["TargetGroups"][0]["HealthCheckTimeoutSeconds"] == 10 + assert response["TargetGroups"][0]["HealthyThresholdCount"] == 10 + assert response["TargetGroups"][0]["Protocol"] == "HTTP" + assert response["TargetGroups"][0]["ProtocolVersion"] == "HTTP1" + assert response["TargetGroups"][0]["UnhealthyThresholdCount"] == 4 @mock_elbv2 @@ -534,18 +539,18 @@ def test_create_target_group_with_target_type(target_type): response = conn.create_target_group(Name="a-target", TargetType=target_type) group = response["TargetGroups"][0] - group.should.have.key("TargetGroupArn") - group.should.have.key("TargetGroupName").equal("a-target") - group.should.have.key("TargetType").equal(target_type) - group.shouldnt.have.key("Protocol") - group.shouldnt.have.key("VpcId") + assert "TargetGroupArn" in group + assert group["TargetGroupName"] == "a-target" + assert group["TargetType"] == target_type + assert "Protocol" not in group + assert "VpcId" not in group group = conn.describe_target_groups()["TargetGroups"][0] - group.should.have.key("TargetGroupArn") - group.should.have.key("TargetGroupName").equal("a-target") - group.should.have.key("TargetType").equal(target_type) - group.shouldnt.have.key("Protocol") - group.shouldnt.have.key("VpcId") + assert "TargetGroupArn" in group + assert group["TargetGroupName"] == "a-target" + assert group["TargetType"] == target_type + assert "Protocol" not in group + assert "VpcId" not in group @mock_elbv2 @@ -555,17 +560,17 @@ def test_delete_target_group_after_modifying_listener(): response, vpc, _, _, _, conn = create_load_balancer() - load_balancer_arn = response.get("LoadBalancers")[0].get("LoadBalancerArn") + load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"] response = client.create_target_group( Name="a-target", Protocol="HTTP", Port=8080, VpcId=vpc.id ) - target_group_arn1 = response.get("TargetGroups")[0]["TargetGroupArn"] + target_group_arn1 = response["TargetGroups"][0]["TargetGroupArn"] response = client.create_target_group( Name="a-target-2", Protocol="HTTPS", Port=8081, VpcId=vpc.id ) - target_group_arn2 = response.get("TargetGroups")[0]["TargetGroupArn"] + target_group_arn2 = response["TargetGroups"][0]["TargetGroupArn"] response = conn.create_listener( LoadBalancerArn=load_balancer_arn, @@ -573,7 +578,7 @@ def test_delete_target_group_after_modifying_listener(): Port=80, DefaultActions=[{"Type": "forward", "TargetGroupArn": target_group_arn1}], ) - listener_arn = response["Listeners"][0].get("ListenerArn") + listener_arn = response["Listeners"][0]["ListenerArn"] client.modify_listener( ListenerArn=listener_arn, @@ -582,9 +587,7 @@ def test_delete_target_group_after_modifying_listener(): response = conn.describe_listeners(LoadBalancerArn=load_balancer_arn) default_actions = response["Listeners"][0]["DefaultActions"] - default_actions.should.equal( - [{"Type": "forward", "TargetGroupArn": target_group_arn2}] - ) + assert default_actions == [{"Type": "forward", "TargetGroupArn": target_group_arn2}] # Target Group 1 can now be deleted, as the LB points to group 2 client.delete_target_group(TargetGroupArn=target_group_arn1) @@ -592,9 +595,7 @@ def test_delete_target_group_after_modifying_listener(): # Sanity check - we're still pointing to group 2 response = conn.describe_listeners(LoadBalancerArn=load_balancer_arn) default_actions = response["Listeners"][0]["DefaultActions"] - default_actions.should.equal( - [{"Type": "forward", "TargetGroupArn": target_group_arn2}] - ) + assert default_actions == [{"Type": "forward", "TargetGroupArn": target_group_arn2}] @mock_elbv2 @@ -604,17 +605,17 @@ def test_create_listener_with_multiple_target_groups(): response, vpc, _, _, _, conn = create_load_balancer() - load_balancer_arn = response.get("LoadBalancers")[0].get("LoadBalancerArn") + load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"] response = client.create_target_group( Name="a-target", Protocol="HTTP", Port=8080, VpcId=vpc.id ) - target_group_arn1 = response.get("TargetGroups")[0]["TargetGroupArn"] + target_group_arn1 = response["TargetGroups"][0]["TargetGroupArn"] response = client.create_target_group( Name="a-target-2", Protocol="HTTPS", Port=8081, VpcId=vpc.id ) - target_group_arn2 = response.get("TargetGroups")[0]["TargetGroupArn"] + target_group_arn2 = response["TargetGroups"][0]["TargetGroupArn"] conn.create_listener( LoadBalancerArn=load_balancer_arn, @@ -640,9 +641,9 @@ def test_create_listener_with_multiple_target_groups(): response = conn.describe_listeners(LoadBalancerArn=load_balancer_arn) listener = response["Listeners"][0] groups = listener["DefaultActions"][0]["ForwardConfig"]["TargetGroups"] - groups.should.have.length_of(2) - groups.should.contain({"TargetGroupArn": target_group_arn1, "Weight": 100}) - groups.should.contain({"TargetGroupArn": target_group_arn2, "Weight": 0}) + assert len(groups) == 2 + assert {"TargetGroupArn": target_group_arn1, "Weight": 100} in groups + assert {"TargetGroupArn": target_group_arn2, "Weight": 0} in groups @mock_elbv2 @@ -650,7 +651,7 @@ def test_create_listener_with_multiple_target_groups(): def test_create_listener_with_invalid_target_group(): response, _, _, _, _, conn = create_load_balancer() - load_balancer_arn = response.get("LoadBalancers")[0].get("LoadBalancerArn") + load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"] with pytest.raises(ClientError) as exc: conn.create_listener( @@ -667,8 +668,8 @@ def test_create_listener_with_invalid_target_group(): ], ) err = exc.value.response["Error"] - err["Code"].should.equal("TargetGroupNotFound") - err["Message"].should.equal("Target group 'unknown' not found") + assert err["Code"] == "TargetGroupNotFound" + assert err["Message"] == "Target group 'unknown' not found" @mock_elbv2 @@ -678,12 +679,12 @@ def test_delete_target_group_while_listener_still_exists(): response, vpc, _, _, _, conn = create_load_balancer() - load_balancer_arn = response.get("LoadBalancers")[0].get("LoadBalancerArn") + load_balancer_arn = response["LoadBalancers"][0]["LoadBalancerArn"] response = client.create_target_group( Name="a-target", Protocol="HTTP", Port=8080, VpcId=vpc.id ) - target_group_arn1 = response.get("TargetGroups")[0]["TargetGroupArn"] + target_group_arn1 = response["TargetGroups"][0]["TargetGroupArn"] response = conn.create_listener( LoadBalancerArn=load_balancer_arn, @@ -706,9 +707,10 @@ def test_delete_target_group_while_listener_still_exists(): with pytest.raises(ClientError) as exc: client.delete_target_group(TargetGroupArn=target_group_arn1) err = exc.value.response["Error"] - err["Code"].should.equal("ResourceInUse") - err["Message"].should.equal( - f"The target group '{target_group_arn1}' is currently in use by a listener or a rule" + assert err["Code"] == "ResourceInUse" + assert ( + err["Message"] + == f"The target group '{target_group_arn1}' is currently in use by a listener or a rule" ) client.delete_listener(ListenerArn=listener_arn) diff --git a/tests/test_elbv2/test_server.py b/tests/test_elbv2/test_server.py index e0a93964b..9977830b0 100644 --- a/tests/test_elbv2/test_server.py +++ b/tests/test_elbv2/test_server.py @@ -1,5 +1,3 @@ -import sure # noqa # pylint: disable=unused-import - import moto.server as server """ @@ -13,4 +11,4 @@ def test_elbv2_describe_load_balancers(): res = test_client.get("/?Action=DescribeLoadBalancers&Version=2015-12-01") - res.data.should.contain(b"DescribeLoadBalancersResponse") + assert b"DescribeLoadBalancersResponse" in res.data