-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from mineiros-io/add-unit-tests
Add unit tests
- Loading branch information
Showing
11 changed files
with
678 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module github.com/mineiros-io/terraform-aws-iam-user | ||
|
||
go 1.14 | ||
|
||
require ( | ||
github.com/gruntwork-io/terratest v0.28.5 | ||
github.com/stretchr/testify v1.4.0 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/gruntwork-io/terratest/modules/random" | ||
|
||
"github.com/gruntwork-io/terratest/modules/aws" | ||
"github.com/gruntwork-io/terratest/modules/terraform" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// TestCreateBasicIamUsers | ||
// tests the creation of a list of IAM Users with some attached default IAM Policies | ||
func TestCreateBasicIamUsers(t *testing.T) { | ||
t.Parallel() | ||
|
||
randomAwsRegion := aws.GetRandomRegion(t, nil, nil) | ||
|
||
expectedUserNames := []string{ | ||
fmt.Sprintf("first.testuser-%s", random.UniqueId()), | ||
fmt.Sprintf("second.testuser-%s", random.UniqueId()), | ||
} | ||
|
||
exptectedIamPolicyARNs := []string{ | ||
"arn:aws:iam::aws:policy/ReadOnlyAccess", | ||
"arn:aws:iam::aws:policy/job-function/Billing", | ||
} | ||
|
||
terraformOptions := &terraform.Options{ | ||
// The path to where your Terraform code is located | ||
TerraformDir: "./create-basic-iam-users", | ||
Vars: map[string]interface{}{ | ||
"aws_region": randomAwsRegion, | ||
"names": expectedUserNames, | ||
"policy_arns": exptectedIamPolicyARNs, | ||
}, | ||
Upgrade: true, | ||
} | ||
|
||
// At the end of the test, run `terraform destroy` to clean up any resources that were created | ||
defer terraform.Destroy(t, terraformOptions) | ||
|
||
// This will run `terraform init` and `terraform apply` and fail the test if there are any errors | ||
terraform.InitAndApply(t, terraformOptions) | ||
|
||
outputs := terraform.OutputAll(t, terraformOptions) | ||
createdUsers, _ := outputs["all"].(map[string]interface{})["users"].(map[string]interface{}) | ||
|
||
// Validate that the qty of creates users matches the desired qty | ||
assert.Equal(t, len(expectedUserNames), len(createdUsers), "Expected %d users to be created. Got %d instead.", len(expectedUserNames), len(createdUsers)) | ||
|
||
// Validate that the users with the expected usernames exist | ||
for _, name := range expectedUserNames { | ||
assert.Contains(t, createdUsers, name, "Expected username %s not found.", name) | ||
} | ||
|
||
// Validate that quantity of user_policy_attachment's located in the outputs | ||
userPolicyAttachments := outputs["all"].(map[string]interface{})["user_policy_attachment"].([]interface{}) | ||
|
||
// If we attach two policies to two users, we should be able to locate four attachments in the outputs | ||
assert.Equal(t, (len(exptectedIamPolicyARNs) * len(expectedUserNames)), len(userPolicyAttachments), "Exptected %s user policy attachment. Found %d instead", len(expectedUserNames), len(userPolicyAttachments)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
# TEST MODULE THAT IS USED BY THE UNIT TESTS | ||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
provider "aws" { | ||
version = "~> 2.0" | ||
region = "eu-west-1" | ||
} | ||
|
||
module "iam-users" { | ||
source = "../.." | ||
|
||
names = var.names | ||
policy_arns = var.policy_arns | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# ------------------------------------------------------------------------------ | ||
# OUTPUT CALCULATED VARIABLES (prefer full objects) | ||
# ------------------------------------------------------------------------------ | ||
|
||
# ------------------------------------------------------------------------------ | ||
# OUTPUT ALL RESOURCES AS FULL OBJECTS | ||
# ------------------------------------------------------------------------------ | ||
|
||
output "all" { | ||
description = "All outputs exposed by the module." | ||
value = module.iam-users | ||
} | ||
|
||
# ------------------------------------------------------------------------------ | ||
# OUTPUT ALL INPUT VARIABLES | ||
# ------------------------------------------------------------------------------ | ||
|
||
# ------------------------------------------------------------------------------ | ||
# OUTPUT MODULE CONFIGURATION | ||
# ------------------------------------------------------------------------------ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
# TEST MODULE THAT IS USED BY THE UNIT TESTS | ||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
variable "aws_region" { | ||
description = "The AWS region to deploy the example in." | ||
type = string | ||
default = "us-east-1" | ||
} | ||
|
||
variable "names" { | ||
description = "A list of names of IAM Users to create." | ||
type = set(string) | ||
default = [ | ||
"testuser", | ||
"another.testuser" | ||
] | ||
} | ||
|
||
variable "policy_arns" { | ||
description = "A list of IAM Policy ARNs that will be attached to the created IAM Users." | ||
type = set(string) | ||
default = [ | ||
"arn:aws:iam::aws:policy/ReadOnlyAccess", | ||
"arn:aws:iam::aws:policy/job-function/Billing", | ||
] | ||
} |