1
0
Fork 0
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:
Henrik Hautakoski 2022-11-23 15:52:19 +01:00
parent b0e5b455ca
commit adb1ad3c6d
No known key found for this signature in database
GPG key ID: 608414D93E862CCD
21 changed files with 884 additions and 950 deletions

View file

@ -5,18 +5,21 @@ import (
"os" "os"
"os/signal" "os/signal"
"syscall" "syscall"
"github.com/eosswedenorg-go/pid"
"github.com/eosswedenorg/eosio-api-healthcheck/internal" "github.com/eosswedenorg/eosio-api-healthcheck/internal"
"github.com/eosswedenorg/eosio-api-healthcheck/internal/utils" "github.com/eosswedenorg/eosio-api-healthcheck/internal/utils"
log "github.com/inconshreveable/log15" log "github.com/inconshreveable/log15"
"github.com/eosswedenorg-go/pid"
"github.com/pborman/getopt/v2" "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,14 +31,16 @@ 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() argv := getopt.Args()
@ -56,9 +61,8 @@ func argv_listen_addr() string {
} }
func setLogFile() { func setLogFile() {
// Open file // Open file
fd, err := os.OpenFile(logFile, os.O_APPEND | os.O_CREATE | os.O_WRONLY, 0644) fd, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil { if err != nil {
logger.Error(err.Error()) logger.Error(err.Error())
} }
@ -78,9 +82,9 @@ func setLogFile() {
// 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 // Setup a channel
sig_ch := make(chan os.Signal, 1) sig_ch := make(chan os.Signal, 1)
@ -92,16 +96,16 @@ func signalEventLoop() {
var run bool = true var run bool = true
for run { for run {
// Block until we get a signal. // Block until we get a signal.
sig := <- sig_ch sig := <-sig_ch
l := logger.New("signal", sig) l := logger.New("signal", sig)
switch sig { switch sig {
case syscall.SIGINT, syscall.SIGTERM : case syscall.SIGINT, syscall.SIGTERM:
l.Info("Program was asked to terminate.") l.Info("Program was asked to terminate.")
run = false run = false
// SIGHUP is sent when logfile is rotated. // SIGHUP is sent when logfile is rotated.
case syscall.SIGHUP : case syscall.SIGHUP:
msg := "Logfile was rotated: " msg := "Logfile was rotated: "
if logfd != nil { if logfd != nil {
@ -120,9 +124,9 @@ func signalEventLoop() {
} }
// main // main
//
// --------------------------------------------------------- // ---------------------------------------------------------
func main() { func main() {
var version bool var version bool
var usage bool var usage bool
var addr string var addr string
@ -147,7 +151,7 @@ func main() {
if version { if version {
fmt.Printf("Version: %s\n", VersionString) fmt.Printf("Version: %s\n", VersionString)
return; return
} }
logfmt = utils.ParseLogFormatter(*logFormatter) logfmt = utils.ParseLogFormatter(*logFormatter)

View file

@ -1,8 +1,8 @@
package api package api
import ( import (
"strings" "strings"
"github.com/eosswedenorg-go/haproxy/agentcheck" "github.com/eosswedenorg-go/haproxy/agentcheck"
) )
@ -11,7 +11,6 @@ type DebugApi struct {
} }
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 // Status with message
@ -30,7 +29,6 @@ func DebugApiFactory(args ApiArguments) ApiInterface {
} }
func NewDebugApi(response string) DebugApi { func NewDebugApi(response string) DebugApi {
resp, _ := parseResponse(response) resp, _ := parseResponse(response)
return DebugApi{ return DebugApi{

View file

@ -3,12 +3,12 @@ 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{ api := DebugApiFactory(ApiArguments{
Url: "up", Url: "up",
Host: "host", Host: "host",
@ -43,7 +43,6 @@ 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{ api := DebugApi{
@ -54,7 +53,6 @@ func TestDebugApi_LogInfo(t *testing.T) {
} }
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{ api := DebugApi{

View file

@ -1,11 +1,11 @@
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 {
@ -15,7 +15,7 @@ type EosioContract struct {
} }
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 {
@ -36,7 +36,6 @@ func (e EosioContract) LogInfo() LogParams {
} }
func (e EosioContract) Call() (agentcheck.Response, string) { func (e EosioContract) Call() (agentcheck.Response, string) {
h, err := e.client.GetHealth() h, err := e.client.GetHealth()
if err != nil { if err != nil {
resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "") resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")

View file

@ -1,17 +1,16 @@
package api package api
import ( import (
"time"
"testing"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"github.com/stretchr/testify/assert" "testing"
"time"
"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{ api := EosioContractFactory(ApiArguments{
Url: "https://atomic.example.com", Url: "https://atomic.example.com",
NumBlocks: 120, NumBlocks: 120,
@ -26,16 +25,14 @@ func TestEosioContractFactory(t *testing.T) {
} }
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) api := NewEosioContract("", 60)
@ -47,8 +44,7 @@ func TestEosioContractSetTime(t *testing.T) {
} }
func TestEosioContractJsonFailure(t *testing.T) { func TestEosioContractJsonFailure(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) {
res.Write([]byte(`!//{invalid-json}!##`)) res.Write([]byte(`!//{invalid-json}!##`))
})) }))
@ -60,8 +56,7 @@ func TestEosioContractJsonFailure(t *testing.T) {
} }
func TestEosioContractHTTP500Down(t *testing.T) { func TestEosioContractHTTP500Down(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) {
res.Header().Add("Content-type", "application/json; charset=utf-8") res.Header().Add("Content-type", "application/json; charset=utf-8")
res.WriteHeader(500) res.WriteHeader(500)
res.Write([]byte(`{}`)) res.Write([]byte(`{}`))
@ -77,8 +72,7 @@ func TestEosioContractHTTP500Down(t *testing.T) {
} }
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,
@ -116,8 +110,7 @@ func TestEosioContractLaggingUp(t *testing.T) {
} }
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,
@ -155,8 +148,7 @@ func TestEosioContractLaggingDown(t *testing.T) {
} }
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,
@ -194,8 +186,7 @@ func TestEosioContractInFutureUp(t *testing.T) {
} }
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,
@ -222,7 +213,7 @@ func TestEosioContractInFutureDown(t *testing.T) {
})) }))
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()
@ -232,10 +223,8 @@ func TestEosioContractInFutureDown(t *testing.T) {
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,
@ -273,8 +262,7 @@ func TestEosioContractRedisDown(t *testing.T) {
} }
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,

View file

@ -1,11 +1,11 @@
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 {
@ -15,11 +15,10 @@ type EosioV1 struct {
} }
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{ api := EosioV1{
client: *eosapi.New(url), client: *eosapi.New(url),
block_time: block_time, block_time: block_time,
@ -46,7 +45,6 @@ func (e EosioV1) LogInfo() LogParams {
} }
func (e EosioV1) Call() (agentcheck.Response, string) { func (e EosioV1) Call() (agentcheck.Response, string) {
info, err := e.client.GetInfo() info, err := e.client.GetInfo()
if err != nil { if err != nil {
resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "") resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")

View file

@ -1,17 +1,16 @@
package api package api
import ( import (
"time"
"testing"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"github.com/stretchr/testify/assert" "testing"
"time"
"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{ api := EosioV1Factory(ApiArguments{
Url: "https://api.v1.example.com", Url: "https://api.v1.example.com",
Host: "host.example.com", Host: "host.example.com",
@ -27,16 +26,14 @@ func TestEosioV1Factory(t *testing.T) {
} }
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) api := NewEosioV1("", "", 60)
@ -48,8 +45,7 @@ func TestEosioV1SetTime(t *testing.T) {
} }
func TestEosioV1JsonFailure(t *testing.T) { func TestEosioV1JsonFailure(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) {
res.Write([]byte(`!//{invalid-json}!##`)) res.Write([]byte(`!//{invalid-json}!##`))
})) }))
@ -61,8 +57,7 @@ func TestEosioV1JsonFailure(t *testing.T) {
} }
func TestEosioV1HTTP500Failed(t *testing.T) { func TestEosioV1HTTP500Failed(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) {
res.WriteHeader(500) res.WriteHeader(500)
res.Write([]byte(`{}`)) res.Write([]byte(`{}`))
})) }))
@ -77,8 +72,7 @@ func TestEosioV1HTTP500Failed(t *testing.T) {
} }
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",
@ -101,8 +95,7 @@ func TestEosioV1LaggingUp(t *testing.T) {
} }
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",
@ -125,8 +118,7 @@ func TestEosioV1LaggingDown(t *testing.T) {
} }
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",
@ -148,10 +140,8 @@ func TestEosioV1TimeInFutureUP(t *testing.T) {
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",

View file

@ -1,11 +1,11 @@
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 {
@ -18,7 +18,6 @@ func EosioV2Factory(args ApiArguments) ApiInterface {
} }
func NewEosioV2(url string, host string, offset int64) EosioV2 { func NewEosioV2(url string, host string, offset int64) EosioV2 {
api := EosioV2{ api := EosioV2{
client: *eosapi.New(url), client: *eosapi.New(url),
offset: offset, offset: offset,
@ -45,7 +44,6 @@ func (e EosioV2) LogInfo() LogParams {
} }
func (e EosioV2) Call() (agentcheck.Response, string) { func (e EosioV2) Call() (agentcheck.Response, string) {
health, err := e.client.GetHealth() health, err := e.client.GetHealth()
if err != nil { if err != nil {
resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "") resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
@ -66,7 +64,7 @@ func (e EosioV2) Call() (agentcheck.Response, string) {
// Error out if ether or both are zero. // Error out if ether or both are zero.
if es_block == 0 || node_block == 0 { if es_block == 0 || node_block == 0 {
msg := fmt.Sprintf("Failed to get Elasticsearch and/or nodeos " + msg := fmt.Sprintf("Failed to get Elasticsearch and/or nodeos "+
"block numbers (es: %d, eos: %d)", es_block, node_block) "block numbers (es: %d, eos: %d)", es_block, node_block)
resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "") resp := agentcheck.NewStatusMessageResponse(agentcheck.Failed, "")
@ -74,13 +72,13 @@ func (e EosioV2) Call() (agentcheck.Response, string) {
} }
// Check if ES is behind or in the future. // Check if ES is behind or in the future.
diff := node_block - es_block; diff := node_block - es_block
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 behind", diff) return resp, fmt.Sprintf("Taking offline because Elastic is %d blocks behind", diff)
} else if diff < -e.offset { } else 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 into the future", -1*diff)
} }
return agentcheck.NewStatusResponse(agentcheck.Up), "OK" return agentcheck.NewStatusResponse(agentcheck.Up), "OK"
} }

View file

@ -1,16 +1,15 @@
package api package api
import ( import (
"testing"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"github.com/stretchr/testify/assert" "testing"
"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{ api := EosioV2Factory(ApiArguments{
Url: "https://api.v2.example.com", Url: "https://api.v2.example.com",
Host: "host.example.com", Host: "host.example.com",
@ -26,17 +25,15 @@ func TestEosioV2Factory(t *testing.T) {
} }
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) {
var srv = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.Write([]byte(`!//{invalid-json}!##`)) res.Write([]byte(`!//{invalid-json}!##`))
})) }))
@ -48,8 +45,7 @@ func TestEosioV2JsonFailure(t *testing.T) {
} }
func TestEosioV2HTTP500Failed(t *testing.T) { func TestEosioV2HTTP500Failed(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) {
res.WriteHeader(500) res.WriteHeader(500)
res.Write([]byte(`{}`)) res.Write([]byte(`{}`))
})) }))
@ -64,8 +60,7 @@ func TestEosioV2HTTP500Failed(t *testing.T) {
} }
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",
@ -111,8 +106,7 @@ func TestEosioV2LaggingUp(t *testing.T) {
} }
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",
@ -158,8 +152,7 @@ func TestEosioV2LaggingDown(t *testing.T) {
} }
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",
@ -205,8 +198,7 @@ func TestEosioV2LaggingESInFutureUP(t *testing.T) {
} }
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",
@ -252,8 +244,7 @@ func TestEosioV2LaggingESInFutureDown(t *testing.T) {
} }
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",
@ -299,8 +290,7 @@ func TestEosioV2ElasticsFailed(t *testing.T) {
} }
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",
@ -346,8 +336,7 @@ func TestEosioV2NodeosRPCFailed(t *testing.T) {
} }
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",

View file

@ -1,4 +1,3 @@
package api package api
import ( import (
@ -20,11 +19,10 @@ 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 // Returns Logging information
LogInfo() LogParams LogInfo() LogParams

View file

@ -1,4 +1,3 @@
package api package api
type LogParams []interface{} type LogParams []interface{}

View file

@ -1,15 +1,12 @@
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 { type test_struct struct {
First string First string
Second int Second int
@ -19,14 +16,15 @@ func TestLogParams(t *testing.T) {
p.Add("one", 1) p.Add("one", 1)
p.Add("string", "str") p.Add("string", "str")
p.Add("struct", test_struct{First:"first_string",Second:1234}) p.Add("struct", test_struct{First: "first_string", Second: 1234})
expected := []interface{}([]interface {}{ expected := []interface{}([]interface{}{
"one",1, "one", 1,
"string","str", "string", "str",
"struct",test_struct{ "struct",
First:"first_string", test_struct{
Second:1234, First: "first_string",
Second: 1234,
}, },
}) })
@ -34,16 +32,15 @@ func TestLogParams(t *testing.T) {
} }
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{ expected := LogParams{
"one",1, "one", 1,
"string1","str1", "string1", "str1",
"two",2, "two", 2,
"string2","str2", "string2", "str2",
} }
assert.Equal(t, expected, a.Combine(b)) assert.Equal(t, expected, a.Combine(b))

View file

@ -1,15 +1,14 @@
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{ a := api.ApiArguments{
NumBlocks: 10, NumBlocks: 10,
} }
@ -34,7 +33,6 @@ func ParseArguments(args []string) api.ApiArguments {
} }
func ParseRequest(request string) (api.ApiInterface, error) { func ParseRequest(request string) (api.ApiInterface, error) {
factories := map[string]api.Factory{ factories := map[string]api.Factory{
"v1": api.EosioV1Factory, "v1": api.EosioV1Factory,
"v2": api.EosioV2Factory, "v2": api.EosioV2Factory,

View file

@ -1,15 +1,13 @@
package internal package internal
import ( import (
// "fmt"
"testing" "testing"
"github.com/stretchr/testify/assert" // "fmt"
"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'")
@ -17,7 +15,6 @@ func TestParseWithInvalidApi(t *testing.T) {
} }
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")
@ -28,7 +25,6 @@ func TestParseWithInvalidParams(t *testing.T) {
// -------------------------------- // --------------------------------
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") api, err := ParseRequest("v1|http://api.example.com")
@ -37,7 +33,6 @@ func TestParseEosioV1(t *testing.T) {
} }
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") api, err := ParseRequest("v1|http://api.example.com|2000")
@ -45,9 +40,7 @@ func TestParseEosioV1WithBlockNumber(t *testing.T) {
assert.Equal(t, expected.LogInfo(), api.LogInfo()) 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") api, err := ParseRequest("v1|http://api.example.com|1000|http://host.example.com")
@ -59,7 +52,6 @@ func TestParseEosioV1Full(t *testing.T) {
// -------------------------------- // --------------------------------
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") api, err := ParseRequest("v2|http://api.v2.example.com")
@ -68,7 +60,6 @@ func TestParseEosioV2(t *testing.T) {
} }
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") api, err := ParseRequest("v2|http://api.v2.example.com|1000")
@ -77,7 +68,6 @@ func TestParseEosioV2WithOffset(t *testing.T) {
} }
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") api, err := ParseRequest("v2|http://api.v2.example.com|1000|http://host.example.com")
@ -90,7 +80,6 @@ func TestParseEosioV2Full(t *testing.T) {
// -------------------------------- // --------------------------------
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") api, err := ParseRequest("contract|http://api.contract.example.com")
@ -99,7 +88,6 @@ func TestParseEosioContract(t *testing.T) {
} }
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") api, err := ParseRequest("contract|http://api.contract.example.com|512")
@ -108,7 +96,6 @@ func TestParseEosioContractWithBlockTime(t *testing.T) {
} }
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") api, err := ParseRequest("debug|some_api_call")

View file

@ -2,17 +2,17 @@ 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. // Check api.

View file

@ -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
} }

View file

@ -8,9 +8,9 @@ func TestJsonGetInt64(t *testing.T) {
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) {

View file

@ -1,4 +1,3 @@
package utils package utils
import ( import (
@ -6,15 +5,14 @@ import (
) )
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()
} }
} }

View file

@ -13,11 +13,11 @@ func Test_ParseLogFormatter(t *testing.T) {
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) {

View file

@ -1,4 +1,3 @@
package utils package utils
import ( import (
@ -14,8 +13,7 @@ func (t *Time) SetTime(value time.Time) {
} }
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)

View file

@ -1,14 +1,13 @@
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 that time is NOW (+-10 seconds)
@ -16,7 +15,6 @@ func TestTimeGetTimeWithDefaultValue(t *testing.T) {
} }
func TestTimeGetTimeWithSetTime(t *testing.T) { func TestTimeGetTimeWithSetTime(t *testing.T) {
var ts Time var ts Time
expected := time.Unix(1048722042, 500) expected := time.Unix(1048722042, 500)