-
Notifications
You must be signed in to change notification settings - Fork 5
/
main_test.go
178 lines (157 loc) · 4.13 KB
/
main_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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"bytes"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
)
func TestIsValidMetricType(t *testing.T) {
var typesTest = []struct {
input string
want bool
}{
{input: "", want: true},
{input: "gauge", want: true},
{input: "rate", want: true},
{input: "count", want: true},
{input: "Foo", want: false},
}
for _, tt := range typesTest {
if got := isValidMetricType(tt.input); got != tt.want {
t.Fatalf("expected %v, got %v", tt.want, got)
}
}
}
func TestIsValidAlertType(t *testing.T) {
var typesTest = []struct {
input string
want bool
}{
{input: "", want: true},
{input: "info", want: true},
{input: "success", want: true},
{input: "warning", want: true},
{input: "error", want: true},
{input: "Foo", want: false},
}
for _, tt := range typesTest {
if got := isValidAlertType(tt.input); got != tt.want {
t.Fatalf("expected %v, got %v", tt.want, got)
}
}
}
func TestIsValidPriority(t *testing.T) {
var typesTest = []struct {
input string
want bool
}{
{input: "", want: true},
{input: "low", want: true},
{input: "normal", want: true},
{input: "Foo", want: false},
}
for _, tt := range typesTest {
if got := isValidPriority(tt.input); got != tt.want {
t.Fatalf("expected %v, got %v", tt.want, got)
}
}
}
func TestIsValidAggregationKey(t *testing.T) {
var typesTest = []struct {
input string
want bool
}{
{input: "", want: true},
{input: "this_is_a_good_key", want: true},
{input: strings.Repeat("a", 101), want: false},
}
for _, tt := range typesTest {
if got := isValidAggregationKey(tt.input); got != tt.want {
t.Fatalf("expected %v, got %v", tt.want, got)
}
}
}
func TestPrintVersion(t *testing.T) {
buf := new(bytes.Buffer)
log.SetOutput(buf)
log.SetFlags(0)
printVersion()
want := "Drone-Datadog Plugin version: snapshot - git commit: -"
if got := strings.TrimSpace(buf.String()); got != want {
t.Fatalf("expected '%v', got '%v'", want, got)
}
buf.Reset()
BuildCommit = "abdcdef"
BuildTag = "v0.4.2"
printVersion()
want = "Drone-Datadog Plugin version: v0.4.2 - git commit: abdcdef"
if got := strings.TrimSpace(buf.String()); got != want {
t.Fatalf("expected '%v', got '%v'", want, got)
}
}
func TestParseConfig(t *testing.T) {
// no dry run and no api key should raise an error
os.Setenv("PLUGIN_DRY_RUN", "false")
if _, err := parseConfig(); err == nil {
t.Fatalf("parseConfig should raise an error")
}
want := "123456"
os.Setenv("PLUGIN_API_KEY", want)
cfg, err := parseConfig()
if err != nil {
t.Fatal("expected no error")
}
if cfg.APIKey != want {
t.Fatalf("expected '%v', got '%v'", want, cfg.APIKey)
}
}
func TestCustomMarshalling(t *testing.T) {
m := Metric{
Name: "foo.bar.baz",
Type: "count",
Value: 1.0,
Host: "127.0.0.1",
Tags: []string{"foo:bar", "foo:baz"},
}
// trim the part containing the timestamp
want := `{"metric":"foo.bar.baz","type":"count","host":"127.0.0.1","tags":["foo:bar","foo:baz"],"points":[[`
got, err := m.MarshalJSON()
if err != nil {
t.Fatal("expected no error")
}
if !strings.Contains(string(got), want) {
t.Fatalf("expected '%v', got '%v'", want, string(got))
}
}
func TestSend(t *testing.T) {
buf := new(bytes.Buffer)
log.SetOutput(buf)
log.SetFlags(0)
// when dry run is true, output should be go to the logger
send("http://example.com", []byte("Hello, World!"), true)
want := "Dry run, logging payload:\nHello, World!"
if got := strings.TrimSpace(buf.String()); got != want {
t.Fatalf("expected '%v', got '%v'", want, got)
}
// let it make the actual HTTP call
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
got, _ := ioutil.ReadAll(r.Body)
if string(got) != "Hello, World!" {
t.Fatalf("expected '%v', got '%v'", "Hello, World!", string(got))
}
if r.Method != "POST" {
t.Fatalf("expected '%v', got '%v'", "POST", r.Method)
}
if val := r.Header.Get("Content-Type"); val != "application/json" {
t.Fatalf("wrong Content-Type header: %v", val)
}
}))
defer ts.Close()
if err := send(ts.URL, []byte("Hello, World!"), false); err != nil {
t.Fatal("expected no error")
}
}