Merge pull request #11637 from hashicorp/b-aws-test-fixes

provider/aws: More lambda test fixes
This commit is contained in:
Clint 2017-02-02 10:30:23 -06:00 committed by GitHub
commit aa7aa19592
2 changed files with 32 additions and 29 deletions

View File

@ -268,6 +268,8 @@ func TestAccAWSLambdaFunction_localUpdate(t *testing.T) {
func TestAccAWSLambdaFunction_localUpdate_nameOnly(t *testing.T) { func TestAccAWSLambdaFunction_localUpdate_nameOnly(t *testing.T) {
var conf lambda.GetFunctionOutput var conf lambda.GetFunctionOutput
rName := fmt.Sprintf("tf_test_iam_%d", acctest.RandInt())
path, zipFile, err := createTempFile("lambda_localUpdate") path, zipFile, err := createTempFile("lambda_localUpdate")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -289,11 +291,11 @@ func TestAccAWSLambdaFunction_localUpdate_nameOnly(t *testing.T) {
PreConfig: func() { PreConfig: func() {
testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func.js": "lambda.js"}, zipFile) testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func.js": "lambda.js"}, zipFile)
}, },
Config: genAWSLambdaFunctionConfig_local_name_only(path), Config: genAWSLambdaFunctionConfig_local_name_only(path, rName),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_local", "tf_acc_lambda_name_local", &conf), testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_local", rName, &conf),
testAccCheckAwsLambdaFunctionName(&conf, "tf_acc_lambda_name_local"), testAccCheckAwsLambdaFunctionName(&conf, rName),
testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, "tf_acc_lambda_name_local"), testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, rName),
testAccCheckAwsLambdaSourceCodeHash(&conf, "8DPiX+G1l2LQ8hjBkwRchQFf1TSCEvPrYGRKlM9UoyY="), testAccCheckAwsLambdaSourceCodeHash(&conf, "8DPiX+G1l2LQ8hjBkwRchQFf1TSCEvPrYGRKlM9UoyY="),
), ),
}, },
@ -301,11 +303,11 @@ func TestAccAWSLambdaFunction_localUpdate_nameOnly(t *testing.T) {
PreConfig: func() { PreConfig: func() {
testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func_modified.js": "lambda.js"}, updatedZipFile) testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func_modified.js": "lambda.js"}, updatedZipFile)
}, },
Config: genAWSLambdaFunctionConfig_local_name_only(updatedPath), Config: genAWSLambdaFunctionConfig_local_name_only(updatedPath, rName),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_local", "tf_acc_lambda_name_local", &conf), testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_local", rName, &conf),
testAccCheckAwsLambdaFunctionName(&conf, "tf_acc_lambda_name_local"), testAccCheckAwsLambdaFunctionName(&conf, rName),
testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, "tf_acc_lambda_name_local"), testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, rName),
testAccCheckAwsLambdaSourceCodeHash(&conf, "0tdaP9H9hsk9c2CycSwOG/sa/x5JyAmSYunA/ce99Pg="), testAccCheckAwsLambdaSourceCodeHash(&conf, "0tdaP9H9hsk9c2CycSwOG/sa/x5JyAmSYunA/ce99Pg="),
), ),
}, },
@ -369,6 +371,8 @@ func TestAccAWSLambdaFunction_s3Update(t *testing.T) {
func TestAccAWSLambdaFunction_s3Update_unversioned(t *testing.T) { func TestAccAWSLambdaFunction_s3Update_unversioned(t *testing.T) {
var conf lambda.GetFunctionOutput var conf lambda.GetFunctionOutput
rName := fmt.Sprintf("tf_iam_lambda_%d", acctest.RandInt())
path, zipFile, err := createTempFile("lambda_s3Update") path, zipFile, err := createTempFile("lambda_s3Update")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -389,7 +393,7 @@ func TestAccAWSLambdaFunction_s3Update_unversioned(t *testing.T) {
// Upload 1st version // Upload 1st version
testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func.js": "lambda.js"}, zipFile) testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func.js": "lambda.js"}, zipFile)
}, },
Config: genAWSLambdaFunctionConfig_s3_unversioned(bucketName, key, path), Config: testAccAWSLambdaFunctionConfig_s3_unversioned_tpl(rName, bucketName, key, path),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_s3", "tf_acc_lambda_name_s3_unversioned", &conf), testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_s3", "tf_acc_lambda_name_s3_unversioned", &conf),
testAccCheckAwsLambdaFunctionName(&conf, "tf_acc_lambda_name_s3_unversioned"), testAccCheckAwsLambdaFunctionName(&conf, "tf_acc_lambda_name_s3_unversioned"),
@ -402,7 +406,7 @@ func TestAccAWSLambdaFunction_s3Update_unversioned(t *testing.T) {
// Upload 2nd version // Upload 2nd version
testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func_modified.js": "lambda.js"}, zipFile) testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func_modified.js": "lambda.js"}, zipFile)
}, },
Config: genAWSLambdaFunctionConfig_s3_unversioned(bucketName, key2, path), Config: testAccAWSLambdaFunctionConfig_s3_unversioned_tpl(rName, bucketName, key2, path),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_s3", "tf_acc_lambda_name_s3_unversioned", &conf), testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_s3", "tf_acc_lambda_name_s3_unversioned", &conf),
testAccCheckAwsLambdaFunctionName(&conf, "tf_acc_lambda_name_s3_unversioned"), testAccCheckAwsLambdaFunctionName(&conf, "tf_acc_lambda_name_s3_unversioned"),
@ -1027,14 +1031,14 @@ func genAWSLambdaFunctionConfig_local(filePath string) string {
filePath, filePath) filePath, filePath)
} }
func genAWSLambdaFunctionConfig_local_name_only(filePath string) string { func genAWSLambdaFunctionConfig_local_name_only(filePath, rName string) string {
return fmt.Sprintf(testAccAWSLambdaFunctionConfig_local_name_only_tpl, return testAccAWSLambdaFunctionConfig_local_name_only_tpl(filePath, rName)
filePath)
} }
const testAccAWSLambdaFunctionConfig_local_name_only_tpl = ` func testAccAWSLambdaFunctionConfig_local_name_only_tpl(filePath, rName string) string {
return fmt.Sprintf(`
resource "aws_iam_role" "iam_for_lambda" { resource "aws_iam_role" "iam_for_lambda" {
name = "iam_for_lambda" name = "%s"
assume_role_policy = <<EOF assume_role_policy = <<EOF
{ {
"Version": "2012-10-17", "Version": "2012-10-17",
@ -1053,12 +1057,12 @@ EOF
} }
resource "aws_lambda_function" "lambda_function_local" { resource "aws_lambda_function" "lambda_function_local" {
filename = "%s" filename = "%s"
function_name = "tf_acc_lambda_name_local" function_name = "%s"
role = "${aws_iam_role.iam_for_lambda.arn}" role = "${aws_iam_role.iam_for_lambda.arn}"
handler = "exports.example" handler = "exports.example"
runtime = "nodejs4.3" runtime = "nodejs4.3"
}`, rName, filePath, rName)
} }
`
const testAccAWSLambdaFunctionConfig_s3_tpl = ` const testAccAWSLambdaFunctionConfig_s3_tpl = `
resource "aws_s3_bucket" "artifacts" { resource "aws_s3_bucket" "artifacts" {
@ -1109,7 +1113,8 @@ func genAWSLambdaFunctionConfig_s3(bucket, key, path string) string {
bucket, key, path, path) bucket, key, path, path)
} }
const testAccAWSLambdaFunctionConfig_s3_unversioned_tpl = ` func testAccAWSLambdaFunctionConfig_s3_unversioned_tpl(rName, bucketName, key, path string) string {
return fmt.Sprintf(`
resource "aws_s3_bucket" "artifacts" { resource "aws_s3_bucket" "artifacts" {
bucket = "%s" bucket = "%s"
acl = "private" acl = "private"
@ -1122,7 +1127,7 @@ resource "aws_s3_bucket_object" "o" {
etag = "${md5(file("%s"))}" etag = "${md5(file("%s"))}"
} }
resource "aws_iam_role" "iam_for_lambda" { resource "aws_iam_role" "iam_for_lambda" {
name = "iam_for_lambda" name = "%s"
assume_role_policy = <<EOF assume_role_policy = <<EOF
{ {
"Version": "2012-10-17", "Version": "2012-10-17",
@ -1146,10 +1151,5 @@ resource "aws_lambda_function" "lambda_function_s3" {
role = "${aws_iam_role.iam_for_lambda.arn}" role = "${aws_iam_role.iam_for_lambda.arn}"
handler = "exports.example" handler = "exports.example"
runtime = "nodejs4.3" runtime = "nodejs4.3"
} }`, bucketName, key, path, path, rName)
`
func genAWSLambdaFunctionConfig_s3_unversioned(bucket, key, path string) string {
return fmt.Sprintf(testAccAWSLambdaFunctionConfig_s3_unversioned_tpl,
bucket, key, path, path)
} }

View File

@ -148,13 +148,15 @@ func TestAccAWSLambdaPermission_basic(t *testing.T) {
var statement LambdaPolicyStatement var statement LambdaPolicyStatement
endsWithFuncName := regexp.MustCompile(":function:lambda_function_name_perm$") endsWithFuncName := regexp.MustCompile(":function:lambda_function_name_perm$")
rName := fmt.Sprintf("tf_iam_%d", acctest.RandInt())
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders, Providers: testAccProviders,
CheckDestroy: testAccCheckAWSLambdaPermissionDestroy, CheckDestroy: testAccCheckAWSLambdaPermissionDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
{ {
Config: testAccAWSLambdaPermissionConfig, Config: testAccAWSLambdaPermissionConfig(rName),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckLambdaPermissionExists("aws_lambda_permission.allow_cloudwatch", &statement), testAccCheckLambdaPermissionExists("aws_lambda_permission.allow_cloudwatch", &statement),
resource.TestCheckResourceAttr("aws_lambda_permission.allow_cloudwatch", "action", "lambda:InvokeFunction"), resource.TestCheckResourceAttr("aws_lambda_permission.allow_cloudwatch", "action", "lambda:InvokeFunction"),
@ -459,7 +461,8 @@ func lambdaPermissionExists(rs *terraform.ResourceState, conn *lambda.Lambda) (*
return findLambdaPolicyStatementById(&policy, rs.Primary.ID) return findLambdaPolicyStatementById(&policy, rs.Primary.ID)
} }
var testAccAWSLambdaPermissionConfig = ` func testAccAWSLambdaPermissionConfig(rName string) string {
return fmt.Sprintf(`
resource "aws_lambda_permission" "allow_cloudwatch" { resource "aws_lambda_permission" "allow_cloudwatch" {
statement_id = "AllowExecutionFromCloudWatch" statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction" action = "lambda:InvokeFunction"
@ -476,7 +479,7 @@ resource "aws_lambda_function" "test_lambda" {
} }
resource "aws_iam_role" "iam_for_lambda" { resource "aws_iam_role" "iam_for_lambda" {
name = "iam_for_lambda_perm" name = "%s"
assume_role_policy = <<EOF assume_role_policy = <<EOF
{ {
"Version": "2012-10-17", "Version": "2012-10-17",
@ -492,8 +495,8 @@ resource "aws_iam_role" "iam_for_lambda" {
] ]
} }
EOF EOF
}`, rName)
} }
`
var testAccAWSLambdaPermissionConfig_withRawFunctionName = ` var testAccAWSLambdaPermissionConfig_withRawFunctionName = `
resource "aws_lambda_permission" "with_raw_func_name" { resource "aws_lambda_permission" "with_raw_func_name" {