Skip to content

Commit 825b860

Browse files
committed
fixing test
1 parent 5f54283 commit 825b860

File tree

13 files changed

+130
-24
lines changed

13 files changed

+130
-24
lines changed

.github/workflows/unit_test.yml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name: Unit Tests
2+
on:
3+
push:
4+
branches:
5+
- main
6+
- develop
7+
paths-ignore:
8+
- 'README.md'
9+
- 'LICENSE'
10+
- '.gitignore'
11+
- '**.md'
12+
pull_request:
13+
paths-ignore:
14+
- 'README.md'
15+
- 'LICENSE'
16+
- '.gitignore'
17+
- '**.md'
18+
jobs:
19+
go-tests:
20+
name: Unit Tests
21+
runs-on: ubuntu-latest
22+
steps:
23+
- uses: actions/checkout@v2
24+
25+
- uses: actions/setup-go@v1
26+
with:
27+
go-version: 1.14
28+
29+
- uses: hashicorp/setup-terraform@v1
30+
with:
31+
terraform_version: 0.14.7
32+
33+
- name: Download Go Modules
34+
working-directory: tests
35+
run: go mod download
36+
37+
- name: Set ssh keys for github remote download
38+
working-directory: tests
39+
run: |
40+
mkdir ~/.ssh
41+
echo "${{ secrets.SSH_GITHUB }}" > ~/.ssh/id_rsa
42+
chmod 600 ~/.ssh/id_rsa
43+
44+
- name: Run Go Tests
45+
working-directory: tests
46+
run: |
47+
go test -v
48+
env:
49+
# TF ENVIRONMENT
50+
TF_AWS_BUCKET: "${{ secrets.TF_NONPROD_AWS_BUCKET }}"
51+
TF_AWS_PROFILE: "${{ secrets.TF_AWS_PROFILE }}"
52+
TF_AWS_BUCKET_REGION: "${{ secrets.TF_AWS_BUCKET_REGION }}"
53+
# AWS CREDENTIALS
54+
AWS_ACCESS_KEY_ID: "${{ secrets.DEV_AWS_ACCESS_KEY_ID }}"
55+
AWS_SECRET_ACCESS_KEY: "${{ secrets.DEV_AWS_SECRET_ACCESS_KEY }}"

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,4 @@ _testmain.go
8080
/test/times.out
8181

8282
# ignore test file(s)
83-
**test**
83+
test.tf

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ pip install tfremote
4848
export TF_AWS_BUCKET=<remote state bucket name>
4949
export TF_AWS_PROFILE=default
5050
export TF_AWS_BUCKET_REGION=us-west-2
51-
export PATH=$PATH:/usr/local/bin/
5251
```
5352

5453
- Make required change to `examples` directory.
@@ -100,6 +99,7 @@ module "lambda" {
10099
```
101100

102101
Please refer to examples directory [link](examples) for references.
102+
103103
## Requirements
104104

105105
| Name | Version |
File renamed without changes.

examples/lambda/main.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module "lambda" {
2+
source = "../../"
3+
4+
account_id = "123456789012"
5+
role = "arn:aws:iam::123456789012:role/LambdaExecutionRole"
6+
runtime = "python3.8"
7+
handler = "lambda_function.lambda_handler"
8+
source_file = "lambda_function.py"
9+
output_file_path = "/tmp/test.zip"
10+
environment = {
11+
variables = {
12+
HELLO = "WORLD"
13+
}
14+
}
15+
# -----------------------------------------
16+
# Do not change the teamid, prjid once set.
17+
teamid = var.teamid
18+
prjid = var.prjid
19+
}
File renamed without changes.
File renamed without changes.
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
# Additional documentation: https://www.terraform.io/docs/configuration/variables.html
21
variable "teamid" {
32
description = "(Required) name of the team/group e.g. devops, dataengineering. Should not be changed after running 'tf apply'"
43
}
54

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

examples/test/lambda_function.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import json
2+
3+
4+
def lambda_handler(event, context):
5+
print("Received event: " + json.dumps(event, indent=2))
6+
return "hi!"

examples/test/output.tf

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
output "lambda_arn" {
2+
description = "ARN of the Lambda function"
3+
value = module.lambda.lambda_arn
4+
}
5+
6+
output "lambda_role" {
7+
description = "IAM role used by Lambda function"
8+
value = module.lambda.lambda_role
9+
}
10+
11+
output "input_file_name" {
12+
description = "Source code location"
13+
value = module.lambda.input_file_name
14+
}
15+
16+
output "output_file_path" {
17+
description = "Output filepath location"
18+
value = module.lambda.output_file_path
19+
}
20+
21+
output "output_file_size" {
22+
description = "Output filepath size"
23+
value = module.lambda.output_file_size
24+
}

examples/test/remote_backend.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# file generated by wrapper script to configure backend
2+
# do not edit or delete!
3+
4+
terraform {
5+
backend "s3" {
6+
}
7+
}

examples/test/variables.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
variable "teamid" {
2+
description = "(Required) name of the team/group e.g. devops, dataengineering. Should not be changed after running 'tf apply'"
3+
}
4+
5+
variable "prjid" {
6+
description = "(Required) name of the project/stack e.g: mystack, nifieks, demoaci. Should not be changed after running 'tf apply'"
7+
}
8+
9+
variable "aws_region" {}

tests/unit_test.go

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
package test
22

33
import (
4-
// "fmt"
54
"testing"
65

76
"github.com/gruntwork-io/terratest/modules/aws"
8-
// "github.com/gruntwork-io/terratest/modules/random"
97
"github.com/gruntwork-io/terratest/modules/terraform"
108
"github.com/stretchr/testify/assert"
11-
// "github.com/stretchr/testify/require"
129
)
1310

14-
// An example of how to test the Terraform module in examples/terraform-aws-lambda-example using Terratest.
11+
// Test Terraform module using Terratest.
1512
func TestTerraformAwsLambda(t *testing.T) {
1613
t.Parallel()
1714
// ----------------------------------------------------------
18-
// TF_VARS_FILE_PATH := "test.tfvars"
15+
// TF_VARS_FILE_PATH := "test.tfvars"
1916
TF_REPO_PATH := "../examples"
2017
functionName := "rumse-demo-lambda"
2118
teamid := "rumse"
@@ -26,9 +23,8 @@ func TestTerraformAwsLambda(t *testing.T) {
2623
// terraform testing.
2724
terraformOptions := &terraform.Options{
2825
TerraformDir: TF_REPO_PATH,
29-
30-
// // Variables to pass to our Terraform code using -var-file options
31-
// VarFiles: []string{TF_VARS_FILE_PATH},
26+
// Variables to pass to our Terraform code using -var-file options
27+
// VarFiles: []string{TF_VARS_FILE_PATH},
3228

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

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

5046
// This function just echos it's input as a JSON string when `ShouldFail` is `false``
5147
assert.Equal(t, `"hi!"`, string(response))
52-
53-
// TODO: need to spend sometime here
54-
// Invoke the function, this time causing it to error and capturing the error
55-
//response, err := aws.InvokeFunctionE(t, awsRegion, functionName, ExampleFunctionPayload{ShouldFail: true, Echo: "hi!"})
56-
57-
// // Function-specific errors have their own special return
58-
// functionError, ok := err.(*aws.FunctionError)
59-
// require.True(t, ok)
60-
//
61-
// // Make sure the function-specific error comes back
62-
// assert.Contains(t, string(functionError.Payload), "Failed to handle")
6348
}
6449

65-
type ExampleFunctionPayload struct {
50+
type LambdaFunctionPayload struct {
6651
Echo string
6752
ShouldFail bool
6853
}

0 commit comments

Comments
 (0)