Skip to content

Commit

Permalink
Add test for TF_HOSTNAME_KEY_NAME
Browse files Browse the repository at this point in the history
  • Loading branch information
cgroschupp committed Jun 19, 2018
1 parent ba8e3b7 commit 41c40fc
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,52 @@ package main
import (
"bytes"
"encoding/json"
"os"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

const exampleStateFileEnvHostname = `
{
"version": 1,
"serial": 1,
"modules": [
{
"resources": {
"libvirt_domain.fourteen": {
"type": "libvirt_domain",
"primary": {
"id": "824c29be-2164-44c8-83e0-787705571d95",
"attributes": {
"name": "fourteen",
"network_interface.#": "1",
"network_interface.0.addresses.#": "1",
"network_interface.0.addresses.0": "192.168.102.14",
"network_interface.0.mac": "96:EE:4D:BD:B2:45"
}
}
}
}
}
]
}`

const expectedListOutputEnvHostname = `
{
"all": {
"hosts": [
"fourteen"
],
"vars": {
}
},
"fourteen": ["fourteen"],
"fourteen.0": ["fourteen"],
"type_libvirt_domain": ["fourteen"]
}`

const exampleStateFile = `
{
"version": 1,
Expand Down Expand Up @@ -643,6 +683,33 @@ func TestListCommand(t *testing.T) {
assert.Equal(t, exp, act)
}

func TestListCommandEnvHostname(t *testing.T) {
var s state
r := strings.NewReader(exampleStateFileEnvHostname)
err := s.read(r)
assert.NoError(t, err)

// Decode expectation as JSON
var exp interface{}
err = json.Unmarshal([]byte(expectedListOutputEnvHostname), &exp)
assert.NoError(t, err)

// Run the command, capture the output
var stdout, stderr bytes.Buffer
os.Setenv("TF_HOSTNAME_KEY_NAME", "name")
exitCode := cmdList(&stdout, &stderr, &s)
os.Unsetenv("TF_HOSTNAME_KEY_NAME")
assert.Equal(t, 0, exitCode)
assert.Equal(t, "", stderr.String())

// Decode the output to compare
var act interface{}
err = json.Unmarshal([]byte(stdout.String()), &act)
assert.NoError(t, err)

assert.Equal(t, exp, act)
}

func TestHostCommand(t *testing.T) {
var s state
r := strings.NewReader(exampleStateFile)
Expand Down

0 comments on commit 41c40fc

Please sign in to comment.