Skip to content

Commit

Permalink
meta tests (#96)
Browse files Browse the repository at this point in the history
* readme: document meta tests (for param fetching) file layout

* make: Add metatest target

* make: Add metatest against to doctest-coverage target

* make: rename doctest-coverage to the more accurate coverage

* Add some example_cache files

* Add example meta test

* fix flake8 errors in iam resource tests

* test iam_inline_policies
  • Loading branch information
g-k authored Mar 5, 2018
1 parent 6f25ef3 commit 5f41931
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ install:
- make install

script:
- make doctest-coverage flake8
- make coverage flake8
13 changes: 12 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ clean-cache: check_venv
doctest: check_venv
pytest --doctest-modules -s --offline --aws-debug-calls

doctest-coverage: check_venv
coverage: check_venv
pytest --cov-config .coveragerc --cov=. --doctest-modules -s --offline --aws-debug-calls
pytest --cov-config .coveragerc --cov=. --cov-append \
--aws-profiles example-account \
--aws-regions us-east-1 \
-o python_files=meta_test*.py \
-o cache_dir=./example_cache/
coverage report -m
coverage html

Expand All @@ -40,6 +45,12 @@ flake8: check_venv
install: venv
( . venv/bin/activate && pip install -r requirements.txt )

metatest:
pytest --aws-profiles example-account \
--aws-regions us-east-1 \
-o python_files=meta_test*.py \
-o cache_dir=./example_cache/

venv:
python3 -m venv venv

Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,9 @@ Additionally we want:
* data fetching functions in a `resources.py`
* data checking and test helpers in a `helpers.py`
* prefix test files with `test_`
* doctests for non test files (e.g. `helpers.py`, `resources.py`, `client.py`)
* tests that depend on external IO or the runtime environment (env vars, file system, HTTP) to use the prefix `meta_test_` (and probably `mock` or `pytest.monkeypatch`)
* JSON fixtures for anonymized cached http call in `example_cache/v/`
* tests to have pytest markers for any services they depend on for data
* HTTP clients should be read only and use read only credentials
* running a test should not modify services
Expand All @@ -451,8 +454,16 @@ Additionally we want:
```console
pytest-services
...
├── example_cache
│   └── v
│   ├── cache
│   │   └── lastfailed
│   ├── pytest_aws:example-account:us-east-1:ec2:describe_instances::.json
│   ├── pytest_aws:example-account:us-east-1:ec2:describe_security_groups::.json
...
├── <third party service A>
│   ├── client.py
│   ├── meta_test_client.py
│   ├── <subservice A (optional)>
│   │   ├── __init__.py
│   │   ├── helpers.py
Expand Down
31 changes: 31 additions & 0 deletions aws/iam/meta_test_resources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@


from aws.iam.resources import (
iam_users,
iam_inline_policies,
)


def test_iam_users():
assert iam_users() == [
{
'Arn': 'arn:aws:iam::123456789012:user/hobbes',
'CreateDate': '1985-11-18T00:01:10+00:00',
'PasswordLastUsed': '2018-01-09T20:43:00+00:00',
'Path': '/',
'UserId': 'H0BBIHMA0CZ0R0K0MN00C',
'UserName': 'tigerone',
'__pytest_meta': {'profile': 'example-account', 'region': 'us-east-1'}},
{
'Arn': 'arn:aws:iam::123456789012:user/calvin',
'CreateDate': '1985-11-18T00:01:10+00:00',
'PasswordLastUsed': '2008-01-09T20:43:00+00:00',
'Path': '/',
'UserId': 'CALCIHMA0CZ0R0K0MN00C',
'UserName': 'spacemanspiff',
'__pytest_meta': {'profile': 'example-account', 'region': 'us-east-1'}}]


def test_iam_inline_policies_for_user_without_policies():
# extracts empty 'PolicyNames'
assert iam_inline_policies(username='tigerone') == []
1 change: 1 addition & 0 deletions example_cache/v/cache/lastfailed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"PolicyNames": [],
"__pytest_meta": {
"profile": "example-account",
"region": "us-east-1"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"PolicyNames": [],
"__pytest_meta": {
"profile": "example-account",
"region": "us-east-1"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"Users": [
{
"Arn": "arn:aws:iam::123456789012:user/hobbes",
"CreateDate": "1985-11-18T00:01:10+00:00",
"PasswordLastUsed": "2018-01-09T20:43:00+00:00",
"Path": "/",
"UserId": "H0BBIHMA0CZ0R0K0MN00C",
"UserName": "tigerone"
},
{
"Arn": "arn:aws:iam::123456789012:user/calvin",
"CreateDate": "1985-11-18T00:01:10+00:00",
"PasswordLastUsed": "2008-01-09T20:43:00+00:00",
"Path": "/",
"UserId": "CALCIHMA0CZ0R0K0MN00C",
"UserName": "spacemanspiff"
}
],
"__pytest_meta": {
"profile": "example-account",
"region": "us-east-1"
}
}

0 comments on commit 5f41931

Please sign in to comment.