github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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 TestValidateIamUserName(t *testing.T) { 16 validNames := []string{ 17 "test-user", 18 "test_user", 19 "testuser123", 20 "TestUser", 21 "Test-User", 22 "test.user", 23 "test.123,user", 24 "testuser@hashicorp", 25 "test+user@hashicorp.com", 26 } 27 for _, v := range validNames { 28 _, errors := validateAwsIamUserName(v, "name") 29 if len(errors) != 0 { 30 t.Fatalf("%q should be a valid IAM User name: %q", v, errors) 31 } 32 } 33 34 invalidNames := []string{ 35 "!", 36 "/", 37 " ", 38 ":", 39 ";", 40 "test name", 41 "/slash-at-the-beginning", 42 "slash-at-the-end/", 43 } 44 for _, v := range invalidNames { 45 _, errors := validateAwsIamUserName(v, "name") 46 if len(errors) == 0 { 47 t.Fatalf("%q should be an invalid IAM User name", v) 48 } 49 } 50 } 51 52 func TestAccAWSUser_basic(t *testing.T) { 53 var conf iam.GetUserOutput 54 55 name1 := fmt.Sprintf("test-user-%d", acctest.RandInt()) 56 name2 := fmt.Sprintf("test-user-%d", acctest.RandInt()) 57 path1 := "/" 58 path2 := "/path2/" 59 60 resource.Test(t, resource.TestCase{ 61 PreCheck: func() { testAccPreCheck(t) }, 62 Providers: testAccProviders, 63 CheckDestroy: testAccCheckAWSUserDestroy, 64 Steps: []resource.TestStep{ 65 resource.TestStep{ 66 Config: testAccAWSUserConfig(name1, path1), 67 Check: resource.ComposeTestCheckFunc( 68 testAccCheckAWSUserExists("aws_iam_user.user", &conf), 69 testAccCheckAWSUserAttributes(&conf, name1, "/"), 70 ), 71 }, 72 resource.TestStep{ 73 Config: testAccAWSUserConfig(name2, path2), 74 Check: resource.ComposeTestCheckFunc( 75 testAccCheckAWSUserExists("aws_iam_user.user", &conf), 76 testAccCheckAWSUserAttributes(&conf, name2, "/path2/"), 77 ), 78 }, 79 }, 80 }) 81 } 82 83 func testAccCheckAWSUserDestroy(s *terraform.State) error { 84 iamconn := testAccProvider.Meta().(*AWSClient).iamconn 85 86 for _, rs := range s.RootModule().Resources { 87 if rs.Type != "aws_iam_user" { 88 continue 89 } 90 91 // Try to get user 92 _, err := iamconn.GetUser(&iam.GetUserInput{ 93 UserName: aws.String(rs.Primary.ID), 94 }) 95 if err == nil { 96 return fmt.Errorf("still exist.") 97 } 98 99 // Verify the error is what we want 100 ec2err, ok := err.(awserr.Error) 101 if !ok { 102 return err 103 } 104 if ec2err.Code() != "NoSuchEntity" { 105 return err 106 } 107 } 108 109 return nil 110 } 111 112 func testAccCheckAWSUserExists(n string, res *iam.GetUserOutput) resource.TestCheckFunc { 113 return func(s *terraform.State) error { 114 rs, ok := s.RootModule().Resources[n] 115 if !ok { 116 return fmt.Errorf("Not found: %s", n) 117 } 118 119 if rs.Primary.ID == "" { 120 return fmt.Errorf("No User name is set") 121 } 122 123 iamconn := testAccProvider.Meta().(*AWSClient).iamconn 124 125 resp, err := iamconn.GetUser(&iam.GetUserInput{ 126 UserName: aws.String(rs.Primary.ID), 127 }) 128 if err != nil { 129 return err 130 } 131 132 *res = *resp 133 134 return nil 135 } 136 } 137 138 func testAccCheckAWSUserAttributes(user *iam.GetUserOutput, name string, path string) resource.TestCheckFunc { 139 return func(s *terraform.State) error { 140 if *user.User.UserName != name { 141 return fmt.Errorf("Bad name: %s", *user.User.UserName) 142 } 143 144 if *user.User.Path != path { 145 return fmt.Errorf("Bad path: %s", *user.User.Path) 146 } 147 148 return nil 149 } 150 } 151 152 func testAccAWSUserConfig(r, p string) string { 153 return fmt.Sprintf(` 154 resource "aws_iam_user" "user" { 155 name = "%s" 156 path = "%s" 157 }`, r, p) 158 }