mirror of https://github.com/statping/statping
Fix gorm issue when saving zero values in 'ExpectedStatus' field
Any zero value like 0, '', false won't be saved into the database for those fields defined default value, you might want to use pointer type or Scanner/Valuer to avoid this. See: https://gorm.io/docs/create.html#Default-Valuespull/943/head
parent
fdeb7fe31a
commit
63baf4a83a
|
@ -208,9 +208,9 @@ func CheckGrpc(s *Service, record bool) (*Service, error) {
|
||||||
s.Online = true
|
s.Online = true
|
||||||
|
|
||||||
if s.GrpcHealthCheck.Bool {
|
if s.GrpcHealthCheck.Bool {
|
||||||
if s.ExpectedStatus != s.LastStatusCode {
|
if *s.ExpectedStatus != s.LastStatusCode {
|
||||||
if record {
|
if record {
|
||||||
RecordFailure(s, fmt.Sprintf("GRPC Service: '%s', Status Code: expected '%v', got '%v'", s.Name, s.ExpectedStatus, s.LastStatusCode), "response_code")
|
RecordFailure(s, fmt.Sprintf("GRPC Service: '%s', Status Code: expected '%v', got '%v'", s.Name, *s.ExpectedStatus, s.LastStatusCode), "response_code")
|
||||||
}
|
}
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
@ -381,9 +381,9 @@ func CheckHttp(s *Service, record bool) (*Service, error) {
|
||||||
return s, err
|
return s, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if s.ExpectedStatus != res.StatusCode {
|
if *s.ExpectedStatus != res.StatusCode {
|
||||||
if record {
|
if record {
|
||||||
RecordFailure(s, fmt.Sprintf("HTTP Status Code %v did not match %v", res.StatusCode, s.ExpectedStatus), "status_code")
|
RecordFailure(s, fmt.Sprintf("HTTP Status Code %v did not match %v", res.StatusCode, *s.ExpectedStatus), "status_code")
|
||||||
}
|
}
|
||||||
return s, err
|
return s, err
|
||||||
}
|
}
|
||||||
|
@ -462,9 +462,9 @@ func CheckSsh(s *Service, record bool) (*Service, error) {
|
||||||
s.Online = true
|
s.Online = true
|
||||||
|
|
||||||
if s.SshHealthCheck.Bool {
|
if s.SshHealthCheck.Bool {
|
||||||
if s.ExpectedStatus != s.LastStatusCode {
|
if *s.ExpectedStatus != s.LastStatusCode {
|
||||||
if record {
|
if record {
|
||||||
RecordFailure(s, fmt.Sprintf("SSH Service: '%s', Exit Code: expected '%v', got '%v'", s.Name, s.ExpectedStatus, s.LastStatusCode), "response_code")
|
RecordFailure(s, fmt.Sprintf("SSH Service: '%s', Exit Code: expected '%v', got '%v'", s.Name, *s.ExpectedStatus, s.LastStatusCode), "response_code")
|
||||||
}
|
}
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,7 +31,7 @@ var testdata = []struct {
|
||||||
Domain: "localhost",
|
Domain: "localhost",
|
||||||
Port: 50053,
|
Port: 50053,
|
||||||
Expected: null.NewNullString("status:SERVING"),
|
Expected: null.NewNullString("status:SERVING"),
|
||||||
ExpectedStatus: 1,
|
ExpectedStatus: &[]int{1}[0],
|
||||||
Type: "grpc",
|
Type: "grpc",
|
||||||
Timeout: 3,
|
Timeout: 3,
|
||||||
VerifySSL: null.NewNullBool(false),
|
VerifySSL: null.NewNullBool(false),
|
||||||
|
@ -47,7 +47,7 @@ var testdata = []struct {
|
||||||
Domain: "localhost",
|
Domain: "localhost",
|
||||||
Port: 50054,
|
Port: 50054,
|
||||||
Expected: null.NewNullString(""),
|
Expected: null.NewNullString(""),
|
||||||
ExpectedStatus: 0,
|
ExpectedStatus: &[]int{0}[0],
|
||||||
Type: "grpc",
|
Type: "grpc",
|
||||||
Timeout: 1,
|
Timeout: 1,
|
||||||
VerifySSL: null.NewNullBool(true),
|
VerifySSL: null.NewNullBool(true),
|
||||||
|
@ -63,7 +63,7 @@ var testdata = []struct {
|
||||||
Domain: "localhost",
|
Domain: "localhost",
|
||||||
Port: 50055,
|
Port: 50055,
|
||||||
Expected: null.NewNullString(""),
|
Expected: null.NewNullString(""),
|
||||||
ExpectedStatus: 0,
|
ExpectedStatus: &[]int{0}[0],
|
||||||
Type: "grpc",
|
Type: "grpc",
|
||||||
Timeout: 1,
|
Timeout: 1,
|
||||||
VerifySSL: null.NewNullBool(false),
|
VerifySSL: null.NewNullBool(false),
|
||||||
|
@ -78,7 +78,7 @@ var testdata = []struct {
|
||||||
Domain: "localhost",
|
Domain: "localhost",
|
||||||
Port: 1000,
|
Port: 1000,
|
||||||
Expected: null.NewNullString(""),
|
Expected: null.NewNullString(""),
|
||||||
ExpectedStatus: 0,
|
ExpectedStatus: &[]int{0}[0],
|
||||||
Type: "grpc",
|
Type: "grpc",
|
||||||
Timeout: 1,
|
Timeout: 1,
|
||||||
VerifySSL: null.NewNullBool(false),
|
VerifySSL: null.NewNullBool(false),
|
||||||
|
@ -94,7 +94,7 @@ var testdata = []struct {
|
||||||
Domain: "localhost",
|
Domain: "localhost",
|
||||||
Port: 1000,
|
Port: 1000,
|
||||||
Expected: null.NewNullString(""),
|
Expected: null.NewNullString(""),
|
||||||
ExpectedStatus: 0,
|
ExpectedStatus: &[]int{0}[0],
|
||||||
Type: "grpc",
|
Type: "grpc",
|
||||||
Timeout: 1,
|
Timeout: 1,
|
||||||
VerifySSL: null.NewNullBool(true),
|
VerifySSL: null.NewNullBool(true),
|
||||||
|
@ -110,7 +110,7 @@ var testdata = []struct {
|
||||||
Domain: "http://localhost",
|
Domain: "http://localhost",
|
||||||
Port: 50058,
|
Port: 50058,
|
||||||
Expected: null.NewNullString("status:SERVING"),
|
Expected: null.NewNullString("status:SERVING"),
|
||||||
ExpectedStatus: 1,
|
ExpectedStatus: &[]int{1}[0],
|
||||||
Type: "grpc",
|
Type: "grpc",
|
||||||
Timeout: 1,
|
Timeout: 1,
|
||||||
VerifySSL: null.NewNullBool(false),
|
VerifySSL: null.NewNullBool(false),
|
||||||
|
@ -126,7 +126,7 @@ var testdata = []struct {
|
||||||
Domain: "http://local//host",
|
Domain: "http://local//host",
|
||||||
Port: 50059,
|
Port: 50059,
|
||||||
Expected: null.NewNullString(""),
|
Expected: null.NewNullString(""),
|
||||||
ExpectedStatus: 0,
|
ExpectedStatus: &[]int{0}[0],
|
||||||
Type: "grpc",
|
Type: "grpc",
|
||||||
Timeout: 1,
|
Timeout: 1,
|
||||||
VerifySSL: null.NewNullBool(false),
|
VerifySSL: null.NewNullBool(false),
|
||||||
|
@ -142,7 +142,7 @@ var testdata = []struct {
|
||||||
Domain: "https://google.com",
|
Domain: "https://google.com",
|
||||||
Port: 443,
|
Port: 443,
|
||||||
Expected: null.NewNullString(""),
|
Expected: null.NewNullString(""),
|
||||||
ExpectedStatus: 0,
|
ExpectedStatus: &[]int{0}[0],
|
||||||
Type: "grpc",
|
Type: "grpc",
|
||||||
Timeout: 1,
|
Timeout: 1,
|
||||||
VerifySSL: null.NewNullBool(false),
|
VerifySSL: null.NewNullBool(false),
|
||||||
|
@ -157,7 +157,7 @@ var testdata = []struct {
|
||||||
Domain: "http://localhost",
|
Domain: "http://localhost",
|
||||||
Port: 50061,
|
Port: 50061,
|
||||||
Expected: null.NewNullString(""),
|
Expected: null.NewNullString(""),
|
||||||
ExpectedStatus: 0,
|
ExpectedStatus: &[]int{0}[0],
|
||||||
Type: "grpc",
|
Type: "grpc",
|
||||||
Timeout: 1,
|
Timeout: 1,
|
||||||
VerifySSL: null.NewNullBool(false),
|
VerifySSL: null.NewNullBool(false),
|
||||||
|
@ -194,7 +194,7 @@ func TestCheckGrpc(t *testing.T) {
|
||||||
server := v.grpcService(v.clientChecker.Port, v.clientChecker.GrpcHealthCheck.Bool)
|
server := v.grpcService(v.clientChecker.Port, v.clientChecker.GrpcHealthCheck.Bool)
|
||||||
defer server.Stop()
|
defer server.Stop()
|
||||||
v.clientChecker.CheckService(false)
|
v.clientChecker.CheckService(false)
|
||||||
if v.clientChecker.LastStatusCode != v.clientChecker.ExpectedStatus || strings.TrimSpace(v.clientChecker.LastResponse) != v.clientChecker.Expected.String {
|
if v.clientChecker.LastStatusCode != *v.clientChecker.ExpectedStatus || strings.TrimSpace(v.clientChecker.LastResponse) != v.clientChecker.Expected.String {
|
||||||
t.Errorf("Expected message: '%v', Got message: '%v' , Expected Status: '%v', Got Status: '%v'", v.clientChecker.Expected.String, v.clientChecker.LastResponse, v.clientChecker.ExpectedStatus, v.clientChecker.LastStatusCode)
|
t.Errorf("Expected message: '%v', Got message: '%v' , Expected Status: '%v', Got Status: '%v'", v.clientChecker.Expected.String, v.clientChecker.LastResponse, v.clientChecker.ExpectedStatus, v.clientChecker.LastStatusCode)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
@ -12,7 +12,7 @@ func Example(online bool) Service {
|
||||||
Name: "Statping Example",
|
Name: "Statping Example",
|
||||||
Domain: "https://statping.com",
|
Domain: "https://statping.com",
|
||||||
Expected: null.NewNullString(""),
|
Expected: null.NewNullString(""),
|
||||||
ExpectedStatus: 200,
|
ExpectedStatus: &[]int{200}[0],
|
||||||
Interval: int(time.Duration(15 * time.Second).Seconds()),
|
Interval: int(time.Duration(15 * time.Second).Seconds()),
|
||||||
Type: "http",
|
Type: "http",
|
||||||
Method: "get",
|
Method: "get",
|
||||||
|
@ -63,7 +63,7 @@ func Samples() error {
|
||||||
s1 := &Service{
|
s1 := &Service{
|
||||||
Name: "Google",
|
Name: "Google",
|
||||||
Domain: "https://google.com",
|
Domain: "https://google.com",
|
||||||
ExpectedStatus: 200,
|
ExpectedStatus: &[]int{200}[0],
|
||||||
Interval: 10,
|
Interval: 10,
|
||||||
Type: "http",
|
Type: "http",
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
|
@ -84,7 +84,7 @@ func Samples() error {
|
||||||
s2 := &Service{
|
s2 := &Service{
|
||||||
Name: "Statping Github",
|
Name: "Statping Github",
|
||||||
Domain: "https://github.com/statping/statping",
|
Domain: "https://github.com/statping/statping",
|
||||||
ExpectedStatus: 200,
|
ExpectedStatus: &[]int{200}[0],
|
||||||
Interval: 30,
|
Interval: 30,
|
||||||
Type: "http",
|
Type: "http",
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
|
@ -103,7 +103,7 @@ func Samples() error {
|
||||||
s3 := &Service{
|
s3 := &Service{
|
||||||
Name: "JSON Users Test",
|
Name: "JSON Users Test",
|
||||||
Domain: "https://jsonplaceholder.typicode.com/users",
|
Domain: "https://jsonplaceholder.typicode.com/users",
|
||||||
ExpectedStatus: 200,
|
ExpectedStatus: &[]int{200}[0],
|
||||||
Interval: 60,
|
Interval: 60,
|
||||||
Type: "http",
|
Type: "http",
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
|
@ -122,7 +122,7 @@ func Samples() error {
|
||||||
s4 := &Service{
|
s4 := &Service{
|
||||||
Name: "JSON API Tester",
|
Name: "JSON API Tester",
|
||||||
Domain: "https://jsonplaceholder.typicode.com/posts",
|
Domain: "https://jsonplaceholder.typicode.com/posts",
|
||||||
ExpectedStatus: 201,
|
ExpectedStatus: &[]int{201}[0],
|
||||||
Expected: null.NewNullString(`(title)": "((\\"|[statping])*)"`),
|
Expected: null.NewNullString(`(title)": "((\\"|[statping])*)"`),
|
||||||
Interval: 30,
|
Interval: 30,
|
||||||
Type: "http",
|
Type: "http",
|
||||||
|
|
|
@ -26,7 +26,7 @@ import (
|
||||||
var example = &Service{
|
var example = &Service{
|
||||||
Name: "Example Service",
|
Name: "Example Service",
|
||||||
Domain: "https://statping.com",
|
Domain: "https://statping.com",
|
||||||
ExpectedStatus: 200,
|
ExpectedStatus: &[]int{200}[0],
|
||||||
Interval: 30,
|
Interval: 30,
|
||||||
Type: "http",
|
Type: "http",
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
|
@ -237,7 +237,7 @@ func TestServices(t *testing.T) {
|
||||||
e := &Service{
|
e := &Service{
|
||||||
Name: "Example HTTP",
|
Name: "Example HTTP",
|
||||||
Domain: "http://localhost:15000",
|
Domain: "http://localhost:15000",
|
||||||
ExpectedStatus: 200,
|
ExpectedStatus: &[]int{200}[0],
|
||||||
Type: "http",
|
Type: "http",
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
Timeout: 5,
|
Timeout: 5,
|
||||||
|
@ -255,7 +255,7 @@ func TestServices(t *testing.T) {
|
||||||
e := &Service{
|
e := &Service{
|
||||||
Name: "Example TLS",
|
Name: "Example TLS",
|
||||||
Domain: "http://localhost:15001",
|
Domain: "http://localhost:15001",
|
||||||
ExpectedStatus: 200,
|
ExpectedStatus: &[]int{200}[0],
|
||||||
Type: "http",
|
Type: "http",
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
Timeout: 5,
|
Timeout: 5,
|
||||||
|
@ -276,7 +276,7 @@ func TestServices(t *testing.T) {
|
||||||
e := &Service{
|
e := &Service{
|
||||||
Name: "Example TLS HTTP",
|
Name: "Example TLS HTTP",
|
||||||
Domain: "https://localhost:15001",
|
Domain: "https://localhost:15001",
|
||||||
ExpectedStatus: 200,
|
ExpectedStatus: &[]int{200}[0],
|
||||||
Type: "http",
|
Type: "http",
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
Timeout: 15,
|
Timeout: 15,
|
||||||
|
@ -487,7 +487,7 @@ func TestServices(t *testing.T) {
|
||||||
example := &Service{
|
example := &Service{
|
||||||
Name: "Example Service 2",
|
Name: "Example Service 2",
|
||||||
Domain: "https://slack.statping.com",
|
Domain: "https://slack.statping.com",
|
||||||
ExpectedStatus: 200,
|
ExpectedStatus: &[]int{200}[0],
|
||||||
Interval: 10,
|
Interval: 10,
|
||||||
Type: "http",
|
Type: "http",
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
|
|
|
@ -16,7 +16,7 @@ type Service struct {
|
||||||
Name string `gorm:"column:name" json:"name" yaml:"name"`
|
Name string `gorm:"column:name" json:"name" yaml:"name"`
|
||||||
Domain string `gorm:"column:domain" json:"domain" yaml:"domain" private:"true" scope:"user,admin"`
|
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"`
|
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"`
|
ExpectedStatus *int `gorm:"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"`
|
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"`
|
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"`
|
Method string `gorm:"column:method" json:"method" scope:"user,admin" yaml:"method"`
|
||||||
|
|
Loading…
Reference in New Issue