mirror of https://github.com/statping/statping
tests
parent
6b9f0766a5
commit
482d182c40
|
@ -116,7 +116,7 @@ func TestInit(t *testing.T) {
|
|||
|
||||
t.Run("Test Samples", func(t *testing.T) {
|
||||
require.Nil(t, Samples())
|
||||
assert.Len(t, All(), 2)
|
||||
assert.Len(t, All(), 3)
|
||||
})
|
||||
|
||||
t.Run("Test Checkin", func(t *testing.T) {
|
||||
|
|
|
@ -27,7 +27,7 @@ CheckinLoop:
|
|||
log.Infoln(fmt.Sprintf("Checkin '%s' expects a request every %s last request was %s ago", c.Name, c.Period(), utils.DurationReadable(ago)))
|
||||
|
||||
if ago.Seconds() > c.Period().Seconds() {
|
||||
issue := fmt.Sprintf("Checkin expects a request every %d seconds", c.Interval)
|
||||
issue := fmt.Sprintf("Checkin expects a request every %d minutes", c.Interval)
|
||||
log.Warnln(issue)
|
||||
|
||||
fail := &failures.Failure{
|
||||
|
|
|
@ -31,12 +31,6 @@ func (f *Failure) AfterCreate() {
|
|||
metrics.Query("failure", "create")
|
||||
}
|
||||
|
||||
func All() []*Failure {
|
||||
var failures []*Failure
|
||||
db.Find(&failures)
|
||||
return failures
|
||||
}
|
||||
|
||||
func (f *Failure) Create() error {
|
||||
q := db.Create(f)
|
||||
return q.Error()
|
||||
|
|
|
@ -3,7 +3,6 @@ package failures
|
|||
import (
|
||||
"github.com/statping/statping/database"
|
||||
"github.com/statping/statping/utils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
)
|
||||
|
@ -19,6 +18,5 @@ func TestInit(t *testing.T) {
|
|||
|
||||
t.Run("Test Samples", func(t *testing.T) {
|
||||
require.Nil(t, Samples())
|
||||
assert.Len(t, All(), 2)
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,28 +0,0 @@
|
|||
package hits
|
||||
|
||||
import (
|
||||
"github.com/statping/statping/database"
|
||||
"github.com/statping/statping/types/services"
|
||||
"github.com/statping/statping/utils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestInit(t *testing.T) {
|
||||
err := utils.InitLogs()
|
||||
require.Nil(t, err)
|
||||
db, err := database.OpenTester()
|
||||
require.Nil(t, err)
|
||||
db.CreateTable(&Hit{}, &services.Service{})
|
||||
SetDB(db)
|
||||
services.SetDB(db)
|
||||
|
||||
for i := 0; i <= 5; i++ {
|
||||
s := services.Example(true)
|
||||
assert.Nil(t, s.Create())
|
||||
assert.Len(t, s.AllHits().List(), 2)
|
||||
}
|
||||
|
||||
require.Nil(t, Samples())
|
||||
}
|
|
@ -5,7 +5,7 @@ import "time"
|
|||
// Hit struct is a 'successful' ping or web response entry for a service.
|
||||
type Hit struct {
|
||||
Id int64 `gorm:"primary_key;column:id" json:"id"`
|
||||
Service int64 `gorm:"column:service" json:"-"`
|
||||
Service int64 `gorm:"index;column:service" json:"-"`
|
||||
Latency int64 `gorm:"column:latency" json:"latency"`
|
||||
PingTime int64 `gorm:"column:ping_time" json:"ping_time"`
|
||||
CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
|
||||
|
|
|
@ -75,7 +75,7 @@ func TestDelete(t *testing.T) {
|
|||
|
||||
func TestSamples(t *testing.T) {
|
||||
require.Nil(t, Samples())
|
||||
assert.Len(t, All(), 2)
|
||||
assert.Len(t, All(), 3)
|
||||
}
|
||||
|
||||
func TestClose(t *testing.T) {
|
||||
|
|
|
@ -4,7 +4,6 @@ import (
|
|||
"encoding/json"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"gopkg.in/yaml.v2"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
@ -51,17 +50,9 @@ func TestJSONMarshal(t *testing.T) {
|
|||
str, err := json.Marshal(test.Input)
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, test.ExpectedJSON, string(str))
|
||||
|
||||
str, err = yaml.Marshal(yamlStruct{test.Input})
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, test.ExpectedJSON, string(str))
|
||||
}
|
||||
}
|
||||
|
||||
type yamlStruct struct {
|
||||
Value interface{} `json:"value" yaml:"value"`
|
||||
}
|
||||
|
||||
func TestNewNullBool(t *testing.T) {
|
||||
val := NewNullBool(true)
|
||||
assert.True(t, val.Bool)
|
||||
|
|
|
@ -1,20 +1,10 @@
|
|||
package services
|
||||
|
||||
import (
|
||||
"github.com/statping/statping/types/checkins"
|
||||
)
|
||||
|
||||
// CheckinProcess runs the checkin routine for each checkin attached to service
|
||||
func CheckinProcess(s *Service) {
|
||||
for _, c := range s.Checkins() {
|
||||
for _, c := range s.Checkins {
|
||||
if last := c.LastHit(); last != nil {
|
||||
c.Start()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) Checkins() []*checkins.Checkin {
|
||||
var chks []*checkins.Checkin
|
||||
db.Where("service = ?", s.Id).Find(&chks)
|
||||
return chks
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@ var (
|
|||
)
|
||||
|
||||
func (s *Service) AfterFind() {
|
||||
db.Model(s).Related(&s.Incidents).Related(&s.Messages).Related(&s.Checkins)
|
||||
metrics.Query("service", "find")
|
||||
}
|
||||
|
||||
|
@ -51,6 +52,7 @@ func Find(id int64) (*Service, error) {
|
|||
if srv == nil {
|
||||
return nil, errors.Missing(&Service{}, id)
|
||||
}
|
||||
db.First(&srv, id)
|
||||
return srv, nil
|
||||
}
|
||||
|
||||
|
@ -120,7 +122,7 @@ func (s *Service) DeleteHits() error {
|
|||
}
|
||||
|
||||
func (s *Service) DeleteCheckins() error {
|
||||
for _, c := range s.Checkins() {
|
||||
for _, c := range s.Checkins {
|
||||
if err := c.Delete(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -1,17 +1,7 @@
|
|||
package services
|
||||
|
||||
import (
|
||||
"github.com/statping/statping/types/incidents"
|
||||
)
|
||||
|
||||
func (s *Service) Incidents() []*incidents.Incident {
|
||||
var i []*incidents.Incident
|
||||
db.Where("service = ?", s.Id).Find(&i)
|
||||
return i
|
||||
}
|
||||
|
||||
func (s *Service) DeleteIncidents() error {
|
||||
for _, i := range s.Incidents() {
|
||||
for _, i := range s.Incidents {
|
||||
if err := i.Delete(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -228,9 +228,6 @@ func SelectAllServices(start bool) (map[int64]*Service, error) {
|
|||
}
|
||||
for _, s := range all() {
|
||||
s.Failures = s.AllFailures().LastAmount(limitedFailures)
|
||||
for _, c := range s.Checkins() {
|
||||
s.AllCheckins = append(s.AllCheckins, c)
|
||||
}
|
||||
s.prevOnline = true
|
||||
// collect initial service stats
|
||||
s.UpdateStats()
|
||||
|
|
|
@ -48,7 +48,6 @@ func Example(online bool) Service {
|
|||
DownText: "The service was responding with 500 status code",
|
||||
LastStatusCode: 200,
|
||||
Failures: nil,
|
||||
AllCheckins: nil,
|
||||
LastLookupTime: 4600,
|
||||
LastLatency: 124399,
|
||||
LastCheck: utils.Now().Add(-37 * time.Second),
|
||||
|
|
|
@ -3,6 +3,8 @@ package services
|
|||
import (
|
||||
"github.com/statping/statping/types/checkins"
|
||||
"github.com/statping/statping/types/failures"
|
||||
"github.com/statping/statping/types/incidents"
|
||||
"github.com/statping/statping/types/messages"
|
||||
"github.com/statping/statping/types/null"
|
||||
"time"
|
||||
)
|
||||
|
@ -48,14 +50,16 @@ type Service struct {
|
|||
UpdateNotify null.NullBool `gorm:"default:true;column:notify_all_changes" json:"notify_all_changes" yaml:"notify_all_changes" scope:"user,admin"` // This Variable is a simple copy of `core.CoreApp.UpdateNotify.Bool`
|
||||
DownText string `gorm:"-" json:"-" yaml:"-"` // Contains the current generated Downtime Text // Is 'true' if the user has already be informed that the Services now again available // Is 'true' if the user has already be informed that the Services now again available
|
||||
LastStatusCode int `gorm:"-" json:"status_code" yaml:"-"`
|
||||
Failures []*failures.Failure `gorm:"-" json:"failures,omitempty" yaml:"-" scope:"user,admin"`
|
||||
AllCheckins []*checkins.Checkin `gorm:"-" json:"checkins,omitempty" yaml:"-" scope:"user,admin"`
|
||||
LastLookupTime int64 `gorm:"-" json:"-" yaml:"-"`
|
||||
LastLatency int64 `gorm:"-" json:"-" yaml:"-"`
|
||||
LastCheck time.Time `gorm:"-" json:"-" yaml:"-"`
|
||||
LastOnline time.Time `gorm:"-" json:"last_success" yaml:"-"`
|
||||
LastOffline time.Time `gorm:"-" json:"last_error" yaml:"-"`
|
||||
Stats *Stats `gorm:"-" json:"stats,omitempty" yaml:"-"`
|
||||
Messages []*messages.Message `gorm:"foreignkey:service;association_foreignkey:id" json:"messages,omitempty" yaml:"messages"`
|
||||
Incidents []*incidents.Incident `gorm:"foreignkey:service;association_foreignkey:id" json:"incidents,omitempty" yaml:"incidents"`
|
||||
Checkins []*checkins.Checkin `gorm:"foreignkey:service;association_foreignkey:id" json:"checkins,omitempty" yaml:"-" scope:"user,admin"`
|
||||
Failures []*failures.Failure `gorm:"-" json:"failures,omitempty" yaml:"-" scope:"user,admin"`
|
||||
|
||||
notifyAfterCount int64 `gorm:"-" json:"-" yaml:"-"`
|
||||
prevOnline bool `gorm:"-" json:"-" yaml:"-"`
|
||||
|
|
|
@ -35,6 +35,16 @@ func TestFind(t *testing.T) {
|
|||
assert.True(t, item.Admin.Bool)
|
||||
}
|
||||
|
||||
func TestAuthUser(t *testing.T) {
|
||||
u, err := AuthUser("example_user", "password12345")
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, "example_user", u.Username)
|
||||
|
||||
u, err = AuthUser("exampleuser2", "wrongpass")
|
||||
assert.NotNil(t, err)
|
||||
assert.Nil(t, u)
|
||||
}
|
||||
|
||||
func TestFindByUsername(t *testing.T) {
|
||||
item, err := FindByUsername("example_user")
|
||||
require.Nil(t, err)
|
||||
|
@ -73,16 +83,6 @@ func TestUpdate(t *testing.T) {
|
|||
assert.Equal(t, "updated_user", item.Username)
|
||||
}
|
||||
|
||||
func TestAuthUser(t *testing.T) {
|
||||
u, err := AuthUser("updated_user", "admin")
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, "admin", u.Username)
|
||||
|
||||
u, err = AuthUser("updated_user", "wrongpass")
|
||||
assert.NotNil(t, err)
|
||||
assert.Nil(t, u)
|
||||
}
|
||||
|
||||
func TestDelete(t *testing.T) {
|
||||
all := All()
|
||||
assert.Len(t, all, 2)
|
||||
|
|
Loading…
Reference in New Issue