Skip to content

Commit

Permalink
provider/aws: Add tests for Lambda function updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Radek Simko committed Feb 21, 2016
1 parent e5c94e1 commit 3de5b52
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 5 deletions.
1 change: 1 addition & 0 deletions builtin/providers/aws/resource_aws_lambda_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ func resourceAwsLambdaFunctionCreate(d *schema.ResourceData, meta interface{}) e
err := resource.Retry(1*time.Minute, func() error {
_, err := conn.CreateFunction(params)
if err != nil {
log.Printf("[ERROR] Received %q, retrying CreateFunction", err)
if awserr, ok := err.(awserr.Error); ok {
if awserr.Code() == "InvalidParameterValueException" {
log.Printf("[DEBUG] InvalidParameterValueException creating Lambda Function: %s", awserr)
Expand Down
165 changes: 160 additions & 5 deletions builtin/providers/aws/resource_aws_lambda_function_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package aws

import (
"archive/zip"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -19,16 +23,55 @@ func TestAccAWSLambdaFunction_basic(t *testing.T) {
CheckDestroy: testAccCheckLambdaFunctionDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSLambdaConfig,
Config: testAccAWSLambdaFunctionConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", &conf),
testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_test", "example_lambda_name", &conf),
testAccCheckAWSLambdaAttributes(&conf),
),
},
},
})
}

func TestAccAWSLambdaFunction_localUpdate(t *testing.T) {
var conf lambda.GetFunctionOutput

path, zipFile, err := createTempFile("lambda_localUpdate")
if err != nil {
t.Fatal(err)
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLambdaFunctionDestroy,
Steps: []resource.TestStep{
resource.TestStep{
PreConfig: func() {
testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func.js": "lambda.js"}, zipFile)
},
Config: genAWSLambdaFunctionConfig_local(path),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_local", "tf_acc_lambda_name_local", &conf),
testAccCheckAwsLambdaFunctionName(&conf, "tf_acc_lambda_name_local"),
testAccCheckAwsLambdaSourceCodeHash(&conf, "un6qF9S9hKvXbWwJ6m2EYaVCWjcr0PCZWiTV3h4zB0I="),
),
},
resource.TestStep{
PreConfig: func() {
testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func_modified.js": "lambda.js"}, zipFile)
},
Config: genAWSLambdaFunctionConfig_local(path),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_local", "tf_acc_lambda_name_local", &conf),
testAccCheckAwsLambdaFunctionName(&conf, "tf_acc_lambda_name_local"),
testAccCheckAwsLambdaSourceCodeHash(&conf, "Y5Jf4Si63UDy1wKNfPs+U56ZL0NxsieKPt9EwRl4GQM="),
),
},
},
})
}

func testAccCheckLambdaFunctionDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).lambdaconn

Expand All @@ -51,7 +94,7 @@ func testAccCheckLambdaFunctionDestroy(s *terraform.State) error {

}

func testAccCheckAwsLambdaFunctionExists(n string, function *lambda.GetFunctionOutput) resource.TestCheckFunc {
func testAccCheckAwsLambdaFunctionExists(n, funcName string, function *lambda.GetFunctionOutput) resource.TestCheckFunc {
// Wait for IAM role
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand All @@ -66,7 +109,7 @@ func testAccCheckAwsLambdaFunctionExists(n string, function *lambda.GetFunctionO
conn := testAccProvider.Meta().(*AWSClient).lambdaconn

params := &lambda.GetFunctionInput{
FunctionName: aws.String("example_lambda_name"),
FunctionName: aws.String(funcName),
}

getFunction, err := conn.GetFunction(params)
Expand All @@ -80,6 +123,40 @@ func testAccCheckAwsLambdaFunctionExists(n string, function *lambda.GetFunctionO
}
}

func testAccCheckAwsLambdaFunctionName(function *lambda.GetFunctionOutput, expectedName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
c := function.Configuration
if *c.FunctionName != expectedName {
return fmt.Errorf("Expected function name %s, got %s", expectedName, *c.FunctionName)
}

return nil
}
}

func testAccCheckAwsLambdaFunctionArn(function *lambda.GetFunctionOutput, expectedArn string) resource.TestCheckFunc {
return func(s *terraform.State) error {
c := function.Configuration
if *c.FunctionArn != expectedArn {
return fmt.Errorf("Expected function ARN %s, got %s", expectedArn, *c.FunctionArn)
}

return nil
}
}

func testAccCheckAwsLambdaSourceCodeHash(function *lambda.GetFunctionOutput, expectedHash string) resource.TestCheckFunc {
return func(s *terraform.State) error {
c := function.Configuration
if *c.CodeSha256 != expectedHash {
return fmt.Errorf("Expected code hash %s, got %s", expectedHash, *c.CodeSha256)
}

return nil
}
}

// TODO: REMOVE
func testAccCheckAWSLambdaAttributes(function *lambda.GetFunctionOutput) resource.TestCheckFunc {
return func(s *terraform.State) error {
c := function.Configuration
Expand All @@ -96,7 +173,51 @@ func testAccCheckAWSLambdaAttributes(function *lambda.GetFunctionOutput) resourc
}
}

const testAccAWSLambdaConfig = `
func testAccCreateZipFromFiles(files map[string]string, zipFile *os.File) error {
zipFile.Truncate(0)
zipFile.Seek(0, 0)

w := zip.NewWriter(zipFile)

for source, destination := range files {
f, err := w.Create(destination)
if err != nil {
return err
}

fileContent, err := ioutil.ReadFile(source)
if err != nil {
return err
}

_, err = f.Write(fileContent)
if err != nil {
return err
}
}

err := w.Close()
if err != nil {
return err
}

return w.Flush()
}

func createTempFile(prefix string) (string, *os.File, error) {
f, err := ioutil.TempFile(os.TempDir(), prefix)
if err != nil {
return "", nil, err
}

pathToFile, err := filepath.Abs(f.Name())
if err != nil {
return "", nil, err
}
return pathToFile, f, nil
}

const testAccAWSLambdaFunctionConfig = `
resource "aws_iam_role_policy" "iam_policy_for_lambda" {
name = "iam_policy_for_lambda"
role = "${aws_iam_role.iam_for_lambda.id}"
Expand Down Expand Up @@ -191,3 +312,37 @@ resource "aws_lambda_function" "lambda_function_test" {
}
}
`

const testAccAWSLambdaFunctionConfig_local_tpl = `
resource "aws_iam_role" "iam_for_lambda" {
name = "iam_for_lambda"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_lambda_function" "lambda_function_local" {
filename = "%s"
source_code_hash = "${base64sha256(file("%s"))}"
function_name = "tf_acc_lambda_name_local"
role = "${aws_iam_role.iam_for_lambda.arn}"
handler = "exports.example"
}
`

func genAWSLambdaFunctionConfig_local(filePath string) string {
return fmt.Sprintf(testAccAWSLambdaFunctionConfig_local_tpl,
filePath, filePath)
}
9 changes: 9 additions & 0 deletions builtin/providers/aws/test-fixtures/lambda_func.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var http = require('http')

exports.handler = function(event, context) {
http.get("http://requestb.in/10m32wg1", function(res) {
console.log("success", res.statusCode, res.body)
}).on('error', function(e) {
console.log("error", e)
})
}
9 changes: 9 additions & 0 deletions builtin/providers/aws/test-fixtures/lambda_func_modified.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var http = require('http')

exports.handler = function(event, context) {
http.get("http://requestb.in/MODIFIED", function(res) {
console.log("success", res.statusCode, res.body)
}).on('error', function(e) {
console.log("error", e)
})
}

0 comments on commit 3de5b52

Please sign in to comment.