-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathevent_test.go
147 lines (126 loc) · 3.3 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package wavefront
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/url"
"testing"
"time"
)
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) {
body, _ := ioutil.ReadAll(req.Body)
search := SearchParams{}
err := json.Unmarshal(body, &search)
if err != nil {
m.T.Fatal(err)
}
if search.TimeRange.EndTime != 1498723080000 && search.TimeRange.StartTime != 1498719480000 {
m.T.Errorf("expected time range 1498719480000 - 1498723080000, got %d - %d",
search.TimeRange.StartTime, search.TimeRange.EndTime,
)
}
response, err := ioutil.ReadFile("./fixtures/search-event-response.json")
if err != nil {
m.T.Fatal(err)
}
return ioutil.NopCloser(bytes.NewReader(response)), nil
}
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)
}
if len(events) != 3 {
t.Errorf("expected 3 events, got %d", len(events))
}
if *events[0].ID != "1498664617084:Alert Fired: Service Errors" {
t.Errorf("expected first event ID '1498664617084:Alert Fired: Service Errors', got %s", *events[0].ID)
}
if events[0].Severity != "warn" || events[0].Type != "alert-detail" || events[0].Details != "some details" {
t.Errorf("unexpected annotations on event")
}
}
func (m *MockCrudEventClient) Do(req *http.Request) (io.ReadCloser, error) {
response, err := ioutil.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)
}
body, _ := ioutil.ReadAll(req.Body)
event := Event{}
err = json.Unmarshal(body, &event)
if err != nil {
m.T.Fatal(err)
}
return ioutil.NopCloser(bytes.NewReader(response)), nil
}
func TestEvents_CreateUpdateDeleteEvent(t *testing.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",
}
if err := e.Update(&event); err == nil {
t.Errorf("expected event update to error with no ID")
}
e.client.(*MockCrudEventClient).method = "POST"
e.Create(&event)
if *event.ID != "1234" {
t.Errorf("event ID expected 1234, got %s", *event.ID)
}
e.client.(*MockCrudEventClient).method = "PUT"
if err := e.Update(&event); err != nil {
t.Error(err)
}
e.client.(*MockCrudEventClient).method = "POST"
if err := e.Close(&event); err != nil {
t.Error(err)
}
e.client.(*MockCrudEventClient).method = "DELETE"
if err := e.Delete(&event); err != nil {
t.Error(err)
}
if event.ID != nil {
t.Errorf("expected event ID to be reset after deletion")
}
}