Skip to content

Commit 5879a1e

Browse files
Merge pull request #12 from mineiros-io/add-unit-tests
Add unit tests
2 parents 41aaf35 + 5be413b commit 5879a1e

File tree

11 files changed

+678
-14
lines changed

11 files changed

+678
-14
lines changed

.semaphore/semaphore.yml

+2-5
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@ agent:
66
os_image: ubuntu1804
77

88
global_job_config:
9-
# secrets:
10-
# - name: TERRAFORM_AWS_TESTACCOUNT_CREDENTIALS
11-
# - name: private-ssh-key-with-iac-library-access
9+
secrets:
10+
- name: TERRAFORM_AWS_TESTACCOUNT_CREDENTIALS
1211
prologue:
1312
commands:
1413
- checkout --use-cache
15-
# - chmod 400 ~/.ssh/id_rsa_iac_library
16-
# - ssh-add ~/.ssh/id_rsa_iac_library
1714

1815
blocks:
1916
- name: "Tests"

Makefile

+2-3
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,8 @@ test/pre-commit:
5757
test/unit-tests: DOCKER_FLAGS += ${DOCKER_SSH_FLAGS}
5858
test/unit-tests: DOCKER_FLAGS += ${DOCKER_AWS_FLAGS}
5959
test/unit-tests:
60-
@echo "${YELLOW}No tests defined.${RESET}"
61-
# @echo "${YELLOW}[TEST] ${GREEN}Start Running Go Tests in Docker Container.${RESET}"
62-
# $(call go-test,./test/...)
60+
@echo "${YELLOW}[TEST] ${GREEN}Start Running Go Tests in Docker Container.${RESET}"
61+
$(call go-test,./test/...)
6362

6463
## Clean up cache and temporary files
6564
.PHONY: clean

examples/simple-users/main.tf

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
# ------------------------------------------------------------------------------
2-
# Example Setup
3-
# ------------------------------------------------------------------------------
1+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2+
# CREATE MULTIPLE IAM USERS AT ONCE
3+
# This example shows how to create multiple users at once by passing a list
4+
# of desired usernames to the module. We also attach some default IAM Policies
5+
# to the created users.
6+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
47

58
provider "aws" {
69
version = "~> 2.0"
710
region = "eu-west-1"
811
}
912

1013
# ------------------------------------------------------------------------------
11-
# Example Usage
14+
# CREATE THE IAM USERS AND ATTACH DEFAULT IAM POLICIES
1215
# ------------------------------------------------------------------------------
1316

1417
module "iam-users" {

go.mod

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/mineiros-io/terraform-aws-iam-user
2+
3+
go 1.14
4+
5+
require (
6+
github.com/gruntwork-io/terratest v0.28.5
7+
github.com/stretchr/testify v1.4.0
8+
)

go.sum

+526
Large diffs are not rendered by default.

main.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# ------------------------------------------------------------------------------
22
# AWS IAM USER
3-
# This module creates a single AWS IAM USER
3+
# This module creates a single or multiple AWS IAM USER
44
# You can attach an inline policy and/or custom/managed policies through their ARNs
55
# You can add the user to a list of groups (use module_depends_on to depend on group resources)
66
# ------------------------------------------------------------------------------
@@ -93,7 +93,7 @@ resource "aws_iam_user_policy_attachment" "policy" {
9393
]
9494
}
9595

96-
# add the user to a list of groups if groups are defined
96+
# Add the user to a list of groups if groups are defined
9797
resource "aws_iam_user_group_membership" "group" {
9898
for_each = var.module_enabled && length(var.groups) > 0 ? var.names : []
9999

outputs.tf

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ output "user_policy" {
1616
value = try(aws_iam_user_policy.policy, null)
1717
}
1818

19+
output "user_policy_attachment" {
20+
description = "The IAM User Policy Attachment objects."
21+
value = try(aws_iam_user_policy_attachment.policy, null)
22+
}
23+
1924
# ------------------------------------------------------------------------------
2025
# OUTPUT ALL INPUT VARIABLES
2126
# ------------------------------------------------------------------------------

test/basic_iam_users_test.go

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
}

test/create-basic-iam-users/main.tf

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2+
# TEST MODULE THAT IS USED BY THE UNIT TESTS
3+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4+
5+
provider "aws" {
6+
version = "~> 2.0"
7+
region = "eu-west-1"
8+
}
9+
10+
module "iam-users" {
11+
source = "../.."
12+
13+
names = var.names
14+
policy_arns = var.policy_arns
15+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# ------------------------------------------------------------------------------
2+
# OUTPUT CALCULATED VARIABLES (prefer full objects)
3+
# ------------------------------------------------------------------------------
4+
5+
# ------------------------------------------------------------------------------
6+
# OUTPUT ALL RESOURCES AS FULL OBJECTS
7+
# ------------------------------------------------------------------------------
8+
9+
output "all" {
10+
description = "All outputs exposed by the module."
11+
value = module.iam-users
12+
}
13+
14+
# ------------------------------------------------------------------------------
15+
# OUTPUT ALL INPUT VARIABLES
16+
# ------------------------------------------------------------------------------
17+
18+
# ------------------------------------------------------------------------------
19+
# OUTPUT MODULE CONFIGURATION
20+
# ------------------------------------------------------------------------------
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2+
# TEST MODULE THAT IS USED BY THE UNIT TESTS
3+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4+
5+
variable "aws_region" {
6+
description = "The AWS region to deploy the example in."
7+
type = string
8+
default = "us-east-1"
9+
}
10+
11+
variable "names" {
12+
description = "A list of names of IAM Users to create."
13+
type = set(string)
14+
default = [
15+
"testuser",
16+
"another.testuser"
17+
]
18+
}
19+
20+
variable "policy_arns" {
21+
description = "A list of IAM Policy ARNs that will be attached to the created IAM Users."
22+
type = set(string)
23+
default = [
24+
"arn:aws:iam::aws:policy/ReadOnlyAccess",
25+
"arn:aws:iam::aws:policy/job-function/Billing",
26+
]
27+
}

0 commit comments

Comments
 (0)