Skip to content

Commit 2b9baac

Browse files
committed
Add tests for TF_ADD_MODULE_PATH.
1 parent bd900f5 commit 2b9baac

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

parser_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,35 @@ import (
1010
"github.com/stretchr/testify/assert"
1111
)
1212

13+
const exampleStateFileEnvModulePath = `
14+
{
15+
"version": 1,
16+
"serial": 1,
17+
"modules": [
18+
{
19+
"path": [
20+
"root",
21+
"web"
22+
],
23+
"resources": {
24+
"libvirt_domain.fourteen": {
25+
"type": "libvirt_domain",
26+
"primary": {
27+
"id": "824c29be-2164-44c8-83e0-787705571d95",
28+
"attributes": {
29+
"name": "fourteen",
30+
"network_interface.#": "1",
31+
"network_interface.0.addresses.#": "1",
32+
"network_interface.0.addresses.0": "192.168.102.14",
33+
"network_interface.0.mac": "96:EE:4D:BD:B2:45"
34+
}
35+
}
36+
}
37+
}
38+
}
39+
]
40+
}`
41+
1342
const exampleStateFileEnvHostname = `
1443
{
1544
"version": 1,
@@ -35,6 +64,7 @@ const exampleStateFileEnvHostname = `
3564
]
3665
}`
3766

67+
3868
const expectedListOutputEnvHostname = `
3969
{
4070
"all": {
@@ -49,6 +79,21 @@ const expectedListOutputEnvHostname = `
4979
"type_libvirt_domain": ["fourteen"]
5080
}`
5181

82+
const expectedListOutputEnvModulePath = `
83+
{
84+
"all": {
85+
"hosts": [
86+
"192.168.102.14"
87+
],
88+
"vars": {
89+
}
90+
},
91+
"root.web.fourteen": ["192.168.102.14"],
92+
"root.web.fourteen.0": ["192.168.102.14"],
93+
"type_libvirt_domain": ["192.168.102.14"]
94+
}`
95+
96+
5297
const exampleStateFile = `
5398
{
5499
"version": 1,
@@ -748,6 +793,34 @@ func TestListCommandEnvHostname(t *testing.T) {
748793
assert.Equal(t, exp, act)
749794
}
750795

796+
func TestListCommandEnvModulePath(t *testing.T) {
797+
var s state
798+
r := strings.NewReader(exampleStateFileEnvModulePath)
799+
err := s.read(r)
800+
assert.NoError(t, err)
801+
802+
// Decode expectation as JSON
803+
var exp interface{}
804+
err = json.Unmarshal([]byte(expectedListOutputEnvModulePath), &exp)
805+
assert.NoError(t, err)
806+
807+
// Run the command, capture the output
808+
var stdout, stderr bytes.Buffer
809+
os.Setenv("TF_ADD_MODULE_PATH", "true")
810+
exitCode := cmdList(&stdout, &stderr, &s)
811+
os.Unsetenv("TF_ADD_MODULE_PATH")
812+
assert.Equal(t, 0, exitCode)
813+
assert.Equal(t, "", stderr.String())
814+
815+
// Decode the output to compare
816+
var act interface{}
817+
err = json.Unmarshal([]byte(stdout.String()), &act)
818+
assert.NoError(t, err)
819+
820+
assert.Equal(t, exp, act)
821+
}
822+
823+
751824
func TestHostCommand(t *testing.T) {
752825
var s state
753826
r := strings.NewReader(exampleStateFile)

0 commit comments

Comments
 (0)