Skip to content

Commit

Permalink
Fix apiserver test
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-taf committed Dec 13, 2024
1 parent 81e9990 commit 22ad21e
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions comp/process/apiserver/apiserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
)

Expand All @@ -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)
Expand All @@ -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)
}

0 comments on commit 22ad21e

Please sign in to comment.