Skip to content

Commit

Permalink
Remove mock for MetricsClient
Browse files Browse the repository at this point in the history
  • Loading branch information
Achooo committed Jul 31, 2023
1 parent 932633e commit 696966f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
39 changes: 36 additions & 3 deletions agent/hcp/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package client
import (
"context"
"fmt"
"net/url"
"regexp"
"testing"
"time"

"github.com/go-openapi/runtime"
hcptelemetry "github.com/hashicorp/hcp-sdk-go/clients/cloud-consul-telemetry-gateway/preview/2023-04-14/client/consul_telemetry_service"
Expand All @@ -27,12 +30,20 @@ func (m *mockTGW) QueryRangeBatch(params *hcptelemetry.QueryRangeBatchParams, au
}
func (m *mockTGW) SetTransport(transport runtime.ClientTransport) {}

type expectedTelemetryCfg struct {
endpoint string
labels map[string]string
filters string
refreshInterval time.Duration
}

func TestFetchTelemetryConfig(t *testing.T) {
t.Parallel()
for name, tc := range map[string]struct {
mockResponse *hcptelemetry.AgentTelemetryConfigOK
mockError error
wantErr string
expected *expectedTelemetryCfg
}{
"errorsWithFetchFailure": {
mockError: fmt.Errorf("failed to fetch from HCP"),
Expand All @@ -56,12 +67,17 @@ func TestFetchTelemetryConfig(t *testing.T) {
Endpoint: "https://test.com",
Labels: map[string]string{"test": "123"},
Metrics: &models.HashicorpCloudConsulTelemetry20230414TelemetryMetricsConfig{
IncludeList: []string{"consul"},
IncludeList: []string{"consul", "test"},
},
},
},
},
mockError: nil,
expected: &expectedTelemetryCfg{
endpoint: "https://test.com/v1/metrics",
labels: map[string]string{"test": "123"},
filters: "consul|test",
refreshInterval: 1 * time.Second,
},
},
} {
tc := tc
Expand All @@ -83,8 +99,25 @@ func TestFetchTelemetryConfig(t *testing.T) {
return
}

urlEndpoint, err := url.Parse(tc.expected.endpoint)
require.NoError(t, err)

regexFilters, err := regexp.Compile(tc.expected.filters)
require.NoError(t, err)

expectedCfg := &TelemetryConfig{
MetricsConfig: &MetricsConfig{
Endpoint: urlEndpoint,
Filters: regexFilters,
Labels: tc.expected.labels,
},
RefreshConfig: &RefreshConfig{
RefreshInterval: tc.expected.refreshInterval,
},
}

require.NoError(t, err)
require.NotNil(t, telemetryCfg)
require.Equal(t, expectedCfg, telemetryCfg)
})
}
}
7 changes: 0 additions & 7 deletions agent/hcp/client/mock_metrics_client.go

This file was deleted.

7 changes: 6 additions & 1 deletion agent/hcp/deps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ import (
"github.com/stretchr/testify/require"

"github.com/hashicorp/consul/agent/hcp/client"
"github.com/hashicorp/consul/agent/hcp/telemetry"
)

type mockMetricsClient struct {
telemetry.MetricsClient
}

func TestSink(t *testing.T) {
t.Parallel()
for name, test := range map[string]struct {
Expand Down Expand Up @@ -56,7 +61,7 @@ func TestSink(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()
c := client.NewMockClient(t)
mc := client.MockMetricsClient{}
mc := mockMetricsClient{}

test.expect(c)
ctx := context.Background()
Expand Down

0 comments on commit 696966f

Please sign in to comment.