github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/aws/resource_aws_iam_instance_profile_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/aws/awserr"
    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 TestAccAWSIAMInstanceProfile_basic(t *testing.T) {
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:  func() { testAccPreCheck(t) },
    18  		Providers: testAccProviders,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccAwsIamInstanceProfileConfig,
    22  			},
    23  		},
    24  	})
    25  }
    26  
    27  func TestAccAWSIAMInstanceProfile_namePrefix(t *testing.T) {
    28  	var conf iam.GetInstanceProfileOutput
    29  
    30  	resource.Test(t, resource.TestCase{
    31  		PreCheck:        func() { testAccPreCheck(t) },
    32  		IDRefreshName:   "aws_iam_instance_profile.test",
    33  		IDRefreshIgnore: []string{"name_prefix"},
    34  		Providers:       testAccProviders,
    35  		CheckDestroy:    testAccCheckAWSInstanceProfileDestroy,
    36  		Steps: []resource.TestStep{
    37  			resource.TestStep{
    38  				Config: testAccAWSInstanceProfilePrefixNameConfig,
    39  				Check: resource.ComposeTestCheckFunc(
    40  					testAccCheckAWSInstanceProfileExists("aws_iam_instance_profile.test", &conf),
    41  					testAccCheckAWSInstanceProfileGeneratedNamePrefix(
    42  						"aws_iam_instance_profile.test", "test-"),
    43  				),
    44  			},
    45  		},
    46  	})
    47  }
    48  
    49  func testAccCheckAWSInstanceProfileGeneratedNamePrefix(resource, prefix string) resource.TestCheckFunc {
    50  	return func(s *terraform.State) error {
    51  		r, ok := s.RootModule().Resources[resource]
    52  		if !ok {
    53  			return fmt.Errorf("Resource not found")
    54  		}
    55  		name, ok := r.Primary.Attributes["name"]
    56  		if !ok {
    57  			return fmt.Errorf("Name attr not found: %#v", r.Primary.Attributes)
    58  		}
    59  		if !strings.HasPrefix(name, prefix) {
    60  			return fmt.Errorf("Name: %q, does not have prefix: %q", name, prefix)
    61  		}
    62  		return nil
    63  	}
    64  }
    65  
    66  func testAccCheckAWSInstanceProfileDestroy(s *terraform.State) error {
    67  	iamconn := testAccProvider.Meta().(*AWSClient).iamconn
    68  
    69  	for _, rs := range s.RootModule().Resources {
    70  		if rs.Type != "aws_iam_instance_profile" {
    71  			continue
    72  		}
    73  
    74  		// Try to get role
    75  		_, err := iamconn.GetInstanceProfile(&iam.GetInstanceProfileInput{
    76  			InstanceProfileName: aws.String(rs.Primary.ID),
    77  		})
    78  		if err == nil {
    79  			return fmt.Errorf("still exist.")
    80  		}
    81  
    82  		// Verify the error is what we want
    83  		ec2err, ok := err.(awserr.Error)
    84  		if !ok {
    85  			return err
    86  		}
    87  		if ec2err.Code() != "NoSuchEntity" {
    88  			return err
    89  		}
    90  	}
    91  
    92  	return nil
    93  }
    94  
    95  func testAccCheckAWSInstanceProfileExists(n string, res *iam.GetInstanceProfileOutput) resource.TestCheckFunc {
    96  	return func(s *terraform.State) error {
    97  		rs, ok := s.RootModule().Resources[n]
    98  		if !ok {
    99  			return fmt.Errorf("Not found: %s", n)
   100  		}
   101  
   102  		if rs.Primary.ID == "" {
   103  			return fmt.Errorf("No Instance Profile name is set")
   104  		}
   105  
   106  		iamconn := testAccProvider.Meta().(*AWSClient).iamconn
   107  
   108  		resp, err := iamconn.GetInstanceProfile(&iam.GetInstanceProfileInput{
   109  			InstanceProfileName: aws.String(rs.Primary.ID),
   110  		})
   111  		if err != nil {
   112  			return err
   113  		}
   114  
   115  		*res = *resp
   116  
   117  		return nil
   118  	}
   119  }
   120  
   121  const testAccAwsIamInstanceProfileConfig = `
   122  resource "aws_iam_role" "test" {
   123  	name = "test"
   124  	assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"ec2.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}"
   125  }
   126  
   127  resource "aws_iam_instance_profile" "test" {
   128  	name = "test"
   129  	roles = ["${aws_iam_role.test.name}"]
   130  }
   131  `
   132  
   133  const testAccAWSInstanceProfilePrefixNameConfig = `
   134  resource "aws_iam_role" "test" {
   135  	name = "test"
   136  	assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"ec2.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}"
   137  }
   138  
   139  resource "aws_iam_instance_profile" "test" {
   140  	name_prefix = "test-"
   141  	roles = ["${aws_iam_role.test.name}"]
   142  }
   143  `