@@ -46,6 +46,11 @@ const (
4646 // If set to any non-empty value, diagnostics are printed to stdout.
4747 // If not set or empty, diagnostics are written to a timestamped file.
4848 DiagnosticsToStdoutEnvVar = "GROVE_E2E_DIAG_TO_STDOUT"
49+
50+ // DiagnosticsDirEnvVar is the environment variable that specifies the directory
51+ // where diagnostics files should be written. If not set, files are written to
52+ // the current directory. This is ignored if DiagnosticsToStdoutEnvVar is set.
53+ DiagnosticsDirEnvVar = "GROVE_E2E_DIAG_DIR"
4954)
5055
5156// isPodReady checks if a pod is ready
@@ -74,8 +79,10 @@ var groveResourceTypes = []groveResourceType{
7479}
7580
7681// createDiagnosticsWriter creates an io.Writer for diagnostics output.
77- // If GROVE_E2E_DIAG_TO_STDOUT is set, returns os.Stdout.
78- // Otherwise, creates a timestamped file using the test name in the current directory.
82+ // If DiagnosticsToStdoutEnvVar env is set, returns os.Stdout.
83+ // Otherwise, creates a timestamped file using the test name.
84+ // If DiagnosticsToStdoutEnvVar env var is set, the file is created in that directory.
85+ // Otherwise, the file is created in the current directory (with fallback to temp dir).
7986// The caller is responsible for closing the returned io.Closer (may be nil for stdout).
8087func createDiagnosticsWriter (testName string ) (io.Writer , io.Closer , string , error ) {
8188 if os .Getenv (DiagnosticsToStdoutEnvVar ) != "" {
@@ -87,28 +94,42 @@ func createDiagnosticsWriter(testName string) (io.Writer, io.Closer, string, err
8794
8895 // Create a timestamped file with test name
8996 timestamp := time .Now ().Format ("2006-01-02_15-04-05" )
90- filename := fmt .Sprintf ("%s_%s.log" , sanitizedName , timestamp )
97+ baseFilename := fmt .Sprintf ("%s_%s.log" , sanitizedName , timestamp )
98+
99+ // Determine the directory for the diagnostics file
100+ diagDir := os .Getenv (DiagnosticsDirEnvVar )
101+ if diagDir != "" {
102+ // Use the specified directory
103+ filename := filepath .Join (diagDir , baseFilename )
104+ file , err := os .Create (filename )
105+ if err != nil {
106+ return nil , nil , "" , fmt .Errorf ("failed to create diagnostics file in %s: %w" , diagDir , err )
107+ }
108+ return file , file , filename , nil
109+ }
91110
92111 // Try to create the file in the current directory
93- file , err := os .Create (filename )
112+ file , err := os .Create (baseFilename )
94113 if err != nil {
95114 // Fall back to a temp directory if we can't write to current dir
96- filename = filepath .Join (os .TempDir (), filename )
115+ filename : = filepath .Join (os .TempDir (), baseFilename )
97116 file , err = os .Create (filename )
98117 if err != nil {
99118 return nil , nil , "" , fmt .Errorf ("failed to create diagnostics file: %w" , err )
100119 }
120+ return file , file , filename , nil
101121 }
102122
103- return file , file , filename , nil
123+ return file , file , baseFilename , nil
104124}
105125
106126// CollectAllDiagnostics collects and prints all diagnostic information at INFO level.
107127// This should be called when a test fails, before cleanup runs.
108128// All output is at INFO level to ensure visibility regardless of log level settings.
109129//
110130// Diagnostics are written to a timestamped file using the test name (e.g., TestRollingUpdate_2025-01-22_15-04-05.log).
111- // Set the DiagnosticsToStdoutEnvVar environment variable to output to stdout instead.
131+ // Set DiagnosticsDirEnvVar env var to specify the output directory for diagnostics files.
132+ // Set DiagnosticsToStdoutEnvVar env var to output to stdout instead of a file.
112133func CollectAllDiagnostics (tc TestContext ) {
113134 // Get test name for the diagnostics file
114135 testName := "unknown_test"
0 commit comments