terraform/builtin/providers/aws/resource_aws_iam_user_test.go

121 lines
2.6 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"
2015-04-18 22:58:08 +02:00
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccAWSUser_basic(t *testing.T) {
2015-04-18 22:58:08 +02:00
var conf iam.GetUserOutput
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSUserDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSUserConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSUserExists("aws_iam_user.user", &conf),
testAccCheckAWSUserAttributes(&conf, "test-user", "/"),
2015-04-18 22:58:08 +02:00
),
},
2015-09-13 09:52:40 +02:00
resource.TestStep{
Config: testAccAWSUserConfig2,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSUserExists("aws_iam_user.user", &conf),
testAccCheckAWSUserAttributes(&conf, "test-user2", "/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
}
}
const testAccAWSUserConfig = `
resource "aws_iam_user" "user" {
name = "test-user"
path = "/"
}
`
2015-09-13 09:52:40 +02:00
const testAccAWSUserConfig2 = `
resource "aws_iam_user" "user" {
name = "test-user2"
path = "/path2/"
2015-09-13 09:52:40 +02:00
}
`