|
| 1 | +package test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/gruntwork-io/terratest/modules/random" |
| 8 | + |
| 9 | + "github.com/gruntwork-io/terratest/modules/aws" |
| 10 | + "github.com/gruntwork-io/terratest/modules/terraform" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +// TestCreateBasicIamUsers |
| 15 | +// tests the creation of a list of IAM Users with some attached default IAM Policies |
| 16 | +func TestCreateBasicIamUsers(t *testing.T) { |
| 17 | + t.Parallel() |
| 18 | + |
| 19 | + randomAwsRegion := aws.GetRandomRegion(t, nil, nil) |
| 20 | + |
| 21 | + expectedUserNames := []string{ |
| 22 | + fmt.Sprintf("first.testuser-%s", random.UniqueId()), |
| 23 | + fmt.Sprintf("second.testuser-%s", random.UniqueId()), |
| 24 | + } |
| 25 | + |
| 26 | + exptectedIamPolicyARNs := []string{ |
| 27 | + "arn:aws:iam::aws:policy/ReadOnlyAccess", |
| 28 | + "arn:aws:iam::aws:policy/job-function/Billing", |
| 29 | + } |
| 30 | + |
| 31 | + terraformOptions := &terraform.Options{ |
| 32 | + // The path to where your Terraform code is located |
| 33 | + TerraformDir: "./create-basic-iam-users", |
| 34 | + Vars: map[string]interface{}{ |
| 35 | + "aws_region": randomAwsRegion, |
| 36 | + "names": expectedUserNames, |
| 37 | + "policy_arns": exptectedIamPolicyARNs, |
| 38 | + }, |
| 39 | + Upgrade: true, |
| 40 | + } |
| 41 | + |
| 42 | + // At the end of the test, run `terraform destroy` to clean up any resources that were created |
| 43 | + defer terraform.Destroy(t, terraformOptions) |
| 44 | + |
| 45 | + // This will run `terraform init` and `terraform apply` and fail the test if there are any errors |
| 46 | + terraform.InitAndApply(t, terraformOptions) |
| 47 | + |
| 48 | + outputs := terraform.OutputAll(t, terraformOptions) |
| 49 | + createdUsers, _ := outputs["all"].(map[string]interface{})["users"].(map[string]interface{}) |
| 50 | + |
| 51 | + // Validate that the qty of creates users matches the desired qty |
| 52 | + assert.Equal(t, len(expectedUserNames), len(createdUsers), "Expected %d users to be created. Got %d instead.", len(expectedUserNames), len(createdUsers)) |
| 53 | + |
| 54 | + // Validate that the users with the expected usernames exist |
| 55 | + for _, name := range expectedUserNames { |
| 56 | + assert.Contains(t, createdUsers, name, "Expected username %s not found.", name) |
| 57 | + } |
| 58 | + |
| 59 | + // Validate that quantity of user_policy_attachment's located in the outputs |
| 60 | + userPolicyAttachments := outputs["all"].(map[string]interface{})["user_policy_attachment"].([]interface{}) |
| 61 | + |
| 62 | + // If we attach two policies to two users, we should be able to locate four attachments in the outputs |
| 63 | + assert.Equal(t, (len(exptectedIamPolicyARNs) * len(expectedUserNames)), len(userPolicyAttachments), "Exptected %s user policy attachment. Found %d instead", len(expectedUserNames), len(userPolicyAttachments)) |
| 64 | +} |
0 commit comments