github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/aws/resource_aws_lambda_alias_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/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccAWSLambdaAlias_basic(t *testing.T) {
    14  	var conf lambda.AliasConfiguration
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckAwsLambdaAliasDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccAwsLambdaAliasConfig,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckAwsLambdaAliasExists("aws_lambda_alias.lambda_alias_test", &conf),
    25  					testAccCheckAwsLambdaAttributes(&conf),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func testAccCheckAwsLambdaAliasDestroy(s *terraform.State) error {
    33  	conn := testAccProvider.Meta().(*AWSClient).lambdaconn
    34  
    35  	for _, rs := range s.RootModule().Resources {
    36  		if rs.Type != "aws_lambda_alias" {
    37  			continue
    38  		}
    39  
    40  		_, err := conn.GetAlias(&lambda.GetAliasInput{
    41  			FunctionName: aws.String(rs.Primary.ID),
    42  		})
    43  
    44  		if err == nil {
    45  			return fmt.Errorf("Lambda alias was not deleted")
    46  		}
    47  
    48  	}
    49  
    50  	return nil
    51  }
    52  
    53  func testAccCheckAwsLambdaAliasExists(n string, mapping *lambda.AliasConfiguration) resource.TestCheckFunc {
    54  	return func(s *terraform.State) error {
    55  		rs, ok := s.RootModule().Resources[n]
    56  		if !ok {
    57  			return fmt.Errorf("Lambda alias not found: %s", n)
    58  		}
    59  
    60  		if rs.Primary.ID == "" {
    61  			return fmt.Errorf("Lambda alias not set")
    62  		}
    63  
    64  		conn := testAccProvider.Meta().(*AWSClient).lambdaconn
    65  
    66  		params := &lambda.GetAliasInput{
    67  			FunctionName: aws.String(rs.Primary.ID),
    68  			Name:         aws.String("testalias"),
    69  		}
    70  
    71  		getAliasConfiguration, err := conn.GetAlias(params)
    72  		if err != nil {
    73  			return err
    74  		}
    75  
    76  		*mapping = *getAliasConfiguration
    77  
    78  		return nil
    79  	}
    80  }
    81  
    82  func testAccCheckAwsLambdaAttributes(mapping *lambda.AliasConfiguration) resource.TestCheckFunc {
    83  	return func(s *terraform.State) error {
    84  		name := *mapping.Name
    85  		arn := *mapping.AliasArn
    86  		if arn == "" {
    87  			return fmt.Errorf("Could not read Lambda alias ARN")
    88  		}
    89  		if name == "" {
    90  			return fmt.Errorf("Could not read Lambda alias name")
    91  		}
    92  		return nil
    93  	}
    94  }
    95  
    96  const testAccAwsLambdaAliasConfig = `
    97  resource "aws_iam_role" "iam_for_lambda" {
    98    name = "iam_for_lambda"
    99  
   100    assume_role_policy = <<EOF
   101  {
   102    "Version": "2012-10-17",
   103    "Statement": [
   104      {
   105        "Action": "sts:AssumeRole",
   106        "Principal": {
   107          "Service": "lambda.amazonaws.com"
   108        },
   109        "Effect": "Allow",
   110        "Sid": ""
   111      }
   112    ]
   113  }
   114  EOF
   115  }
   116  
   117  resource "aws_iam_policy" "policy_for_role" {
   118    name        = "policy_for_role"
   119    path        = "/"
   120    description = "IAM policy for for Lamda alias testing"
   121  
   122    policy = <<EOF
   123  {
   124    "Version": "2012-10-17",
   125    "Statement": [
   126        {
   127            "Effect": "Allow",
   128            "Action": [
   129              "lambda:*"
   130            ],
   131            "Resource": "*"
   132        }
   133    ]
   134  }
   135  EOF
   136  }
   137  
   138  resource "aws_iam_policy_attachment" "policy_attachment_for_role" {
   139    name       = "policy_attachment_for_role"
   140    roles      = ["${aws_iam_role.iam_for_lambda.name}"]
   141    policy_arn = "${aws_iam_policy.policy_for_role.arn}"
   142  }
   143  
   144  resource "aws_lambda_function" "lambda_function_test_create" {
   145    filename      = "test-fixtures/lambdatest.zip"
   146    function_name = "example_lambda_name_create"
   147    role          = "${aws_iam_role.iam_for_lambda.arn}"
   148    handler       = "exports.example"
   149  }
   150  
   151  resource "aws_lambda_alias" "lambda_alias_test" {
   152    name             = "testalias"
   153    description      = "a sample description"
   154    function_name    = "${aws_lambda_function.lambda_function_test_create.arn}"
   155    function_version = "$LATEST"
   156  }
   157  `