Skip to content

Commit

Permalink
ci: Add basic deployment tests
Browse files Browse the repository at this point in the history
ci: Add basic deployment tests

ci: Add basic deployment tests

ci: Add basic deployment tests

ci: Add basic deployment tests

ci: Add basic deployment tests

ci: Add basic deployment tests

ci: Add basic deployment tests
  • Loading branch information
phzietsman committed Oct 29, 2022
1 parent 1440d68 commit a1e984b
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 40 deletions.
18 changes: 2 additions & 16 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,14 @@ on: [workflow_dispatch, pull_request]

env:
AWS_REGION: eu-west-1
TF_VAR_run_id: ${{ github.run_id }}

permissions:
id-token: write
contents: read

jobs:
python_lint:
name: 🐍 Python file formatting
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- run: |
python -m venv .venv
source .venv/bin/activate
pip install -r tests/requirements.txt
flake8
black .
testing:
needs: [python_lint]
name: ✅ Testing
runs-on: ubuntu-latest
steps:
Expand All @@ -34,7 +21,6 @@ jobs:
aws-region: ${{ env.AWS_REGION }}
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
role-duration-seconds: 3600
- uses: actions/setup-python@v2
- uses: hashicorp/setup-terraform@v2
- name: Install tests
Expand All @@ -45,7 +31,7 @@ jobs:
- name: Execute tests
run: |
source .venv/bin/activate
pytest --run-id ${{ github.run_id }}
pytest
env:
PYTEST_ADDOPTS: "--color=yes"
timeout-minutes: 30
35 changes: 32 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,48 @@ terraform {
source = "hashicorp/aws"
version = "4.9.0"
}
random = {
source = "hashicorp/random"
version = "3.4.3"
}
}
}
provider "aws" {
region = var.region
region = "eu-west-1"
}
locals {
tags = {
uasage = "clickops-testing"
run = random_pet.run_id.id
}
naming_prefix = "clickops-test-basic-${random_pet.run_id.id}"
}
resource "random_pet" "run_id" {
keepers = {
# Generate a new pet name each time we switch to a new AMI id
run_id = var.run_id
}
}
module "clickops_notifications" {
source = "../../"
cloudtrail_bucket_name = var.cloudtrail_bucket_name
webhook = var.webhook
naming_prefix = local.naming_prefix
cloudtrail_bucket_name = aws_s3_bucket.test_bucket.id
webhook = "https://fake.com"
message_format = "slack"
tags = local.tags
}
resource "aws_s3_bucket" "test_bucket" {
bucket = local.naming_prefix
tags = local.tags
}
```
----
Expand Down
7 changes: 7 additions & 0 deletions examples/role/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ resource "aws_iam_role" "test_role" {
tags = local.tags
}

resource "aws_iam_role_policy_attachment" "test_attach" {
role = aws_iam_role.test_role.name
policy_arn = "arn:aws:iam::aws:policy/AmazonSQSFullAccess"
}



resource "aws_s3_bucket" "test_bucket" {
bucket = local.naming_prefix
tags = local.tags
Expand Down
25 changes: 6 additions & 19 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import tftest
import os
import logging
import json


def pytest_addoption(parser):
Expand All @@ -19,15 +18,6 @@ def profile(request):
return profile


@pytest.fixture(scope="session")
def terraform_default_variables(request):
"""Return default Terraform variables."""
variables = {"run_id": request.config.getoption("--run-id")}
if variables["run_id"] is None:
raise Exception("--run_id must be specified")
return variables


def terraform_examples_dir():
return os.path.join(os.getcwd(), "examples")

Expand All @@ -47,14 +37,12 @@ def terraform_examples():

@pytest.fixture(scope="session")
def terraform_config(
terraform_binary, terraform_tests, terraform_examples, terraform_default_variables
terraform_binary, terraform_examples
):
"""Convenience fixture for passing around config."""
config = {
"terraform_binary": terraform_binary,
"terraform_tests": terraform_tests,
"terraform_examples": terraform_examples,
"terraform_default_variables": terraform_default_variables,
"terraform_examples": terraform_examples
}
logging.info(config)
return config
Expand All @@ -75,11 +63,10 @@ def get_tf(test_name, terraform_config, variables=None):
tf = tftest.TerraformTest(
tfdir=test_name, basedir=basedir, binary=terraform_config["terraform_binary"]
)
# Populate test.auto.tfvars.json with the specified variables
variables = variables or {}
variables = {**terraform_config["terraform_default_variables"], **variables}
with open(os.path.join(basedir, test_name, "test.auto.tfvars.json"), "w") as f:
json.dump(variables, f)
# # Populate test.auto.tfvars.json with the specified variables
# variables = variables or {}
# with open(os.path.join(basedir, test_name, "test.auto.tfvars.json"), "w") as f:
# json.dump(variables, f)
tf.setup()
return tf

Expand Down
2 changes: 1 addition & 1 deletion tests/test_examples_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

@pytest.fixture(scope="session")
def plan(terraform_config):
yield from terraform_apply(__name__, terraform_config)
yield from terraform_apply("examples_basic", terraform_config)


@pytest.mark.slow
Expand Down
2 changes: 1 addition & 1 deletion tests/test_examples_role.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

@pytest.fixture(scope="session")
def plan(terraform_config):
yield from terraform_apply(__name__, terraform_config)
yield from terraform_apply("examples_role", terraform_config)


@pytest.mark.slow
Expand Down

0 comments on commit a1e984b

Please sign in to comment.