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