Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ jobs:

- name: Pytest
shell: bash
run: poetry run pytest -v tests
run: poetry run pytest -v tests/unit
112 changes: 111 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jsonschema = "^3.2.0"
[tool.poetry.dev-dependencies]
pytest = "^6.2.4"
black = "^21.7b0"
pytest-terraform = { git = "https://github.com/cloud-custodian/pytest-terraform.git", branch = "work-dir-test-api" }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's a pr upstream to avoid the need for this dep being pulled off a branch.


[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down
26 changes: 15 additions & 11 deletions tests/unit/conftest.py → tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@
import pytest


def load_data(filename, location="unit"):
path = Path(__file__).parent / location / "data" / filename
if not path.exists():
return None
with open(path) as f:
return json.load(f)


def write_data(filename, data, location="unit"):
fpath = Path(__file__).parent / location / "data" / filename
if not fpath.exists():
fpath.write_text(data)


@pytest.fixture()
def validate():
def schema_validate(translator, resource):
Expand All @@ -15,9 +29,7 @@ def schema_validate(translator, resource):
cfn = boto3.client("cloudformation")
rtype = cfn.describe_type(TypeName=translator.cfn_type, Type="RESOURCE")
schema = json.loads(rtype["Schema"])
(Path(__file__).parent / "data" / schema_path).write_text(
json.dumps(schema, indent=2)
)
write_data(schema_path, json.dumps(schema, indent=2))

props = set(resource)
sprops = set(schema["properties"].keys())
Expand All @@ -40,11 +52,3 @@ def schema_validate(translator, resource):
)

return schema_validate


def load_data(filename):
path = Path(__file__).parent / "data" / filename
if not path.exists():
return None
with open(path) as f:
return json.load(f)
20 changes: 20 additions & 0 deletions tests/functional/terraform/aws_kinesis_stream/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
resource "random_pet" "name" {
length = 2
separator = "-"
}


resource "aws_kinesis_stream" "test_stream" {
name = "test-${random_pet.name.id}"
shard_count = 1
retention_period = 48

shard_level_metrics = [
"IncomingBytes",
"OutgoingBytes",
]

tags = {
Environment = "test"
}
}
22 changes: 22 additions & 0 deletions tests/functional/test_fresources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import json

import conftest
from pytest_terraform import terraform
from tfdevops.cli import Translator, get_state_resources


def get_state_path(tmpdir, tf_resources):
with open(tmpdir / "state.json", "w") as fh:
fh.write(json.dumps(tf_resources.terraform.show(), indent=2))
return fh.name


@terraform("aws_kinesis_stream")
def test_kinesis_stream(tmpdir, aws_kinesis_stream, validate):
resources = get_state_resources(None, get_state_path(tmpdir, aws_kinesis_stream))
translator = Translator.get_translator("kinesis_stream")()
props = translator.get_properties(resources["aws_kinesis_stream"][0])
conftest.write_data(
"kinesis_stream.json", json.dumps(resources["aws_kinesis_stream"][0], indent=2)
)
validate(translator, props)
40 changes: 40 additions & 0 deletions tests/unit/data/kinesis_stream.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"address": "aws_kinesis_stream.test_stream",
"mode": "managed",
"type": "aws_kinesis_stream",
"name": "test_stream",
"provider_name": "registry.terraform.io/hashicorp/aws",
"schema_version": 1,
"values": {
"arn": "arn:aws:kinesis:us-east-2:112233445566:stream/test-poetic-marten",
"encryption_type": "NONE",
"enforce_consumer_deletion": false,
"id": "arn:aws:kinesis:us-east-2:112233445566:stream/test-poetic-marten",
"kms_key_id": "",
"name": "test-poetic-marten",
"retention_period": 48,
"shard_count": 1,
"shard_level_metrics": [
"IncomingBytes",
"OutgoingBytes"
],
"tags": {
"Environment": "test"
},
"tags_all": {
"Environment": "test"
},
"timeouts": null
},
"sensitive_values": {
"shard_level_metrics": [
false,
false
],
"tags": {},
"tags_all": {}
},
"depends_on": [
"random_pet.name"
]
}
Loading