github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/data_source_aws_iam_role_test.go (about) 1 package aws 2 3 import ( 4 "regexp" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 ) 9 10 func TestAccAWSDataSourceIAMRole_basic(t *testing.T) { 11 resource.Test(t, resource.TestCase{ 12 PreCheck: func() { testAccPreCheck(t) }, 13 Providers: testAccProviders, 14 Steps: []resource.TestStep{ 15 { 16 Config: testAccAwsIAMRoleConfig, 17 Check: resource.ComposeTestCheckFunc( 18 resource.TestCheckResourceAttrSet("data.aws_iam_role.test", "role_id"), 19 resource.TestCheckResourceAttr("data.aws_iam_role.test", "assume_role_policy_document", "%7B%22Version%22%3A%222012-10-17%22%2C%22Statement%22%3A%5B%7B%22Sid%22%3A%22%22%2C%22Effect%22%3A%22Allow%22%2C%22Principal%22%3A%7B%22Service%22%3A%22ec2.amazonaws.com%22%7D%2C%22Action%22%3A%22sts%3AAssumeRole%22%7D%5D%7D"), 20 resource.TestCheckResourceAttr("data.aws_iam_role.test", "path", "/testpath/"), 21 resource.TestCheckResourceAttr("data.aws_iam_role.test", "role_name", "TestRole"), 22 resource.TestMatchResourceAttr("data.aws_iam_role.test", "arn", regexp.MustCompile("^arn:aws:iam::[0-9]{12}:role/testpath/TestRole$")), 23 ), 24 }, 25 }, 26 }) 27 } 28 29 const testAccAwsIAMRoleConfig = ` 30 provider "aws" { 31 region = "us-east-1" 32 } 33 34 resource "aws_iam_role" "test_role" { 35 name = "TestRole" 36 37 assume_role_policy = <<EOF 38 { 39 "Version": "2012-10-17", 40 "Statement": [ 41 { 42 "Action": "sts:AssumeRole", 43 "Principal": { 44 "Service": "ec2.amazonaws.com" 45 }, 46 "Effect": "Allow", 47 "Sid": "" 48 } 49 ] 50 } 51 EOF 52 53 path = "/testpath/" 54 } 55 56 data "aws_iam_role" "test" { 57 role_name = "${aws_iam_role.test_role.name}" 58 } 59 `