2018-09-18 22:02:27 +00:00
|
|
|
// Statup
|
|
|
|
// Copyright (C) 2018. Hunter Long and the project contributors
|
|
|
|
// Written by Hunter Long <info@socialeck.com> and the project contributors
|
|
|
|
//
|
|
|
|
// https://github.com/hunterlong/statup
|
|
|
|
//
|
|
|
|
// The licenses for most software and other practical works are designed
|
|
|
|
// to take away your freedom to share and change the works. By contrast,
|
|
|
|
// the GNU General Public License is intended to guarantee your freedom to
|
|
|
|
// share and change all versions of a program--to make sure it remains free
|
|
|
|
// software for all its users.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2018-09-15 22:21:58 +00:00
|
|
|
package notifiers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/hunterlong/statup/core/notifier"
|
|
|
|
"github.com/hunterlong/statup/utils"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
EMAIL_HOST = os.Getenv("EMAIL_HOST")
|
|
|
|
EMAIL_USER = os.Getenv("EMAIL_USER")
|
|
|
|
EMAIL_PASS = os.Getenv("EMAIL_PASS")
|
|
|
|
EMAIL_OUTGOING = os.Getenv("EMAIL_OUTGOING")
|
|
|
|
EMAIL_SEND_TO = os.Getenv("EMAIL_SEND_TO")
|
|
|
|
EMAIL_PORT = utils.StringInt(os.Getenv("EMAIL_PORT"))
|
|
|
|
)
|
|
|
|
|
2018-10-06 05:03:10 +00:00
|
|
|
var testEmail *emailOutgoing
|
2018-09-15 22:21:58 +00:00
|
|
|
|
|
|
|
func init() {
|
2018-09-17 22:13:42 +00:00
|
|
|
EMAIL_HOST = os.Getenv("EMAIL_HOST")
|
|
|
|
EMAIL_USER = os.Getenv("EMAIL_USER")
|
|
|
|
EMAIL_PASS = os.Getenv("EMAIL_PASS")
|
|
|
|
EMAIL_OUTGOING = os.Getenv("EMAIL_OUTGOING")
|
|
|
|
EMAIL_SEND_TO = os.Getenv("EMAIL_SEND_TO")
|
|
|
|
EMAIL_PORT = utils.StringInt(os.Getenv("EMAIL_PORT"))
|
|
|
|
|
2018-09-15 22:21:58 +00:00
|
|
|
emailer.Host = EMAIL_HOST
|
|
|
|
emailer.Username = EMAIL_USER
|
|
|
|
emailer.Password = EMAIL_PASS
|
|
|
|
emailer.Var1 = EMAIL_OUTGOING
|
|
|
|
emailer.Var2 = EMAIL_SEND_TO
|
|
|
|
emailer.Port = int(EMAIL_PORT)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmailNotifier(t *testing.T) {
|
2018-09-16 07:48:34 +00:00
|
|
|
t.Parallel()
|
2018-09-15 22:21:58 +00:00
|
|
|
if EMAIL_HOST == "" || EMAIL_USER == "" || EMAIL_PASS == "" {
|
2018-10-06 05:00:40 +00:00
|
|
|
t.Log("email notifier testing skipped, missing EMAIL_ environment variables")
|
2018-09-15 22:21:58 +00:00
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
currentCount = CountNotifiers()
|
|
|
|
|
|
|
|
t.Run("New Emailer", func(t *testing.T) {
|
|
|
|
emailer.Host = EMAIL_HOST
|
|
|
|
emailer.Username = EMAIL_USER
|
|
|
|
emailer.Password = EMAIL_PASS
|
|
|
|
emailer.Var1 = EMAIL_OUTGOING
|
|
|
|
emailer.Var2 = EMAIL_SEND_TO
|
|
|
|
emailer.Port = int(EMAIL_PORT)
|
|
|
|
emailer.Delay = time.Duration(100 * time.Millisecond)
|
|
|
|
|
2018-10-06 05:03:10 +00:00
|
|
|
testEmail = &emailOutgoing{
|
2018-09-15 22:21:58 +00:00
|
|
|
To: emailer.GetValue("var2"),
|
|
|
|
Subject: fmt.Sprintf("Service %v is Failing", TestService.Name),
|
2018-10-06 05:05:50 +00:00
|
|
|
Template: mainEmailTemplate,
|
2018-09-28 06:57:03 +00:00
|
|
|
Data: TestService,
|
2018-09-15 22:21:58 +00:00
|
|
|
From: emailer.GetValue("var1"),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-10-06 05:00:40 +00:00
|
|
|
t.Run("Add email Notifier", func(t *testing.T) {
|
2018-09-15 22:21:58 +00:00
|
|
|
err := notifier.AddNotifier(emailer)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "Hunter Long", emailer.Author)
|
|
|
|
assert.Equal(t, EMAIL_HOST, emailer.Host)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Emailer Load", func(t *testing.T) {
|
2018-09-16 07:48:34 +00:00
|
|
|
notifier.Load()
|
2018-09-15 22:21:58 +00:00
|
|
|
})
|
|
|
|
|
2018-10-06 05:00:40 +00:00
|
|
|
t.Run("email Within Limits", func(t *testing.T) {
|
2018-09-15 22:21:58 +00:00
|
|
|
ok, err := emailer.WithinLimits()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.True(t, ok)
|
|
|
|
})
|
|
|
|
|
2018-10-06 05:00:40 +00:00
|
|
|
t.Run("email Test Source", func(t *testing.T) {
|
2018-09-15 22:21:58 +00:00
|
|
|
emailSource(testEmail)
|
|
|
|
assert.NotEmpty(t, testEmail.Source)
|
|
|
|
})
|
|
|
|
|
2018-10-06 05:00:40 +00:00
|
|
|
t.Run("email OnFailure", func(t *testing.T) {
|
2018-09-20 09:46:51 +00:00
|
|
|
emailer.OnFailure(TestService, TestFailure)
|
|
|
|
assert.Len(t, emailer.Queue, 1)
|
|
|
|
})
|
|
|
|
|
2018-10-06 05:00:40 +00:00
|
|
|
t.Run("email Check Offline", func(t *testing.T) {
|
2018-09-20 09:46:51 +00:00
|
|
|
assert.False(t, emailer.Online)
|
|
|
|
})
|
|
|
|
|
2018-10-06 05:00:40 +00:00
|
|
|
t.Run("email OnSuccess", func(t *testing.T) {
|
2018-09-20 09:46:51 +00:00
|
|
|
emailer.OnSuccess(TestService)
|
|
|
|
assert.Len(t, emailer.Queue, 2)
|
|
|
|
})
|
|
|
|
|
2018-10-06 05:00:40 +00:00
|
|
|
t.Run("email Check Back Online", func(t *testing.T) {
|
2018-09-20 09:46:51 +00:00
|
|
|
assert.True(t, emailer.Online)
|
|
|
|
})
|
|
|
|
|
2018-10-06 05:00:40 +00:00
|
|
|
t.Run("email OnSuccess Again", func(t *testing.T) {
|
2018-09-20 09:46:51 +00:00
|
|
|
emailer.OnSuccess(TestService)
|
|
|
|
assert.Len(t, emailer.Queue, 2)
|
|
|
|
})
|
|
|
|
|
2018-10-06 05:00:40 +00:00
|
|
|
t.Run("email Send", func(t *testing.T) {
|
2018-09-15 22:21:58 +00:00
|
|
|
err := emailer.Send(testEmail)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
})
|
|
|
|
|
2018-10-07 06:39:57 +00:00
|
|
|
t.Run("emailer Test", func(t *testing.T) {
|
|
|
|
err := emailer.OnTest()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
})
|
|
|
|
|
2018-10-06 05:00:40 +00:00
|
|
|
t.Run("email Run Queue", func(t *testing.T) {
|
2018-09-15 22:21:58 +00:00
|
|
|
go notifier.Queue(emailer)
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
assert.Equal(t, EMAIL_HOST, emailer.Host)
|
|
|
|
assert.Equal(t, 0, len(emailer.Queue))
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|