diff --git a/test/new-e2e/pkg/e2e/suite.go b/test/new-e2e/pkg/e2e/suite.go index d6612108916b3..859c89f1917e0 100644 --- a/test/new-e2e/pkg/e2e/suite.go +++ b/test/new-e2e/pkg/e2e/suite.go @@ -619,7 +619,7 @@ func (bs *BaseSuite[Env]) GetRootOutputDir() (string, error) { return } // Store the timestamped directory to be used by all tests in the suite - bs.testSessionOutputDir, err = CreateTestSessionOutputDir(outputRoot) + bs.testSessionOutputDir, err = common.CreateTestSessionOutputDir(outputRoot) }) return bs.testSessionOutputDir, err } diff --git a/test/new-e2e/pkg/e2e/suite_utils.go b/test/new-e2e/pkg/e2e/suite_utils.go index a12f9564fb64d..5aeca99fefd9a 100644 --- a/test/new-e2e/pkg/e2e/suite_utils.go +++ b/test/new-e2e/pkg/e2e/suite_utils.go @@ -6,11 +6,9 @@ package e2e import ( - "fmt" "os" "path/filepath" "strings" - "time" "testing" ) @@ -29,53 +27,6 @@ func (tl testLogger) Write(p []byte) (n int, err error) { return len(p), nil } -// CreateTestSessionOutputDir creates and returns a directory for tests to store output files and artifacts. -// A timestamp is included in the path to distinguish between multiple runs, and os.MkdirTemp() is -// used to avoid name collisions between parallel runs. -// -// A new directory is created on each call to this function, it is recommended to save this result -// and use it for all tests in a run. For example see BaseSuite.GetRootOutputDir(). -// -// See runner.GetProfile().GetOutputDir() for the root output directory selection logic. -// -// See CreateTestOutputDir and BaseSuite.CreateTestOutputDir for a function that returns a subdirectory for a specific test. -func CreateTestSessionOutputDir(outputRoot string) (string, error) { - // Append timestamp to distinguish between multiple runs - // Format: YYYY-MM-DD_HH-MM-SS - // Use a custom timestamp format because Windows paths can't contain ':' characters - // and we don't need the timezone information. - timePart := time.Now().Format("2006-01-02_15-04-05") - // create root directory - err := os.MkdirAll(outputRoot, 0755) - if err != nil { - return "", err - } - // Create final output directory - // Use MkdirTemp to avoid name collisions between parallel runs - outputRoot, err = os.MkdirTemp(outputRoot, fmt.Sprintf("%s_*", timePart)) - if err != nil { - return "", err - } - if os.Getenv("CI") == "" { - // Create a symlink to the latest run for user convenience - // TODO: Is there a standard "ci" vs "local" check? - // This code used to be in localProfile.GetOutputDir() - latestLink := filepath.Join(filepath.Dir(outputRoot), "latest") - // Remove the symlink if it already exists - if _, err := os.Lstat(latestLink); err == nil { - err = os.Remove(latestLink) - if err != nil { - return "", err - } - } - err = os.Symlink(outputRoot, latestLink) - if err != nil { - return "", err - } - } - return outputRoot, nil -} - // CreateTestOutputDir creates a directory for a specific test that can be used to store output files and artifacts. // The test name is used in the directory name, and invalid characters are replaced with underscores. // diff --git a/test/new-e2e/pkg/utils/common/output_dirs.go b/test/new-e2e/pkg/utils/common/output_dirs.go new file mode 100644 index 0000000000000..f50dffa04f9e1 --- /dev/null +++ b/test/new-e2e/pkg/utils/common/output_dirs.go @@ -0,0 +1,58 @@ +// Unless explicitly stated otherwise all files in this repository are licensed +// under the Apache License Version 2.0. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright 2024-present Datadog, Inc. + +package common + +import ( + "fmt" + "os" + "path/filepath" + "time" +) + +// CreateTestSessionOutputDir creates and returns a directory for tests to store output files and artifacts. +// A timestamp is included in the path to distinguish between multiple runs, and os.MkdirTemp() is +// used to avoid name collisions between parallel runs. +// +// A new directory is created on each call to this function, it is recommended to save this result +// and use it for all tests in a run. For example see BaseSuite.GetRootOutputDir(). +// +// See CreateTestOutputDir and BaseSuite.CreateTestOutputDir for a function that returns a subdirectory for a specific test. +func CreateTestSessionOutputDir(outputRoot string) (string, error) { + // Append timestamp to distinguish between multiple runs + // Format: YYYY-MM-DD_HH-MM-SS + // Use a custom timestamp format because Windows paths can't contain ':' characters + // and we don't need the timezone information. + timePart := time.Now().Format("2006-01-02_15-04-05") + // create root directory + err := os.MkdirAll(outputRoot, 0755) + if err != nil { + return "", err + } + // Create final output directory + // Use MkdirTemp to avoid name collisions between parallel runs + outputRoot, err = os.MkdirTemp(outputRoot, fmt.Sprintf("%s_*", timePart)) + if err != nil { + return "", err + } + if os.Getenv("CI") == "" { + // Create a symlink to the latest run for user convenience + // TODO: Is there a standard "ci" vs "local" check? + // This code used to be in localProfile.GetOutputDir() + latestLink := filepath.Join(filepath.Dir(outputRoot), "latest") + // Remove the symlink if it already exists + if _, err := os.Lstat(latestLink); err == nil { + err = os.Remove(latestLink) + if err != nil { + return "", err + } + } + err = os.Symlink(outputRoot, latestLink) + if err != nil { + return "", err + } + } + return outputRoot, nil +} diff --git a/test/new-e2e/pkg/utils/e2e/client/agent_client.go b/test/new-e2e/pkg/utils/e2e/client/agent_client.go index 641a2aef6a6b9..86c9f4ed21477 100644 --- a/test/new-e2e/pkg/utils/e2e/client/agent_client.go +++ b/test/new-e2e/pkg/utils/e2e/client/agent_client.go @@ -202,7 +202,7 @@ func generateAndDownloadFlare(t *testing.T, commandRunner *agentCommandRunner, h if err != nil { return fmt.Errorf("could not get root output directory: %w", err) } - root, err := e2e.CreateTestSessionOutputDir(outputRoot) + root, err := common.CreateTestSessionOutputDir(outputRoot) if err != nil { return fmt.Errorf("could not get test session output directory: %w", err) }