-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
90 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package notifier | ||
|
||
import ( | ||
"fmt" | ||
"net/smtp" | ||
"reflect" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestNotify(t *testing.T) { | ||
oldSendMail := sendMail | ||
defer func() { | ||
sendMail = oldSendMail | ||
}() | ||
|
||
host := "mailserver.localdomain" | ||
port := 123 | ||
|
||
expectedAddr := fmt.Sprintf("%s:%d", host, port) | ||
expectedFrom := "[email protected]" | ||
expectedTo := []string{"[email protected]", "[email protected]"} | ||
expectedMsg := `From: "Some Sender" <[email protected]> | ||
To: [email protected], [email protected] | ||
Subject: Some Cluster is HEALTHY | ||
MIME-version: 1.0; | ||
Content-Type: text/html; charset="UTF-8"; | ||
<!DOCTYPE html> | ||
` | ||
|
||
sendMail = func(addr string, a smtp.Auth, from string, to []string, msg []byte) error { | ||
if addr != expectedAddr { | ||
t.Errorf("expected %s, got %s", expectedAddr, addr) | ||
} | ||
|
||
if a == nil { | ||
t.Error("auth must not be null") | ||
} | ||
|
||
if from != expectedFrom { | ||
t.Errorf("expected %s, got %s", expectedFrom, from) | ||
} | ||
|
||
if !reflect.DeepEqual(to, expectedTo) { | ||
t.Errorf("expected %s, got %s", expectedTo, to) | ||
} | ||
|
||
stringMsg := string(msg) | ||
if !strings.HasPrefix(stringMsg, expectedMsg) { | ||
t.Errorf("expected message to start with\n\n%s\n\ngot\n\n%s", expectedMsg, stringMsg) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
notifier := EmailNotifier{ | ||
Username: "some username", | ||
Password: "some password", | ||
ClusterName: "Some Cluster", | ||
Url: host, | ||
Port: port, | ||
SenderEmail: expectedFrom, | ||
SenderAlias: "Some Sender", | ||
Receivers: expectedTo, | ||
} | ||
|
||
if !notifier.Notify(make(Messages, 0)) { | ||
t.Error("Notify must return true") | ||
} | ||
} |