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,59 +3,63 @@ 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"
|
||||
)
|
||||
|
||||
// Service is the main struct for Services
|
||||
type Service struct {
|
||||
Id int64 `gorm:"primary_key;column:id" json:"id" yaml:"id"`
|
||||
Name string `gorm:"column:name" json:"name" yaml:"name"`
|
||||
Domain string `gorm:"column:domain" json:"domain" yaml:"domain" private:"true" scope:"user,admin"`
|
||||
Expected null.NullString `gorm:"column:expected" json:"expected" yaml:"expected" scope:"user,admin"`
|
||||
ExpectedStatus int `gorm:"default:200;column:expected_status" json:"expected_status" yaml:"expected_status" scope:"user,admin"`
|
||||
Interval int `gorm:"default:30;column:check_interval" json:"check_interval" yaml:"check_interval"`
|
||||
Type string `gorm:"column:check_type" json:"type" scope:"user,admin" yaml:"type"`
|
||||
Method string `gorm:"column:method" json:"method" scope:"user,admin" yaml:"method"`
|
||||
PostData null.NullString `gorm:"column:post_data" json:"post_data" scope:"user,admin" yaml:"post_data"`
|
||||
Port int `gorm:"not null;column:port" json:"port" scope:"user,admin" yaml:"port"`
|
||||
Timeout int `gorm:"default:30;column:timeout" json:"timeout" scope:"user,admin" yaml:"timeout"`
|
||||
Order int `gorm:"default:0;column:order_id" json:"order_id" yaml:"order_id"`
|
||||
VerifySSL null.NullBool `gorm:"default:false;column:verify_ssl" json:"verify_ssl" scope:"user,admin" yaml:"verify_ssl"`
|
||||
Public null.NullBool `gorm:"default:true;column:public" json:"public" yaml:"public"`
|
||||
GroupId int `gorm:"default:0;column:group_id" json:"group_id" yaml:"group_id"`
|
||||
TLSCert null.NullString `gorm:"column:tls_cert" json:"tls_cert" scope:"user,admin" yaml:"tls_cert"`
|
||||
TLSCertKey null.NullString `gorm:"column:tls_cert_key" json:"tls_cert_key" scope:"user,admin" yaml:"tls_cert_key"`
|
||||
TLSCertRoot null.NullString `gorm:"column:tls_cert_root" json:"tls_cert_root" scope:"user,admin" yaml:"tls_cert_root"`
|
||||
Headers null.NullString `gorm:"column:headers" json:"headers" scope:"user,admin" yaml:"headers"`
|
||||
Permalink null.NullString `gorm:"column:permalink" json:"permalink" yaml:"permalink"`
|
||||
Redirect null.NullBool `gorm:"default:false;column:redirect" json:"redirect" scope:"user,admin" yaml:"redirect"`
|
||||
CreatedAt time.Time `gorm:"column:created_at" json:"created_at" yaml:"-"`
|
||||
UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at" yaml:"-"`
|
||||
Online bool `gorm:"-" json:"online" yaml:"-"`
|
||||
Latency int64 `gorm:"-" json:"latency" yaml:"-"`
|
||||
PingTime int64 `gorm:"-" json:"ping_time" yaml:"-"`
|
||||
Online24Hours float32 `gorm:"-" json:"online_24_hours" yaml:"-"`
|
||||
Online7Days float32 `gorm:"-" json:"online_7_days" yaml:"-"`
|
||||
AvgResponse int64 `gorm:"-" json:"avg_response" yaml:"-"`
|
||||
FailuresLast24Hours int `gorm:"-" json:"failures_24_hours" yaml:"-"`
|
||||
Running chan bool `gorm:"-" json:"-" yaml:"-"`
|
||||
Checkpoint time.Time `gorm:"-" json:"-" yaml:"-"`
|
||||
SleepDuration time.Duration `gorm:"-" json:"-" yaml:"-"`
|
||||
LastResponse string `gorm:"-" json:"-" yaml:"-"`
|
||||
NotifyAfter int64 `gorm:"column:notify_after" json:"notify_after" yaml:"notify_after" scope:"user,admin"`
|
||||
AllowNotifications null.NullBool `gorm:"default:true;column:allow_notifications" json:"allow_notifications" yaml:"allow_notifications" scope:"user,admin"`
|
||||
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:"-"`
|
||||
Id int64 `gorm:"primary_key;column:id" json:"id" yaml:"id"`
|
||||
Name string `gorm:"column:name" json:"name" yaml:"name"`
|
||||
Domain string `gorm:"column:domain" json:"domain" yaml:"domain" private:"true" scope:"user,admin"`
|
||||
Expected null.NullString `gorm:"column:expected" json:"expected" yaml:"expected" scope:"user,admin"`
|
||||
ExpectedStatus int `gorm:"default:200;column:expected_status" json:"expected_status" yaml:"expected_status" scope:"user,admin"`
|
||||
Interval int `gorm:"default:30;column:check_interval" json:"check_interval" yaml:"check_interval"`
|
||||
Type string `gorm:"column:check_type" json:"type" scope:"user,admin" yaml:"type"`
|
||||
Method string `gorm:"column:method" json:"method" scope:"user,admin" yaml:"method"`
|
||||
PostData null.NullString `gorm:"column:post_data" json:"post_data" scope:"user,admin" yaml:"post_data"`
|
||||
Port int `gorm:"not null;column:port" json:"port" scope:"user,admin" yaml:"port"`
|
||||
Timeout int `gorm:"default:30;column:timeout" json:"timeout" scope:"user,admin" yaml:"timeout"`
|
||||
Order int `gorm:"default:0;column:order_id" json:"order_id" yaml:"order_id"`
|
||||
VerifySSL null.NullBool `gorm:"default:false;column:verify_ssl" json:"verify_ssl" scope:"user,admin" yaml:"verify_ssl"`
|
||||
Public null.NullBool `gorm:"default:true;column:public" json:"public" yaml:"public"`
|
||||
GroupId int `gorm:"default:0;column:group_id" json:"group_id" yaml:"group_id"`
|
||||
TLSCert null.NullString `gorm:"column:tls_cert" json:"tls_cert" scope:"user,admin" yaml:"tls_cert"`
|
||||
TLSCertKey null.NullString `gorm:"column:tls_cert_key" json:"tls_cert_key" scope:"user,admin" yaml:"tls_cert_key"`
|
||||
TLSCertRoot null.NullString `gorm:"column:tls_cert_root" json:"tls_cert_root" scope:"user,admin" yaml:"tls_cert_root"`
|
||||
Headers null.NullString `gorm:"column:headers" json:"headers" scope:"user,admin" yaml:"headers"`
|
||||
Permalink null.NullString `gorm:"column:permalink" json:"permalink" yaml:"permalink"`
|
||||
Redirect null.NullBool `gorm:"default:false;column:redirect" json:"redirect" scope:"user,admin" yaml:"redirect"`
|
||||
CreatedAt time.Time `gorm:"column:created_at" json:"created_at" yaml:"-"`
|
||||
UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at" yaml:"-"`
|
||||
Online bool `gorm:"-" json:"online" yaml:"-"`
|
||||
Latency int64 `gorm:"-" json:"latency" yaml:"-"`
|
||||
PingTime int64 `gorm:"-" json:"ping_time" yaml:"-"`
|
||||
Online24Hours float32 `gorm:"-" json:"online_24_hours" yaml:"-"`
|
||||
Online7Days float32 `gorm:"-" json:"online_7_days" yaml:"-"`
|
||||
AvgResponse int64 `gorm:"-" json:"avg_response" yaml:"-"`
|
||||
FailuresLast24Hours int `gorm:"-" json:"failures_24_hours" yaml:"-"`
|
||||
Running chan bool `gorm:"-" json:"-" yaml:"-"`
|
||||
Checkpoint time.Time `gorm:"-" json:"-" yaml:"-"`
|
||||
SleepDuration time.Duration `gorm:"-" json:"-" yaml:"-"`
|
||||
LastResponse string `gorm:"-" json:"-" yaml:"-"`
|
||||
NotifyAfter int64 `gorm:"column:notify_after" json:"notify_after" yaml:"notify_after" scope:"user,admin"`
|
||||
AllowNotifications null.NullBool `gorm:"default:true;column:allow_notifications" json:"allow_notifications" yaml:"allow_notifications" scope:"user,admin"`
|
||||
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:"-"`
|
||||
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