Skip to content

Commit

Permalink
fixing test
Browse files Browse the repository at this point in the history
  • Loading branch information
tomarv2 committed Mar 30, 2021
1 parent 5f54283 commit 825b860
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 24 deletions.
55 changes: 55 additions & 0 deletions .github/workflows/unit_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: Unit Tests
on:
push:
branches:
- main
- develop
paths-ignore:
- 'README.md'
- 'LICENSE'
- '.gitignore'
- '**.md'
pull_request:
paths-ignore:
- 'README.md'
- 'LICENSE'
- '.gitignore'
- '**.md'
jobs:
go-tests:
name: Unit Tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- uses: actions/setup-go@v1
with:
go-version: 1.14

- uses: hashicorp/setup-terraform@v1
with:
terraform_version: 0.14.7

- name: Download Go Modules
working-directory: tests
run: go mod download

- name: Set ssh keys for github remote download
working-directory: tests
run: |
mkdir ~/.ssh
echo "${{ secrets.SSH_GITHUB }}" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
- name: Run Go Tests
working-directory: tests
run: |
go test -v
env:
# TF ENVIRONMENT
TF_AWS_BUCKET: "${{ secrets.TF_NONPROD_AWS_BUCKET }}"
TF_AWS_PROFILE: "${{ secrets.TF_AWS_PROFILE }}"
TF_AWS_BUCKET_REGION: "${{ secrets.TF_AWS_BUCKET_REGION }}"
# AWS CREDENTIALS
AWS_ACCESS_KEY_ID: "${{ secrets.DEV_AWS_ACCESS_KEY_ID }}"
AWS_SECRET_ACCESS_KEY: "${{ secrets.DEV_AWS_SECRET_ACCESS_KEY }}"
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,4 @@ _testmain.go
/test/times.out

# ignore test file(s)
**test**
test.tf
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ pip install tfremote
export TF_AWS_BUCKET=<remote state bucket name>
export TF_AWS_PROFILE=default
export TF_AWS_BUCKET_REGION=us-west-2
export PATH=$PATH:/usr/local/bin/
```

- Make required change to `examples` directory.
Expand Down Expand Up @@ -100,6 +99,7 @@ module "lambda" {
```

Please refer to examples directory [link](examples) for references.

## Requirements

| Name | Version |
Expand Down
File renamed without changes.
19 changes: 19 additions & 0 deletions examples/lambda/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module "lambda" {
source = "../../"

account_id = "123456789012"
role = "arn:aws:iam::123456789012:role/LambdaExecutionRole"
runtime = "python3.8"
handler = "lambda_function.lambda_handler"
source_file = "lambda_function.py"
output_file_path = "/tmp/test.zip"
environment = {
variables = {
HELLO = "WORLD"
}
}
# -----------------------------------------
# Do not change the teamid, prjid once set.
teamid = var.teamid
prjid = var.prjid
}
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion examples/variables.tf → examples/lambda/variables.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Additional documentation: https://www.terraform.io/docs/configuration/variables.html
variable "teamid" {
description = "(Required) name of the team/group e.g. devops, dataengineering. Should not be changed after running 'tf apply'"
}

variable "prjid" {
description = "(Required) name of the project/stack e.g: mystack, nifieks, demoaci. Should not be changed after running 'tf apply'"
}

variable "aws_region" {}
6 changes: 6 additions & 0 deletions examples/test/lambda_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import json


def lambda_handler(event, context):
print("Received event: " + json.dumps(event, indent=2))
return "hi!"
24 changes: 24 additions & 0 deletions examples/test/output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
output "lambda_arn" {
description = "ARN of the Lambda function"
value = module.lambda.lambda_arn
}

output "lambda_role" {
description = "IAM role used by Lambda function"
value = module.lambda.lambda_role
}

output "input_file_name" {
description = "Source code location"
value = module.lambda.input_file_name
}

output "output_file_path" {
description = "Output filepath location"
value = module.lambda.output_file_path
}

output "output_file_size" {
description = "Output filepath size"
value = module.lambda.output_file_size
}
7 changes: 7 additions & 0 deletions examples/test/remote_backend.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# file generated by wrapper script to configure backend
# do not edit or delete!

terraform {
backend "s3" {
}
}
9 changes: 9 additions & 0 deletions examples/test/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
variable "teamid" {
description = "(Required) name of the team/group e.g. devops, dataengineering. Should not be changed after running 'tf apply'"
}

variable "prjid" {
description = "(Required) name of the project/stack e.g: mystack, nifieks, demoaci. Should not be changed after running 'tf apply'"
}

variable "aws_region" {}
27 changes: 6 additions & 21 deletions tests/unit_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
package test

import (
// "fmt"
"testing"

"github.com/gruntwork-io/terratest/modules/aws"
// "github.com/gruntwork-io/terratest/modules/random"
"github.com/gruntwork-io/terratest/modules/terraform"
"github.com/stretchr/testify/assert"
// "github.com/stretchr/testify/require"
)

// An example of how to test the Terraform module in examples/terraform-aws-lambda-example using Terratest.
// Test Terraform module using Terratest.
func TestTerraformAwsLambda(t *testing.T) {
t.Parallel()
// ----------------------------------------------------------
// TF_VARS_FILE_PATH := "test.tfvars"
// TF_VARS_FILE_PATH := "test.tfvars"
TF_REPO_PATH := "../examples"
functionName := "rumse-demo-lambda"
teamid := "rumse"
Expand All @@ -26,9 +23,8 @@ func TestTerraformAwsLambda(t *testing.T) {
// terraform testing.
terraformOptions := &terraform.Options{
TerraformDir: TF_REPO_PATH,

// // Variables to pass to our Terraform code using -var-file options
// VarFiles: []string{TF_VARS_FILE_PATH},
// Variables to pass to our Terraform code using -var-file options
// VarFiles: []string{TF_VARS_FILE_PATH},

// Variables to pass to our Terraform code using -var options
Vars: map[string]interface{}{
Expand All @@ -45,24 +41,13 @@ func TestTerraformAwsLambda(t *testing.T) {
terraform.InitAndApply(t, terraformOptions)

// Invoke the function, so we can test its output
response := aws.InvokeFunction(t, awsRegion, functionName, ExampleFunctionPayload{ShouldFail: false, Echo: "hi!"})
response := aws.InvokeFunction(t, awsRegion, functionName, LambdaFunctionPayload{ShouldFail: false, Echo: "hi!"})

// This function just echos it's input as a JSON string when `ShouldFail` is `false``
assert.Equal(t, `"hi!"`, string(response))

// TODO: need to spend sometime here
// Invoke the function, this time causing it to error and capturing the error
//response, err := aws.InvokeFunctionE(t, awsRegion, functionName, ExampleFunctionPayload{ShouldFail: true, Echo: "hi!"})

// // Function-specific errors have their own special return
// functionError, ok := err.(*aws.FunctionError)
// require.True(t, ok)
//
// // Make sure the function-specific error comes back
// assert.Contains(t, string(functionError.Payload), "Failed to handle")
}

type ExampleFunctionPayload struct {
type LambdaFunctionPayload struct {
Echo string
ShouldFail bool
}

0 comments on commit 825b860

Please sign in to comment.