consul/agent/hcp/client/http_client_test.go

32 lines
776 B
Go
Raw Normal View History

[CC-7042] Update and enable the HCP metrics sink in the HCP manager (#20072) * Option to set HCP client at runtime Allows us to initially set a nil HCP client for the telemetry provider and update it later. * Set telemetry provider HCP client in HCP manager Set the telemetry provider as a dependency and pass it to the manager. Update the telemetry provider's HCP client when the HCP manager starts. * Add a provider interface for the metrics client This provider will allow us to configure and reconfigure the retryable HTTP client and the headers for the metrics client. * Move HTTP retryable client to separate file Copied directly from the metrics client. * Abstract HCP specific values in HTTP client Remove HCP specific references and instead initiate with a generic TLS configuration and authentication source. * Set up HTTP client and headers in the provider Move setup from the metrics client to the HCP telemetry provider. * Update the telemetry provider in the HCP manager Initialize the provider without the HCP configs and then update it in the HCP manager to enable it. * Improve test assertion, fix method comment * Move client provider to metrics client * Stop the manager on setup error * Add separate lock for http configuration * Start telemetry provider in HCP manager * Update HCP client and config as part of Run * Remove option to set config at initialization * Simplify and clean up setting HCP configs * Add test for telemetry provider Run method * Fix race condition * Use clone of HTTP headers * Only allow initial update and run once
2024-01-16 16:46:12 +00:00
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package client
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/hashicorp/consul/agent/hcp/config"
"github.com/hashicorp/go-hclog"
"github.com/stretchr/testify/require"
)
func TestNewHTTPClient(t *testing.T) {
mockCfg := config.MockCloudCfg{}
mockHCPCfg, err := mockCfg.HCPConfig()
require.NoError(t, err)
client := NewHTTPClient(mockHCPCfg.APITLSConfig(), mockHCPCfg, hclog.NewNullLogger())
require.NotNil(t, client)
var req *http.Request
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
req = r
}))
_, err = client.Get(srv.URL)
require.NoError(t, err)
require.Equal(t, "Bearer test-token", req.Header.Get("Authorization"))
}