mirror of https://github.com/statping/statping
testing
parent
2aa86b28d6
commit
1afd8f01e1
|
@ -17,7 +17,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/gorilla/sessions"
|
|
||||||
"github.com/hunterlong/statup/core"
|
"github.com/hunterlong/statup/core"
|
||||||
"github.com/hunterlong/statup/core/notifier"
|
"github.com/hunterlong/statup/core/notifier"
|
||||||
"github.com/hunterlong/statup/handlers"
|
"github.com/hunterlong/statup/handlers"
|
||||||
|
@ -36,9 +35,7 @@ import (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
route *mux.Router
|
route *mux.Router
|
||||||
testSession *sessions.Session
|
|
||||||
dir string
|
dir string
|
||||||
SERVICE_SINCE, _ = time.Parse(time.RFC3339, "2018-08-30T10:42:08-07:00")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
|
@ -101,7 +101,7 @@ func (c *checkin) Create() (int64, error) {
|
||||||
|
|
||||||
// Update will update a checkin
|
// Update will update a checkin
|
||||||
func (c *checkin) Update() (int64, error) {
|
func (c *checkin) Update() (int64, error) {
|
||||||
row := checkinDB().Update(&c)
|
row := checkinDB().Update(c)
|
||||||
if row.Error != nil {
|
if row.Error != nil {
|
||||||
utils.Log(2, row.Error)
|
utils.Log(2, row.Error)
|
||||||
return 0, row.Error
|
return 0, row.Error
|
||||||
|
|
|
@ -24,15 +24,20 @@ import (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
dir string
|
dir string
|
||||||
|
skipNewDb bool
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
dir = utils.Directory
|
dir = utils.Directory
|
||||||
utils.InitLogs()
|
utils.InitLogs()
|
||||||
source.Assets()
|
source.Assets()
|
||||||
|
skipNewDb = false
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNewCore(t *testing.T) {
|
func TestNewCore(t *testing.T) {
|
||||||
|
if skipNewDb {
|
||||||
|
t.SkipNow()
|
||||||
|
}
|
||||||
utils.DeleteFile(dir + "/config.yml")
|
utils.DeleteFile(dir + "/config.yml")
|
||||||
utils.DeleteFile(dir + "/statup.db")
|
utils.DeleteFile(dir + "/statup.db")
|
||||||
CoreApp = NewCore()
|
CoreApp = NewCore()
|
||||||
|
@ -41,6 +46,9 @@ func TestNewCore(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDbConfig_Save(t *testing.T) {
|
func TestDbConfig_Save(t *testing.T) {
|
||||||
|
if skipNewDb {
|
||||||
|
t.SkipNow()
|
||||||
|
}
|
||||||
var err error
|
var err error
|
||||||
Configs = &DbConfig{
|
Configs = &DbConfig{
|
||||||
DbConn: "sqlite",
|
DbConn: "sqlite",
|
||||||
|
@ -66,11 +74,17 @@ func TestDbConnection(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDropDatabase(t *testing.T) {
|
func TestDropDatabase(t *testing.T) {
|
||||||
|
if skipNewDb {
|
||||||
|
t.SkipNow()
|
||||||
|
}
|
||||||
err := Configs.DropDatabase()
|
err := Configs.DropDatabase()
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSeedSchemaDatabase(t *testing.T) {
|
func TestSeedSchemaDatabase(t *testing.T) {
|
||||||
|
if skipNewDb {
|
||||||
|
t.SkipNow()
|
||||||
|
}
|
||||||
err := Configs.CreateDatabase()
|
err := Configs.CreateDatabase()
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
}
|
}
|
||||||
|
@ -81,6 +95,9 @@ func TestMigrateDatabase(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSeedDatabase(t *testing.T) {
|
func TestSeedDatabase(t *testing.T) {
|
||||||
|
if skipNewDb {
|
||||||
|
t.SkipNow()
|
||||||
|
}
|
||||||
err := InsertLargeSampleData()
|
err := InsertLargeSampleData()
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
}
|
}
|
||||||
|
@ -98,6 +115,9 @@ func TestSelectCore(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInsertNotifierDB(t *testing.T) {
|
func TestInsertNotifierDB(t *testing.T) {
|
||||||
|
if skipNewDb {
|
||||||
|
t.SkipNow()
|
||||||
|
}
|
||||||
err := insertNotifierDB()
|
err := insertNotifierDB()
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -119,7 +119,7 @@ func OnStart(c *types.Core) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NotifierEvents interface
|
// OnNewNotifier is triggered when a new notifier is loaded
|
||||||
func OnNewNotifier(n *Notification) {
|
func OnNewNotifier(n *Notification) {
|
||||||
for _, comm := range AllCommunications {
|
for _, comm := range AllCommunications {
|
||||||
if isType(comm, new(NotifierEvents)) && isEnabled(comm) && inLimits(comm) {
|
if isType(comm, new(NotifierEvents)) && isEnabled(comm) && inLimits(comm) {
|
||||||
|
|
|
@ -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())
|
||||||
|
}
|
|
@ -17,7 +17,6 @@ package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/hunterlong/statup/types"
|
"github.com/hunterlong/statup/types"
|
||||||
"github.com/hunterlong/statup/utils"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
@ -39,7 +38,7 @@ func TestSelectAllServices(t *testing.T) {
|
||||||
services := CoreApp.Services
|
services := CoreApp.Services
|
||||||
for _, s := range services {
|
for _, s := range services {
|
||||||
service := s.(*Service)
|
service := s.(*Service)
|
||||||
service.Check(true)
|
service.Check(false)
|
||||||
assert.True(t, service.IsRunning())
|
assert.True(t, service.IsRunning())
|
||||||
t.Logf("ID: %v %v\n", service.Id, service.Name)
|
t.Logf("ID: %v %v\n", service.Id, service.Name)
|
||||||
}
|
}
|
||||||
|
@ -343,57 +342,3 @@ func TestDNScheckService(t *testing.T) {
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.NotZero(t, amount)
|
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())
|
|
||||||
}
|
|
||||||
|
|
1154
dev/COVERAGE.html
1154
dev/COVERAGE.html
File diff suppressed because one or more lines are too long
585
dev/README.md
585
dev/README.md
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue