mirror of
https://github.com/eosswedenorg/antelope-api-healthcheck
synced 2026-07-04 12:03:43 +02:00
Fix code formatting
This commit is contained in:
parent
b0e5b455ca
commit
adb1ad3c6d
21 changed files with 884 additions and 950 deletions
|
|
@ -1,22 +1,25 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
"github.com/eosswedenorg/eosio-api-healthcheck/internal"
|
|
||||||
"github.com/eosswedenorg/eosio-api-healthcheck/internal/utils"
|
"github.com/eosswedenorg-go/pid"
|
||||||
log "github.com/inconshreveable/log15"
|
"github.com/eosswedenorg/eosio-api-healthcheck/internal"
|
||||||
"github.com/eosswedenorg-go/pid"
|
"github.com/eosswedenorg/eosio-api-healthcheck/internal/utils"
|
||||||
"github.com/pborman/getopt/v2"
|
log "github.com/inconshreveable/log15"
|
||||||
|
"github.com/pborman/getopt/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Command line flags
|
// Command line flags
|
||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
|
|
||||||
var logFile string
|
var (
|
||||||
var pidFile string
|
logFile string
|
||||||
|
pidFile string
|
||||||
|
)
|
||||||
|
|
||||||
// Global variables
|
// Global variables
|
||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
|
|
@ -28,159 +31,160 @@ var VersionString string = "-"
|
||||||
// File descriptor to the current log file.
|
// File descriptor to the current log file.
|
||||||
var logfd *os.File
|
var logfd *os.File
|
||||||
|
|
||||||
var logfmt log.Format
|
var (
|
||||||
var logger log.Logger
|
logfmt log.Format
|
||||||
|
logger log.Logger
|
||||||
|
)
|
||||||
|
|
||||||
// argv_listen_addr
|
// argv_listen_addr
|
||||||
// Parse listen address from command line.
|
// Parse listen address from command line.
|
||||||
|
//
|
||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
func argv_listen_addr() string {
|
func argv_listen_addr() string {
|
||||||
|
var addr string
|
||||||
|
|
||||||
var addr string
|
argv := getopt.Args()
|
||||||
|
if len(argv) > 0 {
|
||||||
|
addr = argv[0]
|
||||||
|
} else {
|
||||||
|
addr = "127.0.0.1"
|
||||||
|
}
|
||||||
|
|
||||||
argv := getopt.Args()
|
addr += ":"
|
||||||
if len(argv) > 0 {
|
if len(argv) > 1 {
|
||||||
addr = argv[0]
|
addr += argv[1]
|
||||||
} else {
|
} else {
|
||||||
addr = "127.0.0.1"
|
addr += "1337"
|
||||||
}
|
}
|
||||||
|
|
||||||
addr += ":"
|
return addr
|
||||||
if len(argv) > 1 {
|
|
||||||
addr += argv[1]
|
|
||||||
} else {
|
|
||||||
addr += "1337"
|
|
||||||
}
|
|
||||||
|
|
||||||
return addr
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func setLogFile() {
|
func setLogFile() {
|
||||||
|
// Open file
|
||||||
|
fd, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
// Open file
|
// Try close if old descriptor is defined.
|
||||||
fd, err := os.OpenFile(logFile, os.O_APPEND | os.O_CREATE | os.O_WRONLY, 0644)
|
if logfd != nil {
|
||||||
if err != nil {
|
if err = logfd.Close(); err != nil {
|
||||||
logger.Error(err.Error())
|
logger.Error(err.Error())
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Try close if old descriptor is defined.
|
// Update variable and set log writer.
|
||||||
if logfd != nil {
|
logfd = fd
|
||||||
if err = logfd.Close(); err != nil {
|
logger.SetHandler(log.StreamHandler(logfd, logfmt))
|
||||||
logger.Error(err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update variable and set log writer.
|
|
||||||
logfd = fd
|
|
||||||
logger.SetHandler(log.StreamHandler(logfd, logfmt))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// signalEventLoop()
|
// signalEventLoop()
|
||||||
// Initialize event channel for OS signals
|
// Initialize event channel for OS signals
|
||||||
// and runs an event loop.
|
// and runs an event loop.
|
||||||
|
//
|
||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
func signalEventLoop() {
|
func signalEventLoop() {
|
||||||
|
// Setup a channel
|
||||||
|
sig_ch := make(chan os.Signal, 1)
|
||||||
|
|
||||||
// Setup a channel
|
// subscribe to SIGHUP signal.
|
||||||
sig_ch := make(chan os.Signal, 1)
|
signal.Notify(sig_ch, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
|
||||||
// subscribe to SIGHUP signal.
|
// Event loop
|
||||||
signal.Notify(sig_ch, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)
|
func() {
|
||||||
|
var run bool = true
|
||||||
|
for run {
|
||||||
|
// Block until we get a signal.
|
||||||
|
sig := <-sig_ch
|
||||||
|
|
||||||
// Event loop
|
l := logger.New("signal", sig)
|
||||||
func() {
|
|
||||||
var run bool = true
|
|
||||||
for run {
|
|
||||||
// Block until we get a signal.
|
|
||||||
sig := <- sig_ch
|
|
||||||
|
|
||||||
l := logger.New("signal", sig)
|
switch sig {
|
||||||
|
case syscall.SIGINT, syscall.SIGTERM:
|
||||||
|
l.Info("Program was asked to terminate.")
|
||||||
|
run = false
|
||||||
|
// SIGHUP is sent when logfile is rotated.
|
||||||
|
case syscall.SIGHUP:
|
||||||
|
msg := "Logfile was rotated: "
|
||||||
|
|
||||||
switch sig {
|
if logfd != nil {
|
||||||
case syscall.SIGINT, syscall.SIGTERM :
|
setLogFile()
|
||||||
l.Info("Program was asked to terminate.")
|
msg += "Filedescriptor was updated"
|
||||||
run = false
|
} else {
|
||||||
// SIGHUP is sent when logfile is rotated.
|
msg += "No Filedescriptor to update (most likely uses standard out/err streams)"
|
||||||
case syscall.SIGHUP :
|
}
|
||||||
msg := "Logfile was rotated: "
|
|
||||||
|
|
||||||
if logfd != nil {
|
l.Info(msg)
|
||||||
setLogFile()
|
default:
|
||||||
msg += "Filedescriptor was updated"
|
l.Warn("Unknown signal")
|
||||||
} else {
|
}
|
||||||
msg += "No Filedescriptor to update (most likely uses standard out/err streams)"
|
}
|
||||||
}
|
}()
|
||||||
|
|
||||||
l.Info(msg)
|
|
||||||
default:
|
|
||||||
l.Warn("Unknown signal")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// main
|
// main
|
||||||
|
//
|
||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
func main() {
|
func main() {
|
||||||
|
var version bool
|
||||||
|
var usage bool
|
||||||
|
var addr string
|
||||||
|
var logFormatter *string
|
||||||
|
|
||||||
var version bool
|
logger = log.Root()
|
||||||
var usage bool
|
|
||||||
var addr string
|
|
||||||
var logFormatter *string
|
|
||||||
|
|
||||||
logger = log.Root()
|
// Command line parsing
|
||||||
|
getopt.SetParameters("[ip] [port]")
|
||||||
|
getopt.FlagLong(&usage, "help", 'h', "Print this help text")
|
||||||
|
getopt.FlagLong(&version, "version", 'v', "Print version")
|
||||||
|
getopt.FlagLong(&logFile, "log", 'l', "Path to log file", "file")
|
||||||
|
getopt.FlagLong(&pidFile, "pid", 'p', "Path to pid file", "file")
|
||||||
|
logFormatter = getopt.EnumLong("log-format", 0, []string{"term", "logfmt", "json", "json-pretty"}, "", "Log format to use: term,logfmt,json,json-pretty")
|
||||||
|
|
||||||
// Command line parsing
|
getopt.Parse()
|
||||||
getopt.SetParameters("[ip] [port]")
|
|
||||||
getopt.FlagLong(&usage, "help", 'h', "Print this help text")
|
|
||||||
getopt.FlagLong(&version, "version", 'v', "Print version")
|
|
||||||
getopt.FlagLong(&logFile, "log", 'l', "Path to log file", "file")
|
|
||||||
getopt.FlagLong(&pidFile, "pid", 'p', "Path to pid file", "file")
|
|
||||||
logFormatter = getopt.EnumLong("log-format", 0, []string{"term", "logfmt", "json", "json-pretty"}, "", "Log format to use: term,logfmt,json,json-pretty")
|
|
||||||
|
|
||||||
getopt.Parse()
|
if usage {
|
||||||
|
getopt.Usage()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if usage {
|
if version {
|
||||||
getopt.Usage()
|
fmt.Printf("Version: %s\n", VersionString)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if version {
|
logfmt = utils.ParseLogFormatter(*logFormatter)
|
||||||
fmt.Printf("Version: %s\n", VersionString)
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
logfmt = utils.ParseLogFormatter(*logFormatter)
|
// Open logfile.
|
||||||
|
if len(logFile) > 0 {
|
||||||
|
setLogFile()
|
||||||
|
} else {
|
||||||
|
logger.SetHandler(log.StreamHandler(os.Stdout, logfmt))
|
||||||
|
}
|
||||||
|
|
||||||
// Open logfile.
|
logger.Info("Process is starting", "pid", pid.Get())
|
||||||
if len(logFile) > 0 {
|
|
||||||
setLogFile()
|
|
||||||
} else {
|
|
||||||
logger.SetHandler(log.StreamHandler(os.Stdout, logfmt))
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.Info("Process is starting", "pid", pid.Get())
|
if len(pidFile) > 0 {
|
||||||
|
logger.Info("Writing pidfile", "file", pidFile)
|
||||||
|
err := pid.Save(pidFile)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("Failed to write pidfile", "msg", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if len(pidFile) > 0 {
|
addr = argv_listen_addr()
|
||||||
logger.Info("Writing pidfile", "file", pidFile)
|
|
||||||
err := pid.Save(pidFile)
|
|
||||||
if err != nil {
|
|
||||||
logger.Error("Failed to write pidfile", "msg", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
addr = argv_listen_addr()
|
// Start listening to TCP Connections
|
||||||
|
err := internal.SpawnTcpServer(addr)
|
||||||
|
if err == nil {
|
||||||
|
logger.Info("TCP Server started", "addr", addr)
|
||||||
|
|
||||||
// Start listening to TCP Connections
|
// Run the signal event loop.
|
||||||
err := internal.SpawnTcpServer(addr)
|
signalEventLoop()
|
||||||
if err == nil {
|
} else {
|
||||||
logger.Info("TCP Server started", "addr", addr)
|
log.Error("Failed to start tcp server", "error", err)
|
||||||
|
}
|
||||||
|
|
||||||
// Run the signal event loop.
|
logger.Info("Shutdown")
|
||||||
signalEventLoop()
|
|
||||||
} else {
|
|
||||||
log.Error("Failed to start tcp server", "error", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.Info("Shutdown")
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,50 +1,48 @@
|
||||||
|
|
||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
|
||||||
|
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DebugApi struct {
|
type DebugApi struct {
|
||||||
response agentcheck.Response
|
response agentcheck.Response
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseResponse(resp string) (agentcheck.Response, error) {
|
func parseResponse(resp string) (agentcheck.Response, error) {
|
||||||
|
parts := strings.SplitN(resp, "#", 2)
|
||||||
|
|
||||||
parts := strings.SplitN(resp, "#", 2)
|
// Status with message
|
||||||
|
if len(parts) > 1 {
|
||||||
|
rtype := agentcheck.StatusMessageResponseType(parts[0])
|
||||||
|
return agentcheck.NewStatusMessageResponse(rtype, parts[1]), nil
|
||||||
|
}
|
||||||
|
|
||||||
// Status with message
|
// Only status.
|
||||||
if len(parts) > 1 {
|
rtype := agentcheck.StatusResponseType(parts[0])
|
||||||
rtype := agentcheck.StatusMessageResponseType(parts[0])
|
return agentcheck.NewStatusResponse(rtype), nil
|
||||||
return agentcheck.NewStatusMessageResponse(rtype, parts[1]), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Only status.
|
|
||||||
rtype := agentcheck.StatusResponseType(parts[0])
|
|
||||||
return agentcheck.NewStatusResponse(rtype), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func DebugApiFactory(args ApiArguments) ApiInterface {
|
func DebugApiFactory(args ApiArguments) ApiInterface {
|
||||||
return NewDebugApi(args.Url)
|
return NewDebugApi(args.Url)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDebugApi(response string) DebugApi {
|
func NewDebugApi(response string) DebugApi {
|
||||||
|
resp, _ := parseResponse(response)
|
||||||
|
|
||||||
resp, _ := parseResponse(response)
|
return DebugApi{
|
||||||
|
response: resp,
|
||||||
return DebugApi{
|
}
|
||||||
response: resp,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d DebugApi) LogInfo() LogParams {
|
func (d DebugApi) LogInfo() LogParams {
|
||||||
return LogParams{
|
return LogParams{
|
||||||
"type", "Debug",
|
"type", "Debug",
|
||||||
"response", strings.TrimSpace(d.response.String()),
|
"response", strings.TrimSpace(d.response.String()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d DebugApi) Call() (agentcheck.Response, string) {
|
func (d DebugApi) Call() (agentcheck.Response, string) {
|
||||||
return d.response, ""
|
return d.response, ""
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,20 +3,20 @@ package api
|
||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDebugApiFactory(t *testing.T) {
|
func TestDebugApiFactory(t *testing.T) {
|
||||||
|
api := DebugApiFactory(ApiArguments{
|
||||||
|
Url: "up",
|
||||||
|
Host: "host",
|
||||||
|
NumBlocks: 40,
|
||||||
|
})
|
||||||
|
|
||||||
api := DebugApiFactory(ApiArguments{
|
assert.IsType(t, DebugApi{}, api)
|
||||||
Url: "up",
|
assert.Equal(t, api.(DebugApi).response, agentcheck.NewStatusResponse(agentcheck.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) {
|
func TestNewDebugApi(t *testing.T) {
|
||||||
|
|
@ -43,26 +43,24 @@ func TestNewDebugApi(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDebugApi_LogInfo(t *testing.T) {
|
func TestDebugApi_LogInfo(t *testing.T) {
|
||||||
|
expected := LogParams{"type", "Debug", "response", "up"}
|
||||||
|
|
||||||
expected := LogParams{"type", "Debug", "response", "up"}
|
api := DebugApi{
|
||||||
|
response: agentcheck.NewStatusResponse(agentcheck.Up),
|
||||||
|
}
|
||||||
|
|
||||||
api := DebugApi{
|
assert.Equal(t, api.LogInfo(), expected)
|
||||||
response: agentcheck.NewStatusResponse(agentcheck.Up),
|
|
||||||
}
|
|
||||||
|
|
||||||
assert.Equal(t, api.LogInfo(), expected)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDebugApi_Call(t *testing.T) {
|
func TestDebugApi_Call(t *testing.T) {
|
||||||
|
expected := agentcheck.NewStatusMessageResponse(agentcheck.Stopped, "message")
|
||||||
|
|
||||||
expected := agentcheck.NewStatusMessageResponse(agentcheck.Stopped, "message")
|
api := DebugApi{
|
||||||
|
response: expected,
|
||||||
|
}
|
||||||
|
|
||||||
api := DebugApi{
|
response, msg := api.Call()
|
||||||
response: expected,
|
|
||||||
}
|
|
||||||
|
|
||||||
response, msg := api.Call()
|
assert.Equal(t, response, expected)
|
||||||
|
assert.Equal(t, msg, "")
|
||||||
assert.Equal(t, response, expected)
|
|
||||||
assert.Equal(t, msg, "")
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,81 +1,80 @@
|
||||||
|
|
||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"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"
|
||||||
contract_api "github.com/eosswedenorg-go/eos-contract-api-client"
|
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
||||||
|
"github.com/eosswedenorg/eosio-api-healthcheck/internal/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
type EosioContract struct {
|
type EosioContract struct {
|
||||||
utils.Time
|
utils.Time
|
||||||
client contract_api.Client
|
client contract_api.Client
|
||||||
block_time float64
|
block_time float64
|
||||||
}
|
}
|
||||||
|
|
||||||
func EosioContractFactory(args ApiArguments) ApiInterface {
|
func EosioContractFactory(args ApiArguments) ApiInterface {
|
||||||
return NewEosioContract(args.Url, float64(args.NumBlocks / 2))
|
return NewEosioContract(args.Url, float64(args.NumBlocks/2))
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewEosioContract(url string, block_time float64) EosioContract {
|
func NewEosioContract(url string, block_time float64) EosioContract {
|
||||||
return EosioContract{
|
return EosioContract{
|
||||||
client: contract_api.Client{
|
client: contract_api.Client{
|
||||||
Url: url,
|
Url: url,
|
||||||
},
|
},
|
||||||
block_time: block_time,
|
block_time: block_time,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e EosioContract) LogInfo() LogParams {
|
func (e EosioContract) LogInfo() LogParams {
|
||||||
return LogParams{
|
return LogParams{
|
||||||
"type", "eosio-contract",
|
"type", "eosio-contract",
|
||||||
"url", e.client.Url,
|
"url", e.client.Url,
|
||||||
"block_time", e.block_time,
|
"block_time", e.block_time,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e EosioContract) Call() (agentcheck.Response, string) {
|
func (e EosioContract) Call() (agentcheck.Response, string) {
|
||||||
|
h, err := e.client.GetHealth()
|
||||||
|
if err != nil {
|
||||||
|
resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
||||||
|
return resp, err.Error()
|
||||||
|
}
|
||||||
|
|
||||||
h, err := e.client.GetHealth()
|
// Check HTTP Status Code
|
||||||
if err != nil {
|
if h.HTTPStatusCode > 299 {
|
||||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||||
return resp, err.Error()
|
msg := "Taking offline because %v was received from backend"
|
||||||
}
|
return resp, fmt.Sprintf(msg, h.HTTPStatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
// Check HTTP Status Code
|
// Check postgres
|
||||||
if h.HTTPStatusCode > 299 {
|
if h.Data.Postgres.Status != "OK" {
|
||||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||||
msg := "Taking offline because %v was received from backend"
|
msg := "Taking offline because Postgres reported '%s'"
|
||||||
return resp, fmt.Sprintf(msg, h.HTTPStatusCode)
|
return resp, fmt.Sprintf(msg, h.Data.Postgres.Status)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check postgres
|
// Check redis
|
||||||
if h.Data.Postgres.Status != "OK" {
|
if h.Data.Redis.Status != "OK" {
|
||||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||||
msg := "Taking offline because Postgres reported '%s'"
|
msg := "Taking offline because Redis reported '%s'"
|
||||||
return resp, fmt.Sprintf(msg, h.Data.Postgres.Status)
|
return resp, fmt.Sprintf(msg, h.Data.Redis.Status)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check redis
|
// Validate head block.
|
||||||
if h.Data.Redis.Status != "OK" {
|
diff := e.GetTime().Sub(h.Data.Chain.HeadTime).Seconds()
|
||||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
|
||||||
msg := "Taking offline because Redis reported '%s'"
|
|
||||||
return resp, fmt.Sprintf(msg, h.Data.Redis.Status)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate head block.
|
if diff > e.block_time {
|
||||||
diff := e.GetTime().Sub(h.Data.Chain.HeadTime).Seconds()
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
if diff > e.block_time {
|
return agentcheck.NewStatusResponse(agentcheck.Up), "OK"
|
||||||
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"
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,86 +1,80 @@
|
||||||
|
|
||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"net/http"
|
||||||
"testing"
|
"net/http/httptest"
|
||||||
"net/http"
|
"testing"
|
||||||
"net/http/httptest"
|
"time"
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestEosioContractFactory(t *testing.T) {
|
func TestEosioContractFactory(t *testing.T) {
|
||||||
|
api := EosioContractFactory(ApiArguments{
|
||||||
|
Url: "https://atomic.example.com",
|
||||||
|
NumBlocks: 120,
|
||||||
|
})
|
||||||
|
|
||||||
api := EosioContractFactory(ApiArguments{
|
expected := NewEosioContract("https://atomic.example.com", 60)
|
||||||
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.IsType(t, expected, api)
|
assert.Equal(t, expected.client.Host, api.(EosioContract).client.Host)
|
||||||
assert.Equal(t, expected.client.Url, api.(EosioContract).client.Url)
|
assert.Equal(t, expected.block_time, api.(EosioContract).block_time)
|
||||||
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) {
|
func TestEosioContractLogInfo(t *testing.T) {
|
||||||
|
api := NewEosioContract("https://atomic.example.com", 120)
|
||||||
|
|
||||||
api := NewEosioContract("https://atomic.example.com", 120)
|
expected := LogParams{"type", "eosio-contract", "url", "https://atomic.example.com", "block_time", float64(120)}
|
||||||
|
|
||||||
expected := LogParams{"type","eosio-contract","url","https://atomic.example.com","block_time",float64(120)}
|
assert.Equal(t, expected, api.LogInfo())
|
||||||
|
|
||||||
assert.Equal(t, expected, api.LogInfo())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEosioContractSetTime(t *testing.T) {
|
func TestEosioContractSetTime(t *testing.T) {
|
||||||
|
expected := time.Date(2019, 3, 18, 20, 29, 32, 0, time.UTC)
|
||||||
|
|
||||||
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 := NewEosioContract("", 60)
|
api.SetTime(expected)
|
||||||
// Assert that time is NOW (+-10 seconds)
|
assert.Equal(t, expected, api.GetTime())
|
||||||
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) {
|
func TestEosioContractJsonFailure(t *testing.T) {
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||||
|
res.Write([]byte(`!//{invalid-json}!##`))
|
||||||
|
}))
|
||||||
|
|
||||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
api := NewEosioContract(srv.URL, 120)
|
||||||
res.Write([]byte(`!//{invalid-json}!##`))
|
check, _ := api.Call()
|
||||||
}))
|
|
||||||
|
|
||||||
api := NewEosioContract(srv.URL, 120)
|
expected := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
||||||
check, _ := api.Call()
|
assert.Equal(t, expected, check)
|
||||||
|
|
||||||
expected := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
|
||||||
assert.Equal(t, expected, check)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEosioContractHTTP500Down(t *testing.T) {
|
func TestEosioContractHTTP500Down(t *testing.T) {
|
||||||
|
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(`{}`))
|
||||||
|
}))
|
||||||
|
|
||||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
api := NewEosioContract(srv.URL, 120)
|
||||||
res.Header().Add("Content-type", "application/json; charset=utf-8")
|
check, status := api.Call()
|
||||||
res.WriteHeader(500)
|
|
||||||
res.Write([]byte(`{}`))
|
|
||||||
}))
|
|
||||||
|
|
||||||
api := NewEosioContract(srv.URL, 120)
|
assert.Equal(t, "Taking offline because 500 was received from backend", status)
|
||||||
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)
|
||||||
expected := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
|
||||||
assert.Equal(t, expected, check)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEosioContractLaggingUp(t *testing.T) {
|
func TestEosioContractLaggingUp(t *testing.T) {
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
if req.URL.String() == "/health" {
|
||||||
if req.URL.String() == "/health" {
|
payload := `{
|
||||||
payload := `{
|
|
||||||
"success":true,
|
"success":true,
|
||||||
"data":{
|
"data":{
|
||||||
"version":"1.0.0",
|
"version":"1.0.0",
|
||||||
|
|
@ -99,27 +93,26 @@ func TestEosioContractLaggingUp(t *testing.T) {
|
||||||
"query_time":1759953929542
|
"query_time":1759953929542
|
||||||
}`
|
}`
|
||||||
|
|
||||||
res.Header().Add("Content-type", "application/json; charset=utf-8")
|
res.Header().Add("Content-type", "application/json; charset=utf-8")
|
||||||
res.Write([]byte(payload))
|
res.Write([]byte(payload))
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
||||||
api := NewEosioContract(srv.URL, 120)
|
api := NewEosioContract(srv.URL, 120)
|
||||||
api.SetTime(time.Date(2025, 10, 8, 20, 7, 27, 0, time.UTC))
|
api.SetTime(time.Date(2025, 10, 8, 20, 7, 27, 0, time.UTC))
|
||||||
|
|
||||||
check, status := api.Call()
|
check, status := api.Call()
|
||||||
|
|
||||||
assert.Equal(t, "OK", status)
|
assert.Equal(t, "OK", status)
|
||||||
|
|
||||||
expected := agentcheck.NewStatusResponse(agentcheck.Up)
|
expected := agentcheck.NewStatusResponse(agentcheck.Up)
|
||||||
assert.Equal(t, expected, check)
|
assert.Equal(t, expected, check)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEosioContractLaggingDown(t *testing.T) {
|
func TestEosioContractLaggingDown(t *testing.T) {
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
if req.URL.String() == "/health" {
|
||||||
if req.URL.String() == "/health" {
|
payload := `{
|
||||||
payload := `{
|
|
||||||
"success":true,
|
"success":true,
|
||||||
"data":{
|
"data":{
|
||||||
"version":"1.0.0",
|
"version":"1.0.0",
|
||||||
|
|
@ -138,27 +131,26 @@ func TestEosioContractLaggingDown(t *testing.T) {
|
||||||
"query_time":1533451895542
|
"query_time":1533451895542
|
||||||
}`
|
}`
|
||||||
|
|
||||||
res.Header().Add("Content-type", "application/json; charset=utf-8")
|
res.Header().Add("Content-type", "application/json; charset=utf-8")
|
||||||
res.Write([]byte(payload))
|
res.Write([]byte(payload))
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
||||||
api := NewEosioContract(srv.URL, 120)
|
api := NewEosioContract(srv.URL, 120)
|
||||||
api.SetTime(time.Date(2018, 8, 5, 6, 53, 35, 0, time.UTC))
|
api.SetTime(time.Date(2018, 8, 5, 6, 53, 35, 0, time.UTC))
|
||||||
|
|
||||||
check, status := api.Call()
|
check, status := api.Call()
|
||||||
|
|
||||||
assert.Equal(t, "Taking offline because head block is lagging 121 seconds", status)
|
assert.Equal(t, "Taking offline because head block is lagging 121 seconds", status)
|
||||||
|
|
||||||
expected := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
expected := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||||
assert.Equal(t, expected, check)
|
assert.Equal(t, expected, check)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEosioContractInFutureUp(t *testing.T) {
|
func TestEosioContractInFutureUp(t *testing.T) {
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
if req.URL.String() == "/health" {
|
||||||
if req.URL.String() == "/health" {
|
payload := `{
|
||||||
payload := `{
|
|
||||||
"success":true,
|
"success":true,
|
||||||
"data":{
|
"data":{
|
||||||
"version":"1.0.0",
|
"version":"1.0.0",
|
||||||
|
|
@ -177,27 +169,26 @@ func TestEosioContractInFutureUp(t *testing.T) {
|
||||||
"query_time":1728954678231
|
"query_time":1728954678231
|
||||||
}`
|
}`
|
||||||
|
|
||||||
res.Header().Add("Content-type", "application/json; charset=utf-8")
|
res.Header().Add("Content-type", "application/json; charset=utf-8")
|
||||||
res.Write([]byte(payload))
|
res.Write([]byte(payload))
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
||||||
api := NewEosioContract(srv.URL, 120)
|
api := NewEosioContract(srv.URL, 120)
|
||||||
api.SetTime(time.Date(2024, 10, 15, 1, 9, 16, 500, time.UTC))
|
api.SetTime(time.Date(2024, 10, 15, 1, 9, 16, 500, time.UTC))
|
||||||
|
|
||||||
check, status := api.Call()
|
check, status := api.Call()
|
||||||
|
|
||||||
assert.Equal(t, "OK", status)
|
assert.Equal(t, "OK", status)
|
||||||
|
|
||||||
expected := agentcheck.NewStatusResponse(agentcheck.Up)
|
expected := agentcheck.NewStatusResponse(agentcheck.Up)
|
||||||
assert.Equal(t, expected, check)
|
assert.Equal(t, expected, check)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEosioContractInFutureDown(t *testing.T) {
|
func TestEosioContractInFutureDown(t *testing.T) {
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
if req.URL.String() == "/health" {
|
||||||
if req.URL.String() == "/health" {
|
payload := `{
|
||||||
payload := `{
|
|
||||||
"success":true,
|
"success":true,
|
||||||
"data":{
|
"data":{
|
||||||
"version":"1.0.0",
|
"version":"1.0.0",
|
||||||
|
|
@ -216,28 +207,26 @@ func TestEosioContractInFutureDown(t *testing.T) {
|
||||||
"query_time":1041122832231
|
"query_time":1041122832231
|
||||||
}`
|
}`
|
||||||
|
|
||||||
res.Header().Add("Content-type", "application/json; charset=utf-8")
|
res.Header().Add("Content-type", "application/json; charset=utf-8")
|
||||||
res.Write([]byte(payload))
|
res.Write([]byte(payload))
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
||||||
api := NewEosioContract(srv.URL, 120)
|
api := NewEosioContract(srv.URL, 120)
|
||||||
api.SetTime(time.Date(2002, 12, 29, 0, 45, 03, 500, time.UTC))
|
api.SetTime(time.Date(2002, 12, 29, 0, 45, 0o3, 500, time.UTC))
|
||||||
|
|
||||||
check, status := api.Call()
|
check, status := api.Call()
|
||||||
|
|
||||||
assert.Equal(t, "Taking offline because head block is -121 seconds into the future", status)
|
assert.Equal(t, "Taking offline because head block is -121 seconds into the future", status)
|
||||||
|
|
||||||
expected := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
expected := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||||
assert.Equal(t, expected, check)
|
assert.Equal(t, expected, check)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func TestEosioContractRedisDown(t *testing.T) {
|
func TestEosioContractRedisDown(t *testing.T) {
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
if req.URL.String() == "/health" {
|
||||||
if req.URL.String() == "/health" {
|
payload := `{
|
||||||
payload := `{
|
|
||||||
"success":true,
|
"success":true,
|
||||||
"data":{
|
"data":{
|
||||||
"version":"1.0.0",
|
"version":"1.0.0",
|
||||||
|
|
@ -256,27 +245,26 @@ func TestEosioContractRedisDown(t *testing.T) {
|
||||||
"query_time":1426072775872
|
"query_time":1426072775872
|
||||||
}`
|
}`
|
||||||
|
|
||||||
res.Header().Add("Content-type", "application/json; charset=utf-8")
|
res.Header().Add("Content-type", "application/json; charset=utf-8")
|
||||||
res.Write([]byte(payload))
|
res.Write([]byte(payload))
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
||||||
api := NewEosioContract(srv.URL, 120)
|
api := NewEosioContract(srv.URL, 120)
|
||||||
api.SetTime(time.Date(2015, 3, 11, 11, 19, 30, 500, time.UTC))
|
api.SetTime(time.Date(2015, 3, 11, 11, 19, 30, 500, time.UTC))
|
||||||
|
|
||||||
check, status := api.Call()
|
check, status := api.Call()
|
||||||
|
|
||||||
assert.Equal(t, "Taking offline because Redis reported 'DOWN'", status)
|
assert.Equal(t, "Taking offline because Redis reported 'DOWN'", status)
|
||||||
|
|
||||||
expected := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
expected := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||||
assert.Equal(t, expected, check)
|
assert.Equal(t, expected, check)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEosioContractPostgresDown(t *testing.T) {
|
func TestEosioContractPostgresDown(t *testing.T) {
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
if req.URL.String() == "/health" {
|
||||||
if req.URL.String() == "/health" {
|
payload := `{
|
||||||
payload := `{
|
|
||||||
"success":true,
|
"success":true,
|
||||||
"data":{
|
"data":{
|
||||||
"version":"1.0.0",
|
"version":"1.0.0",
|
||||||
|
|
@ -295,18 +283,18 @@ func TestEosioContractPostgresDown(t *testing.T) {
|
||||||
"query_time":156286837143
|
"query_time":156286837143
|
||||||
}`
|
}`
|
||||||
|
|
||||||
res.Header().Add("Content-type", "application/json; charset=utf-8")
|
res.Header().Add("Content-type", "application/json; charset=utf-8")
|
||||||
res.Write([]byte(payload))
|
res.Write([]byte(payload))
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
||||||
api := NewEosioContract(srv.URL, 120)
|
api := NewEosioContract(srv.URL, 120)
|
||||||
api.SetTime(time.Date(2019, 7, 11, 18, 6, 11, 500, time.UTC))
|
api.SetTime(time.Date(2019, 7, 11, 18, 6, 11, 500, time.UTC))
|
||||||
|
|
||||||
check, status := api.Call()
|
check, status := api.Call()
|
||||||
|
|
||||||
assert.Equal(t, "Taking offline because Postgres reported 'DOWN'", status)
|
assert.Equal(t, "Taking offline because Postgres reported 'DOWN'", status)
|
||||||
|
|
||||||
expected := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
expected := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||||
assert.Equal(t, expected, check)
|
assert.Equal(t, expected, check)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,73 +1,71 @@
|
||||||
|
|
||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/eosswedenorg/eosio-api-healthcheck/internal/utils"
|
|
||||||
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
"github.com/eosswedenorg-go/eosapi"
|
||||||
"github.com/eosswedenorg-go/eosapi"
|
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
||||||
|
"github.com/eosswedenorg/eosio-api-healthcheck/internal/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
type EosioV1 struct {
|
type EosioV1 struct {
|
||||||
utils.Time
|
utils.Time
|
||||||
client eosapi.Client
|
client eosapi.Client
|
||||||
block_time float64
|
block_time float64
|
||||||
}
|
}
|
||||||
|
|
||||||
func EosioV1Factory(args ApiArguments) ApiInterface {
|
func EosioV1Factory(args ApiArguments) ApiInterface {
|
||||||
return NewEosioV1(args.Url, args.Host, float64(args.NumBlocks / 2))
|
return NewEosioV1(args.Url, args.Host, float64(args.NumBlocks/2))
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewEosioV1(url string, host string, block_time float64) EosioV1 {
|
func NewEosioV1(url string, host string, block_time float64) EosioV1 {
|
||||||
|
api := EosioV1{
|
||||||
|
client: *eosapi.New(url),
|
||||||
|
block_time: block_time,
|
||||||
|
}
|
||||||
|
|
||||||
api := EosioV1{
|
api.client.Host = host
|
||||||
client: *eosapi.New(url),
|
|
||||||
block_time: block_time,
|
|
||||||
}
|
|
||||||
|
|
||||||
api.client.Host = host
|
return api
|
||||||
|
|
||||||
return api
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e EosioV1) LogInfo() LogParams {
|
func (e EosioV1) LogInfo() LogParams {
|
||||||
p := LogParams{
|
p := LogParams{
|
||||||
"type", "eosio-v1",
|
"type", "eosio-v1",
|
||||||
"url", e.client.Url,
|
"url", e.client.Url,
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(e.client.Host) > 0 {
|
if len(e.client.Host) > 0 {
|
||||||
p.Add("host", e.client.Host)
|
p.Add("host", e.client.Host)
|
||||||
}
|
}
|
||||||
|
|
||||||
p.Add("block_time", e.block_time)
|
p.Add("block_time", e.block_time)
|
||||||
|
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e EosioV1) Call() (agentcheck.Response, string) {
|
func (e EosioV1) Call() (agentcheck.Response, string) {
|
||||||
|
info, err := e.client.GetInfo()
|
||||||
|
if err != nil {
|
||||||
|
resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
||||||
|
return resp, err.Error()
|
||||||
|
}
|
||||||
|
|
||||||
info, err := e.client.GetInfo()
|
// Validate head block.
|
||||||
if err != nil {
|
diff := e.GetTime().Sub(info.HeadBlockTime).Seconds()
|
||||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
|
||||||
return resp, err.Error()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate head block.
|
if diff > e.block_time {
|
||||||
diff := e.GetTime().Sub(info.HeadBlockTime).Seconds()
|
|
||||||
|
|
||||||
if diff > e.block_time {
|
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||||
|
|
||||||
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 {
|
||||||
|
|
||||||
msg := "Taking offline because head block is lagging %.0f seconds"
|
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||||
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)
|
||||||
msg := "Taking offline because head block is %.0f seconds into the future"
|
}
|
||||||
return resp, fmt.Sprintf(msg, diff)
|
return agentcheck.NewStatusResponse(agentcheck.Up), "OK"
|
||||||
}
|
|
||||||
return agentcheck.NewStatusResponse(agentcheck.Up), "OK"
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,174 +1,164 @@
|
||||||
|
|
||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"net/http"
|
||||||
"testing"
|
"net/http/httptest"
|
||||||
"net/http"
|
"testing"
|
||||||
"net/http/httptest"
|
"time"
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestEosioV1Factory(t *testing.T) {
|
func TestEosioV1Factory(t *testing.T) {
|
||||||
|
api := EosioV1Factory(ApiArguments{
|
||||||
|
Url: "https://api.v1.example.com",
|
||||||
|
Host: "host.example.com",
|
||||||
|
NumBlocks: 120,
|
||||||
|
})
|
||||||
|
|
||||||
api := EosioV1Factory(ApiArguments{
|
expected := NewEosioV1("https://api.v1.example.com", "host.example.com", 60)
|
||||||
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.IsType(t, expected, api)
|
assert.Equal(t, expected.client.Host, api.(EosioV1).client.Host)
|
||||||
assert.Equal(t, expected.client.Url, api.(EosioV1).client.Url)
|
assert.Equal(t, expected.block_time, api.(EosioV1).block_time)
|
||||||
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) {
|
func TestEosioV1LogInfo(t *testing.T) {
|
||||||
|
api := NewEosioV1("https://api.v1.example.com", "host.example.com", 120)
|
||||||
|
|
||||||
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)}
|
||||||
|
|
||||||
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())
|
||||||
|
|
||||||
assert.Equal(t, expected, api.LogInfo())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEosioV1SetTime(t *testing.T) {
|
func TestEosioV1SetTime(t *testing.T) {
|
||||||
|
expected := time.Date(2022, 2, 24, 13, 38, 0, 0, time.UTC)
|
||||||
|
|
||||||
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 := NewEosioV1("", "", 60)
|
api.SetTime(expected)
|
||||||
// Assert that time is NOW (+-10 seconds)
|
assert.Equal(t, expected, api.GetTime())
|
||||||
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) {
|
func TestEosioV1JsonFailure(t *testing.T) {
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||||
|
res.Write([]byte(`!//{invalid-json}!##`))
|
||||||
|
}))
|
||||||
|
|
||||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
api := NewEosioV1(srv.URL, "", 120)
|
||||||
res.Write([]byte(`!//{invalid-json}!##`))
|
check, _ := api.Call()
|
||||||
}))
|
|
||||||
|
|
||||||
api := NewEosioV1(srv.URL, "", 120)
|
expected := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
||||||
check, _ := api.Call()
|
assert.Equal(t, expected, check)
|
||||||
|
|
||||||
expected := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
|
||||||
assert.Equal(t, expected, check)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEosioV1HTTP500Failed(t *testing.T) {
|
func TestEosioV1HTTP500Failed(t *testing.T) {
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||||
|
res.WriteHeader(500)
|
||||||
|
res.Write([]byte(`{}`))
|
||||||
|
}))
|
||||||
|
|
||||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
api := NewEosioV1(srv.URL, "", 120)
|
||||||
res.WriteHeader(500)
|
check, status := api.Call()
|
||||||
res.Write([]byte(`{}`))
|
|
||||||
}))
|
|
||||||
|
|
||||||
api := NewEosioV1(srv.URL, "", 120)
|
assert.Equal(t, "server returned HTTP 500 Internal Server Error", status)
|
||||||
check, status := api.Call()
|
|
||||||
|
|
||||||
assert.Equal(t, "server returned HTTP 500 Internal Server Error", status)
|
expected := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
||||||
|
assert.Equal(t, expected, check)
|
||||||
expected := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
|
||||||
assert.Equal(t, expected, check)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEosioV1LaggingUp(t *testing.T) {
|
func TestEosioV1LaggingUp(t *testing.T) {
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
if req.URL.String() == "/v1/chain/get_info" {
|
||||||
if req.URL.String() == "/v1/chain/get_info" {
|
info := `{
|
||||||
info := `{
|
|
||||||
"server_version": "8f613ec9",
|
"server_version": "8f613ec9",
|
||||||
"head_block_num": 7272812,
|
"head_block_num": 7272812,
|
||||||
"head_block_time": "2022-02-24T13:37:00"
|
"head_block_time": "2022-02-24T13:37:00"
|
||||||
}`
|
}`
|
||||||
|
|
||||||
res.Write([]byte(info))
|
res.Write([]byte(info))
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
||||||
api := NewEosioV1(srv.URL, "", 60)
|
api := NewEosioV1(srv.URL, "", 60)
|
||||||
api.SetTime(time.Date(2022, 2, 24, 13, 38, 0, 0, time.UTC))
|
api.SetTime(time.Date(2022, 2, 24, 13, 38, 0, 0, time.UTC))
|
||||||
check, status := api.Call()
|
check, status := api.Call()
|
||||||
|
|
||||||
assert.Equal(t, "OK", status)
|
assert.Equal(t, "OK", status)
|
||||||
|
|
||||||
expected := agentcheck.NewStatusResponse(agentcheck.Up)
|
expected := agentcheck.NewStatusResponse(agentcheck.Up)
|
||||||
assert.Equal(t, expected, check)
|
assert.Equal(t, expected, check)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEosioV1LaggingDown(t *testing.T) {
|
func TestEosioV1LaggingDown(t *testing.T) {
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
if req.URL.String() == "/v1/chain/get_info" {
|
||||||
if req.URL.String() == "/v1/chain/get_info" {
|
info := `{
|
||||||
info := `{
|
|
||||||
"server_version": "9a607cce",
|
"server_version": "9a607cce",
|
||||||
"head_block_num": 87263,
|
"head_block_num": 87263,
|
||||||
"head_block_time": "2018-01-01T13:37:01"
|
"head_block_time": "2018-01-01T13:37:01"
|
||||||
}`
|
}`
|
||||||
|
|
||||||
res.Write([]byte(info))
|
res.Write([]byte(info))
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
||||||
api := NewEosioV1(srv.URL, "", 60)
|
api := NewEosioV1(srv.URL, "", 60)
|
||||||
api.SetTime(time.Date(2018, time.January, 1, 13, 38, 2, 0, time.UTC))
|
api.SetTime(time.Date(2018, time.January, 1, 13, 38, 2, 0, time.UTC))
|
||||||
check, status := api.Call()
|
check, status := api.Call()
|
||||||
|
|
||||||
assert.Equal(t, "Taking offline because head block is lagging 61 seconds", status)
|
assert.Equal(t, "Taking offline because head block is lagging 61 seconds", status)
|
||||||
|
|
||||||
expected := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
expected := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||||
assert.Equal(t, expected, check)
|
assert.Equal(t, expected, check)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEosioV1TimeInFutureUP(t *testing.T) {
|
func TestEosioV1TimeInFutureUP(t *testing.T) {
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
if req.URL.String() == "/v1/chain/get_info" {
|
||||||
if req.URL.String() == "/v1/chain/get_info" {
|
info := `{
|
||||||
info := `{
|
|
||||||
"server_version": "d1bec8d3",
|
"server_version": "d1bec8d3",
|
||||||
"head_block_num": 548847,
|
"head_block_num": 548847,
|
||||||
"head_block_time": "2020-09-22T09:32:00"
|
"head_block_time": "2020-09-22T09:32:00"
|
||||||
}`
|
}`
|
||||||
|
|
||||||
res.Write([]byte(info))
|
res.Write([]byte(info))
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
||||||
api := NewEosioV1(srv.URL, "", 120)
|
api := NewEosioV1(srv.URL, "", 120)
|
||||||
api.SetTime(time.Date(2020, 9, 22, 9, 30, 0, 0, time.UTC))
|
api.SetTime(time.Date(2020, 9, 22, 9, 30, 0, 0, time.UTC))
|
||||||
check, status := api.Call()
|
check, status := api.Call()
|
||||||
|
|
||||||
assert.Equal(t, "OK", status)
|
assert.Equal(t, "OK", status)
|
||||||
|
|
||||||
expected := agentcheck.NewStatusResponse(agentcheck.Up)
|
expected := agentcheck.NewStatusResponse(agentcheck.Up)
|
||||||
assert.Equal(t, expected, check)
|
assert.Equal(t, expected, check)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func TestEosioV1TimeInFutureDown(t *testing.T) {
|
func TestEosioV1TimeInFutureDown(t *testing.T) {
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
if req.URL.String() == "/v1/chain/get_info" {
|
||||||
if req.URL.String() == "/v1/chain/get_info" {
|
info := `{
|
||||||
info := `{
|
|
||||||
"server_version": "c879d231",
|
"server_version": "c879d231",
|
||||||
"head_block_num": 2637621,
|
"head_block_num": 2637621,
|
||||||
"head_block_time": "2019-04-14T12:02:01"
|
"head_block_time": "2019-04-14T12:02:01"
|
||||||
}`
|
}`
|
||||||
|
|
||||||
res.Write([]byte(info))
|
res.Write([]byte(info))
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
||||||
api := NewEosioV1(srv.URL, "", 120)
|
api := NewEosioV1(srv.URL, "", 120)
|
||||||
api.SetTime(time.Date(2019, time.April, 14, 12, 0, 0, 0, time.UTC))
|
api.SetTime(time.Date(2019, time.April, 14, 12, 0, 0, 0, time.UTC))
|
||||||
check, status := api.Call()
|
check, status := api.Call()
|
||||||
|
|
||||||
assert.Equal(t, "Taking offline because head block is -121 seconds into the future", status)
|
assert.Equal(t, "Taking offline because head block is -121 seconds into the future", status)
|
||||||
|
|
||||||
expected := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
expected := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||||
assert.Equal(t, expected, check)
|
assert.Equal(t, expected, check)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,86 +1,84 @@
|
||||||
|
|
||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/eosswedenorg/eosio-api-healthcheck/internal/utils"
|
|
||||||
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
"github.com/eosswedenorg-go/eosapi"
|
||||||
"github.com/eosswedenorg-go/eosapi"
|
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
||||||
|
"github.com/eosswedenorg/eosio-api-healthcheck/internal/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
type EosioV2 struct {
|
type EosioV2 struct {
|
||||||
client eosapi.Client
|
client eosapi.Client
|
||||||
offset int64
|
offset int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func EosioV2Factory(args ApiArguments) ApiInterface {
|
func EosioV2Factory(args ApiArguments) ApiInterface {
|
||||||
return NewEosioV2(args.Url, args.Host, int64(args.NumBlocks))
|
return NewEosioV2(args.Url, args.Host, int64(args.NumBlocks))
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewEosioV2(url string, host string, offset int64) EosioV2 {
|
func NewEosioV2(url string, host string, offset int64) EosioV2 {
|
||||||
|
api := EosioV2{
|
||||||
|
client: *eosapi.New(url),
|
||||||
|
offset: offset,
|
||||||
|
}
|
||||||
|
|
||||||
api := EosioV2{
|
api.client.Host = host
|
||||||
client: *eosapi.New(url),
|
|
||||||
offset: offset,
|
|
||||||
}
|
|
||||||
|
|
||||||
api.client.Host = host
|
return api
|
||||||
|
|
||||||
return api
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e EosioV2) LogInfo() LogParams {
|
func (e EosioV2) LogInfo() LogParams {
|
||||||
p := LogParams{
|
p := LogParams{
|
||||||
"type", "eosio-v2",
|
"type", "eosio-v2",
|
||||||
"url", e.client.Url,
|
"url", e.client.Url,
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(e.client.Host) > 0 {
|
if len(e.client.Host) > 0 {
|
||||||
p.Add("host", e.client.Host)
|
p.Add("host", e.client.Host)
|
||||||
}
|
}
|
||||||
|
|
||||||
p.Add("offset", e.offset)
|
p.Add("offset", e.offset)
|
||||||
|
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e EosioV2) Call() (agentcheck.Response, string) {
|
func (e EosioV2) Call() (agentcheck.Response, string) {
|
||||||
|
health, err := e.client.GetHealth()
|
||||||
|
if err != nil {
|
||||||
|
resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
||||||
|
return resp, err.Error()
|
||||||
|
}
|
||||||
|
|
||||||
health, err := e.client.GetHealth()
|
// Fetch elasticsearch and nodeos block numbers from json.
|
||||||
if err != nil {
|
var es_block int64 = 0
|
||||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
var node_block int64 = 0
|
||||||
return resp, err.Error()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fetch elasticsearch and nodeos block numbers from json.
|
for _, v := range health.Health {
|
||||||
var es_block int64 = 0
|
if v.Name == "Elasticsearch" {
|
||||||
var node_block int64 = 0
|
es_block = utils.JsonGetInt64(v.Data["last_indexed_block"])
|
||||||
|
} else if v.Name == "NodeosRPC" {
|
||||||
|
node_block = utils.JsonGetInt64(v.Data["head_block_num"])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for _, v := range health.Health {
|
// Error out if ether or both are zero.
|
||||||
if v.Name == "Elasticsearch" {
|
if es_block == 0 || node_block == 0 {
|
||||||
es_block = utils.JsonGetInt64(v.Data["last_indexed_block"])
|
msg := fmt.Sprintf("Failed to get Elasticsearch and/or nodeos "+
|
||||||
} else if v.Name == "NodeosRPC" {
|
"block numbers (es: %d, eos: %d)", es_block, node_block)
|
||||||
node_block = utils.JsonGetInt64(v.Data["head_block_num"])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Error out if ether or both are zero.
|
resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
||||||
if es_block == 0 || node_block == 0 {
|
return resp, msg
|
||||||
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, "")
|
// Check if ES is behind or in the future.
|
||||||
return resp, msg
|
diff := node_block - es_block
|
||||||
}
|
if diff > e.offset {
|
||||||
|
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||||
// Check if ES is behind or in the future.
|
return resp, fmt.Sprintf("Taking offline because Elastic is %d blocks behind", diff)
|
||||||
diff := node_block - es_block;
|
} else if diff < -e.offset {
|
||||||
if diff > e.offset {
|
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
return resp, fmt.Sprintf("Taking offline because Elastic is %d blocks into the future", -1*diff)
|
||||||
return resp, fmt.Sprintf("Taking offline because Elastic is %d blocks behind", diff)
|
}
|
||||||
} else if diff < -e.offset {
|
return agentcheck.NewStatusResponse(agentcheck.Up), "OK"
|
||||||
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"
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,73 +1,68 @@
|
||||||
|
|
||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"net/http"
|
||||||
"net/http"
|
"net/http/httptest"
|
||||||
"net/http/httptest"
|
"testing"
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestEosioV2Factory(t *testing.T) {
|
func TestEosioV2Factory(t *testing.T) {
|
||||||
|
api := EosioV2Factory(ApiArguments{
|
||||||
|
Url: "https://api.v2.example.com",
|
||||||
|
Host: "host.example.com",
|
||||||
|
NumBlocks: 120,
|
||||||
|
})
|
||||||
|
|
||||||
api := EosioV2Factory(ApiArguments{
|
expected := NewEosioV2("https://api.v2.example.com", "host.example.com", 120)
|
||||||
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.IsType(t, expected, api)
|
assert.Equal(t, expected.client.Host, api.(EosioV2).client.Host)
|
||||||
assert.Equal(t, expected.client.Url, api.(EosioV2).client.Url)
|
assert.Equal(t, expected.offset, api.(EosioV2).offset)
|
||||||
assert.Equal(t, expected.client.Host, api.(EosioV2).client.Host)
|
|
||||||
assert.Equal(t, expected.offset, api.(EosioV2).offset)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEosioV2LogInfo(t *testing.T) {
|
func TestEosioV2LogInfo(t *testing.T) {
|
||||||
|
api := NewEosioV2("https://api.v2.example.com", "host.example.com", 120)
|
||||||
|
|
||||||
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)}
|
||||||
|
|
||||||
expected := LogParams{"type","eosio-v2","url","https://api.v2.example.com","host","host.example.com","offset",int64(120)}
|
assert.Equal(t, expected, api.LogInfo())
|
||||||
|
|
||||||
assert.Equal(t, expected, api.LogInfo())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEosioV2JsonFailure(t *testing.T) {
|
func TestEosioV2JsonFailure(t *testing.T) {
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||||
|
res.Write([]byte(`!//{invalid-json}!##`))
|
||||||
|
}))
|
||||||
|
|
||||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
api := NewEosioV2(srv.URL, "", 120)
|
||||||
res.Write([]byte(`!//{invalid-json}!##`))
|
check, _ := api.Call()
|
||||||
}))
|
|
||||||
|
|
||||||
api := NewEosioV2(srv.URL, "", 120)
|
expected := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
||||||
check, _ := api.Call()
|
assert.Equal(t, expected, check)
|
||||||
|
|
||||||
expected := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
|
||||||
assert.Equal(t, expected, check)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEosioV2HTTP500Failed(t *testing.T) {
|
func TestEosioV2HTTP500Failed(t *testing.T) {
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||||
|
res.WriteHeader(500)
|
||||||
|
res.Write([]byte(`{}`))
|
||||||
|
}))
|
||||||
|
|
||||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
api := NewEosioV2(srv.URL, "", 120)
|
||||||
res.WriteHeader(500)
|
check, status := api.Call()
|
||||||
res.Write([]byte(`{}`))
|
|
||||||
}))
|
|
||||||
|
|
||||||
api := NewEosioV2(srv.URL, "", 120)
|
assert.Equal(t, "server returned HTTP 500 Internal Server Error", status)
|
||||||
check, status := api.Call()
|
|
||||||
|
|
||||||
assert.Equal(t, "server returned HTTP 500 Internal Server Error", status)
|
expected := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
||||||
|
assert.Equal(t, expected, check)
|
||||||
expected := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
|
||||||
assert.Equal(t, expected, check)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEosioV2LaggingUp(t *testing.T) {
|
func TestEosioV2LaggingUp(t *testing.T) {
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
if req.URL.String() == "/v2/health" {
|
||||||
if req.URL.String() == "/v2/health" {
|
info := `{
|
||||||
info := `{
|
|
||||||
"version": "1.0",
|
"version": "1.0",
|
||||||
"version_hash": "028d5a34463884fcbe2ecfd3c0fcb3b5d4d538f4fd64803c1ef7209c85f2f266",
|
"version_hash": "028d5a34463884fcbe2ecfd3c0fcb3b5d4d538f4fd64803c1ef7209c85f2f266",
|
||||||
"host": "api.test.com:443",
|
"host": "api.test.com:443",
|
||||||
|
|
@ -97,24 +92,23 @@ func TestEosioV2LaggingUp(t *testing.T) {
|
||||||
]
|
]
|
||||||
}`
|
}`
|
||||||
|
|
||||||
res.Write([]byte(info))
|
res.Write([]byte(info))
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
||||||
api := NewEosioV2(srv.URL, "", 500)
|
api := NewEosioV2(srv.URL, "", 500)
|
||||||
check, status := api.Call()
|
check, status := api.Call()
|
||||||
|
|
||||||
assert.Equal(t, "OK", status)
|
assert.Equal(t, "OK", status)
|
||||||
|
|
||||||
expected := agentcheck.NewStatusResponse(agentcheck.Up)
|
expected := agentcheck.NewStatusResponse(agentcheck.Up)
|
||||||
assert.Equal(t, expected, check)
|
assert.Equal(t, expected, check)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEosioV2LaggingDown(t *testing.T) {
|
func TestEosioV2LaggingDown(t *testing.T) {
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
if req.URL.String() == "/v2/health" {
|
||||||
if req.URL.String() == "/v2/health" {
|
info := `{
|
||||||
info := `{
|
|
||||||
"version": "1.0",
|
"version": "1.0",
|
||||||
"version_hash": "028d5a34463884fcbe2ecfd3c0fcb3b5d4d538f4fd64803c1ef7209c85f2f266",
|
"version_hash": "028d5a34463884fcbe2ecfd3c0fcb3b5d4d538f4fd64803c1ef7209c85f2f266",
|
||||||
"host": "api.test.com:443",
|
"host": "api.test.com:443",
|
||||||
|
|
@ -144,24 +138,23 @@ func TestEosioV2LaggingDown(t *testing.T) {
|
||||||
]
|
]
|
||||||
}`
|
}`
|
||||||
|
|
||||||
res.Write([]byte(info))
|
res.Write([]byte(info))
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
||||||
api := NewEosioV2(srv.URL, "", 499)
|
api := NewEosioV2(srv.URL, "", 499)
|
||||||
check, status := api.Call()
|
check, status := api.Call()
|
||||||
|
|
||||||
assert.Equal(t, "Taking offline because Elastic is 500 blocks behind", status)
|
assert.Equal(t, "Taking offline because Elastic is 500 blocks behind", status)
|
||||||
|
|
||||||
expected := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
expected := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||||
assert.Equal(t, expected, check)
|
assert.Equal(t, expected, check)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEosioV2LaggingESInFutureUP(t *testing.T) {
|
func TestEosioV2LaggingESInFutureUP(t *testing.T) {
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
if req.URL.String() == "/v2/health" {
|
||||||
if req.URL.String() == "/v2/health" {
|
info := `{
|
||||||
info := `{
|
|
||||||
"version": "1.0",
|
"version": "1.0",
|
||||||
"version_hash": "028d5a34463884fcbe2ecfd3c0fcb3b5d4d538f4fd64803c1ef7209c85f2f266",
|
"version_hash": "028d5a34463884fcbe2ecfd3c0fcb3b5d4d538f4fd64803c1ef7209c85f2f266",
|
||||||
"host": "api.test.com:443",
|
"host": "api.test.com:443",
|
||||||
|
|
@ -191,24 +184,23 @@ func TestEosioV2LaggingESInFutureUP(t *testing.T) {
|
||||||
]
|
]
|
||||||
}`
|
}`
|
||||||
|
|
||||||
res.Write([]byte(info))
|
res.Write([]byte(info))
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
||||||
api := NewEosioV2(srv.URL, "", 200)
|
api := NewEosioV2(srv.URL, "", 200)
|
||||||
check, status := api.Call()
|
check, status := api.Call()
|
||||||
|
|
||||||
assert.Equal(t, "OK", status)
|
assert.Equal(t, "OK", status)
|
||||||
|
|
||||||
expected := agentcheck.NewStatusResponse(agentcheck.Up)
|
expected := agentcheck.NewStatusResponse(agentcheck.Up)
|
||||||
assert.Equal(t, expected, check)
|
assert.Equal(t, expected, check)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEosioV2LaggingESInFutureDown(t *testing.T) {
|
func TestEosioV2LaggingESInFutureDown(t *testing.T) {
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
if req.URL.String() == "/v2/health" {
|
||||||
if req.URL.String() == "/v2/health" {
|
info := `{
|
||||||
info := `{
|
|
||||||
"version": "1.0",
|
"version": "1.0",
|
||||||
"version_hash": "028d5a34463884fcbe2ecfd3c0fcb3b5d4d538f4fd64803c1ef7209c85f2f266",
|
"version_hash": "028d5a34463884fcbe2ecfd3c0fcb3b5d4d538f4fd64803c1ef7209c85f2f266",
|
||||||
"host": "api.test.com:443",
|
"host": "api.test.com:443",
|
||||||
|
|
@ -238,24 +230,23 @@ func TestEosioV2LaggingESInFutureDown(t *testing.T) {
|
||||||
]
|
]
|
||||||
}`
|
}`
|
||||||
|
|
||||||
res.Write([]byte(info))
|
res.Write([]byte(info))
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
||||||
api := NewEosioV2(srv.URL, "", 200)
|
api := NewEosioV2(srv.URL, "", 200)
|
||||||
check, status := api.Call()
|
check, status := api.Call()
|
||||||
|
|
||||||
assert.Equal(t, "Taking offline because Elastic is 201 blocks into the future", status)
|
assert.Equal(t, "Taking offline because Elastic is 201 blocks into the future", status)
|
||||||
|
|
||||||
expected := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
expected := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||||
assert.Equal(t, expected, check)
|
assert.Equal(t, expected, check)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEosioV2ElasticsFailed(t *testing.T) {
|
func TestEosioV2ElasticsFailed(t *testing.T) {
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
if req.URL.String() == "/v2/health" {
|
||||||
if req.URL.String() == "/v2/health" {
|
info := `{
|
||||||
info := `{
|
|
||||||
"version": "1.0",
|
"version": "1.0",
|
||||||
"version_hash": "028d5a34463884fcbe2ecfd3c0fcb3b5d4d538f4fd64803c1ef7209c85f2f266",
|
"version_hash": "028d5a34463884fcbe2ecfd3c0fcb3b5d4d538f4fd64803c1ef7209c85f2f266",
|
||||||
"host": "api.test.com:443",
|
"host": "api.test.com:443",
|
||||||
|
|
@ -285,24 +276,23 @@ func TestEosioV2ElasticsFailed(t *testing.T) {
|
||||||
]
|
]
|
||||||
}`
|
}`
|
||||||
|
|
||||||
res.Write([]byte(info))
|
res.Write([]byte(info))
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
||||||
api := NewEosioV2(srv.URL, "", 500)
|
api := NewEosioV2(srv.URL, "", 500)
|
||||||
check, status := api.Call()
|
check, status := api.Call()
|
||||||
|
|
||||||
assert.Equal(t, "Failed to get Elasticsearch and/or nodeos block numbers (es: 0, eos: 263148621)", status)
|
assert.Equal(t, "Failed to get Elasticsearch and/or nodeos block numbers (es: 0, eos: 263148621)", status)
|
||||||
|
|
||||||
expected := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
expected := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
||||||
assert.Equal(t, expected, check)
|
assert.Equal(t, expected, check)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEosioV2NodeosRPCFailed(t *testing.T) {
|
func TestEosioV2NodeosRPCFailed(t *testing.T) {
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
if req.URL.String() == "/v2/health" {
|
||||||
if req.URL.String() == "/v2/health" {
|
info := `{
|
||||||
info := `{
|
|
||||||
"version": "1.0",
|
"version": "1.0",
|
||||||
"version_hash": "028d5a34463884fcbe2ecfd3c0fcb3b5d4d538f4fd64803c1ef7209c85f2f266",
|
"version_hash": "028d5a34463884fcbe2ecfd3c0fcb3b5d4d538f4fd64803c1ef7209c85f2f266",
|
||||||
"host": "api.test.com:443",
|
"host": "api.test.com:443",
|
||||||
|
|
@ -332,24 +322,23 @@ func TestEosioV2NodeosRPCFailed(t *testing.T) {
|
||||||
]
|
]
|
||||||
}`
|
}`
|
||||||
|
|
||||||
res.Write([]byte(info))
|
res.Write([]byte(info))
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
||||||
api := NewEosioV2(srv.URL, "", 500)
|
api := NewEosioV2(srv.URL, "", 500)
|
||||||
check, status := api.Call()
|
check, status := api.Call()
|
||||||
|
|
||||||
assert.Equal(t, "Failed to get Elasticsearch and/or nodeos block numbers (es: 263148121, eos: 0)", status)
|
assert.Equal(t, "Failed to get Elasticsearch and/or nodeos block numbers (es: 263148121, eos: 0)", status)
|
||||||
|
|
||||||
expected := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
expected := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
||||||
assert.Equal(t, expected, check)
|
assert.Equal(t, expected, check)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEosioV2ElasticsNodeosRPCFailed(t *testing.T) {
|
func TestEosioV2ElasticsNodeosRPCFailed(t *testing.T) {
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||||
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
if req.URL.String() == "/v2/health" {
|
||||||
if req.URL.String() == "/v2/health" {
|
info := `{
|
||||||
info := `{
|
|
||||||
"version": "1.0",
|
"version": "1.0",
|
||||||
"version_hash": "028d5a34463884fcbe2ecfd3c0fcb3b5d4d538f4fd64803c1ef7209c85f2f266",
|
"version_hash": "028d5a34463884fcbe2ecfd3c0fcb3b5d4d538f4fd64803c1ef7209c85f2f266",
|
||||||
"host": "api.test.com:443",
|
"host": "api.test.com:443",
|
||||||
|
|
@ -369,15 +358,15 @@ func TestEosioV2ElasticsNodeosRPCFailed(t *testing.T) {
|
||||||
]
|
]
|
||||||
}`
|
}`
|
||||||
|
|
||||||
res.Write([]byte(info))
|
res.Write([]byte(info))
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
||||||
api := NewEosioV2(srv.URL, "", 500)
|
api := NewEosioV2(srv.URL, "", 500)
|
||||||
check, status := api.Call()
|
check, status := api.Call()
|
||||||
|
|
||||||
assert.Equal(t, "Failed to get Elasticsearch and/or nodeos block numbers (es: 0, eos: 0)", status)
|
assert.Equal(t, "Failed to get Elasticsearch and/or nodeos block numbers (es: 0, eos: 0)", status)
|
||||||
|
|
||||||
expected := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
expected := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
||||||
assert.Equal(t, expected, check)
|
assert.Equal(t, expected, check)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,7 @@
|
||||||
|
|
||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
||||||
)
|
)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -10,9 +9,9 @@ import (
|
||||||
* to configure the API request.
|
* to configure the API request.
|
||||||
*/
|
*/
|
||||||
type ApiArguments struct {
|
type ApiArguments struct {
|
||||||
Url string
|
Url string
|
||||||
Host string
|
Host string
|
||||||
NumBlocks int
|
NumBlocks int
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -20,14 +19,13 @@ type ApiArguments struct {
|
||||||
*
|
*
|
||||||
* Each API must implement this function and process `args`
|
* Each API must implement this function and process `args`
|
||||||
* returing a instance of it's implementation of the ApiInterface
|
* returing a instance of it's implementation of the ApiInterface
|
||||||
*/
|
*/
|
||||||
type Factory func(args ApiArguments) ApiInterface
|
type Factory func(args ApiArguments) ApiInterface
|
||||||
|
|
||||||
type ApiInterface interface {
|
type ApiInterface interface {
|
||||||
|
// Returns Logging information
|
||||||
|
LogInfo() LogParams
|
||||||
|
|
||||||
// Returns Logging information
|
// Call api and validate it's status.
|
||||||
LogInfo() LogParams
|
Call() (agentcheck.Response, string)
|
||||||
|
|
||||||
// Call api and validate it's status.
|
|
||||||
Call() (agentcheck.Response, string)
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,13 @@
|
||||||
|
|
||||||
package api
|
package api
|
||||||
|
|
||||||
type LogParams []interface{}
|
type LogParams []interface{}
|
||||||
|
|
||||||
func (p *LogParams) Add(field string, value interface{}) {
|
func (p *LogParams) Add(field string, value interface{}) {
|
||||||
*p = append(*p, field, value)
|
*p = append(*p, field, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Syntactic sugar for append(p, other...)
|
// Syntactic sugar for append(p, other...)
|
||||||
// Returns a new instance of LogParams with all values from both p and other
|
// Returns a new instance of LogParams with all values from both p and other
|
||||||
func (p LogParams) Combine(other LogParams) LogParams {
|
func (p LogParams) Combine(other LogParams) LogParams {
|
||||||
return append(p, other...)
|
return append(p, other...)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,50 +1,47 @@
|
||||||
|
|
||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
func TestLogParams(t *testing.T) {
|
func TestLogParams(t *testing.T) {
|
||||||
|
type test_struct struct {
|
||||||
|
First string
|
||||||
|
Second int
|
||||||
|
}
|
||||||
|
|
||||||
type test_struct struct {
|
p := LogParams{}
|
||||||
First string
|
|
||||||
Second int
|
|
||||||
}
|
|
||||||
|
|
||||||
p := LogParams{}
|
p.Add("one", 1)
|
||||||
|
p.Add("string", "str")
|
||||||
|
p.Add("struct", test_struct{First: "first_string", Second: 1234})
|
||||||
|
|
||||||
p.Add("one", 1)
|
expected := []interface{}([]interface{}{
|
||||||
p.Add("string", "str")
|
"one", 1,
|
||||||
p.Add("struct", test_struct{First:"first_string",Second:1234})
|
"string", "str",
|
||||||
|
"struct",
|
||||||
|
test_struct{
|
||||||
|
First: "first_string",
|
||||||
|
Second: 1234,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
expected := []interface{}([]interface {}{
|
assert.ElementsMatch(t, expected, p)
|
||||||
"one",1,
|
|
||||||
"string","str",
|
|
||||||
"struct",test_struct{
|
|
||||||
First:"first_string",
|
|
||||||
Second:1234,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
assert.ElementsMatch(t, expected, p)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLogParamsCombine(t *testing.T) {
|
func TestLogParamsCombine(t *testing.T) {
|
||||||
|
a := LogParams{"one", 1, "string1", "str1"}
|
||||||
|
|
||||||
a := LogParams{"one",1,"string1","str1"}
|
b := LogParams{"two", 2, "string2", "str2"}
|
||||||
|
|
||||||
b := LogParams{"two",2,"string2","str2"}
|
expected := LogParams{
|
||||||
|
"one", 1,
|
||||||
|
"string1", "str1",
|
||||||
|
"two", 2,
|
||||||
|
"string2", "str2",
|
||||||
|
}
|
||||||
|
|
||||||
expected := LogParams{
|
assert.Equal(t, expected, a.Combine(b))
|
||||||
"one",1,
|
|
||||||
"string1","str1",
|
|
||||||
"two",2,
|
|
||||||
"string2","str2",
|
|
||||||
}
|
|
||||||
|
|
||||||
assert.Equal(t, expected, a.Combine(b))
|
|
||||||
}
|
}
|
||||||
|
|
@ -1,60 +1,58 @@
|
||||||
|
|
||||||
package internal
|
package internal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"fmt"
|
||||||
"fmt"
|
"strconv"
|
||||||
"strconv"
|
"strings"
|
||||||
"github.com/eosswedenorg/eosio-api-healthcheck/internal/api"
|
|
||||||
|
"github.com/eosswedenorg/eosio-api-healthcheck/internal/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ParseArguments(args []string) api.ApiArguments {
|
func ParseArguments(args []string) api.ApiArguments {
|
||||||
|
a := api.ApiArguments{
|
||||||
|
NumBlocks: 10,
|
||||||
|
}
|
||||||
|
|
||||||
a := api.ApiArguments{
|
// 1. url (scheme + ip/domain + port)
|
||||||
NumBlocks: 10,
|
a.Url = args[0]
|
||||||
}
|
|
||||||
|
|
||||||
// 1. url (scheme + ip/domain + port)
|
// 2. num blocks
|
||||||
a.Url = args[0]
|
if len(args) > 1 {
|
||||||
|
num, err := strconv.ParseInt(args[1], 10, 32)
|
||||||
|
if err == nil {
|
||||||
|
a.NumBlocks = int(num)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 2. num blocks
|
// 3. Host
|
||||||
if len(args) > 1 {
|
if len(args) > 2 {
|
||||||
num, err := strconv.ParseInt(args[1], 10, 32)
|
a.Host = args[2]
|
||||||
if err == nil {
|
}
|
||||||
a.NumBlocks = int(num)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3. Host
|
return a
|
||||||
if len(args) > 2 {
|
|
||||||
a.Host = args[2]
|
|
||||||
}
|
|
||||||
|
|
||||||
return a
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseRequest(request string) (api.ApiInterface, error) {
|
func ParseRequest(request string) (api.ApiInterface, error) {
|
||||||
|
factories := map[string]api.Factory{
|
||||||
|
"v1": api.EosioV1Factory,
|
||||||
|
"v2": api.EosioV2Factory,
|
||||||
|
"contract": api.EosioContractFactory,
|
||||||
|
"debug": api.DebugApiFactory,
|
||||||
|
}
|
||||||
|
|
||||||
factories := map[string]api.Factory{
|
// Parse arguments.
|
||||||
"v1": api.EosioV1Factory,
|
// -------------------
|
||||||
"v2": api.EosioV2Factory,
|
p := strings.Split(strings.TrimSpace(request), "|")
|
||||||
"contract": api.EosioContractFactory,
|
|
||||||
"debug": api.DebugApiFactory,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse arguments.
|
if len(p) < 2 {
|
||||||
// -------------------
|
return nil, fmt.Errorf("invalid number of parameters in agent request")
|
||||||
p := strings.Split(strings.TrimSpace(request), "|")
|
}
|
||||||
|
|
||||||
if len(p) < 2 {
|
a := ParseArguments(p[1:])
|
||||||
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
|
||||||
|
}
|
||||||
|
|
||||||
if factory, ok := factories[p[0]]; ok {
|
return nil, fmt.Errorf("invalid API '%s'", p[0])
|
||||||
return factory(a), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, fmt.Errorf("invalid API '%s'", p[0])
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,117 +1,104 @@
|
||||||
|
|
||||||
package internal
|
package internal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// "fmt"
|
"testing"
|
||||||
"testing"
|
// "fmt"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/eosswedenorg/eosio-api-healthcheck/internal/api"
|
||||||
"github.com/eosswedenorg/eosio-api-healthcheck/internal/api"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestParseWithInvalidApi(t *testing.T) {
|
func TestParseWithInvalidApi(t *testing.T) {
|
||||||
|
api, err := ParseRequest("invalid|http://api.example.com")
|
||||||
api, err := ParseRequest("invalid|http://api.example.com")
|
assert.Error(t, err)
|
||||||
assert.Error(t, err)
|
assert.Equal(t, err.Error(), "invalid API 'invalid'")
|
||||||
assert.Equal(t, err.Error(), "invalid API 'invalid'")
|
assert.Nil(t, api)
|
||||||
assert.Nil(t, api)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseWithInvalidParams(t *testing.T) {
|
func TestParseWithInvalidParams(t *testing.T) {
|
||||||
|
api, err := ParseRequest("v1")
|
||||||
api, err := ParseRequest("v1")
|
assert.Error(t, err)
|
||||||
assert.Error(t, err)
|
assert.Equal(t, err.Error(), "invalid number of parameters in agent request")
|
||||||
assert.Equal(t, err.Error(), "invalid number of parameters in agent request")
|
assert.Nil(t, api)
|
||||||
assert.Nil(t, api)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// EosioV1
|
// EosioV1
|
||||||
// --------------------------------
|
// --------------------------------
|
||||||
|
|
||||||
func TestParseEosioV1(t *testing.T) {
|
func TestParseEosioV1(t *testing.T) {
|
||||||
|
expected := api.NewEosioV1("http://api.example.com", "", 5)
|
||||||
|
|
||||||
expected := api.NewEosioV1("http://api.example.com", "", 5)
|
api, err := ParseRequest("v1|http://api.example.com")
|
||||||
|
assert.NoError(t, err)
|
||||||
api, err := ParseRequest("v1|http://api.example.com")
|
assert.Equal(t, expected.LogInfo(), api.LogInfo())
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, expected.LogInfo(), api.LogInfo())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseEosioV1WithBlockNumber(t *testing.T) {
|
func TestParseEosioV1WithBlockNumber(t *testing.T) {
|
||||||
|
expected := api.NewEosioV1("http://api.example.com", "", 1000)
|
||||||
|
|
||||||
expected := api.NewEosioV1("http://api.example.com", "", 1000)
|
api, err := ParseRequest("v1|http://api.example.com|2000")
|
||||||
|
assert.NoError(t, err)
|
||||||
api, err := ParseRequest("v1|http://api.example.com|2000")
|
assert.Equal(t, expected.LogInfo(), api.LogInfo())
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, expected.LogInfo(), api.LogInfo())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func TestParseEosioV1Full(t *testing.T) {
|
func TestParseEosioV1Full(t *testing.T) {
|
||||||
|
expected := api.NewEosioV1("http://api.example.com", "http://host.example.com", 500)
|
||||||
|
|
||||||
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)
|
||||||
api, err := ParseRequest("v1|http://api.example.com|1000|http://host.example.com")
|
assert.Equal(t, expected.LogInfo(), api.LogInfo())
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, expected.LogInfo(), api.LogInfo())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// EosioV2
|
// EosioV2
|
||||||
// --------------------------------
|
// --------------------------------
|
||||||
|
|
||||||
func TestParseEosioV2(t *testing.T) {
|
func TestParseEosioV2(t *testing.T) {
|
||||||
|
expected := api.NewEosioV2("http://api.v2.example.com", "", 10)
|
||||||
|
|
||||||
expected := api.NewEosioV2("http://api.v2.example.com", "", 10)
|
api, err := ParseRequest("v2|http://api.v2.example.com")
|
||||||
|
assert.NoError(t, err)
|
||||||
api, err := ParseRequest("v2|http://api.v2.example.com")
|
assert.Equal(t, expected.LogInfo(), api.LogInfo())
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, expected.LogInfo(), api.LogInfo())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseEosioV2WithOffset(t *testing.T) {
|
func TestParseEosioV2WithOffset(t *testing.T) {
|
||||||
|
expected := api.NewEosioV2("http://api.v2.example.com", "", 1000)
|
||||||
|
|
||||||
expected := api.NewEosioV2("http://api.v2.example.com", "", 1000)
|
api, err := ParseRequest("v2|http://api.v2.example.com|1000")
|
||||||
|
assert.NoError(t, err)
|
||||||
api, err := ParseRequest("v2|http://api.v2.example.com|1000")
|
assert.Equal(t, expected.LogInfo(), api.LogInfo())
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, expected.LogInfo(), api.LogInfo())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseEosioV2Full(t *testing.T) {
|
func TestParseEosioV2Full(t *testing.T) {
|
||||||
|
expected := api.NewEosioV2("http://api.v2.example.com", "http://host.example.com", 1000)
|
||||||
|
|
||||||
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)
|
||||||
|
|
||||||
api, err := ParseRequest("v2|http://api.v2.example.com|1000|http://host.example.com")
|
assert.Equal(t, expected.LogInfo(), api.LogInfo())
|
||||||
assert.NoError(t, err)
|
|
||||||
|
|
||||||
assert.Equal(t, expected.LogInfo(), api.LogInfo())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// EosioContract
|
// EosioContract
|
||||||
// --------------------------------
|
// --------------------------------
|
||||||
|
|
||||||
func TestParseEosioContract(t *testing.T) {
|
func TestParseEosioContract(t *testing.T) {
|
||||||
|
expected := api.NewEosioContract("http://api.contract.example.com", 5)
|
||||||
|
|
||||||
expected := api.NewEosioContract("http://api.contract.example.com", 5)
|
api, err := ParseRequest("contract|http://api.contract.example.com")
|
||||||
|
assert.NoError(t, err)
|
||||||
api, err := ParseRequest("contract|http://api.contract.example.com")
|
assert.Equal(t, expected.LogInfo(), api.LogInfo())
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, expected.LogInfo(), api.LogInfo())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseEosioContractWithBlockTime(t *testing.T) {
|
func TestParseEosioContractWithBlockTime(t *testing.T) {
|
||||||
|
expected := api.NewEosioContract("http://api.contract.example.com", 256)
|
||||||
|
|
||||||
expected := api.NewEosioContract("http://api.contract.example.com", 256)
|
api, err := ParseRequest("contract|http://api.contract.example.com|512")
|
||||||
|
assert.NoError(t, err)
|
||||||
api, err := ParseRequest("contract|http://api.contract.example.com|512")
|
assert.Equal(t, expected.LogInfo(), api.LogInfo())
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, expected.LogInfo(), api.LogInfo())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseDebugApi(t *testing.T) {
|
func TestParseDebugApi(t *testing.T) {
|
||||||
|
expected := api.NewDebugApi("some_api_call")
|
||||||
|
|
||||||
expected := api.NewDebugApi("some_api_call")
|
api, err := ParseRequest("debug|some_api_call")
|
||||||
|
assert.NoError(t, err)
|
||||||
api, err := ParseRequest("debug|some_api_call")
|
assert.Equal(t, expected.LogInfo(), api.LogInfo())
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, expected.LogInfo(), api.LogInfo())
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,57 +1,57 @@
|
||||||
package internal
|
package internal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
log "github.com/inconshreveable/log15"
|
|
||||||
"github.com/eosswedenorg/eosio-api-healthcheck/internal/api"
|
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
||||||
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
"github.com/eosswedenorg-go/tcp_server"
|
||||||
"github.com/eosswedenorg-go/tcp_server"
|
"github.com/eosswedenorg/eosio-api-healthcheck/internal/api"
|
||||||
|
log "github.com/inconshreveable/log15"
|
||||||
)
|
)
|
||||||
|
|
||||||
// onTcpMessage callback function
|
// onTcpMessage callback function
|
||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
|
|
||||||
func onTcpMessage(c *tcp_server.Client, args string) {
|
func onTcpMessage(c *tcp_server.Client, args string) {
|
||||||
|
logger := log.Root()
|
||||||
|
|
||||||
logger := log.Root()
|
// Check api.
|
||||||
|
// -------------------
|
||||||
|
healthCheckApi, err := ParseRequest(args)
|
||||||
|
if err != nil {
|
||||||
|
logger.Warn("Agent request error", "message", err)
|
||||||
|
resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
||||||
|
|
||||||
// Check api.
|
c.WriteString(resp.String())
|
||||||
// -------------------
|
c.Close()
|
||||||
healthCheckApi, err := ParseRequest(args)
|
return
|
||||||
if err != nil {
|
}
|
||||||
logger.Warn("Agent request error", "message", err)
|
|
||||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
|
|
||||||
|
|
||||||
c.WriteString(resp.String())
|
status, msg := healthCheckApi.Call()
|
||||||
c.Close()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
status, msg := healthCheckApi.Call()
|
params := api.LogParams{}
|
||||||
|
params.Add("status", strings.TrimSpace(status.String()))
|
||||||
|
|
||||||
params := api.LogParams{}
|
if msg != "OK" && len(msg) > 0 {
|
||||||
params.Add("status", strings.TrimSpace(status.String()))
|
params.Add("error", msg)
|
||||||
|
}
|
||||||
|
|
||||||
if msg != "OK" && len(msg) > 0 {
|
logger.Info("API Check", params.Combine(healthCheckApi.LogInfo())...)
|
||||||
params.Add("error", msg)
|
// Report status to HAproxy
|
||||||
}
|
c.WriteString(status.String())
|
||||||
|
c.Close()
|
||||||
logger.Info("API Check", params.Combine(healthCheckApi.LogInfo())...)
|
|
||||||
// Report status to HAproxy
|
|
||||||
c.WriteString(status.String())
|
|
||||||
c.Close()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SpawnTcpServer
|
// SpawnTcpServer
|
||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
|
|
||||||
func SpawnTcpServer(addr string) error {
|
func SpawnTcpServer(addr string) error {
|
||||||
server := tcp_server.New(addr)
|
server := tcp_server.New(addr)
|
||||||
server.OnMessage(onTcpMessage)
|
server.OnMessage(onTcpMessage)
|
||||||
|
|
||||||
err := server.Connect()
|
err := server.Connect()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
go server.Listen()
|
go server.Listen()
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
|
|
||||||
package utils
|
package utils
|
||||||
|
|
||||||
// JsonGetInt64
|
// JsonGetInt64
|
||||||
|
|
@ -8,10 +7,10 @@ package utils
|
||||||
// if the type assertion fails, the function defaults 0 (zero).
|
// if the type assertion fails, the function defaults 0 (zero).
|
||||||
// ---------------------------------------------------------
|
// ---------------------------------------------------------
|
||||||
|
|
||||||
func JsonGetInt64(input interface{}) (int64) {
|
func JsonGetInt64(input interface{}) int64 {
|
||||||
v, res := input.(float64)
|
v, res := input.(float64)
|
||||||
if res {
|
if res {
|
||||||
return (int64) (v)
|
return (int64)(v)
|
||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,20 +3,20 @@ package utils
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
func TestJsonGetInt64(t *testing.T) {
|
func TestJsonGetInt64(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
input interface{}
|
input interface{}
|
||||||
want int64
|
want int64
|
||||||
}{
|
}{
|
||||||
{"String", "test", 0 },
|
{"String", "test", 0},
|
||||||
{"Int", 1234, 0 },
|
{"Int", 1234, 0},
|
||||||
{"Float", float64(1234), 1234 },
|
{"Float", float64(1234), 1234},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
if got := JsonGetInt64(tt.input); got != tt.want {
|
if got := JsonGetInt64(tt.input); got != tt.want {
|
||||||
t.Errorf("JsonGetInt64() = %v, want %v", got, tt.want)
|
t.Errorf("JsonGetInt64() = %v, want %v", got, tt.want)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,18 @@
|
||||||
|
|
||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
log "github.com/inconshreveable/log15"
|
log "github.com/inconshreveable/log15"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ParseLogFormatter(name string) log.Format {
|
func ParseLogFormatter(name string) log.Format {
|
||||||
|
switch name {
|
||||||
switch name {
|
case "logfmt":
|
||||||
case "logfmt" :
|
return log.LogfmtFormat()
|
||||||
return log.LogfmtFormat()
|
case "json":
|
||||||
case "json" :
|
return log.JsonFormat()
|
||||||
return log.JsonFormat()
|
case "json-pretty":
|
||||||
case "json-pretty" :
|
return log.JsonFormatEx(true, true)
|
||||||
return log.JsonFormatEx(true, true)
|
default:
|
||||||
default :
|
return log.TerminalFormat()
|
||||||
return log.TerminalFormat()
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,29 +1,29 @@
|
||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
log "github.com/inconshreveable/log15"
|
log "github.com/inconshreveable/log15"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_ParseLogFormatter(t *testing.T) {
|
func Test_ParseLogFormatter(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
arg string
|
arg string
|
||||||
want log.Format
|
want log.Format
|
||||||
}{
|
}{
|
||||||
{ "Default", "", log.TerminalFormat() },
|
{"Default", "", log.TerminalFormat()},
|
||||||
{ "LogFmt", "logfmt", log.LogfmtFormat() },
|
{"LogFmt", "logfmt", log.LogfmtFormat()},
|
||||||
{ "Json", "json", log.JsonFormat() },
|
{"Json", "json", log.JsonFormat()},
|
||||||
{ "JsonPretty", "json-pretty", log.JsonFormat() },
|
{"JsonPretty", "json-pretty", log.JsonFormat()},
|
||||||
{ "Unknown", "unknown", log.TerminalFormat() },
|
{"Unknown", "unknown", log.TerminalFormat()},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
if got := ParseLogFormatter(tt.arg); reflect.ValueOf(got).Pointer() != reflect.ValueOf(tt.want).Pointer() {
|
if got := ParseLogFormatter(tt.arg); reflect.ValueOf(got).Pointer() != reflect.ValueOf(tt.want).Pointer() {
|
||||||
t.Errorf("parseLogFormatter() = %v, want %v", got, tt.want)
|
t.Errorf("parseLogFormatter() = %v, want %v", got, tt.want)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,22 +1,20 @@
|
||||||
|
|
||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Time struct {
|
type Time struct {
|
||||||
ts time.Time
|
ts time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Time) SetTime(value time.Time) {
|
func (t *Time) SetTime(value time.Time) {
|
||||||
t.ts = value
|
t.ts = value
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Time) GetTime() time.Time {
|
func (t Time) GetTime() time.Time {
|
||||||
|
if !t.ts.IsZero() {
|
||||||
if ! t.ts.IsZero() {
|
return t.ts
|
||||||
return t.ts
|
}
|
||||||
}
|
return time.Now().In(time.UTC)
|
||||||
return time.Now().In(time.UTC)
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,26 +1,24 @@
|
||||||
|
|
||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"testing"
|
||||||
"testing"
|
"time"
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestTimeGetTimeWithDefaultValue(t *testing.T) {
|
func TestTimeGetTimeWithDefaultValue(t *testing.T) {
|
||||||
|
var ts Time
|
||||||
|
|
||||||
var ts Time
|
// Assert that time is NOW (+-10 seconds)
|
||||||
|
assert.InDelta(t, ts.GetTime().Unix(), time.Now().In(time.UTC).Unix(), float64(10))
|
||||||
// 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) {
|
func TestTimeGetTimeWithSetTime(t *testing.T) {
|
||||||
|
var ts Time
|
||||||
|
|
||||||
var ts Time
|
expected := time.Unix(1048722042, 500)
|
||||||
|
ts.SetTime(expected)
|
||||||
|
|
||||||
expected := time.Unix(1048722042, 500)
|
assert.Equal(t, expected, ts.GetTime())
|
||||||
ts.SetTime(expected)
|
|
||||||
|
|
||||||
assert.Equal(t, expected, ts.GetTime())
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue