github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/resource_aws_iam_user_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/acctest"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestValidateIamUserName(t *testing.T) {
    16  	validNames := []string{
    17  		"test-user",
    18  		"test_user",
    19  		"testuser123",
    20  		"TestUser",
    21  		"Test-User",
    22  		"test.user",
    23  		"test.123,user",
    24  		"testuser@hashicorp",
    25  	}
    26  	for _, v := range validNames {
    27  		_, errors := validateAwsIamUserName(v, "name")
    28  		if len(errors) != 0 {
    29  			t.Fatalf("%q should be a valid IAM User 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 := validateAwsIamUserName(v, "name")
    45  		if len(errors) == 0 {
    46  			t.Fatalf("%q should be an invalid IAM User name", v)
    47  		}
    48  	}
    49  }
    50  
    51  func TestAccAWSUser_basic(t *testing.T) {
    52  	var conf iam.GetUserOutput
    53  
    54  	name1 := fmt.Sprintf("test-user-%d", acctest.RandInt())
    55  	name2 := fmt.Sprintf("test-user-%d", acctest.RandInt())
    56  	path1 := "/"
    57  	path2 := "/path2/"
    58  
    59  	resource.Test(t, resource.TestCase{
    60  		PreCheck:     func() { testAccPreCheck(t) },
    61  		Providers:    testAccProviders,
    62  		CheckDestroy: testAccCheckAWSUserDestroy,
    63  		Steps: []resource.TestStep{
    64  			resource.TestStep{
    65  				Config: testAccAWSUserConfig(name1, path1),
    66  				Check: resource.ComposeTestCheckFunc(
    67  					testAccCheckAWSUserExists("aws_iam_user.user", &conf),
    68  					testAccCheckAWSUserAttributes(&conf, name1, "/"),
    69  				),
    70  			},
    71  			resource.TestStep{
    72  				Config: testAccAWSUserConfig(name2, path2),
    73  				Check: resource.ComposeTestCheckFunc(
    74  					testAccCheckAWSUserExists("aws_iam_user.user", &conf),
    75  					testAccCheckAWSUserAttributes(&conf, name2, "/path2/"),
    76  				),
    77  			},
    78  		},
    79  	})
    80  }
    81  
    82  func testAccCheckAWSUserDestroy(s *terraform.State) error {
    83  	iamconn := testAccProvider.Meta().(*AWSClient).iamconn
    84  
    85  	for _, rs := range s.RootModule().Resources {
    86  		if rs.Type != "aws_iam_user" {
    87  			continue
    88  		}
    89  
    90  		// Try to get user
    91  		_, err := iamconn.GetUser(&iam.GetUserInput{
    92  			UserName: aws.String(rs.Primary.ID),
    93  		})
    94  		if err == nil {
    95  			return fmt.Errorf("still exist.")
    96  		}
    97  
    98  		// Verify the error is what we want
    99  		ec2err, ok := err.(awserr.Error)
   100  		if !ok {
   101  			return err
   102  		}
   103  		if ec2err.Code() != "NoSuchEntity" {
   104  			return err
   105  		}
   106  	}
   107  
   108  	return nil
   109  }
   110  
   111  func testAccCheckAWSUserExists(n string, res *iam.GetUserOutput) resource.TestCheckFunc {
   112  	return func(s *terraform.State) error {
   113  		rs, ok := s.RootModule().Resources[n]
   114  		if !ok {
   115  			return fmt.Errorf("Not found: %s", n)
   116  		}
   117  
   118  		if rs.Primary.ID == "" {
   119  			return fmt.Errorf("No User name is set")
   120  		}
   121  
   122  		iamconn := testAccProvider.Meta().(*AWSClient).iamconn
   123  
   124  		resp, err := iamconn.GetUser(&iam.GetUserInput{
   125  			UserName: aws.String(rs.Primary.ID),
   126  		})
   127  		if err != nil {
   128  			return err
   129  		}
   130  
   131  		*res = *resp
   132  
   133  		return nil
   134  	}
   135  }
   136  
   137  func testAccCheckAWSUserAttributes(user *iam.GetUserOutput, name string, path string) resource.TestCheckFunc {
   138  	return func(s *terraform.State) error {
   139  		if *user.User.UserName != name {
   140  			return fmt.Errorf("Bad name: %s", *user.User.UserName)
   141  		}
   142  
   143  		if *user.User.Path != path {
   144  			return fmt.Errorf("Bad path: %s", *user.User.Path)
   145  		}
   146  
   147  		return nil
   148  	}
   149  }
   150  
   151  func testAccAWSUserConfig(r, p string) string {
   152  	return fmt.Sprintf(`
   153  resource "aws_iam_user" "user" {
   154  	name = "%s"
   155  	path = "%s"
   156  }`, r, p)
   157  }