github.com/i0n/terraform@v0.4.3-0.20150506151324-010a39a58ec1/builtin/providers/aws/resource_aws_iam_role_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/awslabs/aws-sdk-go/aws"
     8  	"github.com/awslabs/aws-sdk-go/service/iam"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccAWSRole_normal(t *testing.T) {
    14  	var conf iam.GetRoleOutput
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckAWSRoleDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccAWSRoleConfig,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckAWSRoleExists("aws_iam_role.role", &conf),
    25  					testAccCheckAWSRoleAttributes(&conf),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func testAccCheckAWSRoleDestroy(s *terraform.State) error {
    33  	iamconn := testAccProvider.Meta().(*AWSClient).iamconn
    34  
    35  	for _, rs := range s.RootModule().Resources {
    36  		if rs.Type != "aws_iam_role" {
    37  			continue
    38  		}
    39  
    40  		// Try to get role
    41  		_, err := iamconn.GetRole(&iam.GetRoleInput{
    42  			RoleName: aws.String(rs.Primary.ID),
    43  		})
    44  		if err == nil {
    45  			return fmt.Errorf("still exist.")
    46  		}
    47  
    48  		// Verify the error is what we want
    49  		ec2err, ok := err.(aws.APIError)
    50  		if !ok {
    51  			return err
    52  		}
    53  		if ec2err.Code != "NoSuchEntity" {
    54  			return err
    55  		}
    56  	}
    57  
    58  	return nil
    59  }
    60  
    61  func testAccCheckAWSRoleExists(n string, res *iam.GetRoleOutput) resource.TestCheckFunc {
    62  	return func(s *terraform.State) error {
    63  		rs, ok := s.RootModule().Resources[n]
    64  		if !ok {
    65  			return fmt.Errorf("Not found: %s", n)
    66  		}
    67  
    68  		if rs.Primary.ID == "" {
    69  			return fmt.Errorf("No Role name is set")
    70  		}
    71  
    72  		iamconn := testAccProvider.Meta().(*AWSClient).iamconn
    73  
    74  		resp, err := iamconn.GetRole(&iam.GetRoleInput{
    75  			RoleName: aws.String(rs.Primary.ID),
    76  		})
    77  		if err != nil {
    78  			return err
    79  		}
    80  
    81  		*res = *resp
    82  
    83  		return nil
    84  	}
    85  }
    86  
    87  func testAccCheckAWSRoleAttributes(role *iam.GetRoleOutput) resource.TestCheckFunc {
    88  	return func(s *terraform.State) error {
    89  		if *role.Role.RoleName != "test-role" {
    90  			return fmt.Errorf("Bad name: %s", *role.Role.RoleName)
    91  		}
    92  
    93  		if *role.Role.Path != "/" {
    94  			return fmt.Errorf("Bad path: %s", *role.Role.Path)
    95  		}
    96  		return nil
    97  	}
    98  }
    99  
   100  const testAccAWSRoleConfig = `
   101  resource "aws_iam_role" "role" {
   102  	name   = "test-role"
   103  	path = "/"
   104  	assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"ec2.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}"
   105  }
   106  `