pull/78/head
Hunter Long 2018-10-07 01:16:38 -07:00
parent 2aa86b28d6
commit 1afd8f01e1
8 changed files with 841 additions and 1096 deletions

View File

@ -17,7 +17,6 @@ package main
import (
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
"github.com/hunterlong/statup/core"
"github.com/hunterlong/statup/core/notifier"
"github.com/hunterlong/statup/handlers"
@ -35,10 +34,8 @@ import (
)
var (
route *mux.Router
testSession *sessions.Session
dir string
SERVICE_SINCE, _ = time.Parse(time.RFC3339, "2018-08-30T10:42:08-07:00")
route *mux.Router
dir string
)
func init() {

View File

@ -101,7 +101,7 @@ func (c *checkin) Create() (int64, error) {
// Update will update a checkin
func (c *checkin) Update() (int64, error) {
row := checkinDB().Update(&c)
row := checkinDB().Update(c)
if row.Error != nil {
utils.Log(2, row.Error)
return 0, row.Error

View File

@ -23,16 +23,21 @@ import (
)
var (
dir string
dir string
skipNewDb bool
)
func init() {
dir = utils.Directory
utils.InitLogs()
source.Assets()
skipNewDb = false
}
func TestNewCore(t *testing.T) {
if skipNewDb {
t.SkipNow()
}
utils.DeleteFile(dir + "/config.yml")
utils.DeleteFile(dir + "/statup.db")
CoreApp = NewCore()
@ -41,6 +46,9 @@ func TestNewCore(t *testing.T) {
}
func TestDbConfig_Save(t *testing.T) {
if skipNewDb {
t.SkipNow()
}
var err error
Configs = &DbConfig{
DbConn: "sqlite",
@ -66,11 +74,17 @@ func TestDbConnection(t *testing.T) {
}
func TestDropDatabase(t *testing.T) {
if skipNewDb {
t.SkipNow()
}
err := Configs.DropDatabase()
assert.Nil(t, err)
}
func TestSeedSchemaDatabase(t *testing.T) {
if skipNewDb {
t.SkipNow()
}
err := Configs.CreateDatabase()
assert.Nil(t, err)
}
@ -81,6 +95,9 @@ func TestMigrateDatabase(t *testing.T) {
}
func TestSeedDatabase(t *testing.T) {
if skipNewDb {
t.SkipNow()
}
err := InsertLargeSampleData()
assert.Nil(t, err)
}
@ -98,6 +115,9 @@ func TestSelectCore(t *testing.T) {
}
func TestInsertNotifierDB(t *testing.T) {
if skipNewDb {
t.SkipNow()
}
err := insertNotifierDB()
assert.Nil(t, err)
}

View File

@ -119,7 +119,7 @@ func OnStart(c *types.Core) {
}
}
// NotifierEvents interface
// OnNewNotifier is triggered when a new notifier is loaded
func OnNewNotifier(n *Notification) {
for _, comm := range AllCommunications {
if isType(comm, new(NotifierEvents)) && isEnabled(comm) && inLimits(comm) {

View File

@ -0,0 +1,100 @@
// 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/>.
package core
import (
"github.com/hunterlong/statup/types"
"github.com/hunterlong/statup/utils"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
var (
testCheckin *checkin
)
func TestCreateCheckin(t *testing.T) {
service := SelectService(1)
testCheckin = ReturnCheckin(&types.Checkin{
Service: service.Id,
Interval: 10,
GracePeriod: 5,
ApiKey: utils.RandomString(7),
})
id, err := testCheckin.Create()
assert.Nil(t, err)
assert.NotZero(t, id)
assert.NotEmpty(t, testCheckin.ApiKey)
assert.Equal(t, int64(10), testCheckin.Interval)
assert.Equal(t, int64(5), testCheckin.GracePeriod)
assert.True(t, testCheckin.Expected().Minutes() < 0)
}
func TestSelectCheckin(t *testing.T) {
service := SelectService(1)
checkins := service.Checkins()
assert.NotNil(t, checkins)
assert.Equal(t, 1, len(checkins))
testCheckin = checkins[0]
assert.Equal(t, int64(10), testCheckin.Interval)
assert.Equal(t, int64(5), testCheckin.GracePeriod)
assert.Equal(t, 7, len(testCheckin.ApiKey))
}
func TestUpdateCheckin(t *testing.T) {
testCheckin.Interval = 60
testCheckin.GracePeriod = 15
id, err := testCheckin.Update()
assert.Nil(t, err)
assert.NotZero(t, id)
assert.NotEmpty(t, testCheckin.ApiKey)
service := SelectService(1)
checkin := service.Checkins()[0]
assert.Equal(t, int64(60), checkin.Interval)
assert.Equal(t, int64(15), checkin.GracePeriod)
t.Log(testCheckin.Expected())
assert.True(t, testCheckin.Expected().Minutes() < 0)
}
func TestCreateCheckinHits(t *testing.T) {
service := SelectService(1)
checkins := service.Checkins()
assert.Equal(t, 1, len(checkins))
created := time.Now().UTC().Add(-60 * time.Second)
hit := ReturnCheckinHit(&types.CheckinHit{
Checkin: testCheckin.Id,
From: "192.168.1.1",
CreatedAt: created,
})
hit.Create()
hits := testCheckin.Hits()
assert.Equal(t, 1, len(hits))
}
func TestSelectCheckinMethods(t *testing.T) {
time.Sleep(5 * time.Second)
service := SelectService(1)
checkins := service.Checkins()
assert.NotNil(t, checkins)
lastHit := testCheckin.Last()
assert.Equal(t, float64(60), testCheckin.Period().Seconds())
assert.Equal(t, float64(15), testCheckin.Grace().Seconds())
t.Log(testCheckin.Expected())
assert.True(t, testCheckin.Expected().Seconds() < -5)
assert.Equal(t, time.Now().UTC().Day(), lastHit.CreatedAt.UTC().Day())
assert.Equal(t, "A minute ago", lastHit.Ago())
}

View File

@ -17,7 +17,6 @@ package core
import (
"github.com/hunterlong/statup/types"
"github.com/hunterlong/statup/utils"
"github.com/stretchr/testify/assert"
"testing"
"time"
@ -39,7 +38,7 @@ func TestSelectAllServices(t *testing.T) {
services := CoreApp.Services
for _, s := range services {
service := s.(*Service)
service.Check(true)
service.Check(false)
assert.True(t, service.IsRunning())
t.Logf("ID: %v %v\n", service.Id, service.Name)
}
@ -343,57 +342,3 @@ func TestDNScheckService(t *testing.T) {
assert.Nil(t, err)
assert.NotZero(t, amount)
}
func TestCreateCheckin(t *testing.T) {
checkin := ReturnCheckin(&types.Checkin{
Service: 1,
Interval: 10,
GracePeriod: 5,
ApiKey: utils.RandomString(7),
})
id, err := checkin.Create()
assert.Nil(t, err)
assert.NotZero(t, id)
}
func TestSelectCheckin(t *testing.T) {
service := SelectService(1)
checkins := service.Checkins()
assert.NotNil(t, checkins)
assert.Equal(t, 1, len(checkins))
first := checkins[0]
assert.Equal(t, int64(10), first.Interval)
assert.Equal(t, 7, len(first.ApiKey))
assert.Equal(t, int64(5), first.GracePeriod)
}
func TestCreateCheckinHits(t *testing.T) {
service := SelectService(1)
checkins := service.Checkins()
first := checkins[0]
created := time.Now().Add(-2 * time.Hour)
for i := 0; i <= 20; i++ {
hit := ReturnCheckinHit(&types.CheckinHit{
Checkin: first.Id,
From: "192.168.1.1",
CreatedAt: created,
})
hit.Create()
created = created.Add(10 * time.Second)
}
hits := first.Hits()
assert.Equal(t, 21, len(hits))
}
func TestSelectCheckinMethods(t *testing.T) {
time.Sleep(5 * time.Second)
service := SelectService(1)
checkins := service.Checkins()
assert.NotNil(t, checkins)
first := checkins[0]
lastHit := first.Last()
assert.Equal(t, float64(10), first.Period().Seconds())
assert.Equal(t, float64(5), first.Grace().Seconds())
assert.Equal(t, time.Now().UTC().Day(), lastHit.CreatedAt.UTC().Day())
//assert.Equal(t, "Just now", lastHit.Ago())
}

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff