github.com/gabrielperezs/terraform@v0.7.0-rc2.0.20160715084931-f7da2612946f/builtin/providers/aws/resource_aws_cloudwatch_log_subscription_filter_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/service/lambda" 9 "github.com/hashicorp/terraform/helper/acctest" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccAWSCloudwatchLogSubscriptionFilter_basic(t *testing.T) { 15 var conf lambda.GetFunctionOutput 16 17 rstring := acctest.RandString(5) 18 19 resource.Test(t, resource.TestCase{ 20 PreCheck: func() { testAccPreCheck(t) }, 21 Providers: testAccProviders, 22 CheckDestroy: testAccCheckCloudwatchLogSubscriptionFilterDestroy, 23 Steps: []resource.TestStep{ 24 resource.TestStep{ 25 Config: testAccAWSCloudwatchLogSubscriptionFilterConfig(rstring), 26 Check: resource.ComposeTestCheckFunc( 27 testAccCheckAwsCloudwatchLogSubscriptionFilterExists("aws_cloudwatch_log_subscription_filter.test_lambdafunction_logfilter", &conf, rstring), 28 testAccCheckAWSCloudwatchLogSubscriptionFilterAttributes(&conf, rstring), 29 ), 30 }, 31 }, 32 }) 33 } 34 35 func testAccCheckCloudwatchLogSubscriptionFilterDestroy(s *terraform.State) error { 36 conn := testAccProvider.Meta().(*AWSClient).lambdaconn 37 38 for _, rs := range s.RootModule().Resources { 39 if rs.Type != "aws_cloudwatch_log_subscription_filter" { 40 continue 41 } 42 43 _, err := conn.GetFunction(&lambda.GetFunctionInput{ 44 FunctionName: aws.String(rs.Primary.ID), 45 }) 46 47 if err == nil { 48 return fmt.Errorf("Lambda Function still exists") 49 } 50 51 } 52 53 return nil 54 55 } 56 57 func testAccCheckAwsCloudwatchLogSubscriptionFilterExists(n string, function *lambda.GetFunctionOutput, rstring string) resource.TestCheckFunc { 58 // Wait for IAM role 59 return func(s *terraform.State) error { 60 rs, ok := s.RootModule().Resources[n] 61 if !ok { 62 return fmt.Errorf("Lambda function not found: %s", n) 63 } 64 65 if rs.Primary.ID == "" { 66 return fmt.Errorf("Lambda function ID not set") 67 } 68 69 conn := testAccProvider.Meta().(*AWSClient).lambdaconn 70 71 params := &lambda.GetFunctionInput{ 72 FunctionName: aws.String("example_lambda_name_" + rstring), 73 } 74 75 getFunction, err := conn.GetFunction(params) 76 if err != nil { 77 return err 78 } 79 80 *function = *getFunction 81 82 return nil 83 } 84 } 85 86 func testAccCheckAWSCloudwatchLogSubscriptionFilterAttributes(function *lambda.GetFunctionOutput, rstring string) resource.TestCheckFunc { 87 return func(s *terraform.State) error { 88 c := function.Configuration 89 expectedName := fmt.Sprintf("example_lambda_name_%s", rstring) 90 if *c.FunctionName != expectedName { 91 return fmt.Errorf("Expected function name %s, got %s", expectedName, *c.FunctionName) 92 } 93 94 if *c.FunctionArn == "" { 95 return fmt.Errorf("Could not read Lambda Function's ARN") 96 } 97 98 return nil 99 } 100 } 101 102 func testAccAWSCloudwatchLogSubscriptionFilterConfig(rstring string) string { 103 return fmt.Sprintf(` 104 resource "aws_cloudwatch_log_subscription_filter" "test_lambdafunction_logfilter" { 105 name = "test_lambdafunction_logfilter" 106 log_group_name = "example_lambda_name" 107 filter_pattern = "logtype test" 108 destination_arn = "${aws_lambda_function.test_lambdafunction.arn}" 109 } 110 111 resource "aws_lambda_function" "test_lambdafunction" { 112 filename = "test-fixtures/lambdatest.zip" 113 function_name = "example_lambda_name_%s" 114 role = "${aws_iam_role.iam_for_lambda.arn}" 115 handler = "exports.handler" 116 } 117 118 resource "aws_cloudwatch_log_group" "logs" { 119 name = "example_lambda_name" 120 retention_in_days = 1 121 } 122 123 resource "aws_lambda_permission" "allow_cloudwatch_logs" { 124 statement_id = "AllowExecutionFromCloudWatchLogs" 125 action = "lambda:*" 126 function_name = "${aws_lambda_function.test_lambdafunction.arn}" 127 principal = "logs.us-west-2.amazonaws.com" 128 } 129 130 resource "aws_iam_role" "iam_for_lambda" { 131 name = "test_lambdafuntion_iam_role_%s" 132 133 assume_role_policy = <<EOF 134 { 135 "Version": "2012-10-17", 136 "Statement": [ 137 { 138 "Action": "sts:AssumeRole", 139 "Principal": { 140 "Service": "lambda.amazonaws.com" 141 }, 142 "Effect": "Allow", 143 "Sid": "" 144 } 145 ] 146 } 147 EOF 148 } 149 150 resource "aws_iam_role_policy" "test_lambdafunction_iam_policy" { 151 name = "test_lambdafunction_iam_policy" 152 role = "${aws_iam_role.iam_for_lambda.id}" 153 154 policy = <<EOF 155 { 156 "Version": "2012-10-17", 157 "Statement": [ 158 { 159 "Sid": "Stmt1441111030000", 160 "Effect": "Allow", 161 "Action": [ 162 "dynamodb:*" 163 ], 164 "Resource": [ 165 "*" 166 ] 167 } 168 ] 169 } 170 EOF 171 } 172 `, rstring, rstring) 173 }