Best Python code snippet using localstack_python
test_network_acls.py
Source:test_network_acls.py  
...15@mock_ec2_deprecated16def test_network_acls():17    conn = boto.connect_vpc("the_key", "the secret")18    vpc = conn.create_vpc("10.0.0.0/16")19    network_acl = conn.create_network_acl(vpc.id)20    all_network_acls = conn.get_all_network_acls()21    all_network_acls.should.have.length_of(3)22@mock_ec2_deprecated23def test_new_subnet_associates_with_default_network_acl():24    conn = boto.connect_vpc("the_key", "the secret")25    vpc = conn.get_all_vpcs()[0]26    subnet = conn.create_subnet(vpc.id, "172.31.112.0/20")27    all_network_acls = conn.get_all_network_acls()28    all_network_acls.should.have.length_of(1)29    acl = all_network_acls[0]30    acl.associations.should.have.length_of(7)31    [a.subnet_id for a in acl.associations].should.contain(subnet.id)32@mock_ec2_deprecated33def test_network_acl_entries():34    conn = boto.connect_vpc("the_key", "the secret")35    vpc = conn.create_vpc("10.0.0.0/16")36    network_acl = conn.create_network_acl(vpc.id)37    network_acl_entry = conn.create_network_acl_entry(38        network_acl.id,39        110,40        6,41        "ALLOW",42        "0.0.0.0/0",43        False,44        port_range_from="443",45        port_range_to="443",46    )47    all_network_acls = conn.get_all_network_acls()48    all_network_acls.should.have.length_of(3)49    test_network_acl = next(na for na in all_network_acls if na.id == network_acl.id)50    entries = test_network_acl.network_acl_entries51    entries.should.have.length_of(1)52    entries[0].rule_number.should.equal("110")53    entries[0].protocol.should.equal("6")54    entries[0].rule_action.should.equal("ALLOW")55@mock_ec2_deprecated56def test_delete_network_acl_entry():57    conn = boto.connect_vpc("the_key", "the secret")58    vpc = conn.create_vpc("10.0.0.0/16")59    network_acl = conn.create_network_acl(vpc.id)60    conn.create_network_acl_entry(61        network_acl.id,62        110,63        6,64        "ALLOW",65        "0.0.0.0/0",66        False,67        port_range_from="443",68        port_range_to="443",69    )70    conn.delete_network_acl_entry(network_acl.id, 110, False)71    all_network_acls = conn.get_all_network_acls()72    test_network_acl = next(na for na in all_network_acls if na.id == network_acl.id)73    entries = test_network_acl.network_acl_entries74    entries.should.have.length_of(0)75@mock_ec2_deprecated76def test_replace_network_acl_entry():77    conn = boto.connect_vpc("the_key", "the secret")78    vpc = conn.create_vpc("10.0.0.0/16")79    network_acl = conn.create_network_acl(vpc.id)80    conn.create_network_acl_entry(81        network_acl.id,82        110,83        6,84        "ALLOW",85        "0.0.0.0/0",86        False,87        port_range_from="443",88        port_range_to="443",89    )90    conn.replace_network_acl_entry(91        network_acl.id,92        110,93        -1,94        "DENY",95        "0.0.0.0/0",96        False,97        port_range_from="22",98        port_range_to="22",99    )100    all_network_acls = conn.get_all_network_acls()101    test_network_acl = next(na for na in all_network_acls if na.id == network_acl.id)102    entries = test_network_acl.network_acl_entries103    entries.should.have.length_of(1)104    entries[0].rule_number.should.equal("110")105    entries[0].protocol.should.equal("-1")106    entries[0].rule_action.should.equal("DENY")107@mock_ec2_deprecated108def test_associate_new_network_acl_with_subnet():109    conn = boto.connect_vpc("the_key", "the secret")110    vpc = conn.create_vpc("10.0.0.0/16")111    subnet = conn.create_subnet(vpc.id, "10.0.0.0/18")112    network_acl = conn.create_network_acl(vpc.id)113    conn.associate_network_acl(network_acl.id, subnet.id)114    all_network_acls = conn.get_all_network_acls()115    all_network_acls.should.have.length_of(3)116    test_network_acl = next(na for na in all_network_acls if na.id == network_acl.id)117    test_network_acl.associations.should.have.length_of(1)118    test_network_acl.associations[0].subnet_id.should.equal(subnet.id)119@mock_ec2_deprecated120def test_delete_network_acl():121    conn = boto.connect_vpc("the_key", "the secret")122    vpc = conn.create_vpc("10.0.0.0/16")123    subnet = conn.create_subnet(vpc.id, "10.0.0.0/18")124    network_acl = conn.create_network_acl(vpc.id)125    all_network_acls = conn.get_all_network_acls()126    all_network_acls.should.have.length_of(3)127    any(acl.id == network_acl.id for acl in all_network_acls).should.be.ok128    conn.delete_network_acl(network_acl.id)129    updated_network_acls = conn.get_all_network_acls()130    updated_network_acls.should.have.length_of(2)131    any(acl.id == network_acl.id for acl in updated_network_acls).shouldnt.be.ok132@mock_ec2_deprecated133def test_network_acl_tagging():134    conn = boto.connect_vpc("the_key", "the secret")135    vpc = conn.create_vpc("10.0.0.0/16")136    network_acl = conn.create_network_acl(vpc.id)137    network_acl.add_tag("a key", "some value")138    tag = conn.get_all_tags()[0]139    tag.name.should.equal("a key")140    tag.value.should.equal("some value")141    all_network_acls = conn.get_all_network_acls()142    test_network_acl = next(na for na in all_network_acls if na.id == network_acl.id)143    test_network_acl.tags.should.have.length_of(1)144    test_network_acl.tags["a key"].should.equal("some value")145@mock_ec2146def test_new_subnet_in_new_vpc_associates_with_default_network_acl():147    ec2 = boto3.resource("ec2", region_name="us-west-1")148    new_vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16")149    new_vpc.reload()150    subnet = ec2.create_subnet(VpcId=new_vpc.id, CidrBlock="10.0.0.0/24")151    subnet.reload()152    new_vpcs_default_network_acl = next(iter(new_vpc.network_acls.all()), None)153    new_vpcs_default_network_acl.reload()154    new_vpcs_default_network_acl.vpc_id.should.equal(new_vpc.id)155    new_vpcs_default_network_acl.associations.should.have.length_of(1)156    new_vpcs_default_network_acl.associations[0]["SubnetId"].should.equal(subnet.id)157@mock_ec2158def test_default_network_acl_default_entries():159    ec2 = boto3.resource("ec2", region_name="us-west-1")160    default_network_acl = next(iter(ec2.network_acls.all()), None)161    default_network_acl.is_default.should.be.ok162    default_network_acl.entries.should.have.length_of(4)163    unique_entries = []164    for entry in default_network_acl.entries:165        entry["CidrBlock"].should.equal("0.0.0.0/0")166        entry["Protocol"].should.equal("-1")167        entry["RuleNumber"].should.be.within([100, 32767])168        entry["RuleAction"].should.be.within(["allow", "deny"])169        assert type(entry["Egress"]) is bool170        if entry["RuleAction"] == "allow":171            entry["RuleNumber"].should.be.equal(100)172        else:173            entry["RuleNumber"].should.be.equal(32767)174        if entry not in unique_entries:175            unique_entries.append(entry)176    unique_entries.should.have.length_of(4)177@mock_ec2178def test_delete_default_network_acl_default_entry():179    ec2 = boto3.resource("ec2", region_name="us-west-1")180    default_network_acl = next(iter(ec2.network_acls.all()), None)181    default_network_acl.is_default.should.be.ok182    default_network_acl.entries.should.have.length_of(4)183    first_default_network_acl_entry = default_network_acl.entries[0]184    default_network_acl.delete_entry(185        Egress=first_default_network_acl_entry["Egress"],186        RuleNumber=first_default_network_acl_entry["RuleNumber"],187    )188    default_network_acl.entries.should.have.length_of(3)189@mock_ec2190def test_duplicate_network_acl_entry():191    ec2 = boto3.resource("ec2", region_name="us-west-1")192    default_network_acl = next(iter(ec2.network_acls.all()), None)193    default_network_acl.is_default.should.be.ok194    rule_number = 200195    egress = True196    default_network_acl.create_entry(197        CidrBlock="0.0.0.0/0",198        Egress=egress,199        Protocol="-1",200        RuleAction="allow",201        RuleNumber=rule_number,202    )203    with pytest.raises(ClientError) as ex:204        default_network_acl.create_entry(205            CidrBlock="10.0.0.0/0",206            Egress=egress,207            Protocol="-1",208            RuleAction="deny",209            RuleNumber=rule_number,210        )211    str(ex.value).should.equal(212        "An error occurred (NetworkAclEntryAlreadyExists) when calling the CreateNetworkAclEntry "213        "operation: The network acl entry identified by {} already exists.".format(214            rule_number215        )216    )217@mock_ec2218def test_describe_network_acls():219    conn = boto3.client("ec2", region_name="us-west-2")220    vpc = conn.create_vpc(CidrBlock="10.0.0.0/16")221    vpc_id = vpc["Vpc"]["VpcId"]222    network_acl = conn.create_network_acl(VpcId=vpc_id)223    network_acl_id = network_acl["NetworkAcl"]["NetworkAclId"]224    resp = conn.describe_network_acls(NetworkAclIds=[network_acl_id])225    result = resp["NetworkAcls"]226    result.should.have.length_of(1)227    result[0]["NetworkAclId"].should.equal(network_acl_id)228    resp2 = conn.describe_network_acls()["NetworkAcls"]229    resp2.should.have.length_of(3)230    resp3 = conn.describe_network_acls(231        Filters=[{"Name": "owner-id", "Values": [OWNER_ID]}]232    )["NetworkAcls"]233    resp3.should.have.length_of(3)234    with pytest.raises(ClientError) as ex:235        conn.describe_network_acls(NetworkAclIds=["1"])236    str(ex.value).should.equal(237        "An error occurred (InvalidRouteTableID.NotFound) when calling the "238        "DescribeNetworkAcls operation: The routeTable ID '1' does not exist"239    )240@mock_ec2241def test_create_network_acl_with_tags():242    conn = boto3.client("ec2", region_name="us-west-2")243    vpc = conn.create_vpc(CidrBlock="10.0.0.0/16")244    vpc_id = vpc["Vpc"]["VpcId"]245    network_acl = conn.create_network_acl(246        VpcId=vpc_id,247        TagSpecifications=[248            {249                "ResourceType": "network-acl",250                "Tags": [{"Key": "test", "Value": "TestTags"}],251            }252        ],253    )254    (len(network_acl.get("NetworkAcl").get("Tags"))).should.equal(1)255    network_acl.get("NetworkAcl").get("Tags").should.equal(256        [{"Key": "test", "Value": "TestTags"}]...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
