Skip to content

Commit

Permalink
[e2e] move CreateTestSessionOutputDir to common package
Browse files Browse the repository at this point in the history
  • Loading branch information
pducolin committed Dec 27, 2024
1 parent 796dbb9 commit bb552f8
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 51 deletions.
2 changes: 1 addition & 1 deletion test/new-e2e/pkg/e2e/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
49 changes: 0 additions & 49 deletions test/new-e2e/pkg/e2e/suite_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
package e2e

import (
"fmt"
"os"
"path/filepath"
"strings"
"time"

"testing"
)
Expand All @@ -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.
//
Expand Down
58 changes: 58 additions & 0 deletions test/new-e2e/pkg/utils/common/output_dirs.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion test/new-e2e/pkg/utils/e2e/client/agent_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit bb552f8

Please sign in to comment.