github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/aws/resource_aws_iam_policy_test.go (about)

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