Skip to content

Commit

Permalink
layout alternative function
Browse files Browse the repository at this point in the history
Signed-off-by: Katharina Trentau <[email protected]>
  • Loading branch information
fraugabel committed Sep 12, 2024
1 parent bfc7c47 commit 042b927
Showing 1 changed file with 63 additions and 67 deletions.
130 changes: 63 additions & 67 deletions Tests/iaas/security-groups/default-security-group-rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,73 +178,69 @@ def altern_test_rules(cloud_name: str):
egress_ipv4_custom_sg = 0
egress_ipv6_default_sg = 0
egress_ipv6_custom_sg = 0
try:
if not rules:
print("No default security group rules defined.")
else:
for rule in rules:
# direction = rule['direction']
# ethertype = rule['ethertype']
# r_custom_sg = rule['used_in_non_default_sg']
# r_default_sg = rule['used_in_default_sg']
direction = rule.direction
ethertype = rule.ether_type
# r_custom_sg = rule.used_in_non_default_sg
# r_default_sg = rule.used_in_default_sg

print(f"#############{direction}")
print(f"#############{ethertype}")

if direction == "ingress":
ingress_rules += 1
# we allow ingress from the same security group
# but only for the default security group
# r_group_id = rule.remote_group_id
# if (r_group_id == "PARENT" and not r_custom_sg):
# ingress_from_same_sg += 1
elif direction == "egress" and ethertype == "IPv4":
egress_rules += 1
if rule.remote_ip_prefix:
# this rule does not allow traffic to all external ips
continue
# if r_custom_sg:
# egress_ipv4_custom_sg += 1
# if r_default_sg:
# egress_ipv4_default_sg += 1
elif direction == "egress" and ethertype == "IPv6":
egress_rules += 1
if rule.remote_ip_prefix:
# this rule does not allow traffic to all external ips
continue
# if r_custom_sg:
# egress_ipv6_custom_sg += 1
# if r_default_sg:
# egress_ipv6_default_sg += 1

# test whether there are no other than the allowed ingress rules
# assert ingress_rules == ingress_from_same_sg, (
# f"Expected only ingress rules for default security groups, "
# f"that allow ingress traffic from the same group. "
# f"But there are more - in total {ingress_rules} ingress rules. "
# f"There should be only {ingress_from_same_sg} ingress rules."
# )
assert (
egress_rules > 0
), f"Expected to have more than {egress_rules} egress rules present."
var_list = [
egress_ipv4_default_sg,
egress_ipv4_custom_sg,
egress_ipv6_default_sg,
egress_ipv6_custom_sg,
]
assert all([var > 0 for var in var_list]), (
"Not all expected egress rules are present. "
"Expected rules for egress for IPv4 and IPv6 "
"both for default and custom security groups."
)
except:
print("something went wrong")
result_dict = {"Ingress Rules": ingress_rules, "Egress Rules": egress_rules}
if not rules:
print("No default security group rules defined.")
else:
for rule in rules.security_group_rules:
print (f"#############{rule}")
direction = rule['direction']
ethertype = rule['ethertype']
# r_custom_sg = rule['used_in_non_default_sg']
# r_default_sg = rule['used_in_default_sg']
# r_custom_sg = rule.used_in_non_default_sg
# r_default_sg = rule.used_in_default_sg

print(f"#############{direction}")
print(f"#############{ethertype}")

if direction == "ingress":
ingress_rules += 1
# we allow ingress from the same security group
# but only for the default security group
# r_group_id = rule.remote_group_id
# if (r_group_id == "PARENT" and not r_custom_sg):
# ingress_from_same_sg += 1
elif direction == "egress" and ethertype == "IPv4":
egress_rules += 1
# if rule.remote_ip_prefix:
# # this rule does not allow traffic to all external ips
# continue
# if r_custom_sg:
# egress_ipv4_custom_sg += 1
# if r_default_sg:
# egress_ipv4_default_sg += 1
elif direction == "egress" and ethertype == "IPv6":
egress_rules += 1
# if rule.remote_ip_prefix:
# # this rule does not allow traffic to all external ips
# continue
# if r_custom_sg:
# egress_ipv6_custom_sg += 1
# if r_default_sg:
# egress_ipv6_default_sg += 1

# test whether there are no other than the allowed ingress rules
# assert ingress_rules == ingress_from_same_sg, (
# f"Expected only ingress rules for default security groups, "
# f"that allow ingress traffic from the same group. "
# f"But there are more - in total {ingress_rules} ingress rules. "
# f"There should be only {ingress_from_same_sg} ingress rules."
# )
assert (
egress_rules > 0
), f"Expected to have more than {egress_rules} egress rules present."
var_list = [
egress_ipv4_default_sg,
egress_ipv4_custom_sg,
egress_ipv6_default_sg,
egress_ipv6_custom_sg,
]
assert all([var > 0 for var in var_list]), (
"Not all expected egress rules are present. "
"Expected rules for egress for IPv4 and IPv6 "
"both for default and custom security groups."
)


delete_security_group(connection, sg_id)
result_dict = {"Ingress Rules": ingress_rules, "Egress Rules": egress_rules}
Expand Down

0 comments on commit 042b927

Please sign in to comment.