-
Notifications
You must be signed in to change notification settings - Fork 9
/
inventory_test.go
88 lines (79 loc) · 1.7 KB
/
inventory_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
package main
import (
"strings"
"testing"
)
func TestInventory_resolveNames(t *testing.T) {
inventory := NewInventory()
for host := range inventory.resolveNames("test") {
if host.Name != "test" {
t.Error("no host")
return
}
}
}
func TestInventoryPopulate1(t *testing.T) {
inventory := NewInventory()
inventory.Populate([]string{"test"})
host, ok := <-inventory.GetHosts()
if !ok {
t.Error("no host")
return
}
if host.Name != "test" {
t.Error("bad host")
return
}
}
func TestInventoryPopulate2(t *testing.T) {
inventory := NewInventory()
inventory.Populate([]string{"test1", "test2"})
seen := make(map[string]bool)
for host := range inventory.GetHosts() {
seen[host.Name] = true
}
for _, name := range []string{"test1", "test2"} {
if !seen[name] {
t.Error("no host:", name)
return
}
}
}
func TestExpandHostGroups_NoDuplicates(t *testing.T) {
inventory := NewInventory()
inventory.Populate([]string{"test1", "test2", "test1"})
seen := make(map[string]bool)
for host := range inventory.GetHosts() {
if seen[host.Name] {
t.Error("seen twice:", host.Name)
return
}
seen[host.Name] = true
}
}
func TestReadGroups(t *testing.T) {
r := strings.NewReader("test1\ntest2\n")
seen := make(map[string]bool)
for _, name := range readGroups(r) {
seen[name] = true
}
for _, name := range []string{"test1", "test2"} {
if !seen[name] {
t.Error("no host:", name)
return
}
}
}
func TestReadGroupsComments(t *testing.T) {
r := strings.NewReader(`# a comment
test1 # another comment
test2 garbage
# test3
`)
expect := map[string]bool{"test1": true, "test2": true}
for _, name := range readGroups(r) {
if !expect[name] {
t.Error("unexpected:", name)
}
}
}