@@ -62,6 +62,11 @@ type Fixture struct {
62
62
63
63
// Uninstall token value that is needed for the agent uninstall if it's tamper protected
64
64
uninstallToken string
65
+
66
+ // fileNamePrefix is a prefix to be used when saving files from this test.
67
+ // it's set by FileNamePrefix and once it's set, FileNamePrefix will return
68
+ // its value.
69
+ fileNamePrefix string
65
70
}
66
71
67
72
// FixtureOpt is an option for the fixture.
@@ -1010,16 +1015,13 @@ func (f *Fixture) setClient(c client.Client) {
1010
1015
1011
1016
func (f * Fixture ) DumpProcesses (suffix string ) {
1012
1017
procs := getProcesses (f .t , `.*` )
1013
- dir , err := findProjectRoot ( f . caller )
1018
+ dir , err := f . DiagnosticsDir ( )
1014
1019
if err != nil {
1015
- f .t .Logf ("failed to dump process; failed to find project root : %s" , err )
1020
+ f .t .Logf ("failed to dump process: %s" , err )
1016
1021
return
1017
1022
}
1018
1023
1019
- // Sub-test names are separated by "/" characters which are not valid filenames on Linux.
1020
- sanitizedTestName := strings .ReplaceAll (f .t .Name (), "/" , "-" )
1021
-
1022
- filePath := filepath .Join (dir , "build" , "diagnostics" , fmt .Sprintf ("TEST-%s-%s-%s-ProcessDump%s.json" , sanitizedTestName , f .operatingSystem , f .architecture , suffix ))
1024
+ filePath := filepath .Join (dir , fmt .Sprintf ("%s-ProcessDump%s.json" , f .FileNamePrefix (), suffix ))
1023
1025
fileDir := path .Dir (filePath )
1024
1026
if err := os .MkdirAll (fileDir , 0777 ); err != nil {
1025
1027
f .t .Logf ("failed to dump process; failed to create directory %s: %s" , fileDir , err )
@@ -1044,6 +1046,69 @@ func (f *Fixture) DumpProcesses(suffix string) {
1044
1046
}
1045
1047
}
1046
1048
1049
+ // MoveToDiagnosticsDir moves file to 'build/diagnostics' which contents are
1050
+ // available on CI if the test fails or on the agent's 'build/diagnostics'
1051
+ // if the test is run locally.
1052
+ // If the file name does nos start with Fixture.FileNamePrefix(), it'll be added
1053
+ // to the filename when moving.
1054
+ func (f * Fixture ) MoveToDiagnosticsDir (file string ) {
1055
+ dir , err := f .DiagnosticsDir ()
1056
+ if err != nil {
1057
+ f .t .Logf ("failed to move file to diagnostcs directory: %s" , err )
1058
+ return
1059
+ }
1060
+
1061
+ filename := filepath .Base (file )
1062
+ if ! strings .HasPrefix (filename , f .FileNamePrefix ()) {
1063
+ filename = fmt .Sprintf ("%s-%s" , f .FileNamePrefix (), filename )
1064
+ }
1065
+ destFile := filepath .Join (dir , filename )
1066
+
1067
+ f .t .Logf ("moving %q to %q" , file , destFile )
1068
+ err = os .Rename (file , destFile )
1069
+ if err != nil {
1070
+ f .t .Logf ("failed to move %q to %q: %v" , file , destFile , err )
1071
+ }
1072
+ }
1073
+
1074
+ // FileNamePrefix returns a sanitized and unique name to be used as prefix for
1075
+ // files to be kept as resources for investigation when the test fails.
1076
+ func (f * Fixture ) FileNamePrefix () string {
1077
+ if f .fileNamePrefix != "" {
1078
+ return f .fileNamePrefix
1079
+ }
1080
+
1081
+ stamp := time .Now ().Format (time .RFC3339 )
1082
+ // on Windows a filename cannot contain a ':' as this collides with disk
1083
+ // labels (aka. C:\)
1084
+ stamp = strings .ReplaceAll (stamp , ":" , "-" )
1085
+
1086
+ // Subtest names are separated by "/" characters which are not valid
1087
+ // filenames on Linux.
1088
+ sanitizedTestName := strings .ReplaceAll (f .t .Name (), "/" , "-" )
1089
+ prefix := fmt .Sprintf ("%s-%s" , sanitizedTestName , stamp )
1090
+
1091
+ f .fileNamePrefix = prefix
1092
+ return f .fileNamePrefix
1093
+ }
1094
+
1095
+ // DiagnosticsDir returned {projectRoot}/build/diagnostics path. Files on this path
1096
+ // are saved if any test fails. Use it to save files for further investigation.
1097
+ func (f * Fixture ) DiagnosticsDir () (string , error ) {
1098
+ dir , err := findProjectRoot (f .caller )
1099
+ if err != nil {
1100
+ return "" , fmt .Errorf ("failed to find project root: %w" , err )
1101
+ }
1102
+
1103
+ diagPath := filepath .Join (dir , "build" , "diagnostics" )
1104
+
1105
+ if err := os .MkdirAll (diagPath , 0777 ); err != nil {
1106
+ return "" , fmt .Errorf ("failed to create directory %s: %w" , diagPath , err )
1107
+ }
1108
+
1109
+ return diagPath , nil
1110
+ }
1111
+
1047
1112
// validateComponents ensures that the provided UsableComponent's are valid.
1048
1113
func validateComponents (components ... UsableComponent ) error {
1049
1114
for idx , comp := range components {
0 commit comments