Skip to content

Commit

Permalink
Updating terratest, documentation, and a couple other tweaks (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
danjbh committed Oct 14, 2020
1 parent 6d60cb6 commit e238dc0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 56 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ Available targets:
|------|-------------|------|---------|:--------:|
| additional\_tag\_map | Additional tags for appending to tags\_as\_list\_of\_maps. Not added to `tags`. | `map(string)` | `{}` | no |
| agent\_cli\_args | Extra command line arguments to pass to tfc-agent | `list` | `[]` | no |
| agent\_envs | A map of any extra environment variables to pass to the TFC agent | `map` | `{}` | no |
| agent\_image | Name and tag of Terraform Cloud Agent docker image | `string` | `"hashicorp/tfc-agent:latest"` | no |
| attributes | Additional attributes (e.g. `1`) | `list(string)` | `[]` | no |
| context | Single object for setting entire context at once.<br>See description of individual variables for details.<br>Leave string and numeric variables as `null` to use default value.<br>Individual variable settings (non-null) override settings in context object,<br>except for attributes, tags, and additional\_tag\_map, which are merged. | <pre>object({<br> enabled = bool<br> namespace = string<br> environment = string<br> stage = string<br> name = string<br> delimiter = string<br> attributes = list(string)<br> tags = map(string)<br> additional_tag_map = map(string)<br> regex_replace_chars = string<br> label_order = list(string)<br> id_length_limit = number<br> })</pre> | <pre>{<br> "additional_tag_map": {},<br> "attributes": [],<br> "delimiter": null,<br> "enabled": true,<br> "environment": null,<br> "id_length_limit": null,<br> "label_order": [],<br> "name": null,<br> "namespace": null,<br> "regex_replace_chars": null,<br> "stage": null,<br> "tags": {}<br>}</pre> | no |
Expand All @@ -159,19 +160,18 @@ Available targets:
| stage | Stage, e.g. 'prod', 'staging', 'dev', OR 'source', 'build', 'test', 'deploy', 'release' | `string` | `null` | no |
| tags | Additional tags (e.g. `map('BusinessUnit','XYZ')` | `map(string)` | `{}` | no |
| tfc\_address | The HTTP or HTTPS address of the Terraform Cloud API. | `string` | `"https://app.terraform.io"` | no |
| tfc\_agent\_data\_dir | The path to a directory to store all agent-related data, including<br> Terraform configurations, cached Terraform release archives, etc. It is<br> important to ensure that the given directory is backed by plentiful<br> storage. | `string` | `"~/.tfc-agent"` | no |
| tfc\_agent\_data\_dir | The path to a directory to store all agent-related data, including<br>Terraform configurations, cached Terraform release archives, etc. It is<br>important to ensure that the given directory is backed by plentiful<br>storage. | `string` | `"~/.tfc-agent"` | no |
| tfc\_agent\_disable\_update | Disable automatic core updates. | `bool` | `false` | no |
| tfc\_agent\_log\_level | The log verbosity expressed as a level string. Level options include<br> "trace", "debug", "info", "warn", and "error" | `string` | `"info"` | no |
| tfc\_agent\_single | Enable single mode. This causes the agent to handle at most one job and<br> immediately exit thereafter. Useful for running agents as ephemeral<br> containers, VMs, or other isolated contexts with a higher-level scheduler<br> or process supervisor. | `bool` | `false` | no |
| tfc\_agent\_token | The agent token to use when making requests to the Terraform Cloud API.<br> This token must be obtained from the API or UI. It is recommended to use<br> the environment variable whenever possible for configuring this setting due<br> to the sensitive nature of API tokens. | `string` | `""` | no |
| tfc\_extra\_envs | A map of any extra environment variables to pass to the TFC agent | `map` | `{}` | no |
| tfc\_agent\_log\_level | The log verbosity expressed as a level string. Level options include<br>"trace", "debug", "info", "warn", and "error" | `string` | `"info"` | no |
| tfc\_agent\_single | Enable single mode. This causes the agent to handle at most one job and<br>immediately exit thereafter. Useful for running agents as ephemeral<br>containers, VMs, or other isolated contexts with a higher-level scheduler<br>or process supervisor. | `bool` | `false` | no |
| tfc\_agent\_token | The agent token to use when making requests to the Terraform Cloud API.<br>This token must be obtained from the API or UI. It is recommended to use<br>the environment variable whenever possible for configuring this setting due<br>to the sensitive nature of API tokens. | `string` | `""` | no |

## Outputs

| Name | Description |
|------|-------------|
| namespace | n/a |
| service\_account\_name | n/a |
| namespace | Name of the Kubernetes namespace |
| service\_account\_name | Name of the Kubernetes service account |

<!-- markdownlint-restore -->

Expand Down
14 changes: 7 additions & 7 deletions docs/terraform.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
|------|-------------|------|---------|:--------:|
| additional\_tag\_map | Additional tags for appending to tags\_as\_list\_of\_maps. Not added to `tags`. | `map(string)` | `{}` | no |
| agent\_cli\_args | Extra command line arguments to pass to tfc-agent | `list` | `[]` | no |
| agent\_envs | A map of any extra environment variables to pass to the TFC agent | `map` | `{}` | no |
| agent\_image | Name and tag of Terraform Cloud Agent docker image | `string` | `"hashicorp/tfc-agent:latest"` | no |
| attributes | Additional attributes (e.g. `1`) | `list(string)` | `[]` | no |
| context | Single object for setting entire context at once.<br>See description of individual variables for details.<br>Leave string and numeric variables as `null` to use default value.<br>Individual variable settings (non-null) override settings in context object,<br>except for attributes, tags, and additional\_tag\_map, which are merged. | <pre>object({<br> enabled = bool<br> namespace = string<br> environment = string<br> stage = string<br> name = string<br> delimiter = string<br> attributes = list(string)<br> tags = map(string)<br> additional_tag_map = map(string)<br> regex_replace_chars = string<br> label_order = list(string)<br> id_length_limit = number<br> })</pre> | <pre>{<br> "additional_tag_map": {},<br> "attributes": [],<br> "delimiter": null,<br> "enabled": true,<br> "environment": null,<br> "id_length_limit": null,<br> "label_order": [],<br> "name": null,<br> "namespace": null,<br> "regex_replace_chars": null,<br> "stage": null,<br> "tags": {}<br>}</pre> | no |
Expand All @@ -42,18 +43,17 @@
| stage | Stage, e.g. 'prod', 'staging', 'dev', OR 'source', 'build', 'test', 'deploy', 'release' | `string` | `null` | no |
| tags | Additional tags (e.g. `map('BusinessUnit','XYZ')` | `map(string)` | `{}` | no |
| tfc\_address | The HTTP or HTTPS address of the Terraform Cloud API. | `string` | `"https://app.terraform.io"` | no |
| tfc\_agent\_data\_dir | The path to a directory to store all agent-related data, including<br> Terraform configurations, cached Terraform release archives, etc. It is<br> important to ensure that the given directory is backed by plentiful<br> storage. | `string` | `"~/.tfc-agent"` | no |
| tfc\_agent\_data\_dir | The path to a directory to store all agent-related data, including<br>Terraform configurations, cached Terraform release archives, etc. It is<br>important to ensure that the given directory is backed by plentiful<br>storage. | `string` | `"~/.tfc-agent"` | no |
| tfc\_agent\_disable\_update | Disable automatic core updates. | `bool` | `false` | no |
| tfc\_agent\_log\_level | The log verbosity expressed as a level string. Level options include<br> "trace", "debug", "info", "warn", and "error" | `string` | `"info"` | no |
| tfc\_agent\_single | Enable single mode. This causes the agent to handle at most one job and<br> immediately exit thereafter. Useful for running agents as ephemeral<br> containers, VMs, or other isolated contexts with a higher-level scheduler<br> or process supervisor. | `bool` | `false` | no |
| tfc\_agent\_token | The agent token to use when making requests to the Terraform Cloud API.<br> This token must be obtained from the API or UI. It is recommended to use<br> the environment variable whenever possible for configuring this setting due<br> to the sensitive nature of API tokens. | `string` | `""` | no |
| tfc\_extra\_envs | A map of any extra environment variables to pass to the TFC agent | `map` | `{}` | no |
| tfc\_agent\_log\_level | The log verbosity expressed as a level string. Level options include<br>"trace", "debug", "info", "warn", and "error" | `string` | `"info"` | no |
| tfc\_agent\_single | Enable single mode. This causes the agent to handle at most one job and<br>immediately exit thereafter. Useful for running agents as ephemeral<br>containers, VMs, or other isolated contexts with a higher-level scheduler<br>or process supervisor. | `bool` | `false` | no |
| tfc\_agent\_token | The agent token to use when making requests to the Terraform Cloud API.<br>This token must be obtained from the API or UI. It is recommended to use<br>the environment variable whenever possible for configuring this setting due<br>to the sensitive nature of API tokens. | `string` | `""` | no |

## Outputs

| Name | Description |
|------|-------------|
| namespace | n/a |
| service\_account\_name | n/a |
| namespace | Name of the Kubernetes namespace |
| service\_account\_name | Name of the Kubernetes service account |

<!-- markdownlint-restore -->
2 changes: 1 addition & 1 deletion examples/complete/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ module "tfc_agent" {
tfc_agent_token = var.tfc_agent_token

namespace_creation_enabled = true
kubernetes_namespace = "foo"
kubernetes_namespace = "tfc-agent"
}
48 changes: 7 additions & 41 deletions test/src/examples_complete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import (
"math/rand"
"strconv"
"testing"
"os"

"github.com/gruntwork-io/terratest/modules/terraform"
"github.com/stretchr/testify/assert"
)


// Test the Terraform module in examples/complete using Terratest.
func TestExamplesComplete(t *testing.T) {
t.Parallel()

randId := strconv.Itoa(rand.Intn(100000))
attributes := []string{randId}

exampleInput := "Hello, world!"

terraformOptions := &terraform.Options{
// The path to where our Terraform code is located
TerraformDir: "../../examples/complete",
Expand All @@ -28,7 +28,7 @@ func TestExamplesComplete(t *testing.T) {
// and AWS resources do not interfere with each other
Vars: map[string]interface{}{
"attributes": attributes,
"example": exampleInput,
"tfc_agent_token": os.Getenv("TFC_AGENT_TOKEN"),
},
}
// At the end of the test, run `terraform destroy` to clean up any resources that were created
Expand All @@ -38,44 +38,10 @@ func TestExamplesComplete(t *testing.T) {
terraform.InitAndApply(t, terraformOptions)

// Run `terraform output` to get the value of an output variable
id := terraform.Output(t, terraformOptions, "id")
example := terraform.Output(t, terraformOptions, "example")
random := terraform.Output(t, terraformOptions, "random")
service_account_name := terraform.Output(t, terraformOptions, "service_account_name")
namespace := terraform.Output(t, terraformOptions, "namespace")

// Verify we're getting back the outputs we expect
// Ensure we get a random number appended
assert.Equal(t, exampleInput+" "+random, example)
// Ensure we get the attribute included in the ID
assert.Equal(t, "eg-ue2-test-example-"+randId, id)

// ************************************************************************
// This steps below are unusual, not generally part of the testing
// but included here as an example of testing this specific module.
// This module has a random number that is supposed to change
// only when the example changes. So we run it again to ensure
// it does not change.

// This will run `terraform apply` a second time and fail the test if there are any errors
terraform.Apply(t, terraformOptions)

id2 := terraform.Output(t, terraformOptions, "id")
example2 := terraform.Output(t, terraformOptions, "example")
random2 := terraform.Output(t, terraformOptions, "random")

assert.Equal(t, id, id2, "Expected `id` to be stable")
assert.Equal(t, example, example2, "Expected `example` to be stable")
assert.Equal(t, random, random2, "Expected `random` to be stable")

// Then we run change the example and run it a third time and
// verify that the random number changed
newExample := "Goodbye"
terraformOptions.Vars["example"] = newExample
terraform.Apply(t, terraformOptions)

example3 := terraform.Output(t, terraformOptions, "example")
random3 := terraform.Output(t, terraformOptions, "random")

assert.NotEqual(t, random, random3, "Expected `random` to change when `example` changed")
assert.Equal(t, newExample+" "+random3, example3, "Expected `example` to use new random number")

assert.Equal(t, "eg-ue2-test-example-"+randId, service_account_name)
assert.Equal(t, "tfc-agent", namespace)
}

0 comments on commit e238dc0

Please sign in to comment.