From 6e886dea7cbbee0f962c24afd58c0b3fe7814209 Mon Sep 17 00:00:00 2001 From: Keunhyun Oh Date: Tue, 23 Jan 2024 10:35:05 +0900 Subject: [PATCH] fix warnings and unused import (#1411) --- testing-unit-py/infra.py | 19 +++++++++---------- testing-unit-py/test_ec2.py | 14 ++++++++++---- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/testing-unit-py/infra.py b/testing-unit-py/infra.py index 62df3f6e6..ba1eeb88c 100644 --- a/testing-unit-py/infra.py +++ b/testing-unit-py/infra.py @@ -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 &' @@ -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) diff --git a/testing-unit-py/test_ec2.py b/testing-unit-py/test_ec2.py index 4cc2a5690..3934a7451 100644 --- a/testing-unit-py/test_ec2.py +++ b/testing-unit-py/test_ec2.py @@ -1,6 +1,6 @@ -import unittest import pulumi + class MyMocks(pulumi.runtime.Mocks): def new_resource(self, args: pulumi.runtime.MockResourceArgs): outputs = args.inputs @@ -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 { @@ -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. @@ -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)