From e238dc0f1a20cfdbc3b154e387e7c6784ded3a8d Mon Sep 17 00:00:00 2001 From: Dan Meyers <20687081+danjbh@users.noreply.github.com> Date: Tue, 13 Oct 2020 21:56:08 -0700 Subject: [PATCH] Updating terratest, documentation, and a couple other tweaks (#2) --- README.md | 14 ++++----- docs/terraform.md | 14 ++++----- examples/complete/main.tf | 2 +- test/src/examples_complete_test.go | 48 +++++------------------------- 4 files changed, 22 insertions(+), 56 deletions(-) diff --git a/README.md b/README.md index 40e138a..a138409 100644 --- a/README.md +++ b/README.md @@ -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.
See description of individual variables for details.
Leave string and numeric variables as `null` to use default value.
Individual variable settings (non-null) override settings in context object,
except for attributes, tags, and additional\_tag\_map, which are merged. |
object({
enabled = bool
namespace = string
environment = string
stage = string
name = string
delimiter = string
attributes = list(string)
tags = map(string)
additional_tag_map = map(string)
regex_replace_chars = string
label_order = list(string)
id_length_limit = number
})
|
{
"additional_tag_map": {},
"attributes": [],
"delimiter": null,
"enabled": true,
"environment": null,
"id_length_limit": null,
"label_order": [],
"name": null,
"namespace": null,
"regex_replace_chars": null,
"stage": null,
"tags": {}
}
| no | @@ -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
Terraform configurations, cached Terraform release archives, etc. It is
important to ensure that the given directory is backed by plentiful
storage. | `string` | `"~/.tfc-agent"` | no | +| tfc\_agent\_data\_dir | The path to a directory to store all agent-related data, including
Terraform configurations, cached Terraform release archives, etc. It is
important to ensure that the given directory is backed by plentiful
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
"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
immediately exit thereafter. Useful for running agents as ephemeral
containers, VMs, or other isolated contexts with a higher-level scheduler
or process supervisor. | `bool` | `false` | no | -| tfc\_agent\_token | The agent token to use when making requests to the Terraform Cloud API.
This token must be obtained from the API or UI. It is recommended to use
the environment variable whenever possible for configuring this setting due
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
"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
immediately exit thereafter. Useful for running agents as ephemeral
containers, VMs, or other isolated contexts with a higher-level scheduler
or process supervisor. | `bool` | `false` | no | +| tfc\_agent\_token | The agent token to use when making requests to the Terraform Cloud API.
This token must be obtained from the API or UI. It is recommended to use
the environment variable whenever possible for configuring this setting due
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 | diff --git a/docs/terraform.md b/docs/terraform.md index 084d947..ba3cb14 100644 --- a/docs/terraform.md +++ b/docs/terraform.md @@ -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.
See description of individual variables for details.
Leave string and numeric variables as `null` to use default value.
Individual variable settings (non-null) override settings in context object,
except for attributes, tags, and additional\_tag\_map, which are merged. |
object({
enabled = bool
namespace = string
environment = string
stage = string
name = string
delimiter = string
attributes = list(string)
tags = map(string)
additional_tag_map = map(string)
regex_replace_chars = string
label_order = list(string)
id_length_limit = number
})
|
{
"additional_tag_map": {},
"attributes": [],
"delimiter": null,
"enabled": true,
"environment": null,
"id_length_limit": null,
"label_order": [],
"name": null,
"namespace": null,
"regex_replace_chars": null,
"stage": null,
"tags": {}
}
| no | @@ -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
Terraform configurations, cached Terraform release archives, etc. It is
important to ensure that the given directory is backed by plentiful
storage. | `string` | `"~/.tfc-agent"` | no | +| tfc\_agent\_data\_dir | The path to a directory to store all agent-related data, including
Terraform configurations, cached Terraform release archives, etc. It is
important to ensure that the given directory is backed by plentiful
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
"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
immediately exit thereafter. Useful for running agents as ephemeral
containers, VMs, or other isolated contexts with a higher-level scheduler
or process supervisor. | `bool` | `false` | no | -| tfc\_agent\_token | The agent token to use when making requests to the Terraform Cloud API.
This token must be obtained from the API or UI. It is recommended to use
the environment variable whenever possible for configuring this setting due
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
"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
immediately exit thereafter. Useful for running agents as ephemeral
containers, VMs, or other isolated contexts with a higher-level scheduler
or process supervisor. | `bool` | `false` | no | +| tfc\_agent\_token | The agent token to use when making requests to the Terraform Cloud API.
This token must be obtained from the API or UI. It is recommended to use
the environment variable whenever possible for configuring this setting due
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 | diff --git a/examples/complete/main.tf b/examples/complete/main.tf index 136f805..e6420f9 100644 --- a/examples/complete/main.tf +++ b/examples/complete/main.tf @@ -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" } diff --git a/test/src/examples_complete_test.go b/test/src/examples_complete_test.go index 7805e88..2147117 100644 --- a/test/src/examples_complete_test.go +++ b/test/src/examples_complete_test.go @@ -4,11 +4,13 @@ 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() @@ -16,8 +18,6 @@ func TestExamplesComplete(t *testing.T) { 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", @@ -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 @@ -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) }