forked from spaceapegames/go-wavefront
-
Notifications
You must be signed in to change notification settings - Fork 12
/
event_test.go
123 lines (104 loc) · 2.75 KB
/
event_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
package wavefront
import (
"bytes"
"encoding/json"
"io"
"net/http"
"net/url"
"os"
"testing"
"time"
asserts "github.com/stretchr/testify/assert"
)
type MockEventClient struct {
Client
T *testing.T
}
type MockCrudEventClient struct {
Client
T *testing.T
method string
}
func (m *MockEventClient) Do(req *http.Request) (io.ReadCloser, error) {
search := &SearchParams{}
resp, err := testDo(m.T, req, "./fixtures/search-event-response.json", "POST", search)
assertEqual(m.T, int64(1498719480000), search.TimeRange.StartTime)
assertEqual(m.T, int64(1498723080000), search.TimeRange.EndTime)
return resp, err
}
func TestEvents_Find(t *testing.T) {
baseurl, _ := url.Parse("http://testing.wavefront.com")
e := &Events{
client: &MockEventClient{
Client: Client{
Config: &Config{Token: "1234-5678-9977"},
BaseURL: baseurl,
httpClient: http.DefaultClient,
debug: true,
},
T: t,
},
}
tr, _ := NewTimeRange(1498723080, LastHour)
events, err := e.Find(nil, tr)
if err != nil {
t.Fatal(err)
}
assertEqual(t, 3, len(events))
assertEqual(t, "1498664617084:Alert Fired: Service Errors", *events[0].ID)
assertEqual(t, "warn", events[0].Severity)
assertEqual(t, "alert-detail", events[0].Type)
assertEqual(t, "some details", events[0].Details)
}
func (m *MockCrudEventClient) Do(req *http.Request) (io.ReadCloser, error) {
response, err := os.ReadFile("./fixtures/create-event-response.json")
if err != nil {
m.T.Fatal(err)
}
if req.Method != m.method {
m.T.Errorf("request method expected '%s' got '%s'", m.method, req.Method)
}
if req.Body != nil {
body, _ := io.ReadAll(req.Body)
event := Event{}
err = json.Unmarshal(body, &event)
if err != nil {
m.T.Fatal(err)
}
}
return io.NopCloser(bytes.NewReader(response)), nil
}
func TestEvents_CreateUpdateDeleteEvent(t *testing.T) {
assert := asserts.New(t)
baseurl, _ := url.Parse("http://testing.wavefront.com")
e := &Events{
client: &MockCrudEventClient{
Client: Client{
Config: &Config{Token: "1234-5678-9977"},
BaseURL: baseurl,
httpClient: http.DefaultClient,
debug: true,
},
method: "PUT",
T: t,
},
}
event := Event{
Name: "test event",
StartTime: time.Now().Unix() * 1000,
Tags: []string{"mytag1"},
Severity: "warn",
}
// Update should fail because no ID is set
assert.Error(e.Update(&event))
e.client.(*MockCrudEventClient).method = "POST"
assert.NoError(e.Create(&event))
assert.Equal("1234", *event.ID)
e.client.(*MockCrudEventClient).method = "PUT"
assert.NoError(e.Update(&event))
e.client.(*MockCrudEventClient).method = "POST"
assert.NoError(e.Close(&event))
e.client.(*MockCrudEventClient).method = "DELETE"
assert.NoError(e.Delete(&event))
assert.Nil(event.ID)
}