This repository was archived by the owner on Jan 16, 2026. It is now read-only.
forked from spaceapegames/go-wavefront
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathsearch_test.go
More file actions
135 lines (119 loc) · 3.08 KB
/
search_test.go
File metadata and controls
135 lines (119 loc) · 3.08 KB
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
package wavefront
import (
"bytes"
"encoding/json"
"io"
"net/http"
"net/url"
"os"
"testing"
asserts "github.com/stretchr/testify/assert"
)
type MockSearchClient struct {
Client
Response []byte
T *testing.T
isDeleted bool
}
func (m MockSearchClient) Do(req *http.Request) (io.ReadCloser, error) {
p := SearchParams{}
b, _ := io.ReadAll(req.Body)
err := json.Unmarshal(b, &p)
if err != nil {
m.T.Fatal(err)
}
// check defaults
if p.Offset != 0 || p.Limit != 100 {
m.T.Errorf("default offset and limit, expected 0, 100; got %d, %d", p.Offset, p.Limit)
}
if m.isDeleted == true && req.URL.Path != "/api/v2/search/alert/deleted" {
m.T.Errorf("deleted search path expected /api/v2/search/alert/deleted, got %s", req.URL.Path)
}
return io.NopCloser(bytes.NewReader(m.Response)), nil
}
func TestDefensiveCopy(t *testing.T) {
assert := asserts.New(t)
baseurl, _ := url.Parse("http://testing.wavefront.com")
response, err := os.ReadFile("./fixtures/search-alert-response.json")
if err != nil {
t.Fatal(err)
}
client := &MockSearchClient{
Response: response,
T: t,
Client: Client{
Config: &Config{Token: "1234-5678-9977"},
BaseURL: baseurl,
httpClient: http.DefaultClient,
},
}
sc := &SearchCondition{
Key: "id",
Value: "1234",
MatchingMethod: "EXACT",
}
searchParams := &SearchParams{
Conditions: []*SearchCondition{sc},
}
search := client.NewSearch("extlink", searchParams)
// This step is necessary because the above call injects the real client
// into 'search'. We want search to have our mock client instead
search.client = client
assert.NotSame(search.Params, searchParams)
_, err = search.Execute()
assert.NoError(err)
assert.Equal(0, searchParams.Limit)
assert.Equal(0, searchParams.Offset)
assert.Equal(0, search.Params.Limit)
assert.Equal(0, search.Params.Offset)
}
func TestSearch(t *testing.T) {
assert := asserts.New(t)
sc := &SearchCondition{
Key: "tags",
Value: "myTag",
MatchingMethod: "EXACT",
}
sp := &SearchParams{
Conditions: []*SearchCondition{sc},
}
response, err := os.ReadFile("./fixtures/search-alert-response.json")
if err != nil {
t.Fatal(err)
}
baseurl, _ := url.Parse("http://testing.wavefront.com")
s := &Search{
Params: sp,
Type: "alert",
client: &MockSearchClient{
Response: response,
Client: Client{
Config: &Config{Token: "1234-5678-9977"},
BaseURL: baseurl,
httpClient: http.DefaultClient,
debug: true,
},
T: t,
},
}
resp, err := s.Execute()
if err != nil {
t.Fatal("error executing query:", err)
}
raw, err := io.ReadAll(resp.RawResponse)
if err != nil {
t.Error(err)
}
if err := json.Unmarshal(raw, new(map[string]interface{})); err != nil {
t.Error("raw response is invalid JSON", err)
}
// check offset of next page in paginated response
if resp.NextOffset != 100 {
t.Errorf("next offset, expected 100, got %d", resp.NextOffset)
}
// check deleted path appended
s.Deleted = true
((s.client).(*MockSearchClient)).isDeleted = true
_, err = s.Execute()
assert.NoError(err)
}