-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhostsini_test.go
137 lines (123 loc) · 3.48 KB
/
hostsini_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package ansible
import (
"errors"
"fmt"
"os"
"testing"
)
var testInventory = &Inventory{Hosts: map[string]*Host{
"customer.host": {
Name: "customer.host",
Group: "customers",
Host: "321.321.321.321",
Port: 22,
User: "root",
PrivateKeys: []string{"/from/group/vars"},
},
"setup.host": {
Name: "setup.host",
Group: "setup",
Host: "123.123.123.123",
Port: 123,
User: "setup-user",
BecomePass: "a(*sEtuP1pass_\"5wOrD",
PrivateKeys: []string{"/from/group/vars"},
OrderedAt: "2012-01-01_15:04:05",
},
"todo.host": {
Name: "todo.host",
Group: "setup",
Host: "TODO",
Port: 22,
User: "root",
PrivateKeys: []string{"/from/group/vars"},
},
}}
func TestNewHostsFile(t *testing.T) {
expected := testInventory
actual, err := NewHostsFile("testdata/hosts", nil)
if err != nil {
t.Error("file parsing failed", err)
}
if actual == nil { //nolint:staticcheck // yes, that's the point
t.Error("parsed hosts file is nil")
return
}
t.Log("actual:")
for k, v := range actual.Hosts { //nolint:staticcheck // that's intended
t.Log(k, "=", fmt.Sprintf("%+v", v))
}
if len(expected.Hosts) != len(actual.Hosts) { //nolint:staticcheck // that's intended
t.Error("length is not expected. Expected:", len(expected.Hosts), "actual:", len(actual.Hosts))
}
a := actual.Hosts
for name, host := range expected.Hosts {
equal(t, host.Name, a[name].Name)
equal(t, host.Group, a[name].Group)
equal(t, host.Host, a[name].Host)
equal(t, host.User, a[name].User)
equal(t, host.Port, a[name].Port)
equal(t, host.SSHPass, a[name].SSHPass)
equal(t, host.BecomePass, a[name].BecomePass)
for i, key := range host.PrivateKeys {
equal(t, key, a[name].PrivateKeys[i])
}
}
}
func TestNewHostsFile_NoExists(t *testing.T) {
_, err := NewHostsFile("testdata/not-exists", nil)
if err == nil {
t.Error("error is nil")
}
if !errors.Is(err, os.ErrNotExist) {
t.Error("error is not os.ErrNotExists, it is", err)
}
}
func TestMatch(t *testing.T) {
inv := testInventory
expected := inv.Hosts["setup.host"]
actual := inv.Match("setup.host")
if actual == nil { //nolint:staticcheck // yes, that's the point
t.Error("matched host is nil")
return
}
if expected.Name != actual.Name { //nolint:staticcheck // that's intended
t.Error("name is invalid. Expected: '" + expected.Name + "', actual: '" + actual.Name + "'")
}
}
func TestMerge(t *testing.T) {
expected := testInventory
actual := &Inventory{}
actual.Merge(testInventory)
t.Log("actual:")
for k, v := range actual.Hosts {
t.Log(k, "=", fmt.Sprintf("%+v", v))
}
if len(expected.Hosts) != len(actual.Hosts) {
t.Error("length is not expected. Expected:", len(expected.Hosts), "actual:", len(actual.Hosts))
}
a := actual.Hosts
for name, host := range expected.Hosts {
equal(t, host.Name, a[name].Name)
equal(t, host.Group, a[name].Group)
equal(t, host.Host, a[name].Host)
equal(t, host.User, a[name].User)
equal(t, host.Port, a[name].Port)
equal(t, host.SSHPass, a[name].SSHPass)
equal(t, host.BecomePass, a[name].BecomePass)
for i, key := range host.PrivateKeys {
equal(t, key, a[name].PrivateKeys[i])
}
}
}
func TestHasTODOs(t *testing.T) {
if !testInventory.Hosts["todo.host"].HasTODOs() {
t.Error("host has no TODOs")
}
}
func equal[T comparable](t *testing.T, expected, actual T) {
t.Helper()
if expected != actual {
t.Error("value is not equal. Expected:", expected, ", actual:", actual)
}
}