-
Notifications
You must be signed in to change notification settings - Fork 2
/
inventory_test.go
90 lines (77 loc) · 1.99 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
89
90
package main
import (
"net/http/httptest"
"os"
"testing"
)
const (
configPath = "./config.test.yaml"
)
func setEnvironmentVars(rackhdUrl string, configPath string) error {
err := os.Setenv(RackHdApiUrlEnvVarName, rackhdUrl)
if err != nil {
return err
}
err = os.Setenv(AnsibleRackHdConfigPath, configPath)
if err != nil {
return err
}
return nil
}
func TestConfigReads(t *testing.T) {
server := httptest.NewServer(RackhdPathHandlers())
defer server.Close()
err := setEnvironmentVars(server.URL, configPath)
if err != nil {
t.Errorf("%s\n", err)
}
props := getPropsFromConfig()
if props.rackhdUrl != server.URL {
t.Errorf("\n%s \n%s", props.rackhdUrl, server.URL)
}
if len(props.groups) != 3 {
t.Errorf("\n%d \n%d", len(props.groups), 3)
}
if props.groups[1] != TagCephNode {
t.Errorf("\n%s \n%s", props.groups[2], TagCephNode)
}
if props.filterGroup != TagNew {
t.Errorf("\n%s \n%s", props.filterGroup, TagNew)
}
}
func TestHandleList(t *testing.T) {
server := httptest.NewServer(RackhdPathHandlers())
defer server.Close()
err := setEnvironmentVars(server.URL, configPath)
if err != nil {
t.Errorf("%s\n", err)
}
props := getPropsFromConfig()
output, err := handleList(props)
if err != nil {
t.Errorf("%s\n", err)
}
if value, ok := output["all"]; !ok {
t.Errorf("Expected key 'all' got %s\n", value)
}
if value, ok := output["ungrouped"]; !ok {
t.Errorf("Expected key 'ungrouped' got %s\n", value)
}
}
func TestHandleHost(t *testing.T) {
server := httptest.NewServer(RackhdPathHandlers())
defer server.Close()
const hostname = "192.168.1.130"
err := setEnvironmentVars(server.URL, configPath)
if err != nil {
t.Errorf("%s\n", err)
}
props := getPropsFromConfig()
hostvarItem, err := handleHost(hostname, props)
if hostvarItem.AnsibleSSHHostPrivate != hostname {
t.Errorf("\n%s \n%s", hostvarItem.AnsibleSSHHostPrivate, hostname)
}
if hostvarItem.AnsibleSSHHost != hostname {
t.Errorf("\n%s \n%s", hostvarItem.AnsibleSSHHost, hostname)
}
}