Skip to content

Commit

Permalink
fix warnings and unused import (#1411)
Browse files Browse the repository at this point in the history
  • Loading branch information
ocworld committed Jan 23, 2024
1 parent 2e62f83 commit 6e886de
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
19 changes: 9 additions & 10 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

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)

0 comments on commit 6e886de

Please sign in to comment.