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