terraform/builtin/providers/aws/resource_aws_iam_user_test.go

158 lines
3.4 KiB
Go
Raw Normal View History

2015-04-18 22:58:08 +02:00
package aws
import (
"fmt"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/hashicorp/terraform/helper/acctest"
2015-04-18 22:58:08 +02:00
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
provider/aws: Add validation to IAM User and Group Name This will allow us to catch errors at plan time rather than waiting for the API to tell us... Documentation for IAM User NAme Validation - http://docs.aws.amazon.com/cli/latest/reference/iam/create-user.html Documentation for IAM Group Name validation - http://docs.aws.amazon.com/cli/latest/reference/iam/create-group.html ``` % make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSIAMGroup_' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2016/10/25 13:18:41 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSIAMGroup_ -timeout 120m === RUN TestAccAWSIAMGroup_importBasic --- PASS: TestAccAWSIAMGroup_importBasic (13.80s) === RUN TestAccAWSIAMGroup_basic --- PASS: TestAccAWSIAMGroup_basic (23.30s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws37.121s ``` ``` % make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSUser_' ✚ ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2016/10/25 13:22:23 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSUser_ -timeout 120m === RUN TestAccAWSUser_importBasic --- PASS: TestAccAWSUser_importBasic (14.33s) === RUN TestAccAWSUser_basic --- PASS: TestAccAWSUser_basic (25.36s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 39.710s ```
2016-10-25 14:18:41 +02:00
func TestValidateIamUserName(t *testing.T) {
validNames := []string{
"test-user",
"testuser123",
"TestUser",
"Test-User",
"test.user",
"test.123,user",
"testuser@hashicorp",
}
for _, v := range validNames {
_, errors := validateAwsIamUserName(v, "name")
if len(errors) != 0 {
t.Fatalf("%q should be a valid IAM User name: %q", v, errors)
}
}
invalidNames := []string{
"!",
"/",
" ",
":",
";",
"testuser_123",
"test name",
"/slash-at-the-beginning",
"slash-at-the-end/",
}
for _, v := range invalidNames {
_, errors := validateAwsIamUserName(v, "name")
if len(errors) == 0 {
t.Fatalf("%q should be an invalid IAM User name", v)
}
}
}
func TestAccAWSUser_basic(t *testing.T) {
2015-04-18 22:58:08 +02:00
var conf iam.GetUserOutput
name1 := fmt.Sprintf("test-user-%d", acctest.RandInt())
name2 := fmt.Sprintf("test-user-%d", acctest.RandInt())
path1 := "/"
path2 := "/path2/"
2015-04-18 22:58:08 +02:00
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSUserDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSUserConfig(name1, path1),
2015-04-18 22:58:08 +02:00
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSUserExists("aws_iam_user.user", &conf),
testAccCheckAWSUserAttributes(&conf, name1, "/"),
2015-04-18 22:58:08 +02:00
),
},
2015-09-13 09:52:40 +02:00
resource.TestStep{
Config: testAccAWSUserConfig(name2, path2),
2015-09-13 09:52:40 +02:00
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSUserExists("aws_iam_user.user", &conf),
testAccCheckAWSUserAttributes(&conf, name2, "/path2/"),
2015-09-13 09:52:40 +02:00
),
},
2015-04-18 22:58:08 +02:00
},
})
}
func testAccCheckAWSUserDestroy(s *terraform.State) error {
iamconn := testAccProvider.Meta().(*AWSClient).iamconn
for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_iam_user" {
continue
}
// Try to get user
_, err := iamconn.GetUser(&iam.GetUserInput{
UserName: aws.String(rs.Primary.ID),
})
if err == nil {
return fmt.Errorf("still exist.")
}
// Verify the error is what we want
ec2err, ok := err.(awserr.Error)
2015-04-18 22:58:08 +02:00
if !ok {
return err
}
if ec2err.Code() != "NoSuchEntity" {
2015-04-18 22:58:08 +02:00
return err
}
}
return nil
}
func testAccCheckAWSUserExists(n string, res *iam.GetUserOutput) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No User name is set")
}
iamconn := testAccProvider.Meta().(*AWSClient).iamconn
resp, err := iamconn.GetUser(&iam.GetUserInput{
UserName: aws.String(rs.Primary.ID),
})
if err != nil {
return err
}
*res = *resp
return nil
}
}
func testAccCheckAWSUserAttributes(user *iam.GetUserOutput, name string, path string) resource.TestCheckFunc {
2015-04-18 22:58:08 +02:00
return func(s *terraform.State) error {
if *user.User.UserName != name {
2015-04-18 22:58:08 +02:00
return fmt.Errorf("Bad name: %s", *user.User.UserName)
}
if *user.User.Path != path {
2015-04-18 22:58:08 +02:00
return fmt.Errorf("Bad path: %s", *user.User.Path)
}
return nil
}
}
func testAccAWSUserConfig(r, p string) string {
return fmt.Sprintf(`
2015-09-13 09:52:40 +02:00
resource "aws_iam_user" "user" {
name = "%s"
path = "%s"
}`, r, p)
2015-09-13 09:52:40 +02:00
}