github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_sfn_state_machine_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/aws/awserr"
     9  	"github.com/aws/aws-sdk-go/service/sfn"
    10  	"github.com/hashicorp/terraform/helper/acctest"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccAWSSfnStateMachine_basic(t *testing.T) {
    16  	name := acctest.RandString(10)
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testAccCheckAWSSfnStateMachineDestroy,
    22  		Steps: []resource.TestStep{
    23  			{
    24  				Config: testAccAWSSfnStateMachineBasicConfig(name),
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCheckAWSSfnExists("aws_sfn_state_machine.foo"),
    27  					resource.TestCheckResourceAttr("aws_sfn_state_machine.foo", "status", sfn.StateMachineStatusActive),
    28  					resource.TestCheckResourceAttrSet("aws_sfn_state_machine.foo", "name"),
    29  					resource.TestCheckResourceAttrSet("aws_sfn_state_machine.foo", "creation_date"),
    30  					resource.TestCheckResourceAttrSet("aws_sfn_state_machine.foo", "definition"),
    31  					resource.TestCheckResourceAttrSet("aws_sfn_state_machine.foo", "role_arn"),
    32  				),
    33  			},
    34  		},
    35  	})
    36  }
    37  
    38  func testAccCheckAWSSfnExists(n string) resource.TestCheckFunc {
    39  	return func(s *terraform.State) error {
    40  		rs, ok := s.RootModule().Resources[n]
    41  		if !ok {
    42  			return fmt.Errorf("Not found: %s", n)
    43  		}
    44  
    45  		if rs.Primary.ID == "" {
    46  			return fmt.Errorf("No Step Function ID set")
    47  		}
    48  
    49  		conn := testAccProvider.Meta().(*AWSClient).sfnconn
    50  
    51  		_, err := conn.DescribeStateMachine(&sfn.DescribeStateMachineInput{
    52  			StateMachineArn: aws.String(rs.Primary.ID),
    53  		})
    54  
    55  		if err != nil {
    56  			return err
    57  		}
    58  
    59  		return nil
    60  	}
    61  }
    62  
    63  func testAccCheckAWSSfnStateMachineDestroy(s *terraform.State) error {
    64  	conn := testAccProvider.Meta().(*AWSClient).sfnconn
    65  
    66  	for _, rs := range s.RootModule().Resources {
    67  		if rs.Type != "aws_sfn_state_machine" {
    68  			continue
    69  		}
    70  
    71  		out, err := conn.DescribeStateMachine(&sfn.DescribeStateMachineInput{
    72  			StateMachineArn: aws.String(rs.Primary.ID),
    73  		})
    74  
    75  		if err != nil {
    76  			if wserr, ok := err.(awserr.Error); ok && wserr.Code() == "StateMachineDoesNotExist" {
    77  				return nil
    78  			}
    79  			return err
    80  		}
    81  
    82  		if out != nil && *out.Status != sfn.StateMachineStatusDeleting {
    83  			return fmt.Errorf("Expected AWS Step Function State Machine to be destroyed, but was still found")
    84  		}
    85  
    86  		return nil
    87  	}
    88  
    89  	return fmt.Errorf("Default error in Step Function Test")
    90  }
    91  
    92  func testAccAWSSfnStateMachineBasicConfig(rName string) string {
    93  	return fmt.Sprintf(`
    94  data "aws_region" "current" {
    95    current = true
    96  }
    97  
    98  resource "aws_iam_role_policy" "iam_policy_for_lambda" {
    99    name = "iam_policy_for_lambda_%s"
   100    role = "${aws_iam_role.iam_for_lambda.id}"
   101    policy = <<EOF
   102  {
   103    "Version": "2012-10-17",
   104    "Statement": [{
   105      "Effect": "Allow",
   106      "Action": [
   107        "logs:CreateLogGroup",
   108        "logs:CreateLogStream",
   109        "logs:PutLogEvents"
   110      ],
   111      "Resource": "arn:aws:logs:*:*:*"
   112    }]
   113  }
   114  EOF
   115  }
   116  
   117  resource "aws_iam_role" "iam_for_lambda" {
   118    name = "iam_for_lambda_%s"
   119    assume_role_policy = <<EOF
   120  {
   121    "Version": "2012-10-17",
   122    "Statement": [
   123      {
   124        "Action": "sts:AssumeRole",
   125        "Principal": {
   126          "Service": "lambda.amazonaws.com"
   127        },
   128        "Effect": "Allow",
   129        "Sid": ""
   130      }
   131    ]
   132  }
   133  EOF
   134  }
   135  
   136  resource "aws_iam_role_policy" "iam_policy_for_sfn" {
   137    name = "iam_policy_for_sfn_%s"
   138    role = "${aws_iam_role.iam_for_sfn.id}"
   139    policy = <<EOF
   140  {
   141    "Version": "2012-10-17",
   142    "Statement": [
   143      {
   144        "Effect": "Allow",
   145        "Action": [
   146          "lambda:InvokeFunction"
   147        ],
   148          "Resource": "*"
   149        }
   150    ]
   151  }
   152  EOF
   153  }
   154  
   155  resource "aws_iam_role" "iam_for_sfn" {
   156    name = "iam_for_sfn_%s"
   157    assume_role_policy = <<EOF
   158  {
   159    "Version": "2012-10-17",
   160    "Statement": [
   161      {
   162        "Effect": "Allow",
   163        "Principal": {
   164          "Service": "states.${data.aws_region.current.name}.amazonaws.com"
   165        },
   166        "Action": "sts:AssumeRole"
   167      }
   168    ]
   169  }
   170  EOF
   171  }
   172  
   173  resource "aws_lambda_function" "lambda_function_test" {
   174    filename = "test-fixtures/lambdatest.zip"
   175    function_name = "sfn-%s"
   176    role = "${aws_iam_role.iam_for_lambda.arn}"
   177    handler = "exports.example"
   178    runtime = "nodejs4.3"
   179  }
   180  
   181  resource "aws_sfn_state_machine" "foo" {
   182    name     = "test_sfn_%s"
   183    role_arn = "${aws_iam_role.iam_for_sfn.arn}"
   184  
   185    definition = <<EOF
   186  {
   187    "Comment": "A Hello World example of the Amazon States Language using an AWS Lambda Function",
   188    "StartAt": "HelloWorld",
   189    "States": {
   190      "HelloWorld": {
   191        "Type": "Task",
   192        "Resource": "${aws_lambda_function.lambda_function_test.arn}",
   193        "End": true
   194      }
   195    }
   196  }
   197  EOF
   198  }
   199  
   200  `, rName, rName, rName, rName, rName, rName)
   201  }