Skip to content

Commit

Permalink
Add unit test for hostvars
Browse files Browse the repository at this point in the history
  • Loading branch information
jtopjian committed Apr 12, 2018
1 parent f27ad05 commit 5aa4e88
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
12 changes: 12 additions & 0 deletions fixtures/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,17 @@ resource "ansible_host" "host_1" {
vars {
ansible_user = "ubuntu"
ansible_host = "1.2.3.4"
test = "host_1"
}
}

resource "ansible_host" "host_2" {
inventory_hostname = "host_2"
groups = ["group_1"]

vars {
ansible_user = "ubuntu"
ansible_host = "1.2.3.5"
test = "host_2"
}
}
26 changes: 24 additions & 2 deletions fixtures/terraform.tfstate
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,31 @@
"groups.0": "group_1",
"id": "host_1",
"inventory_hostname": "host_1",
"vars.%": "2",
"vars.%": "3",
"vars.ansible_host": "1.2.3.4",
"vars.ansible_user": "ubuntu"
"vars.ansible_user": "ubuntu",
"vars.test": "host_1"
},
"meta": {},
"tainted": false
},
"deposed": [],
"provider": "provider.ansible"
},
"ansible_host.host_2": {
"type": "ansible_host",
"depends_on": [],
"primary": {
"id": "host_2",
"attributes": {
"groups.#": "1",
"groups.0": "group_1",
"id": "host_2",
"inventory_hostname": "host_2",
"vars.%": "3",
"vars.ansible_host": "1.2.3.5",
"vars.ansible_user": "ubuntu",
"vars.test": "host_2"
},
"meta": {},
"tainted": false
Expand Down
32 changes: 28 additions & 4 deletions state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,26 @@ var expectedState = State{
"inventory_hostname": "host_1",
"groups.#": "1",
"groups.0": "group_1",
"vars.%": "2",
"vars.%": "3",
"vars.ansible_host": "1.2.3.4",
"vars.ansible_user": "ubuntu",
"vars.test": "host_1",
},
},
},
"ansible_host.host_2": Resource{
Type: "ansible_host",
Primary: Primary{
ID: "host_2",
Attributes: map[string]string{
"id": "host_2",
"inventory_hostname": "host_2",
"groups.#": "1",
"groups.0": "group_1",
"vars.%": "3",
"vars.ansible_host": "1.2.3.5",
"vars.ansible_user": "ubuntu",
"vars.test": "host_2",
},
},
},
Expand Down Expand Up @@ -56,7 +73,7 @@ var expectedState = State{

var expectedInventory = map[string]interface{}{
"group_1": map[string]interface{}{
"hosts": []string{"host_1"},
"hosts": []string{"host_1", "host_2"},
"children": []string{"group_2"},
"vars": map[string]interface{}{
"foo": "bar",
Expand All @@ -70,6 +87,12 @@ var expectedInventory = map[string]interface{}{
"host_1": map[string]interface{}{
"ansible_host": "1.2.3.4",
"ansible_user": "ubuntu",
"test": "host_1",
},
"host_2": map[string]interface{}{
"ansible_host": "1.2.3.5",
"ansible_user": "ubuntu",
"test": "host_2",
},
},
},
Expand All @@ -91,7 +114,7 @@ func TestState_basic(t *testing.T) {

assert.Equal(t, expectedGroups, actualGroups)

expectedHosts := []string{"host_1"}
expectedHosts := []string{"host_1", "host_2"}
actualHosts, err := actual.GetHostsForGroup(expectedGroups[0])
if err != nil {
t.Fatal(err)
Expand All @@ -102,9 +125,10 @@ func TestState_basic(t *testing.T) {
expectedVars := map[string]interface{}{
"ansible_host": "1.2.3.4",
"ansible_user": "ubuntu",
"test": "host_1",
}

actualVars, err := actual.GetVarsForHost(expectedHosts[0])
actualVars, err := actual.GetVarsForHost("host_1")
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 5aa4e88

Please sign in to comment.