Skip to content

Commit

Permalink
Remove one abstraction to use the config from deps
Browse files Browse the repository at this point in the history
  • Loading branch information
Achooo committed Apr 26, 2023
1 parent 25f588b commit 2580ad7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
10 changes: 1 addition & 9 deletions agent/hcp/client/metrics_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package client
import (
"bytes"
"context"
"crypto/tls"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -37,15 +36,9 @@ type MetricsClient interface {
ExportMetrics(ctx context.Context, protoMetrics *metricpb.ResourceMetrics, endpoint string) error
}

// hcpConfig represents HCP config for TLS abstracted in an interface for easy testing.
type hcpConfig interface {
oauth2.TokenSource
APITLSConfig() *tls.Config
}

// cloudConfig represents cloud config for TLS abstracted in an interface for easy testing.
type cloudConfig interface {
HCPConfig(opts ...hcpcfg.HCPConfigOption) (hcpConfig, error)
HCPConfig(opts ...hcpcfg.HCPConfigOption) (hcpcfg.HCPConfig, error)
}

// otlpClient is an implementation of MetricsClient with a retryable http client for retries and to honor throttle.
Expand Down Expand Up @@ -143,7 +136,6 @@ func (o *otlpClient) ExportMetrics(ctx context.Context, protoMetrics *metricpb.R
if err != nil {
return fmt.Errorf("failed to export metrics: %v", err)
}
defer resp.Body.Close()

var respData bytes.Buffer
if _, err := io.Copy(&respData, resp.Body); err != nil {
Expand Down
19 changes: 13 additions & 6 deletions agent/hcp/client/metrics_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"net/http"
"net/http/httptest"
"net/url"
"testing"

"golang.org/x/oauth2"
Expand All @@ -24,25 +25,31 @@ import (

type mockHCPCfg struct{}

func (m *mockHCPCfg) APITLSConfig() *tls.Config {
return nil
}

func (m *mockHCPCfg) Token() (*oauth2.Token, error) {
return &oauth2.Token{
AccessToken: "test-token",
}, nil
}

func (m *mockHCPCfg) APITLSConfig() *tls.Config { return nil }

func (m *mockHCPCfg) SCADAAddress() string { return "" }

func (m *mockHCPCfg) SCADATLSConfig() *tls.Config { return &tls.Config{} }

func (m *mockHCPCfg) APIAddress() string { return "" }

func (m *mockHCPCfg) PortalURL() *url.URL { return &url.URL{} }

type mockCloudCfg struct{}

func (m mockCloudCfg) HCPConfig(opts ...hcpcfg.HCPConfigOption) (hcpConfig, error) {
func (m mockCloudCfg) HCPConfig(opts ...hcpcfg.HCPConfigOption) (hcpcfg.HCPConfig, error) {
return &mockHCPCfg{}, nil
}

type mockErrCloudCfg struct{}

func (m mockErrCloudCfg) HCPConfig(opts ...hcpcfg.HCPConfigOption) (hcpConfig, error) {
func (m mockErrCloudCfg) HCPConfig(opts ...hcpcfg.HCPConfigOption) (hcpcfg.HCPConfig, error) {
return nil, errors.New("test bad HCP config")
}

Expand Down

0 comments on commit 2580ad7

Please sign in to comment.