This repository has been archived by the owner on Oct 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnotify_test.go
167 lines (133 loc) · 4.47 KB
/
notify_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
package grim
import (
"bytes"
"fmt"
"log"
"strings"
"testing"
)
// Copyright 2015 MediaMath <http://www.mediamath.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
var testContext = &grimNotificationContext{
Owner: "rain",
Repo: "spain",
EventName: "falls",
Target: "plain",
UserName: "mainly",
Workspace: "boogey/nights",
LogDir: "once/again/where/it/rains",
}
var testConfig = localConfig{local: configMap{
"PendingTemplate": "pending {{.Owner}}",
"ErrorTemplate": "error {{.Repo}}",
"FailureTemplate": "failure {{.Target}}",
"SuccessTemplate": "success {{.UserName}}"}}
var testHook = hookEvent{
Owner: "MediaMath",
Repo: "grim",
EventName: "push",
}
func TestLoggingHipChatIncompleteSetup(t *testing.T) {
var buf bytes.Buffer
logger := log.New(&buf, "", log.Lshortfile)
notify(testConfig, testHook, "", "", GrimPending, logger)
message := fmt.Sprintf("%v", &buf)
if !strings.Contains(message, "pending MediaMath") {
t.Errorf("Failed to log message")
}
if !strings.Contains(message, "HipChat: config.hipChatToken and config.hitChatRoom not set") {
t.Errorf("Failed to log that token and room from config are not set")
}
}
func TestLoggingHipChatErrorCreatingMessage(t *testing.T) {
var buf bytes.Buffer
logger := log.New(&buf, "", log.Lshortfile)
testConfigWithHC := localConfig{local: configMap{
"PendingTemplate": "pending {{.NOPE}}",
"HipChatToken": "NOT_EMPTY",
"HipChatRoom": "NON_EMPTY",
}}
notify(testConfigWithHC, testHook, "", "", GrimPending, logger)
message := fmt.Sprintf("%v", &buf)
if !strings.Contains(message, "Hipchat: Error while rendering message") {
t.Errorf("Failed to log error in creating to room")
}
}
func TestLoggingHipChatErrorSendingMessage(t *testing.T) {
var buf bytes.Buffer
logger := log.New(&buf, "", log.Lshortfile)
testConfigWithHC := localConfig{local: configMap{
"PendingTemplate": "pending {{.Owner}}",
"HipChatToken": "NOT_EMPTY",
"HipChatRoom": "NON_EMPTY",
}}
notify(testConfigWithHC, testHook, "", "", GrimPending, logger)
message := fmt.Sprintf("%v", &buf)
if !strings.Contains(message, "pending MediaMath") {
t.Errorf("Failed to log message")
}
if !strings.Contains(message, "Hipchat: Error while sending message to room") {
t.Errorf("Failed to log error in sending to room")
}
}
func TestLogDirForBasename(t *testing.T) {
var buf bytes.Buffer
logger := log.New(&buf, "", log.Lshortfile)
testConfigWithHC := localConfig{local: configMap{
"ErrorTemplate": "error {{.LogDir}}",
"HipChatToken": "NOT_EMPTY",
"HipChatRoom": "NON_EMPTY",
}}
notify(testConfigWithHC, testHook, "", "temp/MediaMath/grim/123123", GrimError, logger)
message := fmt.Sprintf("%v", &buf)
if !strings.Contains(message, "error temp/MediaMath/grim/123123") {
t.Errorf("Failed to log proper log directory")
}
}
func TestPending(t *testing.T) {
if err := compareNotification(GrimPending, RSPending, "yellow", "pending rain"); err != nil {
t.Errorf("%v", err)
}
}
func TestError(t *testing.T) {
if err := compareNotification(GrimError, RSError, "gray", "error spain"); err != nil {
t.Errorf("%v", err)
}
}
func TestFailure(t *testing.T) {
if err := compareNotification(GrimFailure, RSFailure, "red", "failure plain"); err != nil {
t.Errorf("%v", err)
}
}
func TestSuccess(t *testing.T) {
if err := compareNotification(GrimSuccess, RSSuccess, "green", "success mainly"); err != nil {
t.Errorf("%v", err)
}
}
func compareNotification(n *standardGrimNotification, state refStatusState, color string, message string) error {
if n.GithubRefStatus() != state {
return fmt.Errorf("Github: %v", n)
}
msg, color, err := n.HipchatNotification(testContext, testConfig)
if err != nil {
return fmt.Errorf("error %v", err)
}
if msg != message || color != color {
return fmt.Errorf("%v %v", message, color)
}
return nil
}
func TestContextRender(t *testing.T) {
str, err := testContext.render("The {{.Owner}} in {{.Repo}} {{.EventName}} {{.UserName}} on the {{.Target}} {{.Workspace}}!")
errStr, err := testContext.render("The {{.Owner}} in {{.Repo}} {{.EventName}} {{.UserName}} on the {{.Target}} {{.LogDir}}!")
if err != nil {
t.Errorf("error %v", err)
}
if str != "The rain in spain falls mainly on the plain boogey/nights!" {
t.Errorf("Didn't match %v", str)
}
if errStr != "The rain in spain falls mainly on the plain once/again/where/it/rains!" {
t.Errorf("Didn't match %v", errStr)
}
}