Skip to content

feat: add lint and unit test workflow checks for pull requests #152

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

Merged
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
12 changes: 12 additions & 0 deletions .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,15 @@ jobs:
run: |
python -m pip install --upgrade pip
python -m pip install pipenv

- name: Install local deps
run: pipenv install --dev

- name: Lint check
run: pipenv run lint:ci

- name: Type check
run: pipenv run typecheck

- name: Unit tests
run: pipenv run test
55 changes: 53 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,59 @@ override.tf.json
.terraformrc
terraform.rc

# Locals
.swp
.idea
.idea*
.vscode/*
*.DS_Store
*.zip
.env
.envrc

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# Unit test / coverage reports
.pytest*
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.coverage
.hypothesis/
.mypy_cache/

# Lockfile
Pipfile.lock

# Lambda directories
builds/
__pycache__/
functions/pytest.ini
*.zip

# Integration testing file
.int.env
23 changes: 3 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,11 @@ Doing serverless with Terraform? Check out [serverless.tf framework](https://ser
- Create new SNS topic or use existing one
- Support plaintext and encrypted version of Slack webhook URL
- Most of Slack message options are customizable
- Support different types of SNS messages:
- Various event types are supported, even generic messages:
- AWS CloudWatch Alarms
- AWS CloudWatch LogMetrics Alarms
- AWS GuardDuty Findings
- Local pytest driven testing of the lambda to a Slack sandbox channel

## Feature Roadmap

- More SNS message types: [Send pull-request to add support of other message types](https://github.com/terraform-aws-modules/terraform-aws-notify-slack/pulls)

## Usage

Expand All @@ -37,10 +33,6 @@ module "notify_slack" {
}
```

## Upgrade from 2.0 to 3.0

Version 3 uses [Terraform AWS Lambda module](https://github.com/terraform-aws-modules/terraform-aws-lambda) to handle most of heavy-lifting related to Lambda packaging, roles, and permissions, while maintaining the same interface for the user of this module after many of resources will be recreated.

## Using with Terraform Cloud Agents

[Terraform Cloud Agents](https://www.terraform.io/docs/cloud/workspaces/agent.html) are a paid feature, available as part of the Terraform Cloud for Business upgrade package.
Expand All @@ -62,18 +54,9 @@ If you want to subscribe the AWS Lambda Function created by this module to an ex
- [notify-slack-simple](https://github.com/terraform-aws-modules/terraform-aws-notify-slack/tree/master/examples/notify-slack-simple) - Creates SNS topic which sends messages to Slack channel.
- [cloudwatch-alerts-to-slack](https://github.com/terraform-aws-modules/terraform-aws-notify-slack/tree/master/examples/cloudwatch-alerts-to-slack) - End to end example which shows how to send AWS Cloudwatch alerts to Slack channel and use KMS to encrypt webhook URL.

## Testing with pytest

To run the tests:

1. Set up a dedicated slack channel as a test sandbox with it's own webhook. See [Slack Incoming Webhooks docs](https://api.slack.com/messaging/webhooks) for details.
2. Make a copy of the sample pytest configuration and edit as needed.

cp functions/pytest.ini.sample functions/pytest.ini

3. Run the tests:
## Local Development and Testing

pytest functions/notify_slack_test.py
See the [functions](https://github.com/terraform-aws-modules/terraform-aws-notify-slack/tree/master/functions) for further details.

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Requirements
Expand Down
5 changes: 4 additions & 1 deletion examples/notify-slack-simple/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ Note that this example may create resources which can cost money (AWS Elastic IP
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13.1 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.61 |
| <a name="requirement_local"></a> [local](#requirement\_local) | >= 2.0 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.61 |
| <a name="provider_local"></a> [local](#provider\_local) | >= 2.0 |

## Modules

Expand All @@ -42,7 +44,8 @@ Note that this example may create resources which can cost money (AWS Elastic IP

| Name | Type |
|------|------|
| [aws_sns_topic.my_sns](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sns_topic) | resource |
| [aws_sns_topic.example](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sns_topic) | resource |
| [local_file.integration_testing](https://registry.terraform.io/providers/hashicorp/local/latest/docs/resources/file) | resource |

## Inputs

Expand Down
42 changes: 35 additions & 7 deletions examples/notify-slack-simple/main.tf
Original file line number Diff line number Diff line change
@@ -1,24 +1,52 @@
provider "aws" {
region = local.region
}

locals {
name = "ex-${replace(basename(path.cwd), "_", "-")}"
region = "eu-west-1"
tags = {
Owner = "user"
Environment = "dev"
}
}

resource "aws_sns_topic" "my_sns" {
name = "my-sns"
################################################################################
# Supporting Resources
################################################################################

resource "aws_sns_topic" "example" {
name = local.name
tags = local.tags
}

################################################################################
# Slack Notify Module
################################################################################

module "notify_slack" {
source = "../../"

sns_topic_name = aws_sns_topic.my_sns.name
sns_topic_name = aws_sns_topic.example.name
create_sns_topic = false

slack_webhook_url = "https://hooks.slack.com/services/AAA/BBB/CCC"
slack_channel = "aws-notification"
slack_username = "reporter"

tags = {
Name = "notify-slack-simple"
}
tags = local.tags
}

################################################################################
# Integration Testing Support
# This populates a file that is gitignored to aid in executing the integration tests locally
################################################################################

depends_on = [aws_sns_topic.my_sns]
resource "local_file" "integration_testing" {
filename = "${path.module}/../../functions/.int.env"
content = <<-EOT
REGION=${local.region}
LAMBDA_FUNCTION_NAME=${module.notify_slack.notify_slack_lambda_function_name}
SNS_TOPIC_ARN=${aws_sns_topic.example.arn}
EOT
}
4 changes: 4 additions & 0 deletions examples/notify-slack-simple/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@ terraform {
source = "hashicorp/aws"
version = ">= 3.61"
}
local = {
source = "hashicorp/local"
version = ">= 2.0"
}
}
}
10 changes: 10 additions & 0 deletions functions/.flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[flake8]
max-complexity = 10
max-line-length = 120
exclude =
.pytest_cache
__pycache__/
*tests/
events/
messages/
snapshots/
58 changes: 58 additions & 0 deletions functions/.pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
[tool.black]
line-length = 120
target-version = ['py38']
include = '\.pyi?$'
verbose = true
exclude = '''
/(
| \.git
| \.mypy_cache
| dist
| \.pants\.d
| virtualenvs
| \.venv
| _build
| build
| dist
| snapshots
)/
'''

[tool.isort]
line_length = 120
skip = '.terraform'
sections = 'FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER'
known_third_party = 'aws_lambda_powertools,boto3,botocore,pytest,snapshottest'
known_first_party = 'events,notify_slack'
indent = ' '

[tool.mypy]
namespace_packages = true
explicit_package_bases = true

no_implicit_optional = true
implicit_reexport = false
strict_equality = true

warn_unused_configs = true
warn_unused_ignores = true
warn_return_any = true
warn_redundant_casts = true
warn_unreachable = true

pretty = true
show_column_numbers = true
show_error_context = true
show_error_codes = true
show_traceback = true

[tool.coverage.run]
branch = true
omit = ["*_test.py", "tests/*", "events/*", "messages/*", "snapshots/*", "venv/*", ".mypy_cache/*", ".pytest_cache/*"]

[tool.coverage.report]
show_missing = true
skip_covered = true
skip_empty = true
sort = "-Miss"
fail_under = 75
36 changes: 36 additions & 0 deletions functions/Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]

[dev-packages]
boto3 = "~=1.20"
botocore = "~=1.23"
black = "*"
flake8 = "*"
isort = "*"
mypy = "*"
pytest = "*"
pytest-cov = "*"
radon = "*"
snapshottest = "~=0.6"

[requires]
python_version = "3.8"

[scripts]
test = "python3 -m pytest --cov --cov-report=term"
'test:updatesnapshots' = "python3 -m pytest --snapshot-update"
cover = "python3 -m coverage html"
complexity = "python3 -m radon cc notify_slack.py -a"
halstead = "python3 -m radon hal notify_slack.py"
typecheck = "python3 -m mypy . --ignore-missing-imports"
lint = "python3 -m flake8 . --count --statistics --benchmark --exit-zero --config=.flake8"
'lint:ci' = "python3 -m flake8 . --config=.flake8"
imports = "python3 -m isort . --profile black"
format = "python3 -m black ."

[pipenv]
allow_prereleases = true
Loading