Skip to content

Commit

Permalink
improving e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
misteriaud committed Nov 20, 2024
1 parent 7260a25 commit 4fe8be7
Show file tree
Hide file tree
Showing 20 changed files with 15,011 additions and 24 deletions.
42 changes: 30 additions & 12 deletions test/new-e2e/tests/agent-shared-components/gui/gui_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package gui

import (
"embed"
"fmt"
"io"
"net"
Expand All @@ -26,9 +27,11 @@ const (
agentAPIPort = 5001
guiPort = 5002
guiAPIEndpoint = "/agent/gui/intent"
intervalSec = 10
)

//go:embed view
var expectedAssets embed.FS

// assertAgentsUseKey checks that all agents are using the given key.
func getGUIIntentToken(t assert.TestingT, host *components.RemoteHost, authtoken string) string {
if h, ok := t.(testing.TB); ok {
Expand Down Expand Up @@ -91,16 +94,17 @@ func getGUIClient(t assert.TestingT, host *components.RemoteHost, authtoken stri
assert.Equalf(t, http.StatusOK, resp.StatusCode, "unexpected status code for %s", guiURL.String())
defer resp.Body.Close()

cookies := resp.Cookies()
cookies := guiClient.Jar.Cookies(&guiURL)
assert.NotEmpty(t, cookies)
assert.Equal(t, cookies[0].Name, "accessToken", "GUI server didn't the accessToken cookie")

// Assert redirection to "/"
assert.Equal(t, fmt.Sprintf("http://%v", net.JoinHostPort("localhost", strconv.Itoa(guiPort))), resp.Request.URL.String())
assert.Equal(t, fmt.Sprintf("http://%v", net.JoinHostPort("localhost", strconv.Itoa(guiPort)))+"/", resp.Request.URL.String(), "GUI auth endpoint didn't redirect to root endpoint")

return guiClient
}

func checkStaticFiles(t assert.TestingT, client *http.Client) {
func checkStaticFiles(t *testing.T, client *http.Client) {

var links []string
var traverse func(*html.Node)
Expand Down Expand Up @@ -144,19 +148,33 @@ func checkStaticFiles(t assert.TestingT, client *http.Client) {

traverse(doc)
for _, link := range links {
t.Logf("trying to reach asset %v", link)
fullLink := fmt.Sprintf("http://%v/%v", net.JoinHostPort("localhost", strconv.Itoa(guiPort)), link)
resp, err := client.Get(fullLink)
assert.NoErrorf(t, err, "failed to reach GUI at address %s", fullLink)
assert.NoErrorf(t, err, "failed to reach GUI asset at address %s", fullLink)
defer resp.Body.Close()
assert.Equalf(t, http.StatusOK, resp.StatusCode, "unexpected status code for %s", fullLink)

content, err := io.ReadAll(resp.Body)
assert.NoErrorf(t, err, "failed to read content of GUI asset at address %s", fullLink)

expectedContent, err := expectedAssets.ReadFile(link)
assert.NoErrorf(t, err, "unable to retreive file %v in the expected served files", link)

assert.Equalf(t, expectedContent, content, "content of the file %v is not the same as expected", link)
}
}

// func checkGUIFonctionalities() {

// // Use the html package to parse the response body from the request
// doc, err := html.Parse(resp.Body)
// assert.NoErrorf(t, err, "failed to parse HTML response from GUI at address %s", guiURL.String())
func checkPingEndpoint(t *testing.T, client *http.Client) {
guiURL := url.URL{
Scheme: "http",
Host: net.JoinHostPort("localhost", strconv.Itoa(guiPort)),
Path: "/agent/ping",
}

// checkStaticFiles(t, host, doc)
// }
// Make the GET request
resp, err := client.Post(guiURL.String(), "", nil)
assert.NoErrorf(t, err, "failed to reach GUI at address %s", guiURL.String())
assert.Equalf(t, http.StatusOK, resp.StatusCode, "unexpected status code for %s", guiURL.String())
defer resp.Body.Close()
}
13 changes: 6 additions & 7 deletions test/new-e2e/tests/agent-shared-components/gui/gui_nix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,12 @@ type guiLinuxSuite struct {

func TestGUILinuxSuite(t *testing.T) {
t.Parallel()
e2e.Run(t, &guiLinuxSuite{}, e2e.WithProvisioner(awshost.Provisioner()))
e2e.Run(t, &guiLinuxSuite{}, e2e.WithProvisioner(awshost.ProvisionerNoFakeIntake()))
}

func (v *guiLinuxSuite) TestGUI() {
// rootDir := "/tmp/" + v.T().Name()
// v.Env().RemoteHost.MkdirAll(rootDir)

authTokenFilePath := "/etc/datadog-agent/auth_token"

v.T().Log("Setting up the secret resolver and the initial api key file")

config := fmt.Sprintf(`cmd_port: %d
GUI_port: %d`, agentAPIPort, guiPort)
// start the agent with that configuration
Expand All @@ -57,13 +52,17 @@ GUI_port: %d`, agentAPIPort, guiPort)
authtokenContent := v.Env().RemoteHost.MustExecute("sudo cat " + authTokenFilePath)
authtoken := strings.TrimSpace(authtokenContent)

v.T().Log("Testing GUI Server")
v.T().Log("Testing GUI authentication flow")

var guiClient *http.Client
// and check that the agents are using the new key
require.EventuallyWithT(v.T(), func(t *assert.CollectT) {
guiClient = getGUIClient(t, v.Env().RemoteHost, authtoken)
}, 1*time.Minute, 10*time.Second)

v.T().Log("Testing GUI static file server")
checkStaticFiles(v.T(), guiClient)

v.T().Log("Testing GUI ping endpoint")
checkPingEndpoint(v.T(), guiClient)
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,13 @@ type guiWindowsSuite struct {

func TestGUIWindowsSuite(t *testing.T) {
t.Parallel()
e2e.Run(t, &guiWindowsSuite{}, e2e.WithProvisioner(awshost.Provisioner(awshost.WithEC2InstanceOptions(ec2.WithOS(os.WindowsDefault)))))
e2e.Run(t, &guiWindowsSuite{}, e2e.WithProvisioner(awshost.ProvisionerNoFakeIntake(awshost.WithEC2InstanceOptions(ec2.WithOS(os.WindowsDefault)))))
}

func (v *guiWindowsSuite) TestGUI() {
// rootDir := "/tmp/" + v.T().Name()
// v.Env().RemoteHost.MkdirAll(rootDir)

authTokenFilePath := `C:\ProgramData\Datadog\auth_token`

v.T().Log("Setting up the secret resolver and the initial api key file")

config := fmt.Sprintf(`cmd_port: %d
GUI_port: %d`, agentAPIPort, guiPort)
// start the agent with that configuration
Expand Down
Loading

0 comments on commit 4fe8be7

Please sign in to comment.