github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_iam_user_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/aws/awserr" 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 TestAccAWSUser_basic(t *testing.T) { 15 var conf iam.GetUserOutput 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 CheckDestroy: testAccCheckAWSUserDestroy, 21 Steps: []resource.TestStep{ 22 resource.TestStep{ 23 Config: testAccAWSUserConfig, 24 Check: resource.ComposeTestCheckFunc( 25 testAccCheckAWSUserExists("aws_iam_user.user", &conf), 26 testAccCheckAWSUserAttributes(&conf, "test-user", "/"), 27 ), 28 }, 29 resource.TestStep{ 30 Config: testAccAWSUserConfig2, 31 Check: resource.ComposeTestCheckFunc( 32 testAccCheckAWSUserExists("aws_iam_user.user", &conf), 33 testAccCheckAWSUserAttributes(&conf, "test-user2", "/path2/"), 34 ), 35 }, 36 }, 37 }) 38 } 39 40 func testAccCheckAWSUserDestroy(s *terraform.State) error { 41 iamconn := testAccProvider.Meta().(*AWSClient).iamconn 42 43 for _, rs := range s.RootModule().Resources { 44 if rs.Type != "aws_iam_user" { 45 continue 46 } 47 48 // Try to get user 49 _, err := iamconn.GetUser(&iam.GetUserInput{ 50 UserName: aws.String(rs.Primary.ID), 51 }) 52 if err == nil { 53 return fmt.Errorf("still exist.") 54 } 55 56 // Verify the error is what we want 57 ec2err, ok := err.(awserr.Error) 58 if !ok { 59 return err 60 } 61 if ec2err.Code() != "NoSuchEntity" { 62 return err 63 } 64 } 65 66 return nil 67 } 68 69 func testAccCheckAWSUserExists(n string, res *iam.GetUserOutput) resource.TestCheckFunc { 70 return func(s *terraform.State) error { 71 rs, ok := s.RootModule().Resources[n] 72 if !ok { 73 return fmt.Errorf("Not found: %s", n) 74 } 75 76 if rs.Primary.ID == "" { 77 return fmt.Errorf("No User name is set") 78 } 79 80 iamconn := testAccProvider.Meta().(*AWSClient).iamconn 81 82 resp, err := iamconn.GetUser(&iam.GetUserInput{ 83 UserName: aws.String(rs.Primary.ID), 84 }) 85 if err != nil { 86 return err 87 } 88 89 *res = *resp 90 91 return nil 92 } 93 } 94 95 func testAccCheckAWSUserAttributes(user *iam.GetUserOutput, name string, path string) resource.TestCheckFunc { 96 return func(s *terraform.State) error { 97 if *user.User.UserName != name { 98 return fmt.Errorf("Bad name: %s", *user.User.UserName) 99 } 100 101 if *user.User.Path != path { 102 return fmt.Errorf("Bad path: %s", *user.User.Path) 103 } 104 105 return nil 106 } 107 } 108 109 const testAccAWSUserConfig = ` 110 resource "aws_iam_user" "user" { 111 name = "test-user" 112 path = "/" 113 } 114 ` 115 const testAccAWSUserConfig2 = ` 116 resource "aws_iam_user" "user" { 117 name = "test-user2" 118 path = "/path2/" 119 } 120 `