Skip to content

Commit

Permalink
require-executable should ignore empty list
Browse files Browse the repository at this point in the history
  • Loading branch information
yorinasub17 committed Jan 8, 2019
1 parent 47fcb05 commit 60837ee
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
11 changes: 11 additions & 0 deletions examples/require-executable/main.tf
Expand Up @@ -7,3 +7,14 @@ module "require_executables" {
required_executables = ["${var.required_executables}"]
error_message = "${var.error_message}"
}

# Conditional checking example
module "conditional_require_executables" {
# When using these modules in your own templates, you will need to use a Git URL with a ref attribute that pins you
# to a specific version of the modules, such as the following example:
# source = "git::git@github.com:gruntwork-io/package-terraform-utilities.git//modules/require-executable?ref=v1.0.8"
source = "../../modules/require-executable"

required_executables = ["${var.validate_bad_executable ? "this-executable-should-not-exist" : ""}"]
error_message = "${var.bad_executable_error_message}"
}
10 changes: 10 additions & 0 deletions examples/require-executable/variables.tf
Expand Up @@ -11,3 +11,13 @@ variable "required_executables" {
variable "error_message" {
description = "Error message to show if the required executable is not found. This is printed for each executable that was not found. The module will make the following substitutions in the string: `__EXECUTABLE_NAME__` will become the name of the executable that was not found."
}

variable "validate_bad_executable" {
description = "Whether or not to validate the existence of a bad executable."
default = false
}

variable "bad_executable_error_message" {
description = "Error message to show for bad_executable check."
default = ""
}
17 changes: 15 additions & 2 deletions modules/require-executable/README.md
Expand Up @@ -10,8 +10,21 @@ which executables were missing.
This module uses Python under the hood, so Python must be installed and available on the OS.




## Example code

See the [require-executable example](/examples/require-executable) for working sample code.


## Conditional check

Sometimes you might want to guard the check for a required executable on a condition (e.g only check if an executable
exists if a particular input flag is set). For this reason, this module will accept and noop on empty strings. For
example, suppose you want to check if `go` is installed based on the condition `validate_go`. You can achieve this with
the following terraform code:

```hcl
module "require_executables" {
source = "git::git@github.com:gruntwork-io/package-terraform-utilities.git//modules/require-executable?ref=v1.0.8"
required_executables = ["${var.validate_go ? "go" : ""}"]
}
```
4 changes: 4 additions & 0 deletions modules/require-executable/require_executable.py
Expand Up @@ -54,6 +54,10 @@ def main():
found = {}
not_found = []
for executable in required_executables:
# Ignore empty string
if not executable.strip():
continue

maybe_executable = spawn.find_executable(executable)
if not maybe_executable:
not_found.append(executable)
Expand Down
22 changes: 22 additions & 0 deletions test/require_executable_test.go
Expand Up @@ -40,3 +40,25 @@ func TestRequireExecutableFailsForMissingExecutable(t *testing.T) {
assert.Error(t, err)
assert.True(t, strings.Contains(out, randomString))
}

func TestConditionalRequireExecutable(t *testing.T) {
t.Parallel()

randomString := random.UniqueId()
terratestOptions := createBaseTerratestOptions(t, "../examples/require-executable")
terratestOptions.Vars = map[string]interface{}{
"required_executables": []string{},
"error_message": "",
"validate_bad_executable": "1",
"bad_executable_error_message": randomString,
}
defer terraform.Destroy(t, terratestOptions)

out, err := terraform.InitAndApplyE(t, terratestOptions)
assert.Error(t, err)
assert.True(t, strings.Contains(out, randomString))

terratestOptions.Vars["validate_bad_executable"] = "0"
out = terraform.InitAndApply(t, terratestOptions)
assert.False(t, strings.Contains(out, randomString))
}

0 comments on commit 60837ee

Please sign in to comment.