github.com/jrperritt/terraform@v0.1.1-0.20170525065507-96f391dafc38/builtin/providers/aws/resource_aws_iam_policy_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "regexp" 6 "strings" 7 "testing" 8 9 "github.com/aws/aws-sdk-go/aws" 10 "github.com/aws/aws-sdk-go/service/iam" 11 "github.com/hashicorp/terraform/helper/resource" 12 "github.com/hashicorp/terraform/terraform" 13 ) 14 15 func TestAWSPolicy_namePrefix(t *testing.T) { 16 var out iam.GetPolicyOutput 17 18 resource.Test(t, resource.TestCase{ 19 PreCheck: func() { testAccPreCheck(t) }, 20 Providers: testAccProviders, 21 CheckDestroy: testAccCheckAWSPolicyDestroy, 22 Steps: []resource.TestStep{ 23 { 24 Config: testAccAWSPolicyPrefixNameConfig, 25 Check: resource.ComposeTestCheckFunc( 26 testAccCheckAWSPolicyExists("aws_iam_policy.policy", &out), 27 testAccCheckAWSPolicyGeneratedNamePrefix( 28 "aws_iam_policy.policy", "test-policy-"), 29 ), 30 }, 31 }, 32 }) 33 } 34 35 func TestAWSPolicy_invalidJson(t *testing.T) { 36 resource.Test(t, resource.TestCase{ 37 PreCheck: func() { testAccPreCheck(t) }, 38 Providers: testAccProviders, 39 CheckDestroy: testAccCheckAWSPolicyDestroy, 40 Steps: []resource.TestStep{ 41 { 42 Config: testAccAWSPolicyInvalidJsonConfig, 43 ExpectError: regexp.MustCompile("invalid JSON"), 44 }, 45 }, 46 }) 47 } 48 49 func testAccCheckAWSPolicyExists(resource string, res *iam.GetPolicyOutput) resource.TestCheckFunc { 50 return func(s *terraform.State) error { 51 rs, ok := s.RootModule().Resources[resource] 52 if !ok { 53 return fmt.Errorf("Not found: %s", resource) 54 } 55 56 if rs.Primary.ID == "" { 57 return fmt.Errorf("No Policy name is set") 58 } 59 60 iamconn := testAccProvider.Meta().(*AWSClient).iamconn 61 62 resp, err := iamconn.GetPolicy(&iam.GetPolicyInput{ 63 PolicyArn: aws.String(rs.Primary.Attributes["arn"]), 64 }) 65 if err != nil { 66 return err 67 } 68 69 *res = *resp 70 71 return nil 72 } 73 } 74 75 func testAccCheckAWSPolicyGeneratedNamePrefix(resource, prefix string) resource.TestCheckFunc { 76 return func(s *terraform.State) error { 77 r, ok := s.RootModule().Resources[resource] 78 if !ok { 79 return fmt.Errorf("Resource not found") 80 } 81 name, ok := r.Primary.Attributes["name"] 82 if !ok { 83 return fmt.Errorf("Name attr not found: %#v", r.Primary.Attributes) 84 } 85 if !strings.HasPrefix(name, prefix) { 86 return fmt.Errorf("Name: %q, does not have prefix: %q", name, prefix) 87 } 88 return nil 89 } 90 } 91 92 const testAccAWSPolicyPrefixNameConfig = ` 93 resource "aws_iam_policy" "policy" { 94 name_prefix = "test-policy-" 95 path = "/" 96 policy = <<EOF 97 { 98 "Version": "2012-10-17", 99 "Statement": [ 100 { 101 "Action": [ 102 "ec2:Describe*" 103 ], 104 "Effect": "Allow", 105 "Resource": "*" 106 } 107 ] 108 } 109 EOF 110 } 111 ` 112 const testAccAWSPolicyInvalidJsonConfig = ` 113 resource "aws_iam_policy" "policy" { 114 name_prefix = "test-policy-" 115 path = "/" 116 policy = <<EOF 117 { 118 "Version": "2012-10-17", 119 "Statement": [ 120 { 121 "Action": [ 122 "ec2:Describe*" 123 ], 124 "Effect": "Allow", 125 "Resource": "*" 126 } 127 ] 128 } 129 EOF 130 } 131 `