Skip to content

Commit

Permalink
test(internal): metrics tests 100% coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
PhearZero committed Nov 21, 2024
1 parent af577a7 commit ab6e066
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 33 deletions.
23 changes: 11 additions & 12 deletions internal/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,20 @@ package internal

import (
"context"
"github.com/algorandfoundation/hack-tui/internal/test"
"strconv"
"testing"

"github.com/algorandfoundation/hack-tui/api"
"github.com/oapi-codegen/oapi-codegen/v2/pkg/securityprovider"
)

func Test_GetMetrics(t *testing.T) {
// Setup elevated client
apiToken, err := securityprovider.NewSecurityProviderApiKey("header", "X-Algo-API-Token", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
if err != nil {
t.Fatal(err)
}
client, err := api.NewClientWithResponses("http://localhost:8080", api.WithRequestEditorFn(apiToken.Intercept))
client := test.GetClient(true)

metrics, err := GetMetrics(context.Background(), client)
if err != nil {
t.Fatal(err)
if err == nil {
t.Error("error expected")
}

// TODO: ensure localnet is running before tests
client = test.GetClient(false)
metrics, err = GetMetrics(context.Background(), client)
if err != nil {
t.Fatal(err)
Expand All @@ -31,6 +24,12 @@ func Test_GetMetrics(t *testing.T) {
if metrics["algod_agreement_dropped"] != 0 {
t.Fatal(strconv.Itoa(metrics["algod_agreement_dropped"]) + " is not zero")
}

client = test.NewClient(false, true)
metrics, err = GetMetrics(context.Background(), client)
if err == nil {
t.Error("expected error")
}
}

func Test_parseMetrics(t *testing.T) {
Expand Down
109 changes: 88 additions & 21 deletions internal/test/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,119 @@ import (
)

func GetClient(throws bool) api.ClientWithResponsesInterface {
return NewClient(throws)
return NewClient(throws, false)
}

type Client struct {
api.ClientWithResponsesInterface
Errors bool
Errors bool
Invalid bool
}

func NewClient(throws bool) api.ClientWithResponsesInterface {
func NewClient(throws bool, invalid bool) api.ClientWithResponsesInterface {
client := new(Client)
if throws {
client.Errors = true
}
if invalid {
client.Invalid = true
}
return client
}

func (c *Client) MetricsWithResponse(ctx context.Context, reqEditors ...api.RequestEditorFn) (*api.MetricsResponse, error) {
var res api.MetricsResponse
body := `# HELP algod_telemetry_drops_total telemetry messages dropped due to full queues
# TYPE algod_telemetry_drops_total counter
algod_telemetry_drops_total 0
# HELP algod_telemetry_errs_total telemetry messages dropped due to server error
# TYPE algod_telemetry_errs_total counter
algod_telemetry_errs_total 0
# HELP algod_ram_usage number of bytes runtime.ReadMemStats().HeapInuse
# TYPE algod_ram_usage gauge
algod_ram_usage 0
# HELP algod_crypto_vrf_generate_total Total number of calls to GenerateVRFSecrets
# TYPE algod_crypto_vrf_generate_total counter
algod_crypto_vrf_generate_total 0
# HELP algod_crypto_vrf_prove_total Total number of calls to VRFSecrets.Prove
# TYPE algod_crypto_vrf_prove_total counter
algod_crypto_vrf_prove_total 0
# HELP algod_crypto_vrf_hash_total Total number of calls to VRFProof.Hash
# TYPE algod_crypto_vrf_hash_total counter
algod_crypto_vrf_hash_total 0`
if !c.Invalid {
httpResponse := http.Response{StatusCode: 200}
res = api.MetricsResponse{
Body: []byte(body),
HTTPResponse: &httpResponse,
}
} else {
httpResponse := http.Response{StatusCode: 404}
res = api.MetricsResponse{
Body: []byte(body),
HTTPResponse: &httpResponse,
}
}
if c.Errors {
return &res, errors.New("test error")
}
return &res, nil
}
func (c *Client) GetParticipationKeysWithResponse(ctx context.Context, reqEditors ...api.RequestEditorFn) (*api.GetParticipationKeysResponse, error) {
httpResponse := http.Response{StatusCode: 200}
var res api.GetParticipationKeysResponse
clone := mock.Keys
res := api.GetParticipationKeysResponse{
Body: nil,
HTTPResponse: &httpResponse,
JSON200: &clone,
JSON400: nil,
JSON401: nil,
JSON404: nil,
JSON500: nil,
if !c.Invalid {
httpResponse := http.Response{StatusCode: 200}
res = api.GetParticipationKeysResponse{
Body: nil,
HTTPResponse: &httpResponse,
JSON200: &clone,
JSON400: nil,
JSON401: nil,
JSON404: nil,
JSON500: nil,
}
} else {
httpResponse := http.Response{StatusCode: 404}
res = api.GetParticipationKeysResponse{
Body: nil,
HTTPResponse: &httpResponse,
JSON200: &clone,
JSON400: nil,
JSON401: nil,
JSON404: nil,
JSON500: nil,
}
}

if c.Errors {
return nil, errors.New("test error")
}
return &res, nil
}

func (c *Client) DeleteParticipationKeyByIDWithResponse(ctx context.Context, participationId string, reqEditors ...api.RequestEditorFn) (*api.DeleteParticipationKeyByIDResponse, error) {
httpResponse := http.Response{StatusCode: 200}
res := api.DeleteParticipationKeyByIDResponse{
Body: nil,
HTTPResponse: &httpResponse,
JSON400: nil,
JSON401: nil,
JSON404: nil,
JSON500: nil,
var res api.DeleteParticipationKeyByIDResponse
if !c.Invalid {
httpResponse := http.Response{StatusCode: 200}
res = api.DeleteParticipationKeyByIDResponse{
Body: nil,
HTTPResponse: &httpResponse,
JSON400: nil,
JSON401: nil,
JSON404: nil,
JSON500: nil,
}
} else {
httpResponse := http.Response{StatusCode: 404}
res = api.DeleteParticipationKeyByIDResponse{
Body: nil,
HTTPResponse: &httpResponse,
}
}

if c.Errors {
return nil, errors.New("test error")
return &res, errors.New("test error")
}
return &res, nil
}
Expand Down

0 comments on commit ab6e066

Please sign in to comment.