@@ -3,6 +3,8 @@ package runner
3
3
import (
4
4
"fmt"
5
5
"os"
6
+ "os/exec"
7
+ "path"
6
8
"path/filepath"
7
9
"strings"
8
10
"testing"
@@ -16,62 +18,20 @@ import (
16
18
var (
17
19
multiLoaderTestConfigPath string
18
20
configPath string
21
+ rootPath string
19
22
)
20
23
21
24
func init () {
22
25
wd , _ := os .Getwd ()
23
- multiLoaderTestConfigPath = filepath .Join (wd , "../multi_loader_config.json" )
24
- configPath = filepath .Join (wd , "../base_loader_config.json" )
25
- log .Info ("Test config path: " , multiLoaderTestConfigPath )
26
- log .Info ("Test config path: " , configPath )
27
-
28
- // Override the BaseConfigPath field in multi_loader_config.json
29
- mlConfig := ml_common .ReadMultiLoaderConfigurationFile (multiLoaderTestConfigPath )
30
- mlConfig .BaseConfigPath = "../base_loader_config.json"
31
- multiLoaderTestConfigPath = "../multi_loader_config_test.json"
32
- ml_common .WriteMultiLoaderConfigurationFile (mlConfig , multiLoaderTestConfigPath )
26
+ rootPath = path .Join (wd , ".." , ".." , ".." )
33
27
}
34
28
35
29
func TestUnpackExperiment (t * testing.T ) {
36
- // helper func to validate unpacked experiments
37
- validateUnpackedExperiment := func (t * testing.T , experimentConfig []types.LoaderExperiment , studyConfig types.LoaderStudy , expectedNames []string , expectedOutputPrefixes []string ) {
38
- if len (experimentConfig ) != len (expectedNames ) {
39
- t .Errorf ("Expected %d sub-experiments, got %d" , len (expectedNames ), len (experimentConfig ))
40
- return
41
- }
42
-
43
- for i , subExp := range experimentConfig {
44
- // check name
45
- if subExp .Name != expectedNames [i ] {
46
- t .Errorf ("Expected subexperiment name '%s', got '%s'" , expectedNames [i ], subExp .Name )
47
- }
48
-
49
- // validate selected configs
50
- if subExp .Config ["ExperimentDuration" ] != studyConfig .Config ["ExperimentDuration" ] {
51
- t .Errorf ("Expected ExperimentDuration %v, got %v" , studyConfig .Config ["ExperimentDuration" ], subExp .Config ["ExperimentDuration" ])
52
- }
53
- if subExp .OutputDir != studyConfig .OutputDir {
54
- t .Errorf ("Expected OutputDir '%s', got '%s'" , studyConfig .OutputDir , subExp .OutputDir )
55
- }
56
-
57
- // check OutputPathPrefix if needed
58
- if len (expectedOutputPrefixes ) > 0 {
59
- if outputPathPrefix , ok := subExp .Config ["OutputPathPrefix" ].(string ); ! (ok && strings .HasSuffix (outputPathPrefix , expectedOutputPrefixes [i ])) {
60
- t .Errorf ("Expected OutputPathPrefix '%s', got '%s'" , expectedOutputPrefixes [i ], subExp .Config ["OutputPathPrefix" ])
61
- }
62
- }
63
- }
64
- }
65
-
66
- // create multiloader
67
- multiLoader , err := NewMultiLoaderRunner (multiLoaderTestConfigPath , "info" , false , false , false )
68
- if err != nil {
69
- t .Fatalf ("Failed to create multi-loader driver: %v" , err )
70
- }
71
-
30
+ cleanup , multiLoader := setup ()
31
+ defer cleanup ()
72
32
t .Run ("Unpack using TracesDir (Success)" , func (t * testing.T ) {
73
33
// Set TracesDir to test directory
74
- multiLoader .MultiLoaderConfig .Studies [0 ].TracesDir = "../test_data"
34
+ multiLoader .MultiLoaderConfig .Studies [0 ].TracesDir = path . Join ( rootPath , "data" , "multi_traces" )
75
35
76
36
for _ , experiment := range multiLoader .MultiLoaderConfig .Studies {
77
37
subExperiments := multiLoader .unpackStudy (experiment )
@@ -117,17 +77,13 @@ func TestUnpackExperiment(t *testing.T) {
117
77
}
118
78
119
79
func TestPrepareExperiment (t * testing.T ) {
120
- // Create a new multi-loader driver with the test config path
121
- multiLoader , err := NewMultiLoaderRunner (multiLoaderTestConfigPath , "info" , false , false , false )
122
- if err != nil {
123
- t .Fatalf ("Failed to create multi-loader driver: %v" , err )
124
- }
125
-
80
+ cleanup , multiLoader := setup ()
81
+ defer cleanup ()
126
82
subExperiment := types.LoaderExperiment {
127
83
Name : "example_1" ,
128
84
Config : map [string ]interface {}{
129
85
"ExperimentDuration" : 10 ,
130
- "TracePath" : "../test_data/ example_1_test" ,
86
+ "TracePath" : path . Join ( rootPath , "data" , "multi_traces" , " example_1_test") ,
131
87
"OutputPathPrefix" : "./test_output/example_1_test" ,
132
88
},
133
89
}
@@ -158,23 +114,22 @@ func TestPrepareExperiment(t *testing.T) {
158
114
159
115
// Test mergeConfigurations method
160
116
func TestMergeConfig (t * testing.T ) {
161
- // Create a new multi-loader driver with the test config path
162
- multiLoader , err := NewMultiLoaderRunner (multiLoaderTestConfigPath , "info" , false , false , false )
163
- if err != nil {
164
- t .Fatalf ("Failed to create multi-loader driver: %v" , err )
165
- }
117
+ cleanup , multiLoader := setup ()
118
+ defer cleanup ()
119
+
120
+ newTracePath := path .Join (rootPath , "data" , "multi_traces" , "example_1_test" )
166
121
experiment := types.LoaderExperiment {
167
122
Name : "example_1" ,
168
123
Config : map [string ]interface {}{
169
124
"ExperimentDuration" : 10 ,
170
- "TracePath" : "../test_data/example_1_test" ,
125
+ "TracePath" : newTracePath ,
171
126
"OutputPathPrefix" : "./test_output/example_1_test" ,
172
127
},
173
128
}
174
129
outputConfig := multiLoader .mergeConfigurations (configPath , experiment )
175
130
// Check if the configurations are merged
176
- if outputConfig .TracePath != "../test_data/example_1_test" {
177
- t .Errorf ("Expected TracePath to be '../test_data/example_1_test ', got %v" , experiment .Config ["TracePath" ])
131
+ if outputConfig .TracePath != newTracePath {
132
+ t .Errorf ("Expected TracePath to be '%v ', got %v" , newTracePath , experiment .Config ["TracePath" ])
178
133
}
179
134
if outputConfig .OutputPathPrefix != "./test_output/example_1_test" {
180
135
t .Errorf ("Expected OutputPathPrefix to be './test_output/example_1_test', got %v" , experiment .Config ["OutputPathPrefix" ])
@@ -185,11 +140,8 @@ func TestMergeConfig(t *testing.T) {
185
140
}
186
141
187
142
func TestMultiConfigValidator (t * testing.T ) {
188
- // Create a new multi-loader driver with the test config path
189
- multiLoader , err := NewMultiLoaderRunner (multiLoaderTestConfigPath , "info" , false , false , false )
190
- if err != nil {
191
- t .Fatalf ("Failed to create multi-loader driver: %v" , err )
192
- }
143
+ cleanup , multiLoader := setup ()
144
+ defer cleanup ()
193
145
t .Run ("CheckMultiLoaderConfig (Success)" , func (t * testing.T ) {
194
146
// Check if all paths are valid
195
147
ml_common .CheckMultiLoaderConfig (multiLoader .MultiLoaderConfig )
@@ -237,15 +189,81 @@ func TestMultiConfigValidator(t *testing.T) {
237
189
})
238
190
}
239
191
192
+ func setup () (func (), MultiLoaderRunner ) {
193
+ wd , _ := os .Getwd ()
194
+ multiLoaderTestConfigPath = filepath .Join (wd , "../multi_loader_config.json" )
195
+ configPath = filepath .Join (rootPath , "pkg/config/test_config.json" )
196
+ log .Info ("Test config path: " , multiLoaderTestConfigPath )
197
+ log .Info ("Test config path: " , configPath )
198
+
199
+ // Override the BaseConfigPath field in multi_loader_config.json
200
+ mlConfig := ml_common .ReadMultiLoaderConfigurationFile (multiLoaderTestConfigPath )
201
+ mlConfig .BaseConfigPath = filepath .Join (rootPath , "pkg/config/test_config.json" )
202
+ multiLoaderTestConfigPath = "../multi_loader_config_test.json"
203
+ ml_common .WriteMultiLoaderConfigurationFile (mlConfig , multiLoaderTestConfigPath )
204
+
205
+ // Create test_data
206
+ filePath := filepath .Join (rootPath , "scripts" , "setup" , "setup_multi_test_trace.sh" )
207
+ cmd := exec .Command ("bash" , filePath )
208
+ cmd .Dir = rootPath
209
+ out , err := cmd .CombinedOutput ()
210
+ if err != nil {
211
+ log .Fatal ("Failed to create test traces:" , string (out ), err )
212
+ }
213
+
214
+ cleanup := func () {
215
+ os .Remove (multiLoaderTestConfigPath )
216
+ os .RemoveAll (path .Join (rootPath , "data" , "multi_traces" ))
217
+ }
218
+
219
+ // Create a new multi-loader driver with the test config path
220
+ multiLoader , err := NewMultiLoaderRunner (multiLoaderTestConfigPath , "info" , false , false , false )
221
+ if err != nil {
222
+ log .Fatalf ("Failed to create multi-loader driver: %v" , err )
223
+ }
224
+
225
+ return cleanup , * multiLoader
226
+ }
227
+
228
+ // helper func to validate unpacked experiments
229
+ func validateUnpackedExperiment (t * testing.T , experimentConfig []types.LoaderExperiment , studyConfig types.LoaderStudy , expectedNames []string , expectedOutputPrefixes []string ) {
230
+ if len (experimentConfig ) != len (expectedNames ) {
231
+ t .Errorf ("Expected %d sub-experiments, got %d" , len (expectedNames ), len (experimentConfig ))
232
+ return
233
+ }
234
+
235
+ for i , subExp := range experimentConfig {
236
+ // check name
237
+ if subExp .Name != expectedNames [i ] {
238
+ t .Errorf ("Expected subexperiment name '%s', got '%s'" , expectedNames [i ], subExp .Name )
239
+ }
240
+
241
+ // validate selected configs
242
+ if subExp .Config ["ExperimentDuration" ] != studyConfig .Config ["ExperimentDuration" ] {
243
+ t .Errorf ("Expected ExperimentDuration %v, got %v" , studyConfig .Config ["ExperimentDuration" ], subExp .Config ["ExperimentDuration" ])
244
+ }
245
+ if subExp .OutputDir != studyConfig .OutputDir {
246
+ t .Errorf ("Expected OutputDir '%s', got '%s'" , studyConfig .OutputDir , subExp .OutputDir )
247
+ }
248
+
249
+ // check OutputPathPrefix if needed
250
+ if len (expectedOutputPrefixes ) > 0 {
251
+ if outputPathPrefix , ok := subExp .Config ["OutputPathPrefix" ].(string ); ! (ok && strings .HasSuffix (outputPathPrefix , expectedOutputPrefixes [i ])) {
252
+ t .Errorf ("Expected OutputPathPrefix '%s', got '%s'" , expectedOutputPrefixes [i ], subExp .Config ["OutputPathPrefix" ])
253
+ }
254
+ }
255
+ }
256
+ }
257
+
240
258
func expectFatal (t * testing.T , funcToTest func ()) {
241
259
fatal := false
242
260
originalExitFunc := log .StandardLogger ().ExitFunc
243
-
244
- // replace logrus exit function
261
+ log . Info ( "Expecting a fatal message during the test, overriding the exit function" )
262
+ // Replace logrus exit function
245
263
log .StandardLogger ().ExitFunc = func (int ) { fatal = true }
246
264
247
265
funcToTest ()
248
- // restore original state
266
+ // Restore original exit function
249
267
log .StandardLogger ().ExitFunc = originalExitFunc
250
268
assert .True (t , fatal , "Expected log.Fatal to be called" )
251
269
}
0 commit comments