forked from spaceapegames/go-wavefront
-
Notifications
You must be signed in to change notification settings - Fork 0
/
target_test.go
131 lines (113 loc) · 2.85 KB
/
target_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
package wavefront
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/url"
"testing"
)
type MockTargetClient struct {
Client
T *testing.T
}
type MockCrudTargetClient struct {
Client
method string
T *testing.T
}
func (m *MockTargetClient) Do(req *http.Request) (io.ReadCloser, error) {
response, err := ioutil.ReadFile("./fixtures/list-targets.json")
if err != nil {
m.T.Fatal(err)
}
return ioutil.NopCloser(bytes.NewReader(response)), nil
}
func TestTargets_Find(t *testing.T) {
baseurl, _ := url.Parse("http://testing.wavefront.com")
tgts := &Targets{
client: &MockTargetClient{
Client: Client{
Config: &Config{Token: "1234-5678-9977"},
BaseURL: baseurl,
httpClient: http.DefaultClient,
debug: true,
},
T: t,
},
}
targets, err := tgts.Find(nil)
if err != nil {
t.Fatal(err)
}
if len(targets) != 2 {
t.Errorf("list target response, expected 2, got %d", len(targets))
}
if targets[0].Method != "WEBHOOK" {
t.Errorf("expected target method to be WEBHOOK, got %s", targets[0].Method)
}
}
func (m *MockCrudTargetClient) Do(req *http.Request) (io.ReadCloser, error) {
response, err := ioutil.ReadFile("./fixtures/create-target-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)
target := Target{}
err = json.Unmarshal(body, &target)
if err != nil {
m.T.Fatal(err)
}
return ioutil.NopCloser(bytes.NewReader(response)), nil
}
func TestTargets_CreateUpdateDeleteTarget(t *testing.T) {
baseurl, _ := url.Parse("http://testing.wavefront.com")
a := &Targets{
client: &MockCrudTargetClient{
Client: Client{
Config: &Config{Token: "1234-5678-9977"},
BaseURL: baseurl,
httpClient: http.DefaultClient,
debug: true,
},
method: "PUT",
T: t,
},
}
tmpl, _ := ioutil.ReadFile("./target-template.tmpl")
target := Target{
Title: "test target",
Description: "testing something",
Method: "WEBHOOK",
Recipient: "https://hooks.slack.com/services/test/me",
ContentType: "application/json",
CustomHeaders: map[string]string{
"Testing": "true",
},
Triggers: []string{"ALERT_OPENED", "ALERT_RESOLVED"},
Template: string(tmpl),
}
if err := a.Update(&target); err == nil {
t.Errorf("expected target update to error with no ID")
}
a.client.(*MockCrudTargetClient).method = "POST"
a.Create(&target)
if *target.ID != "7" {
t.Errorf("target ID expected 7, got %s", *target.ID)
}
a.client.(*MockCrudTargetClient).method = "PUT"
if err := a.Update(&target); err != nil {
t.Error(err)
}
a.client.(*MockCrudTargetClient).method = "DELETE"
if err := a.Delete(&target); err != nil {
t.Error(err)
}
if target.ID != nil {
t.Error("expected target ID to be reset after deletion")
}
}