forked from wavefrontHQ/go-wavefront-management-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
target_test.go
105 lines (90 loc) · 2.34 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
package wavefront
import (
"io"
"io/ioutil"
"net/http"
"net/url"
"testing"
asserts "github.com/stretchr/testify/assert"
)
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) {
return testDo(m.T, req, "./fixtures/list-targets.json", "POST", &SearchParams{})
}
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)
}
assertEqual(t, 2, len(targets))
assertEqual(t, "WEBHOOK", targets[0].Method)
}
func (m *MockCrudTargetClient) Do(req *http.Request) (io.ReadCloser, error) {
return testDo(m.T, req, "./fixtures/create-target-response.json", m.method, &Target{})
}
func TestTargets_CreateUpdateDeleteTarget(t *testing.T) {
assert := asserts.New(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",
Routes: []AlertRoute{
{
Method: "WEBHOOK",
Target: "https://hooks.slack.com/services/test/me",
Filter: "env prod*",
},
},
ContentType: "application/json",
CustomHeaders: map[string]string{
"Testing": "true",
},
Triggers: []string{"ALERT_OPENED", "ALERT_RESOLVED"},
Template: string(tmpl),
}
// Update should fail as no ID is set
assert.Error(a.Update(&target))
a.client.(*MockCrudTargetClient).method = "POST"
assert.NoError(a.Create(&target))
assert.Equal("7", *target.ID)
a.client.(*MockCrudTargetClient).method = "PUT"
assert.NoError(a.Update(&target))
a.client.(*MockCrudTargetClient).method = "DELETE"
assert.NoError(a.Delete(&target))
assert.Nil(target.ID)
}