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