-
Notifications
You must be signed in to change notification settings - Fork 65
/
search_test.go
176 lines (144 loc) · 4.52 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
package chef
import (
"encoding/json"
"fmt"
"net/http"
"os"
"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()
assert.Nil(t, err, "Search.Get returned error")
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",
}
assert.Equal(t, wantedIdx, indexes, "Search for indexes")
}
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": [
{
"url": "path1",
"data": {
"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")
assert.NotNil(t, err, "Bad query wasn't caught, NewQuery")
// test the fail case
_, err = client.Search.Exec("foo", "failsauce")
assert.NotNil(t, err, "Bad query wasn't caught, Exec")
// test the positive case
query, err := client.Search.NewQuery("nodes", "name:latte")
assert.Nil(t, err, "failed to create query")
// for now we aren't testing the result..
_, err = query.Do(client)
assert.Nil(t, err, "Search Do failed")
_, err = client.Search.Exec("nodes", "name:latte")
assert.Nil(t, err, "Search Exec failed")
}
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": [
{
"url": "path2",
"data": {
"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.PartialExecJSON("node", "*.*", query)
assert.Nil(t, err, "Search.PartialExecJSON failed")
assert.Len(t, pres.Rows, 1)
actualNode := Node{}
assert.NoError(t, json.Unmarshal(pres.Rows[0].Data, &actualNode))
assert.Equal(t, "grafana", actualNode.PolicyName)
}
func TestSearch_PartialExecMultipleCalls(t *testing.T) {
setup()
defer teardown()
searchResponseOne, err := os.ReadFile(partialSearchResponseFile_1)
assert.Nil(t, err, "Read response file 1 failed")
searchResponseTwo, err := os.ReadFile(partialSearchResponseFile_2)
assert.Nil(t, err, "Read response file 2 failed")
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.PartialExecJSON("node", "*.*", query)
assert.Nil(t, err, "Search.PartialExec failed")
assert.Len(t, pres.Rows, 12)
firstNode := Node{}
assert.NoError(t, json.Unmarshal(pres.Rows[0].Data, &firstNode))
assert.Equal(t, "node1", firstNode.Name)
lastNode := Node{}
assert.NoError(t, json.Unmarshal(pres.Rows[len(pres.Rows)-1].Data, &lastNode))
assert.Equal(t, "node12", lastNode.Name)
}