forked from go-chef/chef
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_test.go
184 lines (153 loc) · 4.41 KB
/
search_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package chef
import (
"fmt"
"io/ioutil"
"net/http"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
const partialSearchResponseFile_1 = "test/partial_search_test_1.json"
const partialSearchResponseFile_2 = "test/partial_search_test_2.json"
func TestSearch_Get(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/search", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, `{
"node": "http://localhost:4000/search/node",
"role": "http://localhost:4000/search/role",
"client": "http://localhost:4000/search/client",
"users": "http://localhost:4000/search/users"
}`)
})
indexes, err := client.Search.Indexes()
if err != nil {
t.Errorf("Search.Get returned error: %+v", err)
}
wantedIdx := map[string]string{
"node": "http://localhost:4000/search/node",
"role": "http://localhost:4000/search/role",
"client": "http://localhost:4000/search/client",
"users": "http://localhost:4000/search/users",
}
if !reflect.DeepEqual(indexes, wantedIdx) {
t.Errorf("Search.Get returned %+v, want %+v", indexes, wantedIdx)
}
}
func TestSearch_ExecDo(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/search/nodes", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, `{
"total": 1,
"start": 0,
"rows": [
{
"overrides": {"hardware_type": "laptop"},
"name": "latte",
"chef_type": "node",
"json_class": "Chef::Node",
"attributes": {"hardware_type": "laptop"},
"run_list": ["recipe[unicorn]"],
"defaults": {}
}
]
}`)
})
// test the fail case
_, err := client.Search.NewQuery("foo", "failsauce")
if err == nil {
t.Error("Bad query wasn't caught")
}
// test the fail case
_, err = client.Search.Exec("foo", "failsauce")
if err == nil {
t.Error("Bad query wasn't caught")
}
// test the positive case
query, err := client.Search.NewQuery("nodes", "name:latte")
if err != nil {
t.Error("failed to create query")
}
// for now we aren't testing the result..
_, err = query.Do(client)
if err != nil {
t.Errorf("Search.Exec failed err: %+v", err)
}
_, err = client.Search.Exec("nodes", "name:latte")
if err != nil {
t.Errorf("Search.Exec failed err: %+v", err)
}
}
func TestSearch_PartialExec(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/search/node", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, `{
"total": 1,
"start": 0,
"rows": [
{
"overrides": {"hardware_type": "laptop"},
"name": "latte",
"chef_type": "node",
"json_class": "Chef::Node",
"policy_group": "testing",
"policy_name": "grafana",
"policy_revision": "123xyz00009999",
"attributes": {"hardware_type": "laptop"},
"run_list": ["recipe[unicorn]"],
"defaults": {}
}
]
}`)
})
query := map[string]interface{}{
"name": []string{"name"},
"policy_group": []string{"policy_group"},
"policy_name": []string{"policy_name"},
"policy_revision": []string{"policy_revision"},
}
pres, err := client.Search.PartialExec("node", "*.*", query)
if err != nil {
t.Errorf("Search.PartialExec failed err: %+v", err)
}
assert.Equal(t, "grafana", pres.Rows[0].(map[string]interface{})["policy_name"])
}
func TestSearch_PartialExecMultipleCalls(t *testing.T) {
setup()
defer teardown()
searchResponseOne, err := ioutil.ReadFile(partialSearchResponseFile_1)
if err != nil {
t.Error(err)
}
searchResponseTwo, err := ioutil.ReadFile(partialSearchResponseFile_2)
if err != nil {
t.Error(err)
}
mux.HandleFunc("/search/node", func(w http.ResponseWriter, r *http.Request) {
start, ok := r.URL.Query()["start"]
if !ok || len(start[0]) < 1 {
fmt.Println("Url Param 'start' is missing")
return
}
if start[0] == "0" {
fmt.Fprintf(w, string(searchResponseOne))
} else {
fmt.Fprintf(w, string(searchResponseTwo))
}
})
query := map[string]interface{}{
"name": []string{"name"},
"policy_group": []string{"policy_group"},
"policy_name": []string{"policy_name"},
"policy_revision": []string{"policy_revision"},
}
pres, err := client.Search.PartialExec("node", "*.*", query)
if err != nil {
t.Errorf("Search.PartialExec failed err: %+v", err)
}
assert.Equal(t, 1185, len(pres.Rows))
assert.Equal(t, "node1", pres.Rows[0].(map[string]interface{})["name"])
assert.Equal(t, "node1185", pres.Rows[len(pres.Rows)-1].(map[string]interface{})["name"])
}