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

Refactor to use pytest over unittest #1037

Merged
merged 1 commit into from
Jul 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
Refactor to use pytest over unittest
  • Loading branch information
jandom committed Jul 3, 2021
commit 3f9c9045fe330f66af46753fed0de7a894268a11
5 changes: 3 additions & 2 deletions testing-unit-py/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ An example of writing mock-based unit tests with both infrastructure definition
```bash
$ python3 -m venv venv
$ source venv/bin/activate
$ pip3 install -r requirements.txt
$ python -m pip install --upgrade pip
$ python -m pip install -r requirements.txt
```

2. Run the tests:

```
$ python -m unittest
$ python -m pytest # or simply `pytest`

------------------------------------------------------------
Ran 2 tests in 0.004s
Expand Down
1 change: 1 addition & 0 deletions testing-unit-py/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pulumi>=3.0.0,<4.0.0
pulumi-aws>=4.0.0,<5.0.0
pytest
60 changes: 30 additions & 30 deletions testing-unit-py/test_ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,33 @@ def call(self, args: pulumi.runtime.MockCallArgs):
# Now actually import the code that creates resources, and then test it.
import infra

class TestingWithMocks(unittest.TestCase):
# Test if the service has tags and a name tag.
@pulumi.runtime.test
def test_server_tags(self):
def check_tags(args):
urn, tags = args
self.assertIsNotNone(tags, f'server {urn} must have tags')
self.assertIn('Name', tags, 'server {urn} must have a name tag')

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(self):
def check_user_data(args):
urn, user_data = args
self.assertFalse(user_data, 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(self):
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])
self.assertFalse(ssh_open, 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)

# Test if the service has tags and a name tag.
@pulumi.runtime.test
def test_server_tags():
def check_tags(args):
urn, tags = args
assert tags, f'server {urn} must have tags'
assert 'Name' in tags, 'server {urn} must have a name tag'

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}'

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)'

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