Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PEP warnings in testing-unittest-py #1411

Merged
merged 2 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions testing-unit-py/infra.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import pulumi
from pulumi_aws import ec2, get_ami, GetAmiFilterArgs
from pulumi_aws import ec2

group = ec2.SecurityGroup('web-secgrp', ingress=[
# Uncomment to fail a test:
#{ "protocol": "tcp", "from_port": 22, "to_port": 22, "cidr_blocks": ["0.0.0.0/0"] },
{ "protocol": "tcp", "from_port": 80, "to_port": 80, "cidr_blocks": ["0.0.0.0/0"] },
# {"protocol": "tcp", "from_port": 22, "to_port": 22, "cidr_blocks": ["0.0.0.0/0"]},
{"protocol": "tcp", "from_port": 80, "to_port": 80, "cidr_blocks": ["0.0.0.0/0"]},
])

user_data = '#!/bin/bash echo "Hello, World!" > index.html nohup python -m SimpleHTTPServer 80 &'
Expand All @@ -20,10 +19,10 @@
).id

server = ec2.Instance('web-server-www',
instance_type="t2.micro",
vpc_security_group_ids=[ group.id ], # reference the group object above
# Comment out to fail a test:
tags={'Name': 'webserver'}, # name tag
# Uncomment to fail a test:
#user_data=user_data) # start a simple web server
ami=ami_id)
instance_type="t2.micro",
vpc_security_group_ids=[group.id], # reference the group object above
# Comment out to fail a test:
tags={'Name': 'webserver'}, # name tag
# Uncomment to fail a test:
# user_data=user_data) # start a simple web server
ami=ami_id)
14 changes: 10 additions & 4 deletions testing-unit-py/test_ec2.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest
import pulumi


class MyMocks(pulumi.runtime.Mocks):
def new_resource(self, args: pulumi.runtime.MockResourceArgs):
outputs = args.inputs
Expand All @@ -11,6 +11,7 @@ def new_resource(self, args: pulumi.runtime.MockResourceArgs):
"publicDns": "ec2-203-0-113-12.compute-1.amazonaws.com",
}
return [args.name + '_id', outputs]

def call(self, args: pulumi.runtime.MockCallArgs):
if args.token == "aws:ec2/getAmi:getAmi":
return {
Expand All @@ -19,6 +20,7 @@ def call(self, args: pulumi.runtime.MockCallArgs):
}
return {}


pulumi.runtime.set_mocks(MyMocks())

# Now actually import the code that creates resources, and then test it.
Expand All @@ -35,22 +37,26 @@ def check_tags(args):

return pulumi.Output.all(infra.server.urn, infra.server.tags).apply(check_tags)


# Test if the instance is configured with user_data.
@pulumi.runtime.test
def test_server_userdata():
def check_user_data(args):
urn, user_data = args
assert user_data == None, f'illegal use of user_data on server {urn}'
assert user_data is None, f'illegal use of user_data on server {urn}'

return pulumi.Output.all(infra.server.urn, infra.server.user_data).apply(check_user_data)


# Test if port 22 for ssh is exposed.
@pulumi.runtime.test
def test_security_group_rules():
def check_security_group_rules(args):
urn, ingress = args
ssh_open = any([rule['from_port'] == 22 and any([block == "0.0.0.0/0" for block in rule['cidr_blocks']]) for rule in ingress])
assert ssh_open == False, f'security group {urn} exposes port 22 to the Internet (CIDR 0.0.0.0/0)'
ssh_open = any(
[rule['from_port'] == 22 and any([block == "0.0.0.0/0" for block in rule['cidr_blocks']]) for rule in
ingress])
assert ssh_open is False, f'security group {urn} exposes port 22 to the Internet (CIDR 0.0.0.0/0)'

# Return the results of the unit tests.
return pulumi.Output.all(infra.group.urn, infra.group.ingress).apply(check_security_group_rules)