Refresh

This website github.com/mdb/terraputs/commit/2840c7445bbf1e7110dc3f8a030e48c28ebdf961 is currently offline. Cloudflare's Always Online™ shows a snapshot of this web page from the Internet Archive's Wayback Machine. To check for the live version, click Refresh.

Skip to content

Commit

Permalink
Merge pull request #8 from mdb/protect-against-no-outputs
Browse files Browse the repository at this point in the history
Protect against error scenarios
  • Loading branch information
mdb authored Aug 23, 2021
2 parents 7c7d5e4 + 2a5ff58 commit 2840c74
Show file tree
Hide file tree
Showing 15 changed files with 156 additions and 17 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
dist
testdata/terraform.tfstate.backup
testdata/**/terraform.tfstate.backup
testdata/**/.terraform*
testdata/**/greeting.txt
28 changes: 24 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
VERSION = 0.0.4
VERSION = 0.0.5
SOURCE = ./...

.PHONY: help \
build \
vet \
test-fmt \
test \
testdata
testdata \
clean

.DEFAULT_GOAL := build

Expand All @@ -20,6 +21,7 @@ help:
# check-tag: check if a $(VERSION) git tag already exists
# tag: create a $(VERSION) git tag
# release: build and publish a terraputs GitHub release
# clean: remove testdata fixtures and compiled artifacts

tools:
echo "Installing tools from tools.go"
Expand All @@ -40,18 +42,24 @@ test-fmt:
test: vet test-fmt
go test -cover $(SOURCE) -count=1

testdata:
define generate-testdata
docker run \
--interactive \
--tty \
--volume $(shell pwd):/src \
--workdir /src/testdata \
--workdir /src/testdata/$(1) \
--entrypoint /bin/sh \
hashicorp/terraform \
-c \
"terraform init && \
terraform apply -auto-approve && \
terraform show -json > show.json"
endef

testdata:
$(call generate-testdata,basic)
$(call generate-testdata,nooutputs)
$(call generate-testdata,emptyconfig)

check-tag:
./scripts/ensure_unique_version.sh "$(VERSION)"
Expand All @@ -71,3 +79,15 @@ demo:
--out demo.svg \
--window \
--no-cursor

define clean-testdata
rm -rf testdata/$(1)/.terraform*
rm -rf testdata/$(1)/terraform.tfstate.backup
rm -rf testdata/$(1)/greeting.txt
endef

clean:
$(call clean-testdata,basic)
$(call clean-testdata,nooutputs)
$(call clean-testdata,emptyconfig)
rm -rf dist
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,13 @@ func main() {
panic(err)
}

outputs := map[string]*tfjson.StateOutput{}
if state.Values != nil {
outputs = state.Values.Outputs
}

err = t.Execute(os.Stdout, data{
Outputs: state.Values.Outputs,
Outputs: outputs,
Heading: *heading,
})
if err != nil {
Expand Down
31 changes: 25 additions & 6 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,17 @@ func TestVersionArg(t *testing.T) {
}
}

// these should all be ok and functionally equivalent:
// These should all be ok and functionally equivalent:
// terraputs -state "$(cat stateFile)"
// terraputs < stateFile
// cat stateFile | terraputs

func TestTerraputs(t *testing.T) {
tests := []struct {
command string
shouldError bool
expectedOutput string
}{{
command: `./terraputs -state $(cat testdata/show.json)`,
command: `./terraputs -state $(cat testdata/basic/show.json)`,
expectedOutput: `# Outputs
Terraform state outputs.
Expand All @@ -113,7 +112,7 @@ Terraform state outputs.
`,
}, {
command: `./terraputs < testdata/show.json`,
command: `./terraputs < testdata/basic/show.json`,
expectedOutput: `# Outputs
Terraform state outputs.
Expand All @@ -128,7 +127,7 @@ Terraform state outputs.
`,
}, {
command: `cat testdata/show.json | ./terraputs`,
command: `cat testdata/basic/show.json | ./terraputs`,
expectedOutput: `# Outputs
Terraform state outputs.
Expand All @@ -143,7 +142,7 @@ Terraform state outputs.
`,
}, {
command: `./terraputs -state $(cat testdata/show.json) -heading foo`,
command: `./terraputs -state $(cat testdata/basic/show.json) -heading foo`,
expectedOutput: `# foo
Terraform state outputs.
Expand All @@ -156,6 +155,26 @@ Terraform state outputs.
| a_sensitive_value | sensitive; redacted | string
| a_string | foo | string
`,
}, {
command: `./terraputs -state $(cat testdata/nooutputs/show.json) -heading foo`,
expectedOutput: `# foo
Terraform state outputs.
| Output | Value | Type
| --- | --- | --- |
`,
}, {
command: `./terraputs -state $(cat testdata/emptyconfig/show.json) -heading foo`,
expectedOutput: `# foo
Terraform state outputs.
| Output | Value | Type
| --- | --- | --- |
`,
}}

Expand Down
6 changes: 3 additions & 3 deletions testdata/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# testdata

The `testdata/terraform.tfstate` and `testdata/show.json` files in this directory are generated
from the `testdata/main.tf` Terraform configuration via `make tfstate`. The `testdata/show.json`
file is used by `terraputs`'s tests.
The `testdata/**/terraform.tfstate` and `testdata/**/show.json` files in this directory are generated
from the `testdata/**/main.tf` Terraform configuration via `make tfstate`. The `testdata/**/show.json`
files are used by `terraputs`'s tests.
File renamed without changes.
2 changes: 1 addition & 1 deletion testdata/show.json → testdata/basic/show.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"format_version":"0.2","terraform_version":"1.0.1","values":{"outputs":{"a_basic_map":{"sensitive":false,"value":{"foo":"bar","number":42}},"a_list":{"sensitive":false,"value":["foo","bar"]},"a_nested_map":{"sensitive":false,"value":{"baz":{"bar":"baz","id":"123"},"foo":"bar","number":42}},"a_sensitive_value":{"sensitive":true,"value":"foo"},"a_string":{"sensitive":false,"value":"foo"}},"root_module":{}}}
{"format_version":"0.2","terraform_version":"1.0.5","values":{"outputs":{"a_basic_map":{"sensitive":false,"value":{"foo":"bar","number":42}},"a_list":{"sensitive":false,"value":["foo","bar"]},"a_nested_map":{"sensitive":false,"value":{"baz":{"bar":"baz","id":"123"},"foo":"bar","number":42}},"a_sensitive_value":{"sensitive":true,"value":"foo"},"a_string":{"sensitive":false,"value":"foo"}},"root_module":{}}}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"version": 4,
"terraform_version": "1.0.1",
"terraform_version": "1.0.5",
"serial": 3,
"lineage": "4e315b50-123b-2e86-8d75-d3b96d0a75bf",
"outputs": {
Expand Down
7 changes: 7 additions & 0 deletions testdata/emptyconfig/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This is a sample Terraform configuration
# used in terraputs tests.
#
# 'make testdata' offers a convenience make
# target to generate terraform.tfstate and
# show.json state state files from this
# configuration.
1 change: 1 addition & 0 deletions testdata/emptyconfig/show.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"format_version":"0.2"}
8 changes: 8 additions & 0 deletions testdata/emptyconfig/terraform.tfstate
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"version": 4,
"terraform_version": "1.0.5",
"serial": 1,
"lineage": "b270cd61-d9a3-aebf-af74-45d022a1efb0",
"outputs": {},
"resources": []
}
18 changes: 18 additions & 0 deletions testdata/nooutputs/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# This is a sample Terraform configuration
# used in terraputs tests.
#
# 'make testdata' offers a convenience make
# target to generate terraform.tfstate and
# show.json state state files from this
# configuration.

data "template_file" "greeting" {
template = <<-EOT
${var.greeting}
EOT
}

resource "local_file" "greeting" {
content = data.template_file.greeting.rendered
filename = "${path.module}/greeting.txt"
}
1 change: 1 addition & 0 deletions testdata/nooutputs/show.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"format_version":"0.2","terraform_version":"1.0.5","values":{"root_module":{"resources":[{"address":"data.template_file.greeting","mode":"data","type":"template_file","name":"greeting","provider_name":"registry.terraform.io/hashicorp/template","schema_version":0,"values":{"filename":null,"id":"5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03","rendered":"hello\n","template":"hello\n","vars":null},"sensitive_values":{}},{"address":"local_file.greeting","mode":"managed","type":"local_file","name":"greeting","provider_name":"registry.terraform.io/hashicorp/local","schema_version":0,"values":{"content":"hello\n","content_base64":null,"directory_permission":"0777","file_permission":"0777","filename":"./greeting.txt","id":"f572d396fae9206628714fb2ce00f72e94f2258f","sensitive_content":null,"source":null},"sensitive_values":{},"depends_on":["data.template_file.greeting"]}]}}}
54 changes: 54 additions & 0 deletions testdata/nooutputs/terraform.tfstate
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"version": 4,
"terraform_version": "1.0.5",
"serial": 12,
"lineage": "47d01279-eabc-fe8a-3aac-7135f7545e4c",
"outputs": {},
"resources": [
{
"mode": "data",
"type": "template_file",
"name": "greeting",
"provider": "provider[\"registry.terraform.io/hashicorp/template\"]",
"instances": [
{
"schema_version": 0,
"attributes": {
"filename": null,
"id": "5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03",
"rendered": "hello\n",
"template": "hello\n",
"vars": null
},
"sensitive_attributes": []
}
]
},
{
"mode": "managed",
"type": "local_file",
"name": "greeting",
"provider": "provider[\"registry.terraform.io/hashicorp/local\"]",
"instances": [
{
"schema_version": 0,
"attributes": {
"content": "hello\n",
"content_base64": null,
"directory_permission": "0777",
"file_permission": "0777",
"filename": "./greeting.txt",
"id": "f572d396fae9206628714fb2ce00f72e94f2258f",
"sensitive_content": null,
"source": null
},
"sensitive_attributes": [],
"private": "bnVsbA==",
"dependencies": [
"data.template_file.greeting"
]
}
]
}
]
}
4 changes: 4 additions & 0 deletions testdata/nooutputs/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
variable greeting {
description = "the greeting to print"
default = "hello"
}

0 comments on commit 2840c74

Please sign in to comment.