terraform/builtin/providers/aws/resource_aws_iam_group_test.go

108 lines
2.3 KiB
Go
Raw Normal View History

2015-04-18 22:58:08 +02:00
package aws
import (
"fmt"
"testing"
"github.com/awslabs/aws-sdk-go/aws"
"github.com/awslabs/aws-sdk-go/aws/awserr"
2015-04-18 22:58:08 +02:00
"github.com/awslabs/aws-sdk-go/service/iam"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
2015-05-06 16:18:00 +02:00
func TestAccAWSIAMGroup_normal(t *testing.T) {
2015-04-18 22:58:08 +02:00
var conf iam.GetGroupOutput
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSGroupDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSGroupConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSGroupExists("aws_iam_group.group", &conf),
testAccCheckAWSGroupAttributes(&conf),
),
},
},
})
}
func testAccCheckAWSGroupDestroy(s *terraform.State) error {
iamconn := testAccProvider.Meta().(*AWSClient).iamconn
for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_iam_group" {
continue
}
// Try to get group
_, err := iamconn.GetGroup(&iam.GetGroupInput{
GroupName: 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 testAccCheckAWSGroupExists(n string, res *iam.GetGroupOutput) 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 Group name is set")
}
iamconn := testAccProvider.Meta().(*AWSClient).iamconn
resp, err := iamconn.GetGroup(&iam.GetGroupInput{
GroupName: aws.String(rs.Primary.ID),
})
if err != nil {
return err
}
*res = *resp
return nil
}
}
func testAccCheckAWSGroupAttributes(group *iam.GetGroupOutput) resource.TestCheckFunc {
return func(s *terraform.State) error {
if *group.Group.GroupName != "test-group" {
return fmt.Errorf("Bad name: %s", *group.Group.GroupName)
}
if *group.Group.Path != "/" {
return fmt.Errorf("Bad path: %s", *group.Group.Path)
}
return nil
}
}
const testAccAWSGroupConfig = `
resource "aws_iam_group" "group" {
name = "test-group"
path = "/"
}
`