From 59399c7cf42f3c10bf32d9436085ed3fed07b015 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Tue, 7 Feb 2023 08:38:21 +0100 Subject: [PATCH] internal/api/atomicasset.go: Support host parameter. --- internal/api/atomicasset.go | 7 +++++-- internal/api/atomicasset_test.go | 22 +++++++++++----------- internal/server/parse_request_test.go | 4 ++-- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/internal/api/atomicasset.go b/internal/api/atomicasset.go index b3dc7e2..19dee7b 100644 --- a/internal/api/atomicasset.go +++ b/internal/api/atomicasset.go @@ -13,16 +13,18 @@ type AtomicAsset struct { utils.Time url string + host string block_time float64 } func AtomicAssetFactory(args ApiArguments) ApiInterface { - return NewAtomicAsset(args.Url, float64(args.NumBlocks/2)) + return NewAtomicAsset(args.Url, args.Host, float64(args.NumBlocks/2)) } -func NewAtomicAsset(url string, block_time float64) AtomicAsset { +func NewAtomicAsset(url string, host string, block_time float64) AtomicAsset { return AtomicAsset{ url: url, + host: host, block_time: block_time, } } @@ -37,6 +39,7 @@ func (e AtomicAsset) LogInfo() LogParams { func (e AtomicAsset) Call(ctx context.Context) (agentcheck.Response, string) { client := atomicasset.NewWithContext(e.url, ctx) + client.Host = e.host h, err := client.GetHealth() if err != nil { diff --git a/internal/api/atomicasset_test.go b/internal/api/atomicasset_test.go index 84d88db..f4dc71d 100644 --- a/internal/api/atomicasset_test.go +++ b/internal/api/atomicasset_test.go @@ -17,7 +17,7 @@ func TestAtomicAsset_Factory(t *testing.T) { NumBlocks: 120, }) - expected := NewAtomicAsset("https://atomic.example.com", 60) + expected := NewAtomicAsset("https://atomic.example.com", "", 60) assert.IsType(t, expected, api) assert.Equal(t, expected.url, api.(AtomicAsset).url) @@ -25,7 +25,7 @@ func TestAtomicAsset_Factory(t *testing.T) { } func TestAtomicAsset_LogInfo(t *testing.T) { - api := NewAtomicAsset("https://atomic.example.com", 120) + api := NewAtomicAsset("https://atomic.example.com", "", 120) expected := LogParams{"type", "atomicasset", "url", "https://atomic.example.com", "block_time", float64(120)} @@ -35,7 +35,7 @@ func TestAtomicAsset_LogInfo(t *testing.T) { func TestAtomicAsset_SetTime(t *testing.T) { expected := time.Date(2019, 3, 18, 20, 29, 32, 0, time.UTC) - api := NewAtomicAsset("", 60) + api := NewAtomicAsset("", "", 60) // Assert that time is NOW (+-10 seconds) assert.InDelta(t, api.GetTime().Unix(), time.Now().In(time.UTC).Unix(), float64(10)) @@ -49,7 +49,7 @@ func TestAtomicAsset_JsonFailure(t *testing.T) { assert.NoError(t, err) })) - api := NewAtomicAsset(srv.URL, 120) + api := NewAtomicAsset(srv.URL, "", 120) check, _ := api.Call(context.Background()) expected := agentcheck.NewStatusMessageResponse(agentcheck.Fail, "") @@ -64,7 +64,7 @@ func TestAtomicAsset_HTTP500Down(t *testing.T) { assert.NoError(t, err) })) - api := NewAtomicAsset(srv.URL, 120) + api := NewAtomicAsset(srv.URL, "", 120) check, status := api.Call(context.Background()) assert.Equal(t, "Taking offline because 500 was received from backend", status) @@ -101,7 +101,7 @@ func TestAtomicAsset_LaggingUp(t *testing.T) { } })) - api := NewAtomicAsset(srv.URL, 120) + api := NewAtomicAsset(srv.URL, "", 120) api.SetTime(time.Date(2025, 10, 8, 20, 7, 27, 0, time.UTC)) check, status := api.Call(context.Background()) @@ -140,7 +140,7 @@ func TestAtomicAsset_LaggingDown(t *testing.T) { } })) - api := NewAtomicAsset(srv.URL, 120) + api := NewAtomicAsset(srv.URL, "", 120) api.SetTime(time.Date(2018, 8, 5, 6, 53, 35, 0, time.UTC)) check, status := api.Call(context.Background()) @@ -179,7 +179,7 @@ func TestAtomicAsset_InFutureUp(t *testing.T) { } })) - api := NewAtomicAsset(srv.URL, 120) + api := NewAtomicAsset(srv.URL, "", 120) api.SetTime(time.Date(2024, 10, 15, 1, 9, 16, 500, time.UTC)) check, status := api.Call(context.Background()) @@ -218,7 +218,7 @@ func TestAtomicAsset_InFutureDown(t *testing.T) { } })) - api := NewAtomicAsset(srv.URL, 120) + api := NewAtomicAsset(srv.URL, "", 120) api.SetTime(time.Date(2002, 12, 29, 0, 45, 0o3, 500, time.UTC)) check, status := api.Call(context.Background()) @@ -257,7 +257,7 @@ func TestAtomicAsset_RedisDown(t *testing.T) { } })) - api := NewAtomicAsset(srv.URL, 120) + api := NewAtomicAsset(srv.URL, "", 120) api.SetTime(time.Date(2015, 3, 11, 11, 19, 30, 500, time.UTC)) check, status := api.Call(context.Background()) @@ -296,7 +296,7 @@ func TestAtomicAsset_PostgresDown(t *testing.T) { } })) - api := NewAtomicAsset(srv.URL, 120) + api := NewAtomicAsset(srv.URL, "", 120) api.SetTime(time.Date(2019, 7, 11, 18, 6, 11, 500, time.UTC)) check, status := api.Call(context.Background()) diff --git a/internal/server/parse_request_test.go b/internal/server/parse_request_test.go index d0ce82d..51c131b 100644 --- a/internal/server/parse_request_test.go +++ b/internal/server/parse_request_test.go @@ -80,7 +80,7 @@ func TestParseRequest_AntelopeV2Full(t *testing.T) { // -------------------------------- func TestParseRequest_AtomicAsset(t *testing.T) { - expected := api.NewAtomicAsset("http://api.atomicassets.io", 5) + expected := api.NewAtomicAsset("http://api.atomicassets.io", "", 5) api, err := ParseRequest("atomic|http://api.atomicassets.io") assert.NoError(t, err) @@ -88,7 +88,7 @@ func TestParseRequest_AtomicAsset(t *testing.T) { } func TestParseRequest_AtomicAssetWithBlockTime(t *testing.T) { - expected := api.NewAtomicAsset("http://api.atomicassets.io", 256) + expected := api.NewAtomicAsset("http://api.atomicassets.io", "", 256) api, err := ParseRequest("atomic|http://api.atomicassets.io|512") assert.NoError(t, err)