From 22ad21e1fa7e2ab11cd860eb3c4399a58b7fe983 Mon Sep 17 00:00:00 2001 From: Daniel Tafoya Date: Fri, 13 Dec 2024 02:20:47 -0500 Subject: [PATCH] Fix apiserver test --- comp/process/apiserver/apiserver_test.go | 28 +++++++++++++++--------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/comp/process/apiserver/apiserver_test.go b/comp/process/apiserver/apiserver_test.go index a44dfbebd2bcf1..da5040b2653149 100644 --- a/comp/process/apiserver/apiserver_test.go +++ b/comp/process/apiserver/apiserver_test.go @@ -7,11 +7,11 @@ package apiserver import ( "net/http" - "net/http/httptest" "testing" "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "go.uber.org/fx" "github.com/DataDog/datadog-agent/comp/core" @@ -23,6 +23,7 @@ import ( workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def" workloadmetafx "github.com/DataDog/datadog-agent/comp/core/workloadmeta/fx" "github.com/DataDog/datadog-agent/pkg/api/util" + pkgconfigsetup "github.com/DataDog/datadog-agent/pkg/config/setup" "github.com/DataDog/datadog-agent/pkg/util/fxutil" ) @@ -44,10 +45,13 @@ func TestLifecycle(t *testing.T) { )) assert.EventuallyWithT(t, func(c *assert.CollectT) { - req := httptest.NewRequest("GET", "http://localhost:6162/config", nil) + // With authentication + req, err := http.NewRequest("GET", "http://localhost:6162/config", nil) + require.NoError(c, err) + util.SetAuthToken(pkgconfigsetup.Datadog()) req.Header.Set("Authorization", "Bearer "+util.GetAuthToken()) - res, err := http.DefaultClient.Do(req) - assert.NoError(c, err) + res, err := util.GetClient(false).Do(req) + require.NoError(c, err) defer res.Body.Close() assert.Equal(c, http.StatusOK, res.StatusCode) }, 5*time.Second, time.Second) @@ -71,17 +75,21 @@ func TestAuthentication(t *testing.T) { )) assert.EventuallyWithT(t, func(c *assert.CollectT) { - req := httptest.NewRequest("GET", "http://localhost:6162/config", nil) - res, err := http.DefaultClient.Do(req) - assert.NoError(c, err) + // No authentication + req, err := http.NewRequest("GET", "http://localhost:6162/config", nil) + require.NoError(c, err) + res, err := util.GetClient(false).Do(req) + require.NoError(c, err) defer res.Body.Close() assert.Equal(c, http.StatusUnauthorized, res.StatusCode) - req = httptest.NewRequest("GET", "http://localhost:6162/config", nil) + // With authentication + util.SetAuthToken(pkgconfigsetup.Datadog()) req.Header.Set("Authorization", "Bearer "+util.GetAuthToken()) - res, err = http.DefaultClient.Do(req) - assert.NoError(c, err) + res, err = util.GetClient(false).Do(req) + require.NoError(c, err) defer res.Body.Close() assert.Equal(c, http.StatusOK, res.StatusCode) + }, 5*time.Second, time.Second) }