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-Values
pull/943/head
Luis Gustavo S. Barreto 2021-01-18 16:05:55 -03:00
parent fdeb7fe31a
commit 63baf4a83a
5 changed files with 27 additions and 27 deletions

View File

@ -208,9 +208,9 @@ func CheckGrpc(s *Service, record bool) (*Service, error) {
s.Online = true
if s.GrpcHealthCheck.Bool {
if s.ExpectedStatus != s.LastStatusCode {
if *s.ExpectedStatus != s.LastStatusCode {
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
}
@ -381,9 +381,9 @@ func CheckHttp(s *Service, record bool) (*Service, error) {
return s, err
}
}
if s.ExpectedStatus != res.StatusCode {
if *s.ExpectedStatus != res.StatusCode {
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
}
@ -462,9 +462,9 @@ func CheckSsh(s *Service, record bool) (*Service, error) {
s.Online = true
if s.SshHealthCheck.Bool {
if s.ExpectedStatus != s.LastStatusCode {
if *s.ExpectedStatus != s.LastStatusCode {
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
}

View File

@ -31,7 +31,7 @@ var testdata = []struct {
Domain: "localhost",
Port: 50053,
Expected: null.NewNullString("status:SERVING"),
ExpectedStatus: 1,
ExpectedStatus: &[]int{1}[0],
Type: "grpc",
Timeout: 3,
VerifySSL: null.NewNullBool(false),
@ -47,7 +47,7 @@ var testdata = []struct {
Domain: "localhost",
Port: 50054,
Expected: null.NewNullString(""),
ExpectedStatus: 0,
ExpectedStatus: &[]int{0}[0],
Type: "grpc",
Timeout: 1,
VerifySSL: null.NewNullBool(true),
@ -63,7 +63,7 @@ var testdata = []struct {
Domain: "localhost",
Port: 50055,
Expected: null.NewNullString(""),
ExpectedStatus: 0,
ExpectedStatus: &[]int{0}[0],
Type: "grpc",
Timeout: 1,
VerifySSL: null.NewNullBool(false),
@ -78,7 +78,7 @@ var testdata = []struct {
Domain: "localhost",
Port: 1000,
Expected: null.NewNullString(""),
ExpectedStatus: 0,
ExpectedStatus: &[]int{0}[0],
Type: "grpc",
Timeout: 1,
VerifySSL: null.NewNullBool(false),
@ -94,7 +94,7 @@ var testdata = []struct {
Domain: "localhost",
Port: 1000,
Expected: null.NewNullString(""),
ExpectedStatus: 0,
ExpectedStatus: &[]int{0}[0],
Type: "grpc",
Timeout: 1,
VerifySSL: null.NewNullBool(true),
@ -110,7 +110,7 @@ var testdata = []struct {
Domain: "http://localhost",
Port: 50058,
Expected: null.NewNullString("status:SERVING"),
ExpectedStatus: 1,
ExpectedStatus: &[]int{1}[0],
Type: "grpc",
Timeout: 1,
VerifySSL: null.NewNullBool(false),
@ -126,7 +126,7 @@ var testdata = []struct {
Domain: "http://local//host",
Port: 50059,
Expected: null.NewNullString(""),
ExpectedStatus: 0,
ExpectedStatus: &[]int{0}[0],
Type: "grpc",
Timeout: 1,
VerifySSL: null.NewNullBool(false),
@ -142,7 +142,7 @@ var testdata = []struct {
Domain: "https://google.com",
Port: 443,
Expected: null.NewNullString(""),
ExpectedStatus: 0,
ExpectedStatus: &[]int{0}[0],
Type: "grpc",
Timeout: 1,
VerifySSL: null.NewNullBool(false),
@ -157,7 +157,7 @@ var testdata = []struct {
Domain: "http://localhost",
Port: 50061,
Expected: null.NewNullString(""),
ExpectedStatus: 0,
ExpectedStatus: &[]int{0}[0],
Type: "grpc",
Timeout: 1,
VerifySSL: null.NewNullBool(false),
@ -194,7 +194,7 @@ func TestCheckGrpc(t *testing.T) {
server := v.grpcService(v.clientChecker.Port, v.clientChecker.GrpcHealthCheck.Bool)
defer server.Stop()
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)
}
})

View File

@ -12,7 +12,7 @@ func Example(online bool) Service {
Name: "Statping Example",
Domain: "https://statping.com",
Expected: null.NewNullString(""),
ExpectedStatus: 200,
ExpectedStatus: &[]int{200}[0],
Interval: int(time.Duration(15 * time.Second).Seconds()),
Type: "http",
Method: "get",
@ -63,7 +63,7 @@ func Samples() error {
s1 := &Service{
Name: "Google",
Domain: "https://google.com",
ExpectedStatus: 200,
ExpectedStatus: &[]int{200}[0],
Interval: 10,
Type: "http",
Method: "GET",
@ -84,7 +84,7 @@ func Samples() error {
s2 := &Service{
Name: "Statping Github",
Domain: "https://github.com/statping/statping",
ExpectedStatus: 200,
ExpectedStatus: &[]int{200}[0],
Interval: 30,
Type: "http",
Method: "GET",
@ -103,7 +103,7 @@ func Samples() error {
s3 := &Service{
Name: "JSON Users Test",
Domain: "https://jsonplaceholder.typicode.com/users",
ExpectedStatus: 200,
ExpectedStatus: &[]int{200}[0],
Interval: 60,
Type: "http",
Method: "GET",
@ -122,7 +122,7 @@ func Samples() error {
s4 := &Service{
Name: "JSON API Tester",
Domain: "https://jsonplaceholder.typicode.com/posts",
ExpectedStatus: 201,
ExpectedStatus: &[]int{201}[0],
Expected: null.NewNullString(`(title)": "((\\"|[statping])*)"`),
Interval: 30,
Type: "http",

View File

@ -26,7 +26,7 @@ import (
var example = &Service{
Name: "Example Service",
Domain: "https://statping.com",
ExpectedStatus: 200,
ExpectedStatus: &[]int{200}[0],
Interval: 30,
Type: "http",
Method: "GET",
@ -237,7 +237,7 @@ func TestServices(t *testing.T) {
e := &Service{
Name: "Example HTTP",
Domain: "http://localhost:15000",
ExpectedStatus: 200,
ExpectedStatus: &[]int{200}[0],
Type: "http",
Method: "GET",
Timeout: 5,
@ -255,7 +255,7 @@ func TestServices(t *testing.T) {
e := &Service{
Name: "Example TLS",
Domain: "http://localhost:15001",
ExpectedStatus: 200,
ExpectedStatus: &[]int{200}[0],
Type: "http",
Method: "GET",
Timeout: 5,
@ -276,7 +276,7 @@ func TestServices(t *testing.T) {
e := &Service{
Name: "Example TLS HTTP",
Domain: "https://localhost:15001",
ExpectedStatus: 200,
ExpectedStatus: &[]int{200}[0],
Type: "http",
Method: "GET",
Timeout: 15,
@ -487,7 +487,7 @@ func TestServices(t *testing.T) {
example := &Service{
Name: "Example Service 2",
Domain: "https://slack.statping.com",
ExpectedStatus: 200,
ExpectedStatus: &[]int{200}[0],
Interval: 10,
Type: "http",
Method: "GET",

View File

@ -16,7 +16,7 @@ type Service struct {
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"`
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"`
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"`