mirror of
https://github.com/eosswedenorg/antelope-api-healthcheck
synced 2026-08-27 20:58:13 +02:00
Refactor: move internal package from src/ to internal/ and move src/main.go to cmd/eosio-api-healthcheck/main.go
This commit is contained in:
parent
c27abb5ed9
commit
6448aeb0f7
22 changed files with 19 additions and 17 deletions
50
internal/api/debug.go
Normal file
50
internal/api/debug.go
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
|
||||
package api
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
||||
)
|
||||
|
||||
type DebugApi struct {
|
||||
response agentcheck.Response
|
||||
}
|
||||
|
||||
func parseResponse(resp string) (agentcheck.Response, error) {
|
||||
|
||||
parts := strings.SplitN(resp, "#", 2)
|
||||
|
||||
// Status with message
|
||||
if len(parts) > 1 {
|
||||
rtype := agentcheck.StatusMessageResponseType(parts[0])
|
||||
return agentcheck.NewStatusMessageResponse(rtype, parts[1]), nil
|
||||
}
|
||||
|
||||
// Only status.
|
||||
rtype := agentcheck.StatusResponseType(parts[0])
|
||||
return agentcheck.NewStatusResponse(rtype), nil
|
||||
}
|
||||
|
||||
func DebugApiFactory(args ApiArguments) ApiInterface {
|
||||
return NewDebugApi(args.Url)
|
||||
}
|
||||
|
||||
func NewDebugApi(response string) DebugApi {
|
||||
|
||||
resp, _ := parseResponse(response)
|
||||
|
||||
return DebugApi{
|
||||
response: resp,
|
||||
}
|
||||
}
|
||||
|
||||
func (d DebugApi) LogInfo() LogParams {
|
||||
return LogParams{
|
||||
"type", "Debug",
|
||||
"response", strings.TrimSpace(d.response.String()),
|
||||
}
|
||||
}
|
||||
|
||||
func (d DebugApi) Call() (agentcheck.Response, string) {
|
||||
return d.response, ""
|
||||
}
|
||||
68
internal/api/debug_test.go
Normal file
68
internal/api/debug_test.go
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
||||
)
|
||||
|
||||
func TestDebugApiFactory(t *testing.T) {
|
||||
|
||||
api := DebugApiFactory(ApiArguments{
|
||||
Url: "up",
|
||||
Host: "host",
|
||||
NumBlocks: 40,
|
||||
})
|
||||
|
||||
assert.IsType(t, DebugApi{}, api)
|
||||
assert.Equal(t, api.(DebugApi).response, agentcheck.NewStatusResponse(agentcheck.Up))
|
||||
}
|
||||
|
||||
func TestNewDebugApi(t *testing.T) {
|
||||
type args struct {
|
||||
response string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want DebugApi
|
||||
}{
|
||||
{"Up", args{"up"}, DebugApi{response: agentcheck.NewStatusResponse(agentcheck.Up)}},
|
||||
{"Down", args{"down"}, DebugApi{response: agentcheck.NewStatusResponse("down")}},
|
||||
{"DownMessage", args{"down#some message"}, DebugApi{response: agentcheck.NewStatusMessageResponse(agentcheck.Down, "some message")}},
|
||||
{"Ready", args{"ready"}, DebugApi{response: agentcheck.NewStatusResponse(agentcheck.Ready)}},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := NewDebugApi(tt.args.response); !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("NewDebugApi() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDebugApi_LogInfo(t *testing.T) {
|
||||
|
||||
expected := LogParams{"type", "Debug", "response", "up"}
|
||||
|
||||
api := DebugApi{
|
||||
response: agentcheck.NewStatusResponse(agentcheck.Up),
|
||||
}
|
||||
|
||||
assert.Equal(t, api.LogInfo(), expected)
|
||||
}
|
||||
|
||||
func TestDebugApi_Call(t *testing.T) {
|
||||
|
||||
expected := agentcheck.NewStatusMessageResponse(agentcheck.Stopped, "message")
|
||||
|
||||
api := DebugApi{
|
||||
response: expected,
|
||||
}
|
||||
|
||||
response, msg := api.Call()
|
||||
|
||||
assert.Equal(t, response, expected)
|
||||
assert.Equal(t, msg, "")
|
||||
}
|
||||
81
internal/api/eosio_contract.go
Normal file
81
internal/api/eosio_contract.go
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/eosswedenorg/eosio-api-healthcheck/internal/utils"
|
||||
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
||||
contract_api "github.com/eosswedenorg-go/eos-contract-api-client"
|
||||
)
|
||||
|
||||
type EosioContract struct {
|
||||
utils.Time
|
||||
client contract_api.Client
|
||||
block_time float64
|
||||
}
|
||||
|
||||
func EosioContractFactory(args ApiArguments) ApiInterface {
|
||||
return NewEosioContract(args.Url, float64(args.NumBlocks / 2))
|
||||
}
|
||||
|
||||
func NewEosioContract(url string, block_time float64) EosioContract {
|
||||
return EosioContract{
|
||||
client: contract_api.Client{
|
||||
Url: url,
|
||||
},
|
||||
block_time: block_time,
|
||||
}
|
||||
}
|
||||
|
||||
func (e EosioContract) LogInfo() LogParams {
|
||||
return LogParams{
|
||||
"type", "eosio-contract",
|
||||
"url", e.client.Url,
|
||||
"block_time", e.block_time,
|
||||
}
|
||||
}
|
||||
|
||||
func (e EosioContract) Call() (agentcheck.Response, string) {
|
||||
|
||||
h, err := e.client.GetHealth()
|
||||
if err != nil {
|
||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
||||
return resp, err.Error()
|
||||
}
|
||||
|
||||
// Check HTTP Status Code
|
||||
if h.HTTPStatusCode > 299 {
|
||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||
msg := "Taking offline because %v was received from backend"
|
||||
return resp, fmt.Sprintf(msg, h.HTTPStatusCode)
|
||||
}
|
||||
|
||||
// Check postgres
|
||||
if h.Data.Postgres.Status != "OK" {
|
||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||
msg := "Taking offline because Postgres reported '%s'"
|
||||
return resp, fmt.Sprintf(msg, h.Data.Postgres.Status)
|
||||
}
|
||||
|
||||
// Check redis
|
||||
if h.Data.Redis.Status != "OK" {
|
||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||
msg := "Taking offline because Redis reported '%s'"
|
||||
return resp, fmt.Sprintf(msg, h.Data.Redis.Status)
|
||||
}
|
||||
|
||||
// Validate head block.
|
||||
diff := e.GetTime().Sub(h.Data.Chain.HeadTime).Seconds()
|
||||
|
||||
if diff > e.block_time {
|
||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||
msg := "Taking offline because head block is lagging %.0f seconds"
|
||||
return resp, fmt.Sprintf(msg, diff)
|
||||
} else if diff < -e.block_time {
|
||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||
msg := "Taking offline because head block is %.0f seconds into the future"
|
||||
return resp, fmt.Sprintf(msg, diff)
|
||||
}
|
||||
|
||||
return agentcheck.NewStatusResponse(agentcheck.Up), "OK"
|
||||
}
|
||||
312
internal/api/eosio_contract_test.go
Normal file
312
internal/api/eosio_contract_test.go
Normal file
|
|
@ -0,0 +1,312 @@
|
|||
|
||||
package api
|
||||
|
||||
import (
|
||||
"time"
|
||||
"testing"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
||||
)
|
||||
|
||||
func TestEosioContractFactory(t *testing.T) {
|
||||
|
||||
api := EosioContractFactory(ApiArguments{
|
||||
Url: "https://atomic.example.com",
|
||||
NumBlocks: 120,
|
||||
})
|
||||
|
||||
expected := NewEosioContract("https://atomic.example.com", 60)
|
||||
|
||||
assert.IsType(t, expected, api)
|
||||
assert.Equal(t, expected.client.Url, api.(EosioContract).client.Url)
|
||||
assert.Equal(t, expected.client.Host, api.(EosioContract).client.Host)
|
||||
assert.Equal(t, expected.block_time, api.(EosioContract).block_time)
|
||||
}
|
||||
|
||||
func TestEosioContractLogInfo(t *testing.T) {
|
||||
|
||||
api := NewEosioContract("https://atomic.example.com", 120)
|
||||
|
||||
expected := LogParams{"type","eosio-contract","url","https://atomic.example.com","block_time",float64(120)}
|
||||
|
||||
assert.Equal(t, expected, api.LogInfo())
|
||||
}
|
||||
|
||||
func TestEosioContractSetTime(t *testing.T) {
|
||||
|
||||
expected := time.Date(2019, 3, 18, 20, 29, 32, 0, time.UTC)
|
||||
|
||||
api := NewEosioContract("", 60)
|
||||
// Assert that time is NOW (+-10 seconds)
|
||||
assert.InDelta(t, api.GetTime().Unix(), time.Now().In(time.UTC).Unix(), float64(10))
|
||||
|
||||
api.SetTime(expected)
|
||||
assert.Equal(t, expected, api.GetTime())
|
||||
}
|
||||
|
||||
func TestEosioContractJsonFailure(t *testing.T) {
|
||||
|
||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||
res.Write([]byte(`!//{invalid-json}!##`))
|
||||
}))
|
||||
|
||||
api := NewEosioContract(srv.URL, 120)
|
||||
check, _ := api.Call()
|
||||
|
||||
expected := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
||||
assert.Equal(t, expected, check)
|
||||
}
|
||||
|
||||
func TestEosioContractHTTP500Down(t *testing.T) {
|
||||
|
||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||
res.Header().Add("Content-type", "application/json; charset=utf-8")
|
||||
res.WriteHeader(500)
|
||||
res.Write([]byte(`{}`))
|
||||
}))
|
||||
|
||||
api := NewEosioContract(srv.URL, 120)
|
||||
check, status := api.Call()
|
||||
|
||||
assert.Equal(t, "Taking offline because 500 was received from backend", status)
|
||||
|
||||
expected := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||
assert.Equal(t, expected, check)
|
||||
}
|
||||
|
||||
func TestEosioContractLaggingUp(t *testing.T) {
|
||||
|
||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||
if req.URL.String() == "/health" {
|
||||
payload := `{
|
||||
"success":true,
|
||||
"data":{
|
||||
"version":"1.0.0",
|
||||
"postgres":{
|
||||
"status":"OK"
|
||||
},
|
||||
"redis":{
|
||||
"status":"OK"
|
||||
},
|
||||
"chain":{
|
||||
"status":"OK",
|
||||
"head_block":2173612361,
|
||||
"head_time":1759953927000
|
||||
}
|
||||
},
|
||||
"query_time":1759953929542
|
||||
}`
|
||||
|
||||
res.Header().Add("Content-type", "application/json; charset=utf-8")
|
||||
res.Write([]byte(payload))
|
||||
}
|
||||
}))
|
||||
|
||||
api := NewEosioContract(srv.URL, 120)
|
||||
api.SetTime(time.Date(2025, 10, 8, 20, 7, 27, 0, time.UTC))
|
||||
|
||||
check, status := api.Call()
|
||||
|
||||
assert.Equal(t, "OK", status)
|
||||
|
||||
expected := agentcheck.NewStatusResponse(agentcheck.Up)
|
||||
assert.Equal(t, expected, check)
|
||||
}
|
||||
|
||||
func TestEosioContractLaggingDown(t *testing.T) {
|
||||
|
||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||
if req.URL.String() == "/health" {
|
||||
payload := `{
|
||||
"success":true,
|
||||
"data":{
|
||||
"version":"1.0.0",
|
||||
"postgres":{
|
||||
"status":"OK"
|
||||
},
|
||||
"redis":{
|
||||
"status":"OK"
|
||||
},
|
||||
"chain":{
|
||||
"status":"OK",
|
||||
"head_block":213671263812,
|
||||
"head_time":1533451894000
|
||||
}
|
||||
},
|
||||
"query_time":1533451895542
|
||||
}`
|
||||
|
||||
res.Header().Add("Content-type", "application/json; charset=utf-8")
|
||||
res.Write([]byte(payload))
|
||||
}
|
||||
}))
|
||||
|
||||
api := NewEosioContract(srv.URL, 120)
|
||||
api.SetTime(time.Date(2018, 8, 5, 6, 53, 35, 0, time.UTC))
|
||||
|
||||
check, status := api.Call()
|
||||
|
||||
assert.Equal(t, "Taking offline because head block is lagging 121 seconds", status)
|
||||
|
||||
expected := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||
assert.Equal(t, expected, check)
|
||||
}
|
||||
|
||||
func TestEosioContractInFutureUp(t *testing.T) {
|
||||
|
||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||
if req.URL.String() == "/health" {
|
||||
payload := `{
|
||||
"success":true,
|
||||
"data":{
|
||||
"version":"1.0.0",
|
||||
"postgres":{
|
||||
"status":"OK"
|
||||
},
|
||||
"redis":{
|
||||
"status":"OK"
|
||||
},
|
||||
"chain":{
|
||||
"status":"OK",
|
||||
"head_block":213671263812,
|
||||
"head_time":1728954676500
|
||||
}
|
||||
},
|
||||
"query_time":1728954678231
|
||||
}`
|
||||
|
||||
res.Header().Add("Content-type", "application/json; charset=utf-8")
|
||||
res.Write([]byte(payload))
|
||||
}
|
||||
}))
|
||||
|
||||
api := NewEosioContract(srv.URL, 120)
|
||||
api.SetTime(time.Date(2024, 10, 15, 1, 9, 16, 500, time.UTC))
|
||||
|
||||
check, status := api.Call()
|
||||
|
||||
assert.Equal(t, "OK", status)
|
||||
|
||||
expected := agentcheck.NewStatusResponse(agentcheck.Up)
|
||||
assert.Equal(t, expected, check)
|
||||
}
|
||||
|
||||
func TestEosioContractInFutureDown(t *testing.T) {
|
||||
|
||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||
if req.URL.String() == "/health" {
|
||||
payload := `{
|
||||
"success":true,
|
||||
"data":{
|
||||
"version":"1.0.0",
|
||||
"postgres":{
|
||||
"status":"OK"
|
||||
},
|
||||
"redis":{
|
||||
"status":"OK"
|
||||
},
|
||||
"chain":{
|
||||
"status":"OK",
|
||||
"head_block":213671263812,
|
||||
"head_time":1041122824500
|
||||
}
|
||||
},
|
||||
"query_time":1041122832231
|
||||
}`
|
||||
|
||||
res.Header().Add("Content-type", "application/json; charset=utf-8")
|
||||
res.Write([]byte(payload))
|
||||
}
|
||||
}))
|
||||
|
||||
api := NewEosioContract(srv.URL, 120)
|
||||
api.SetTime(time.Date(2002, 12, 29, 0, 45, 03, 500, time.UTC))
|
||||
|
||||
check, status := api.Call()
|
||||
|
||||
assert.Equal(t, "Taking offline because head block is -121 seconds into the future", status)
|
||||
|
||||
expected := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||
assert.Equal(t, expected, check)
|
||||
}
|
||||
|
||||
|
||||
func TestEosioContractRedisDown(t *testing.T) {
|
||||
|
||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||
if req.URL.String() == "/health" {
|
||||
payload := `{
|
||||
"success":true,
|
||||
"data":{
|
||||
"version":"1.0.0",
|
||||
"postgres":{
|
||||
"status":"OK"
|
||||
},
|
||||
"redis":{
|
||||
"status":"DOWN"
|
||||
},
|
||||
"chain":{
|
||||
"status":"OK",
|
||||
"head_block":213671263812,
|
||||
"head_time":1426072770500
|
||||
}
|
||||
},
|
||||
"query_time":1426072775872
|
||||
}`
|
||||
|
||||
res.Header().Add("Content-type", "application/json; charset=utf-8")
|
||||
res.Write([]byte(payload))
|
||||
}
|
||||
}))
|
||||
|
||||
api := NewEosioContract(srv.URL, 120)
|
||||
api.SetTime(time.Date(2015, 3, 11, 11, 19, 30, 500, time.UTC))
|
||||
|
||||
check, status := api.Call()
|
||||
|
||||
assert.Equal(t, "Taking offline because Redis reported 'DOWN'", status)
|
||||
|
||||
expected := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||
assert.Equal(t, expected, check)
|
||||
}
|
||||
|
||||
func TestEosioContractPostgresDown(t *testing.T) {
|
||||
|
||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||
if req.URL.String() == "/health" {
|
||||
payload := `{
|
||||
"success":true,
|
||||
"data":{
|
||||
"version":"1.0.0",
|
||||
"postgres":{
|
||||
"status":"DOWN"
|
||||
},
|
||||
"redis":{
|
||||
"status":"OK"
|
||||
},
|
||||
"chain":{
|
||||
"status":"OK",
|
||||
"head_block":213671263812,
|
||||
"head_time":1562868371500
|
||||
}
|
||||
},
|
||||
"query_time":156286837143
|
||||
}`
|
||||
|
||||
res.Header().Add("Content-type", "application/json; charset=utf-8")
|
||||
res.Write([]byte(payload))
|
||||
}
|
||||
}))
|
||||
|
||||
api := NewEosioContract(srv.URL, 120)
|
||||
api.SetTime(time.Date(2019, 7, 11, 18, 6, 11, 500, time.UTC))
|
||||
|
||||
check, status := api.Call()
|
||||
|
||||
assert.Equal(t, "Taking offline because Postgres reported 'DOWN'", status)
|
||||
|
||||
expected := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||
assert.Equal(t, expected, check)
|
||||
}
|
||||
82
internal/api/eosio_v1.go
Normal file
82
internal/api/eosio_v1.go
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/eosswedenorg/eosio-api-healthcheck/internal/utils"
|
||||
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
||||
"github.com/eosswedenorg-go/eosapi"
|
||||
)
|
||||
|
||||
type EosioV1 struct {
|
||||
utils.Time
|
||||
client eosapi.Client
|
||||
block_time float64
|
||||
}
|
||||
|
||||
func EosioV1Factory(args ApiArguments) ApiInterface {
|
||||
return NewEosioV1(args.Url, args.Host, float64(args.NumBlocks / 2))
|
||||
}
|
||||
|
||||
func NewEosioV1(url string, host string, block_time float64) EosioV1 {
|
||||
|
||||
api := EosioV1{
|
||||
client: *eosapi.New(url),
|
||||
block_time: block_time,
|
||||
}
|
||||
|
||||
api.client.Host = host
|
||||
|
||||
return api
|
||||
}
|
||||
|
||||
func (e EosioV1) LogInfo() LogParams {
|
||||
p := LogParams{
|
||||
"type", "eosio-v1",
|
||||
"url", e.client.Url,
|
||||
}
|
||||
|
||||
if len(e.client.Host) > 0 {
|
||||
p.Add("host", e.client.Host)
|
||||
}
|
||||
|
||||
p.Add("block_time", e.block_time)
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
func (e EosioV1) Call() (agentcheck.Response, string) {
|
||||
|
||||
info, err := e.client.GetInfo()
|
||||
if err != nil {
|
||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
||||
return resp, err.Error()
|
||||
}
|
||||
|
||||
// Check HTTP Status Code
|
||||
if info.HTTPStatusCode > 299 {
|
||||
|
||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||
|
||||
msg := "Taking offline because %v was received from backend"
|
||||
return resp, fmt.Sprintf(msg, info.HTTPStatusCode)
|
||||
}
|
||||
|
||||
// Validate head block.
|
||||
diff := e.GetTime().Sub(info.HeadBlockTime).Seconds()
|
||||
|
||||
if diff > e.block_time {
|
||||
|
||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||
|
||||
msg := "Taking offline because head block is lagging %.0f seconds"
|
||||
return resp, fmt.Sprintf(msg, diff)
|
||||
} else if diff < -e.block_time {
|
||||
|
||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||
|
||||
msg := "Taking offline because head block is %.0f seconds into the future"
|
||||
return resp, fmt.Sprintf(msg, diff)
|
||||
}
|
||||
return agentcheck.NewStatusResponse(agentcheck.Up), "OK"
|
||||
}
|
||||
174
internal/api/eosio_v1_test.go
Normal file
174
internal/api/eosio_v1_test.go
Normal file
|
|
@ -0,0 +1,174 @@
|
|||
|
||||
package api
|
||||
|
||||
import (
|
||||
"time"
|
||||
"testing"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
||||
)
|
||||
|
||||
func TestEosioV1Factory(t *testing.T) {
|
||||
|
||||
api := EosioV1Factory(ApiArguments{
|
||||
Url: "https://api.v1.example.com",
|
||||
Host: "host.example.com",
|
||||
NumBlocks: 120,
|
||||
})
|
||||
|
||||
expected := NewEosioV1("https://api.v1.example.com", "host.example.com", 60)
|
||||
|
||||
assert.IsType(t, expected, api)
|
||||
assert.Equal(t, expected.client.Url, api.(EosioV1).client.Url)
|
||||
assert.Equal(t, expected.client.Host, api.(EosioV1).client.Host)
|
||||
assert.Equal(t, expected.block_time, api.(EosioV1).block_time)
|
||||
}
|
||||
|
||||
func TestEosioV1LogInfo(t *testing.T) {
|
||||
|
||||
api := NewEosioV1("https://api.v1.example.com", "host.example.com", 120)
|
||||
|
||||
expected := LogParams{"type","eosio-v1","url","https://api.v1.example.com","host","host.example.com","block_time",float64(120)}
|
||||
|
||||
assert.Equal(t, expected, api.LogInfo())
|
||||
}
|
||||
|
||||
func TestEosioV1SetTime(t *testing.T) {
|
||||
|
||||
expected := time.Date(2022, 2, 24, 13, 38, 0, 0, time.UTC)
|
||||
|
||||
api := NewEosioV1("", "", 60)
|
||||
// Assert that time is NOW (+-10 seconds)
|
||||
assert.InDelta(t, api.GetTime().Unix(), time.Now().In(time.UTC).Unix(), float64(10))
|
||||
|
||||
api.SetTime(expected)
|
||||
assert.Equal(t, expected, api.GetTime())
|
||||
}
|
||||
|
||||
func TestEosioV1JsonFailure(t *testing.T) {
|
||||
|
||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||
res.Write([]byte(`!//{invalid-json}!##`))
|
||||
}))
|
||||
|
||||
api := NewEosioV1(srv.URL, "", 120)
|
||||
check, _ := api.Call()
|
||||
|
||||
expected := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
||||
assert.Equal(t, expected, check)
|
||||
}
|
||||
|
||||
func TestEosioV1HTTP500Down(t *testing.T) {
|
||||
|
||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||
res.WriteHeader(500)
|
||||
res.Write([]byte(`{}`))
|
||||
}))
|
||||
|
||||
api := NewEosioV1(srv.URL, "", 120)
|
||||
check, status := api.Call()
|
||||
|
||||
assert.Equal(t, "Taking offline because 500 was received from backend", status)
|
||||
|
||||
expected := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||
assert.Equal(t, expected, check)
|
||||
}
|
||||
|
||||
func TestEosioV1LaggingUp(t *testing.T) {
|
||||
|
||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||
if req.URL.String() == "/v1/chain/get_info" {
|
||||
info := `{
|
||||
"server_version": "8f613ec9",
|
||||
"head_block_num": 7272812,
|
||||
"head_block_time": "2022-02-24T13:37:00"
|
||||
}`
|
||||
|
||||
res.Write([]byte(info))
|
||||
}
|
||||
}))
|
||||
|
||||
api := NewEosioV1(srv.URL, "", 60)
|
||||
api.SetTime(time.Date(2022, 2, 24, 13, 38, 0, 0, time.UTC))
|
||||
check, status := api.Call()
|
||||
|
||||
assert.Equal(t, "OK", status)
|
||||
|
||||
expected := agentcheck.NewStatusResponse(agentcheck.Up)
|
||||
assert.Equal(t, expected, check)
|
||||
}
|
||||
|
||||
func TestEosioV1LaggingDown(t *testing.T) {
|
||||
|
||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||
if req.URL.String() == "/v1/chain/get_info" {
|
||||
info := `{
|
||||
"server_version": "9a607cce",
|
||||
"head_block_num": 87263,
|
||||
"head_block_time": "2018-01-01T13:37:01"
|
||||
}`
|
||||
|
||||
res.Write([]byte(info))
|
||||
}
|
||||
}))
|
||||
|
||||
api := NewEosioV1(srv.URL, "", 60)
|
||||
api.SetTime(time.Date(2018, time.January, 1, 13, 38, 2, 0, time.UTC))
|
||||
check, status := api.Call()
|
||||
|
||||
assert.Equal(t, "Taking offline because head block is lagging 61 seconds", status)
|
||||
|
||||
expected := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||
assert.Equal(t, expected, check)
|
||||
}
|
||||
|
||||
func TestEosioV1TimeInFutureUP(t *testing.T) {
|
||||
|
||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||
if req.URL.String() == "/v1/chain/get_info" {
|
||||
info := `{
|
||||
"server_version": "d1bec8d3",
|
||||
"head_block_num": 548847,
|
||||
"head_block_time": "2020-09-22T09:32:00"
|
||||
}`
|
||||
|
||||
res.Write([]byte(info))
|
||||
}
|
||||
}))
|
||||
|
||||
api := NewEosioV1(srv.URL, "", 120)
|
||||
api.SetTime(time.Date(2020, 9, 22, 9, 30, 0, 0, time.UTC))
|
||||
check, status := api.Call()
|
||||
|
||||
assert.Equal(t, "OK", status)
|
||||
|
||||
expected := agentcheck.NewStatusResponse(agentcheck.Up)
|
||||
assert.Equal(t, expected, check)
|
||||
}
|
||||
|
||||
|
||||
func TestEosioV1TimeInFutureDown(t *testing.T) {
|
||||
|
||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||
if req.URL.String() == "/v1/chain/get_info" {
|
||||
info := `{
|
||||
"server_version": "c879d231",
|
||||
"head_block_num": 2637621,
|
||||
"head_block_time": "2019-04-14T12:02:01"
|
||||
}`
|
||||
|
||||
res.Write([]byte(info))
|
||||
}
|
||||
}))
|
||||
|
||||
api := NewEosioV1(srv.URL, "", 120)
|
||||
api.SetTime(time.Date(2019, time.April, 14, 12, 0, 0, 0, time.UTC))
|
||||
check, status := api.Call()
|
||||
|
||||
assert.Equal(t, "Taking offline because head block is -121 seconds into the future", status)
|
||||
|
||||
expected := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||
assert.Equal(t, expected, check)
|
||||
}
|
||||
92
internal/api/eosio_v2.go
Normal file
92
internal/api/eosio_v2.go
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/eosswedenorg/eosio-api-healthcheck/internal/utils"
|
||||
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
||||
"github.com/eosswedenorg-go/eosapi"
|
||||
)
|
||||
|
||||
type EosioV2 struct {
|
||||
client eosapi.Client
|
||||
offset int64
|
||||
}
|
||||
|
||||
func EosioV2Factory(args ApiArguments) ApiInterface {
|
||||
return NewEosioV2(args.Url, args.Host, int64(args.NumBlocks))
|
||||
}
|
||||
|
||||
func NewEosioV2(url string, host string, offset int64) EosioV2 {
|
||||
|
||||
api := EosioV2{
|
||||
client: *eosapi.New(url),
|
||||
offset: offset,
|
||||
}
|
||||
|
||||
api.client.Host = host
|
||||
|
||||
return api
|
||||
}
|
||||
|
||||
func (e EosioV2) LogInfo() LogParams {
|
||||
p := LogParams{
|
||||
"type", "eosio-v2",
|
||||
"url", e.client.Url,
|
||||
}
|
||||
|
||||
if len(e.client.Host) > 0 {
|
||||
p.Add("host", e.client.Host)
|
||||
}
|
||||
|
||||
p.Add("offset", e.offset)
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
func (e EosioV2) Call() (agentcheck.Response, string) {
|
||||
|
||||
health, err := e.client.GetHealth()
|
||||
if err != nil {
|
||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
||||
return resp, err.Error()
|
||||
}
|
||||
|
||||
// Check HTTP Status Code
|
||||
if health.HTTPStatusCode > 299 {
|
||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||
return resp, fmt.Sprintf("Taking offline because %v was received from backend", health.HTTPStatusCode)
|
||||
}
|
||||
|
||||
// Fetch elasticsearch and nodeos block numbers from json.
|
||||
var es_block int64 = 0
|
||||
var node_block int64 = 0
|
||||
|
||||
for _, v := range health.Health {
|
||||
if v.Name == "Elasticsearch" {
|
||||
es_block = utils.JsonGetInt64(v.Data["last_indexed_block"])
|
||||
} else if v.Name == "NodeosRPC" {
|
||||
node_block = utils.JsonGetInt64(v.Data["head_block_num"])
|
||||
}
|
||||
}
|
||||
|
||||
// Error out if ether or both are zero.
|
||||
if es_block == 0 || node_block == 0 {
|
||||
msg := fmt.Sprintf("Failed to get Elasticsearch and/or nodeos " +
|
||||
"block numbers (es: %d, eos: %d)", es_block, node_block)
|
||||
|
||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
||||
return resp, msg
|
||||
}
|
||||
|
||||
// Check if ES is behind or in the future.
|
||||
diff := node_block - es_block;
|
||||
if diff > e.offset {
|
||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||
return resp, fmt.Sprintf("Taking offline because Elastic is %d blocks behind", diff)
|
||||
} else if diff < -e.offset {
|
||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||
return resp, fmt.Sprintf("Taking offline because Elastic is %d blocks into the future", -1 * diff)
|
||||
}
|
||||
return agentcheck.NewStatusResponse(agentcheck.Up), "OK"
|
||||
}
|
||||
383
internal/api/eosio_v2_test.go
Normal file
383
internal/api/eosio_v2_test.go
Normal file
|
|
@ -0,0 +1,383 @@
|
|||
|
||||
package api
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
||||
)
|
||||
|
||||
func TestEosioV2Factory(t *testing.T) {
|
||||
|
||||
api := EosioV2Factory(ApiArguments{
|
||||
Url: "https://api.v2.example.com",
|
||||
Host: "host.example.com",
|
||||
NumBlocks: 120,
|
||||
})
|
||||
|
||||
expected := NewEosioV2("https://api.v2.example.com", "host.example.com", 120)
|
||||
|
||||
assert.IsType(t, expected, api)
|
||||
assert.Equal(t, expected.client.Url, api.(EosioV2).client.Url)
|
||||
assert.Equal(t, expected.client.Host, api.(EosioV2).client.Host)
|
||||
assert.Equal(t, expected.offset, api.(EosioV2).offset)
|
||||
}
|
||||
|
||||
func TestEosioV2LogInfo(t *testing.T) {
|
||||
|
||||
api := NewEosioV2("https://api.v2.example.com", "host.example.com", 120)
|
||||
|
||||
expected := LogParams{"type","eosio-v2","url","https://api.v2.example.com","host","host.example.com","offset",int64(120)}
|
||||
|
||||
assert.Equal(t, expected, api.LogInfo())
|
||||
}
|
||||
|
||||
func TestEosioV2JsonFailure(t *testing.T) {
|
||||
|
||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||
res.Write([]byte(`!//{invalid-json}!##`))
|
||||
}))
|
||||
|
||||
api := NewEosioV2(srv.URL, "", 120)
|
||||
check, _ := api.Call()
|
||||
|
||||
expected := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
||||
assert.Equal(t, expected, check)
|
||||
}
|
||||
|
||||
func TestEosioV2HTTP500Down(t *testing.T) {
|
||||
|
||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||
res.WriteHeader(500)
|
||||
res.Write([]byte(`{}`))
|
||||
}))
|
||||
|
||||
api := NewEosioV2(srv.URL, "", 120)
|
||||
check, status := api.Call()
|
||||
|
||||
assert.Equal(t, "Taking offline because 500 was received from backend", status)
|
||||
|
||||
expected := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||
assert.Equal(t, expected, check)
|
||||
}
|
||||
|
||||
func TestEosioV2LaggingUp(t *testing.T) {
|
||||
|
||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||
if req.URL.String() == "/v2/health" {
|
||||
info := `{
|
||||
"version": "1.0",
|
||||
"version_hash": "028d5a34463884fcbe2ecfd3c0fcb3b5d4d538f4fd64803c1ef7209c85f2f266",
|
||||
"host": "api.test.com:443",
|
||||
"health": [
|
||||
{
|
||||
"service": "NodeosRPC",
|
||||
"status": "OK",
|
||||
"service_data": {
|
||||
"head_block_num": 263148621,
|
||||
"head_block_time": "2022-08-17T14:16:36.000",
|
||||
"time_offset": 190,
|
||||
"last_irreversible_block": 263148296,
|
||||
"chain_id": "f8c74ccb7f9dea6f26a6d7f786809ddd1bce9fada3867f567dd83691b5348534"
|
||||
},
|
||||
"time": 1642174781678
|
||||
},
|
||||
{
|
||||
"service": "Elasticsearch",
|
||||
"status": "OK",
|
||||
"service_data": {
|
||||
"last_indexed_block": 263148121,
|
||||
"total_indexed_blocks": 263148121,
|
||||
"active_shards": "100.0%"
|
||||
},
|
||||
"time": 1642174781736
|
||||
}
|
||||
]
|
||||
}`
|
||||
|
||||
res.Write([]byte(info))
|
||||
}
|
||||
}))
|
||||
|
||||
api := NewEosioV2(srv.URL, "", 500)
|
||||
check, status := api.Call()
|
||||
|
||||
assert.Equal(t, "OK", status)
|
||||
|
||||
expected := agentcheck.NewStatusResponse(agentcheck.Up)
|
||||
assert.Equal(t, expected, check)
|
||||
}
|
||||
|
||||
func TestEosioV2LaggingDown(t *testing.T) {
|
||||
|
||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||
if req.URL.String() == "/v2/health" {
|
||||
info := `{
|
||||
"version": "1.0",
|
||||
"version_hash": "028d5a34463884fcbe2ecfd3c0fcb3b5d4d538f4fd64803c1ef7209c85f2f266",
|
||||
"host": "api.test.com:443",
|
||||
"health": [
|
||||
{
|
||||
"service": "NodeosRPC",
|
||||
"status": "OK",
|
||||
"service_data": {
|
||||
"head_block_num": 263148621,
|
||||
"head_block_time": "2022-08-17T14:16:36.000",
|
||||
"time_offset": 190,
|
||||
"last_irreversible_block": 263148296,
|
||||
"chain_id": "f8c74ccb7f9dea6f26a6d7f786809ddd1bce9fada3867f567dd83691b5348534"
|
||||
},
|
||||
"time": 1642174781678
|
||||
},
|
||||
{
|
||||
"service": "Elasticsearch",
|
||||
"status": "OK",
|
||||
"service_data": {
|
||||
"last_indexed_block": 263148121,
|
||||
"total_indexed_blocks": 263148121,
|
||||
"active_shards": "100.0%"
|
||||
},
|
||||
"time": 1642174781736
|
||||
}
|
||||
]
|
||||
}`
|
||||
|
||||
res.Write([]byte(info))
|
||||
}
|
||||
}))
|
||||
|
||||
api := NewEosioV2(srv.URL, "", 499)
|
||||
check, status := api.Call()
|
||||
|
||||
assert.Equal(t, "Taking offline because Elastic is 500 blocks behind", status)
|
||||
|
||||
expected := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||
assert.Equal(t, expected, check)
|
||||
}
|
||||
|
||||
func TestEosioV2LaggingESInFutureUP(t *testing.T) {
|
||||
|
||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||
if req.URL.String() == "/v2/health" {
|
||||
info := `{
|
||||
"version": "1.0",
|
||||
"version_hash": "028d5a34463884fcbe2ecfd3c0fcb3b5d4d538f4fd64803c1ef7209c85f2f266",
|
||||
"host": "api.test.com:443",
|
||||
"health": [
|
||||
{
|
||||
"service": "NodeosRPC",
|
||||
"status": "OK",
|
||||
"service_data": {
|
||||
"head_block_num": 263148621,
|
||||
"head_block_time": "2022-08-17T14:16:36.000",
|
||||
"time_offset": 190,
|
||||
"last_irreversible_block": 263148296,
|
||||
"chain_id": "f8c74ccb7f9dea6f26a6d7f786809ddd1bce9fada3867f567dd83691b5348534"
|
||||
},
|
||||
"time": 1642174781678
|
||||
},
|
||||
{
|
||||
"service": "Elasticsearch",
|
||||
"status": "OK",
|
||||
"service_data": {
|
||||
"last_indexed_block": 263148821,
|
||||
"total_indexed_blocks": 263148821,
|
||||
"active_shards": "100.0%"
|
||||
},
|
||||
"time": 1642174781736
|
||||
}
|
||||
]
|
||||
}`
|
||||
|
||||
res.Write([]byte(info))
|
||||
}
|
||||
}))
|
||||
|
||||
api := NewEosioV2(srv.URL, "", 200)
|
||||
check, status := api.Call()
|
||||
|
||||
assert.Equal(t, "OK", status)
|
||||
|
||||
expected := agentcheck.NewStatusResponse(agentcheck.Up)
|
||||
assert.Equal(t, expected, check)
|
||||
}
|
||||
|
||||
func TestEosioV2LaggingESInFutureDown(t *testing.T) {
|
||||
|
||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||
if req.URL.String() == "/v2/health" {
|
||||
info := `{
|
||||
"version": "1.0",
|
||||
"version_hash": "028d5a34463884fcbe2ecfd3c0fcb3b5d4d538f4fd64803c1ef7209c85f2f266",
|
||||
"host": "api.test.com:443",
|
||||
"health": [
|
||||
{
|
||||
"service": "NodeosRPC",
|
||||
"status": "OK",
|
||||
"service_data": {
|
||||
"head_block_num": 263148621,
|
||||
"head_block_time": "2022-08-17T14:16:36.000",
|
||||
"time_offset": 190,
|
||||
"last_irreversible_block": 263148296,
|
||||
"chain_id": "f8c74ccb7f9dea6f26a6d7f786809ddd1bce9fada3867f567dd83691b5348534"
|
||||
},
|
||||
"time": 1642174781678
|
||||
},
|
||||
{
|
||||
"service": "Elasticsearch",
|
||||
"status": "OK",
|
||||
"service_data": {
|
||||
"last_indexed_block": 263148822,
|
||||
"total_indexed_blocks": 263148822,
|
||||
"active_shards": "100.0%"
|
||||
},
|
||||
"time": 1642174781736
|
||||
}
|
||||
]
|
||||
}`
|
||||
|
||||
res.Write([]byte(info))
|
||||
}
|
||||
}))
|
||||
|
||||
api := NewEosioV2(srv.URL, "", 200)
|
||||
check, status := api.Call()
|
||||
|
||||
assert.Equal(t, "Taking offline because Elastic is 201 blocks into the future", status)
|
||||
|
||||
expected := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||
assert.Equal(t, expected, check)
|
||||
}
|
||||
|
||||
func TestEosioV2ElasticsFailed(t *testing.T) {
|
||||
|
||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||
if req.URL.String() == "/v2/health" {
|
||||
info := `{
|
||||
"version": "1.0",
|
||||
"version_hash": "028d5a34463884fcbe2ecfd3c0fcb3b5d4d538f4fd64803c1ef7209c85f2f266",
|
||||
"host": "api.test.com:443",
|
||||
"health": [
|
||||
{
|
||||
"service": "NodeosRPC",
|
||||
"status": "OK",
|
||||
"service_data": {
|
||||
"head_block_num": 263148621,
|
||||
"head_block_time": "2022-08-17T14:16:36.000",
|
||||
"time_offset": 190,
|
||||
"last_irreversible_block": 263148296,
|
||||
"chain_id": "f8c74ccb7f9dea6f26a6d7f786809ddd1bce9fada3867f567dd83691b5348534"
|
||||
},
|
||||
"time": 1660745796190
|
||||
},
|
||||
{
|
||||
"service": "Elasticsearch",
|
||||
"status": "DOWN",
|
||||
"service_data": {
|
||||
"last_indexed_block": 0,
|
||||
"total_indexed_blocks": 0,
|
||||
"active_shards": "0.0%"
|
||||
},
|
||||
"time": 1660745796204
|
||||
}
|
||||
]
|
||||
}`
|
||||
|
||||
res.Write([]byte(info))
|
||||
}
|
||||
}))
|
||||
|
||||
api := NewEosioV2(srv.URL, "", 500)
|
||||
check, status := api.Call()
|
||||
|
||||
assert.Equal(t, "Failed to get Elasticsearch and/or nodeos block numbers (es: 0, eos: 263148621)", status)
|
||||
|
||||
expected := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
||||
assert.Equal(t, expected, check)
|
||||
}
|
||||
|
||||
func TestEosioV2NodeosRPCFailed(t *testing.T) {
|
||||
|
||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||
if req.URL.String() == "/v2/health" {
|
||||
info := `{
|
||||
"version": "1.0",
|
||||
"version_hash": "028d5a34463884fcbe2ecfd3c0fcb3b5d4d538f4fd64803c1ef7209c85f2f266",
|
||||
"host": "api.test.com:443",
|
||||
"health": [
|
||||
{
|
||||
"service": "NodeosRPC",
|
||||
"status": "DOWN",
|
||||
"service_data": {
|
||||
"head_block_num": 0,
|
||||
"head_block_time": "",
|
||||
"time_offset": 0,
|
||||
"last_irreversible_block": 0,
|
||||
"chain_id": ""
|
||||
},
|
||||
"time": 1642174781678
|
||||
},
|
||||
{
|
||||
"service": "Elasticsearch",
|
||||
"status": "DOWN",
|
||||
"service_data": {
|
||||
"last_indexed_block": 263148121,
|
||||
"total_indexed_blocks": 263148121,
|
||||
"active_shards": "100.0%"
|
||||
},
|
||||
"time": 1642174781736
|
||||
}
|
||||
]
|
||||
}`
|
||||
|
||||
res.Write([]byte(info))
|
||||
}
|
||||
}))
|
||||
|
||||
api := NewEosioV2(srv.URL, "", 500)
|
||||
check, status := api.Call()
|
||||
|
||||
assert.Equal(t, "Failed to get Elasticsearch and/or nodeos block numbers (es: 263148121, eos: 0)", status)
|
||||
|
||||
expected := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
||||
assert.Equal(t, expected, check)
|
||||
}
|
||||
|
||||
func TestEosioV2ElasticsNodeosRPCFailed(t *testing.T) {
|
||||
|
||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||
if req.URL.String() == "/v2/health" {
|
||||
info := `{
|
||||
"version": "1.0",
|
||||
"version_hash": "028d5a34463884fcbe2ecfd3c0fcb3b5d4d538f4fd64803c1ef7209c85f2f266",
|
||||
"host": "api.test.com:443",
|
||||
"health": [
|
||||
{
|
||||
"service": "NodeosRPC",
|
||||
"status": "DOWN",
|
||||
"service_data": {},
|
||||
"time": 1642174781678
|
||||
},
|
||||
{
|
||||
"service": "Elasticsearch",
|
||||
"status": "DOWN",
|
||||
"service_data": {},
|
||||
"time": 1642174781736
|
||||
}
|
||||
]
|
||||
}`
|
||||
|
||||
res.Write([]byte(info))
|
||||
}
|
||||
}))
|
||||
|
||||
api := NewEosioV2(srv.URL, "", 500)
|
||||
check, status := api.Call()
|
||||
|
||||
assert.Equal(t, "Failed to get Elasticsearch and/or nodeos block numbers (es: 0, eos: 0)", status)
|
||||
|
||||
expected := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
||||
assert.Equal(t, expected, check)
|
||||
}
|
||||
33
internal/api/interface.go
Normal file
33
internal/api/interface.go
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
||||
)
|
||||
|
||||
/**
|
||||
* Generic struct that is passed to factory functions
|
||||
* to configure the API request.
|
||||
*/
|
||||
type ApiArguments struct {
|
||||
Url string
|
||||
Host string
|
||||
NumBlocks int
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory function
|
||||
*
|
||||
* Each API must implement this function and process `args`
|
||||
* returing a instance of it's implementation of the ApiInterface
|
||||
*/
|
||||
type Factory func(args ApiArguments) ApiInterface
|
||||
|
||||
type ApiInterface interface {
|
||||
|
||||
// Returns Logging information
|
||||
LogInfo() LogParams
|
||||
|
||||
// Call api and validate it's status.
|
||||
Call() (agentcheck.Response, string)
|
||||
}
|
||||
14
internal/api/log_params.go
Normal file
14
internal/api/log_params.go
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
|
||||
package api
|
||||
|
||||
type LogParams []interface{}
|
||||
|
||||
func (p *LogParams) Add(field string, value interface{}) {
|
||||
*p = append(*p, field, value)
|
||||
}
|
||||
|
||||
// Syntactic sugar for append(p, other...)
|
||||
// Returns a new instance of LogParams with all values from both p and other
|
||||
func (p LogParams) Combine(other LogParams) LogParams {
|
||||
return append(p, other...)
|
||||
}
|
||||
50
internal/api/log_params_test.go
Normal file
50
internal/api/log_params_test.go
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
|
||||
package api
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
|
||||
|
||||
func TestLogParams(t *testing.T) {
|
||||
|
||||
type test_struct struct {
|
||||
First string
|
||||
Second int
|
||||
}
|
||||
|
||||
p := LogParams{}
|
||||
|
||||
p.Add("one", 1)
|
||||
p.Add("string", "str")
|
||||
p.Add("struct", test_struct{First:"first_string",Second:1234})
|
||||
|
||||
expected := []interface{}([]interface {}{
|
||||
"one",1,
|
||||
"string","str",
|
||||
"struct",test_struct{
|
||||
First:"first_string",
|
||||
Second:1234,
|
||||
},
|
||||
})
|
||||
|
||||
assert.ElementsMatch(t, expected, p)
|
||||
}
|
||||
|
||||
func TestLogParamsCombine(t *testing.T) {
|
||||
|
||||
a := LogParams{"one",1,"string1","str1"}
|
||||
|
||||
b := LogParams{"two",2,"string2","str2"}
|
||||
|
||||
expected := LogParams{
|
||||
"one",1,
|
||||
"string1","str1",
|
||||
"two",2,
|
||||
"string2","str2",
|
||||
}
|
||||
|
||||
assert.Equal(t, expected, a.Combine(b))
|
||||
}
|
||||
60
internal/parse_request.go
Normal file
60
internal/parse_request.go
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
|
||||
package internal
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"github.com/eosswedenorg/eosio-api-healthcheck/internal/api"
|
||||
)
|
||||
|
||||
func ParseArguments(args []string) api.ApiArguments {
|
||||
|
||||
a := api.ApiArguments{
|
||||
NumBlocks: 10,
|
||||
}
|
||||
|
||||
// 1. url (scheme + ip/domain + port)
|
||||
a.Url = args[0]
|
||||
|
||||
// 2. num blocks
|
||||
if len(args) > 1 {
|
||||
num, err := strconv.ParseInt(args[1], 10, 32)
|
||||
if err == nil {
|
||||
a.NumBlocks = int(num)
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Host
|
||||
if len(args) > 2 {
|
||||
a.Host = args[2]
|
||||
}
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
func ParseRequest(request string) (api.ApiInterface, error) {
|
||||
|
||||
factories := map[string]api.Factory{
|
||||
"v1": api.EosioV1Factory,
|
||||
"v2": api.EosioV2Factory,
|
||||
"contract": api.EosioContractFactory,
|
||||
"debug": api.DebugApiFactory,
|
||||
}
|
||||
|
||||
// Parse arguments.
|
||||
// -------------------
|
||||
p := strings.Split(strings.TrimSpace(request), "|")
|
||||
|
||||
if len(p) < 2 {
|
||||
return nil, fmt.Errorf("invalid number of parameters in agent request")
|
||||
}
|
||||
|
||||
a := ParseArguments(p[1:])
|
||||
|
||||
if factory, ok := factories[p[0]]; ok {
|
||||
return factory(a), nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("invalid API '%s'", p[0])
|
||||
}
|
||||
117
internal/parse_request_test.go
Normal file
117
internal/parse_request_test.go
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
|
||||
package internal
|
||||
|
||||
import (
|
||||
// "fmt"
|
||||
"testing"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/eosswedenorg/eosio-api-healthcheck/internal/api"
|
||||
)
|
||||
|
||||
func TestParseWithInvalidApi(t *testing.T) {
|
||||
|
||||
api, err := ParseRequest("invalid|http://api.example.com")
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, err.Error(), "invalid API 'invalid'")
|
||||
assert.Nil(t, api)
|
||||
}
|
||||
|
||||
func TestParseWithInvalidParams(t *testing.T) {
|
||||
|
||||
api, err := ParseRequest("v1")
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, err.Error(), "invalid number of parameters in agent request")
|
||||
assert.Nil(t, api)
|
||||
}
|
||||
|
||||
// EosioV1
|
||||
// --------------------------------
|
||||
|
||||
func TestParseEosioV1(t *testing.T) {
|
||||
|
||||
expected := api.NewEosioV1("http://api.example.com", "", 5)
|
||||
|
||||
api, err := ParseRequest("v1|http://api.example.com")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected.LogInfo(), api.LogInfo())
|
||||
}
|
||||
|
||||
func TestParseEosioV1WithBlockNumber(t *testing.T) {
|
||||
|
||||
expected := api.NewEosioV1("http://api.example.com", "", 1000)
|
||||
|
||||
api, err := ParseRequest("v1|http://api.example.com|2000")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected.LogInfo(), api.LogInfo())
|
||||
}
|
||||
|
||||
|
||||
func TestParseEosioV1Full(t *testing.T) {
|
||||
|
||||
expected := api.NewEosioV1("http://api.example.com", "http://host.example.com", 500)
|
||||
|
||||
api, err := ParseRequest("v1|http://api.example.com|1000|http://host.example.com")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected.LogInfo(), api.LogInfo())
|
||||
}
|
||||
|
||||
// EosioV2
|
||||
// --------------------------------
|
||||
|
||||
func TestParseEosioV2(t *testing.T) {
|
||||
|
||||
expected := api.NewEosioV2("http://api.v2.example.com", "", 10)
|
||||
|
||||
api, err := ParseRequest("v2|http://api.v2.example.com")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected.LogInfo(), api.LogInfo())
|
||||
}
|
||||
|
||||
func TestParseEosioV2WithOffset(t *testing.T) {
|
||||
|
||||
expected := api.NewEosioV2("http://api.v2.example.com", "", 1000)
|
||||
|
||||
api, err := ParseRequest("v2|http://api.v2.example.com|1000")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected.LogInfo(), api.LogInfo())
|
||||
}
|
||||
|
||||
func TestParseEosioV2Full(t *testing.T) {
|
||||
|
||||
expected := api.NewEosioV2("http://api.v2.example.com", "http://host.example.com", 1000)
|
||||
|
||||
api, err := ParseRequest("v2|http://api.v2.example.com|1000|http://host.example.com")
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, expected.LogInfo(), api.LogInfo())
|
||||
}
|
||||
|
||||
// EosioContract
|
||||
// --------------------------------
|
||||
|
||||
func TestParseEosioContract(t *testing.T) {
|
||||
|
||||
expected := api.NewEosioContract("http://api.contract.example.com", 5)
|
||||
|
||||
api, err := ParseRequest("contract|http://api.contract.example.com")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected.LogInfo(), api.LogInfo())
|
||||
}
|
||||
|
||||
func TestParseEosioContractWithBlockTime(t *testing.T) {
|
||||
|
||||
expected := api.NewEosioContract("http://api.contract.example.com", 256)
|
||||
|
||||
api, err := ParseRequest("contract|http://api.contract.example.com|512")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected.LogInfo(), api.LogInfo())
|
||||
}
|
||||
|
||||
func TestParseDebugApi(t *testing.T) {
|
||||
|
||||
expected := api.NewDebugApi("some_api_call")
|
||||
|
||||
api, err := ParseRequest("debug|some_api_call")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected.LogInfo(), api.LogInfo())
|
||||
}
|
||||
57
internal/server.go
Normal file
57
internal/server.go
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
package internal
|
||||
|
||||
import (
|
||||
"strings"
|
||||
log "github.com/inconshreveable/log15"
|
||||
"github.com/eosswedenorg/eosio-api-healthcheck/internal/api"
|
||||
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
||||
"github.com/eosswedenorg-go/tcp_server"
|
||||
)
|
||||
|
||||
// onTcpMessage callback function
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func onTcpMessage(c *tcp_server.Client, args string) {
|
||||
|
||||
logger := log.Root()
|
||||
|
||||
// Check api.
|
||||
// -------------------
|
||||
healthCheckApi, err := ParseRequest(args)
|
||||
if err != nil {
|
||||
logger.Warn("Agent request error", "message", err)
|
||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
||||
|
||||
c.WriteString(resp.String())
|
||||
c.Close()
|
||||
return
|
||||
}
|
||||
|
||||
status, msg := healthCheckApi.Call()
|
||||
|
||||
params := api.LogParams{}
|
||||
params.Add("status", strings.TrimSpace(status.String()))
|
||||
|
||||
if msg != "OK" && len(msg) > 0 {
|
||||
params.Add("error", msg)
|
||||
}
|
||||
|
||||
logger.Info("API Check", params.Combine(healthCheckApi.LogInfo())...)
|
||||
// Report status to HAproxy
|
||||
c.WriteString(status.String())
|
||||
c.Close()
|
||||
}
|
||||
|
||||
// SpawnTcpServer
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func SpawnTcpServer(addr string) error {
|
||||
server := tcp_server.New(addr)
|
||||
server.OnMessage(onTcpMessage)
|
||||
|
||||
err := server.Connect()
|
||||
if err == nil {
|
||||
go server.Listen()
|
||||
}
|
||||
return err
|
||||
}
|
||||
17
internal/utils/json.go
Normal file
17
internal/utils/json.go
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
|
||||
package utils
|
||||
|
||||
// JsonGetInt64
|
||||
// performs float64 (json numbers are always float64)
|
||||
// type assertion and casts to int64.
|
||||
//
|
||||
// if the type assertion fails, the function defaults 0 (zero).
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func JsonGetInt64(input interface{}) (int64) {
|
||||
v, res := input.(float64)
|
||||
if res {
|
||||
return (int64) (v)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
22
internal/utils/json_test.go
Normal file
22
internal/utils/json_test.go
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
package utils
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestJsonGetInt64(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input interface{}
|
||||
want int64
|
||||
}{
|
||||
{"String", "test", 0 },
|
||||
{"Int", 1234, 0 },
|
||||
{"Float", float64(1234), 1234 },
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := JsonGetInt64(tt.input); got != tt.want {
|
||||
t.Errorf("JsonGetInt64() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
20
internal/utils/parse_log_formatter.go
Normal file
20
internal/utils/parse_log_formatter.go
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
|
||||
package utils
|
||||
|
||||
import (
|
||||
log "github.com/inconshreveable/log15"
|
||||
)
|
||||
|
||||
func ParseLogFormatter(name string) log.Format {
|
||||
|
||||
switch name {
|
||||
case "logfmt" :
|
||||
return log.LogfmtFormat()
|
||||
case "json" :
|
||||
return log.JsonFormat()
|
||||
case "json-pretty" :
|
||||
return log.JsonFormatEx(true, true)
|
||||
default :
|
||||
return log.TerminalFormat()
|
||||
}
|
||||
}
|
||||
29
internal/utils/parse_log_formatter_test.go
Normal file
29
internal/utils/parse_log_formatter_test.go
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
log "github.com/inconshreveable/log15"
|
||||
)
|
||||
|
||||
func Test_ParseLogFormatter(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
arg string
|
||||
want log.Format
|
||||
}{
|
||||
{ "Default", "", log.TerminalFormat() },
|
||||
{ "LogFmt", "logfmt", log.LogfmtFormat() },
|
||||
{ "Json", "json", log.JsonFormat() },
|
||||
{ "JsonPretty", "json-pretty", log.JsonFormat() },
|
||||
{ "Unknown", "unknown", log.TerminalFormat() },
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := ParseLogFormatter(tt.arg); reflect.ValueOf(got).Pointer() != reflect.ValueOf(tt.want).Pointer() {
|
||||
t.Errorf("parseLogFormatter() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
22
internal/utils/time.go
Normal file
22
internal/utils/time.go
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Time struct {
|
||||
ts time.Time
|
||||
}
|
||||
|
||||
func (t *Time) SetTime(value time.Time) {
|
||||
t.ts = value
|
||||
}
|
||||
|
||||
func (t Time) GetTime() time.Time {
|
||||
|
||||
if ! t.ts.IsZero() {
|
||||
return t.ts
|
||||
}
|
||||
return time.Now().In(time.UTC)
|
||||
}
|
||||
26
internal/utils/time_test.go
Normal file
26
internal/utils/time_test.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"time"
|
||||
"testing"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestTimeGetTimeWithDefaultValue(t *testing.T) {
|
||||
|
||||
var ts Time
|
||||
|
||||
// Assert that time is NOW (+-10 seconds)
|
||||
assert.InDelta(t, ts.GetTime().Unix(), time.Now().In(time.UTC).Unix(), float64(10))
|
||||
}
|
||||
|
||||
func TestTimeGetTimeWithSetTime(t *testing.T) {
|
||||
|
||||
var ts Time
|
||||
|
||||
expected := time.Unix(1048722042, 500)
|
||||
ts.SetTime(expected)
|
||||
|
||||
assert.Equal(t, expected, ts.GetTime())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue