@@ -2,7 +2,7 @@ package core
2
2
3
3
import (
4
4
"context"
5
- "encoding/json "
5
+ "encoding/xml "
6
6
"fmt"
7
7
"os"
8
8
"strings"
@@ -15,25 +15,25 @@ type Framework struct {
15
15
outputDir string
16
16
}
17
17
18
- // Create a new testing framework
18
+ // NewFrameworkCreate a new testing framework
19
19
func NewFramework (outputDir string ) * Framework {
20
20
return & Framework {
21
21
tests : make (map [string ]Test ),
22
22
outputDir : outputDir ,
23
23
}
24
24
}
25
25
26
- // Add a test to the framework
26
+ // RegisterTest adds a test to the framework
27
27
func (f * Framework ) RegisterTest (test Test ) {
28
28
f .tests [test .Name ()] = test
29
29
}
30
30
31
- // Return all registered tests
31
+ // GetAvailableTests returns all registered tests
32
32
func (f * Framework ) GetAvailableTests () map [string ]Test {
33
33
return f .tests
34
34
}
35
35
36
- // Check if the provided test configurations are valid
36
+ // ValidateConfiguration checks if the provided test configurations are valid
37
37
func (f * Framework ) ValidateConfiguration (testsToRun []Test ,
38
38
getExpectedValuesFunc func (string ) map [string ]interface {}) []string {
39
39
@@ -84,12 +84,12 @@ func (f *Framework) ValidateConfiguration(testsToRun []Test,
84
84
return warnings
85
85
}
86
86
87
- // Execute a single test with the given expected values
87
+ // RunTest executes a single test with the given expected values
88
88
func (f * Framework ) RunTest (ctx context.Context , test Test , expectedValues map [string ]interface {}) TestResult {
89
89
return test .Run (ctx , expectedValues )
90
90
}
91
91
92
- // Execute the given tests with their expected values
92
+ // RunTests executes the given tests with their expected values
93
93
func (f * Framework ) RunTests (ctx context.Context , testsToRun []Test ,
94
94
getExpectedValuesFunc func (string ) map [string ]interface {}) ([]TestResult , error ) {
95
95
@@ -112,21 +112,104 @@ func (f *Framework) RunTests(ctx context.Context, testsToRun []Test,
112
112
return results , nil
113
113
}
114
114
115
- // Write test results to a JSON file
115
+ // saveResults saves test results in JUnit XML format
116
116
func (f * Framework ) saveResults (results []TestResult ) error {
117
- filename := fmt .Sprintf ("%s/results_%s.json" ,
117
+ // Create the root element
118
+ testSuites := JUnitTestSuites {}
119
+
120
+ // Create a test suite
121
+ testSuite := JUnitTestSuite {
122
+ Name : "KataContainerTests" ,
123
+ Tests : len (results ),
124
+ Failures : 0 ,
125
+ Time : 0 ,
126
+ }
127
+
128
+ // Process each test result
129
+ for _ , result := range results {
130
+ // Calculate test duration
131
+ duration := result .EndTime .Sub (result .StartTime )
132
+
133
+ // Create test case
134
+ testCase := JUnitTestCase {
135
+ Name : result .Name ,
136
+ ClassName : "KataContainerTest" ,
137
+ Time : duration .Seconds (),
138
+ }
139
+
140
+ // Store metrics and expected values as properties
141
+ var properties []JUnitProperty
142
+
143
+ // Add metrics properties
144
+ for key , value := range result .Metrics {
145
+ properties = append (properties , JUnitProperty {
146
+ Name : fmt .Sprintf ("metric.%s" , key ),
147
+ Value : fmt .Sprintf ("%v" , value ),
148
+ })
149
+ }
150
+
151
+ // Add expected value properties (if any)
152
+ for key , value := range result .ExpectedValues {
153
+ properties = append (properties , JUnitProperty {
154
+ Name : fmt .Sprintf ("expected.%s" , key ),
155
+ Value : fmt .Sprintf ("%v" , value ),
156
+ })
157
+ }
158
+
159
+ // Only add properties if we have any
160
+ if len (properties ) > 0 {
161
+ testCase .Properties = JUnitProperties {
162
+ Properties : properties ,
163
+ }
164
+ }
165
+
166
+ // Add failure information if test failed
167
+ if ! result .Success {
168
+ testSuite .Failures ++
169
+ testCase .Failure = & JUnitFailure {
170
+ Message : result .Error ,
171
+ Type : "AssertionFailure" ,
172
+ Value : fmt .Sprintf ("Test failed: %s" , result .Error ),
173
+ }
174
+ }
175
+
176
+ // Add test case to suite
177
+ testSuite .TestCases = append (testSuite .TestCases , testCase )
178
+
179
+ // Add to total time
180
+ testSuite .Time += duration .Seconds ()
181
+ }
182
+
183
+ // Add suite to root element
184
+ testSuites .Suites = append (testSuites .Suites , testSuite )
185
+
186
+ // Create the XML file
187
+ filename := fmt .Sprintf ("%s/results_%s.xml" ,
118
188
f .outputDir ,
119
189
time .Now ().Format ("20060102_150405" ))
120
190
121
- data , err := json . MarshalIndent ( results , "" , " " )
191
+ file , err := os . Create ( filename )
122
192
if err != nil {
123
- return err
193
+ return fmt .Errorf ("failed to create JUnit XML file: %v" , err )
194
+ }
195
+ defer file .Close ()
196
+
197
+ // Write XML header
198
+ file .WriteString (xml .Header )
199
+
200
+ // Create encoder with indentation for readability
201
+ encoder := xml .NewEncoder (file )
202
+ encoder .Indent ("" , " " )
203
+
204
+ // Encode and write
205
+ if err := encoder .Encode (testSuites ); err != nil {
206
+ return fmt .Errorf ("failed to encode JUnit XML: %v" , err )
124
207
}
125
208
126
- return os . WriteFile ( filename , data , 0644 )
209
+ return nil
127
210
}
128
211
129
- // Helper function to get a human-readable result string
212
+ // getResultString is a helper function to get a human-readable result string
130
213
func getResultString (result TestResult ) string {
131
214
if result .Success {
132
215
return "PASSED"
0 commit comments