forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestreceivers_test.go
82 lines (72 loc) · 2.14 KB
/
testreceivers_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
package notifier
import (
"context"
"errors"
"net/url"
"testing"
alertingNotify "github.com/grafana/alerting/notify"
"github.com/stretchr/testify/require"
)
func TestInvalidReceiverError_Error(t *testing.T) {
e := alertingNotify.IntegrationValidationError{
Integration: &alertingNotify.GrafanaIntegrationConfig{
Name: "test",
Type: "test-type",
UID: "uid",
},
Err: errors.New("this is an error"),
}
require.Equal(t, `failed to validate integration "test" (UID uid) of type "test-type": this is an error`, e.Error())
}
func TestReceiverTimeoutError_Error(t *testing.T) {
e := alertingNotify.IntegrationTimeoutError{
Integration: &alertingNotify.GrafanaIntegrationConfig{
Name: "test",
UID: "uid",
},
Err: errors.New("context deadline exceeded"),
}
require.Equal(t, "the receiver timed out: context deadline exceeded", e.Error())
}
type timeoutError struct{}
func (e timeoutError) Error() string {
return "the request timed out"
}
func (e timeoutError) Timeout() bool {
return true
}
func TestProcessNotifierError(t *testing.T) {
t.Run("assert ReceiverTimeoutError is returned for context deadline exceeded", func(t *testing.T) {
r := &alertingNotify.GrafanaIntegrationConfig{
Name: "test",
UID: "uid",
}
require.Equal(t, alertingNotify.IntegrationTimeoutError{
Integration: r,
Err: context.DeadlineExceeded,
}, alertingNotify.ProcessIntegrationError(r, context.DeadlineExceeded))
})
t.Run("assert ReceiverTimeoutError is returned for *url.Error timeout", func(t *testing.T) {
r := &alertingNotify.GrafanaIntegrationConfig{
Name: "test",
UID: "uid",
}
urlError := &url.Error{
Op: "Get",
URL: "https://grafana.net",
Err: timeoutError{},
}
require.Equal(t, alertingNotify.IntegrationTimeoutError{
Integration: r,
Err: urlError,
}, alertingNotify.ProcessIntegrationError(r, urlError))
})
t.Run("assert unknown error is returned unmodified", func(t *testing.T) {
r := &alertingNotify.GrafanaIntegrationConfig{
Name: "test",
UID: "uid",
}
err := errors.New("this is an error")
require.Equal(t, err, alertingNotify.ProcessIntegrationError(r, err))
})
}