statping/core/services_test.go

412 lines
11 KiB
Go
Raw Normal View History

2018-12-04 05:57:11 +00:00
// Statping
2018-08-16 06:22:20 +00:00
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
2018-12-04 04:17:29 +00:00
// https://github.com/hunterlong/statping
2018-08-16 06:22:20 +00:00
//
// 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-07-27 04:45:42 +00:00
package core
import (
2018-12-04 04:17:29 +00:00
"github.com/hunterlong/statping/types"
"github.com/hunterlong/statping/utils"
2018-07-27 04:45:42 +00:00
"github.com/stretchr/testify/assert"
"testing"
2018-08-19 00:37:00 +00:00
"time"
2018-07-27 04:45:42 +00:00
)
var (
newServiceId int64
)
func TestSelectHTTPService(t *testing.T) {
2018-10-11 16:53:13 +00:00
services, err := CoreApp.SelectAllServices(false)
assert.Nil(t, err)
assert.Equal(t, 15, len(services))
assert.Equal(t, "Google", services[0].Name)
assert.Equal(t, "http", services[0].Type)
}
2018-07-27 04:45:42 +00:00
func TestSelectAllServices(t *testing.T) {
services := CoreApp.Services
for _, s := range services {
service := s.(*Service)
2018-10-07 08:16:38 +00:00
service.Check(false)
2018-10-11 16:53:13 +00:00
assert.False(t, service.IsRunning())
t.Logf("ID: %v %v\n", service.Id, service.Name)
}
assert.Equal(t, 15, len(services))
2018-07-28 01:50:13 +00:00
}
2018-09-16 10:29:52 +00:00
func TestServiceDowntime(t *testing.T) {
2018-11-02 23:19:52 +00:00
t.SkipNow()
2018-09-16 10:29:52 +00:00
service := SelectService(15)
downtime := service.Downtime()
2018-11-01 16:24:44 +00:00
assert.True(t, downtime.Seconds() > 0)
2018-09-16 10:29:52 +00:00
}
2018-07-28 01:50:13 +00:00
func TestSelectTCPService(t *testing.T) {
services := CoreApp.Services
assert.Equal(t, 15, len(services))
2018-07-28 01:50:13 +00:00
service := SelectService(5)
assert.NotNil(t, service)
2018-08-19 00:37:00 +00:00
assert.Equal(t, "Google DNS", service.Name)
assert.Equal(t, "tcp", service.Type)
2018-07-27 04:45:42 +00:00
}
func TestUpdateService(t *testing.T) {
service := SelectService(1)
2018-08-19 00:37:00 +00:00
assert.Equal(t, "Google", service.Name)
service.Name = "Updated Google"
service.Interval = 5
err := service.Update(true)
assert.Nil(t, err)
// check if updating pointer array shutdown any other service
service = SelectService(1)
assert.Equal(t, "Updated Google", service.Name)
assert.Equal(t, 5, service.Interval)
2018-07-27 04:45:42 +00:00
}
2018-08-19 00:37:00 +00:00
func TestUpdateAllServices(t *testing.T) {
2018-10-11 16:53:13 +00:00
services, err := CoreApp.SelectAllServices(false)
2018-08-19 00:37:00 +00:00
assert.Nil(t, err)
for k, srv := range services {
2018-08-19 00:37:00 +00:00
srv.Name = "Changed " + srv.Name
srv.Interval = k + 3
err := srv.Update(true)
assert.Nil(t, err)
2018-08-19 00:37:00 +00:00
}
}
2018-07-27 04:45:42 +00:00
func TestServiceHTTPCheck(t *testing.T) {
service := SelectService(1)
service.Check(true)
assert.Equal(t, "Changed Updated Google", service.Name)
assert.True(t, service.Online)
2018-07-27 04:45:42 +00:00
}
2018-07-28 01:50:13 +00:00
func TestCheckHTTPService(t *testing.T) {
2018-08-19 00:37:00 +00:00
service := SelectService(1)
assert.Equal(t, "Changed Updated Google", service.Name)
2018-07-27 04:45:42 +00:00
assert.True(t, service.Online)
assert.Equal(t, 200, service.LastStatusCode)
assert.NotZero(t, service.Latency)
assert.NotZero(t, service.PingTime)
2018-07-27 04:45:42 +00:00
}
2018-07-28 01:50:13 +00:00
func TestServiceTCPCheck(t *testing.T) {
service := SelectService(5)
service.Check(true)
assert.Equal(t, "Changed Google DNS", service.Name)
assert.True(t, service.Online)
2018-07-28 01:50:13 +00:00
}
func TestCheckTCPService(t *testing.T) {
2018-08-19 00:37:00 +00:00
service := SelectService(5)
assert.Equal(t, "Changed Google DNS", service.Name)
2018-07-28 01:50:13 +00:00
assert.True(t, service.Online)
assert.NotZero(t, service.Latency)
assert.NotZero(t, service.PingTime)
2018-07-28 01:50:13 +00:00
}
func TestServiceOnline24Hours(t *testing.T) {
since := utils.Now().Add(-24 * time.Hour).Add(-10 * time.Minute)
service := SelectService(1)
assert.Equal(t, float32(100), service.OnlineSince(since))
service2 := SelectService(5)
assert.Equal(t, float32(100), service2.OnlineSince(since))
service3 := SelectService(14)
assert.True(t, service3.OnlineSince(since) > float32(49))
}
func TestServiceSmallText(t *testing.T) {
service := SelectService(5)
text := service.SmallText()
assert.Contains(t, text, "Online since")
}
func TestServiceAvgUptime(t *testing.T) {
since := utils.Now().Add(-24 * time.Hour).Add(-10 * time.Minute)
service := SelectService(1)
2018-09-10 09:27:53 +00:00
assert.NotEqual(t, "0.00", service.AvgUptime(since))
service2 := SelectService(5)
assert.Equal(t, "100", service2.AvgUptime(since))
service3 := SelectService(13)
2018-09-15 03:02:27 +00:00
assert.NotEqual(t, "0", service3.AvgUptime(since))
service4 := SelectService(15)
assert.NotEqual(t, "0", service4.AvgUptime(since))
}
func TestServiceHits(t *testing.T) {
service := SelectService(5)
hits, err := service.Hits()
assert.Nil(t, err)
2018-10-06 09:50:39 +00:00
assert.True(t, len(hits) > 1400)
}
func TestServiceLimitedHits(t *testing.T) {
service := SelectService(5)
hits, err := service.LimitedHits(1024)
assert.Nil(t, err)
assert.Equal(t, int(1024), len(hits))
}
func TestServiceTotalHits(t *testing.T) {
service := SelectService(5)
hits, err := service.TotalHits()
assert.Nil(t, err)
2018-10-03 10:47:32 +00:00
assert.NotZero(t, hits)
}
func TestServiceSum(t *testing.T) {
service := SelectService(5)
2019-01-29 19:14:55 +00:00
sum := service.Sum()
assert.NotZero(t, sum)
}
func TestCountOnline(t *testing.T) {
amount := CoreApp.CountOnline()
assert.True(t, amount >= 2)
}
2018-07-27 04:45:42 +00:00
func TestCreateService(t *testing.T) {
s := ReturnService(&types.Service{
2018-08-19 00:37:00 +00:00
Name: "That'll do 🐢",
Domain: "https://www.youtube.com/watch?v=rjQtzV9IZ0Q",
2018-07-27 04:45:42 +00:00
ExpectedStatus: 200,
2018-08-19 00:37:00 +00:00
Interval: 3,
2018-07-27 04:45:42 +00:00
Type: "http",
Method: "GET",
Timeout: 20,
2019-01-03 19:13:48 +00:00
GroupId: 1,
})
2018-07-27 04:45:42 +00:00
var err error
newServiceId, err = s.Create(false)
2018-07-27 04:45:42 +00:00
assert.Nil(t, err)
assert.NotZero(t, newServiceId)
2018-08-19 00:37:00 +00:00
newService := SelectService(newServiceId)
assert.Equal(t, "That'll do 🐢", newService.Name)
}
func TestViewNewService(t *testing.T) {
newService := SelectService(newServiceId)
assert.Equal(t, "That'll do 🐢", newService.Name)
2018-07-27 04:45:42 +00:00
}
2018-07-28 01:50:13 +00:00
func TestCreateFailingHTTPService(t *testing.T) {
s := ReturnService(&types.Service{
2018-07-28 01:50:13 +00:00
Name: "Bad URL",
Domain: "http://localhost/iamnothere",
ExpectedStatus: 200,
2018-08-19 00:37:00 +00:00
Interval: 2,
2018-07-28 01:50:13 +00:00
Type: "http",
Method: "GET",
Timeout: 5,
2019-01-03 19:13:48 +00:00
GroupId: 1,
})
2018-07-28 01:50:13 +00:00
var err error
newServiceId, err = s.Create(false)
2018-07-28 01:50:13 +00:00
assert.Nil(t, err)
assert.NotZero(t, newServiceId)
2018-08-19 00:37:00 +00:00
newService := SelectService(newServiceId)
2018-07-28 01:50:13 +00:00
assert.Equal(t, "Bad URL", newService.Name)
t.Log("new service ID: ", newServiceId)
2018-07-28 01:50:13 +00:00
}
func TestServiceFailedCheck(t *testing.T) {
service := SelectService(17)
assert.Equal(t, "Bad URL", service.Name)
2018-10-06 03:35:53 +00:00
service.Check(false)
assert.Equal(t, "Bad URL", service.Name)
assert.False(t, service.Online)
2018-07-28 01:50:13 +00:00
}
func TestCreateFailingTCPService(t *testing.T) {
s := ReturnService(&types.Service{
2018-07-28 01:50:13 +00:00
Name: "Bad TCP",
Domain: "localhost",
Port: 5050,
Interval: 30,
Type: "tcp",
Timeout: 5,
2019-01-03 19:13:48 +00:00
GroupId: 1,
})
2018-07-28 01:50:13 +00:00
var err error
newServiceId, err = s.Create(false)
2018-07-28 01:50:13 +00:00
assert.Nil(t, err)
assert.NotZero(t, newServiceId)
2018-08-19 00:37:00 +00:00
newService := SelectService(newServiceId)
2018-07-28 01:50:13 +00:00
assert.Equal(t, "Bad TCP", newService.Name)
t.Log("new failing tcp service ID: ", newServiceId)
2018-07-28 01:50:13 +00:00
}
func TestServiceFailedTCPCheck(t *testing.T) {
service := SelectService(newServiceId)
2018-10-06 03:35:53 +00:00
service.Check(false)
assert.Equal(t, "Bad TCP", service.Name)
assert.False(t, service.Online)
2018-07-28 01:50:13 +00:00
}
func TestCreateServiceFailure(t *testing.T) {
fail := &types.Failure{
Issue: "This is not an issue, but it would container HTTP response errors.",
Method: "http",
}
2018-07-28 01:50:13 +00:00
service := SelectService(8)
id, err := service.CreateFailure(fail)
2018-07-28 01:50:13 +00:00
assert.Nil(t, err)
assert.NotZero(t, id)
}
2018-07-27 04:45:42 +00:00
func TestDeleteService(t *testing.T) {
2018-08-19 00:37:00 +00:00
service := SelectService(newServiceId)
2018-07-27 04:45:42 +00:00
2018-10-11 16:53:13 +00:00
count, err := CoreApp.SelectAllServices(false)
2018-07-27 04:45:42 +00:00
assert.Nil(t, err)
assert.Equal(t, 18, len(count))
2018-07-27 04:45:42 +00:00
err = service.Delete()
2018-07-27 04:45:42 +00:00
assert.Nil(t, err)
services := CoreApp.Services
assert.Equal(t, 17, len(services))
2018-07-27 04:45:42 +00:00
}
2018-08-19 00:37:00 +00:00
func TestServiceCloseRoutine(t *testing.T) {
s := ReturnService(new(types.Service))
2018-08-19 00:37:00 +00:00
s.Name = "example"
s.Domain = "https://google.com"
s.Type = "http"
s.Method = "GET"
s.ExpectedStatus = 200
s.Interval = 1
s.Start()
assert.True(t, s.IsRunning())
2018-08-21 01:22:42 +00:00
t.Log(s.Checkpoint)
t.Log(s.SleepDuration)
go s.CheckQueue(false)
2018-08-21 01:22:42 +00:00
t.Log(s.Checkpoint)
t.Log(s.SleepDuration)
2018-08-21 01:22:42 +00:00
time.Sleep(5 * time.Second)
t.Log(s.Checkpoint)
t.Log(s.SleepDuration)
2018-08-21 01:22:42 +00:00
assert.True(t, s.IsRunning())
s.Close()
assert.False(t, s.IsRunning())
s.Close()
assert.False(t, s.IsRunning())
}
func TestServiceCheckQueue(t *testing.T) {
s := ReturnService(new(types.Service))
s.Name = "example"
s.Domain = "https://google.com"
s.Type = "http"
s.Method = "GET"
s.ExpectedStatus = 200
s.Interval = 1
s.Start()
assert.True(t, s.IsRunning())
go s.CheckQueue(false)
go func() {
time.Sleep(5 * time.Second)
t.Log(s.Checkpoint)
time.Sleep(6 * time.Second)
}()
2018-08-19 00:37:00 +00:00
time.Sleep(5 * time.Second)
assert.True(t, s.IsRunning())
s.Close()
assert.False(t, s.IsRunning())
s.Close()
assert.False(t, s.IsRunning())
2018-08-19 00:37:00 +00:00
}
func TestDNScheckService(t *testing.T) {
s := ReturnService(new(types.Service))
s.Name = "example"
s.Domain = "http://localhost:9000"
s.Type = "http"
s.Method = "GET"
s.ExpectedStatus = 200
s.Interval = 1
amount, err := s.dnsCheck()
assert.Nil(t, err)
assert.NotZero(t, amount)
}
2019-02-11 21:10:17 +00:00
func TestSelectServiceLink(t *testing.T) {
service := SelectService(1)
assert.Equal(t, "google", service.Permalink.String)
}
func TestDbtimestamp(t *testing.T) {
2019-12-30 08:08:51 +00:00
CoreApp.Config.DbConn = "mysql"
2019-02-11 21:10:17 +00:00
query := Dbtimestamp("minute", "latency")
assert.Equal(t, "CONCAT(date_format(created_at, '%Y-%m-%d %H:00:00')) AS timeframe, AVG(latency) AS value", query)
2019-12-30 08:08:51 +00:00
CoreApp.Config.DbConn = "postgres"
2019-02-11 21:10:17 +00:00
query = Dbtimestamp("minute", "latency")
assert.Equal(t, "date_trunc('minute', created_at) AS timeframe, AVG(latency) AS value", query)
2019-12-30 08:08:51 +00:00
CoreApp.Config.DbConn = "sqlite"
2019-02-11 21:10:17 +00:00
query = Dbtimestamp("minute", "latency")
assert.Equal(t, "datetime((strftime('%s', created_at) / 60) * 60, 'unixepoch') AS timeframe, AVG(latency) as value", query)
}
func TestGroup_Create(t *testing.T) {
group := &Group{&types.Group{
Name: "Testing",
}}
newGroupId, err := group.Create()
assert.Nil(t, err)
assert.NotZero(t, newGroupId)
}
func TestGroup_Services(t *testing.T) {
group := SelectGroup(1)
assert.NotEmpty(t, group.Services())
}
func TestSelectGroups(t *testing.T) {
groups := SelectGroups(true, false)
assert.Equal(t, int(3), len(groups))
groups = SelectGroups(true, true)
2019-02-20 18:49:04 +00:00
assert.Equal(t, int(5), len(groups))
2019-02-11 21:10:17 +00:00
}
func TestService_TotalFailures(t *testing.T) {
service := SelectService(8)
failures, err := service.TotalFailures()
assert.Nil(t, err)
assert.Equal(t, uint64(1), failures)
}
func TestService_TotalFailures24(t *testing.T) {
service := SelectService(8)
failures, err := service.TotalFailures24()
assert.Nil(t, err)
assert.Equal(t, uint64(1), failures)
}
func TestService_TotalFailuresOnDate(t *testing.T) {
2019-04-11 22:25:59 +00:00
t.SkipNow()
ago := utils.Now().UTC()
2019-02-11 21:10:17 +00:00
service := SelectService(8)
failures, err := service.TotalFailuresOnDate(ago)
assert.Nil(t, err)
2019-02-20 18:49:04 +00:00
assert.Equal(t, uint64(1), failures)
2019-02-11 21:10:17 +00:00
}
func TestCountFailures(t *testing.T) {
failures := CountFailures()
2019-02-20 19:31:31 +00:00
assert.NotEqual(t, uint64(0), failures)
2019-02-11 21:10:17 +00:00
}