1 package middlechild
2
3 import (
4 "fmt"
5 "os"
6 "sort"
7 "testing"
8 "time"
9
10 "github.com/google/uuid"
11 "gotest.tools/v3/assert"
12
13 "edge-infra.dev/pkg/f8n/devinfra/testinfra/model"
14 )
15
16 func TestGetArgoPodName(t *testing.T) {
17 tfe := []struct {
18 job *Job
19 found bool
20 pod []string
21 }{
22 {
23 &Job{
24 Metadata: []model.EdgeJobMetadata{
25 {Key: "some", Value: "a"},
26 },
27 },
28 false,
29 []string{},
30 },
31 {
32 &Job{
33 Metadata: []model.EdgeJobMetadata{
34 {Key: "some", Value: "a"},
35 {Key: "ID", Value: "hourly-l2-serial-test-1725372000-2227150978"},
36 },
37 },
38 true,
39 []string{"hourly-l2-serial-test-1725372000-rosa-2227150978", "hourly-l2-serial-test-1725372000-rosa-with-kubeconfig-2227150978"},
40 },
41 {
42 &Job{
43 Metadata: []model.EdgeJobMetadata{
44 {Key: "some", Value: "a"},
45 {Key: "ID", Value: ""},
46 },
47 },
48 true,
49 []string{"", ""},
50 },
51 {
52 &Job{
53 Metadata: []model.EdgeJobMetadata{
54 {Key: "some", Value: "a"},
55 {Key: "ID", Value: "1-2"},
56 },
57 },
58 true,
59 []string{"1-rosa-2", "1-rosa-with-kubeconfig-2"},
60 },
61 {
62 &Job{
63 Metadata: []model.EdgeJobMetadata{
64 {Key: "some", Value: "a"},
65 {Key: "ID", Value: "12"},
66 },
67 },
68 true,
69 []string{"12", "12"},
70 },
71 }
72
73 for i, tc := range tfe {
74 t.Run(fmt.Sprintf("%s_%d", t.Name(), i), func(t *testing.T) {
75 found, pod := getArgoPodName(tc.job)
76 assert.Equal(t, tc.found, found)
77 if tc.found {
78 assert.DeepEqual(t, tc.pod, pod)
79 }
80 })
81 }
82 }
83
84 func TestBuildArgoLogPath(t *testing.T) {
85 tfe := []struct {
86 job *Job
87 id string
88 path string
89 }{
90 {
91 &Job{
92 JobData: model.EdgeJob{
93 Run: "12345678-abcd",
94 Started: time.Unix(1725887349, 0).UTC(),
95 },
96 },
97 "hourly-l2-serial-test-1-rosa-2",
98 "2024/09/09/12345678-abcd/hourly-l2-serial-test-1-rosa-2/main.log",
99 },
100 {
101 &Job{
102 JobData: model.EdgeJob{
103 Run: "12345678-abcd",
104 Started: time.Unix(1725973749, 0).UTC(),
105 },
106 },
107 "hourly-l2-serial-test-1-rosa-2",
108 "2024/09/10/12345678-abcd/hourly-l2-serial-test-1-rosa-2/main.log",
109 },
110 }
111
112 for i, tc := range tfe {
113 t.Run(fmt.Sprintf("%s_%d", t.Name(), i), func(t *testing.T) {
114 path := buildArgoLogPath(tc.job, tc.id)
115 assert.Equal(t, tc.path, path)
116 })
117 }
118 }
119
120 func TestBuildArgoResultPath(t *testing.T) {
121 tfe := []struct {
122 job *Job
123 path string
124 }{
125 {
126 &Job{
127 JobData: model.EdgeJob{
128 Workflow: "hourly-slow-l2-test",
129 Number: "39998649-1be4-4103-af4a-6f18f1f19aea-AE85iO",
130 },
131 },
132 "argo/edge-infra/hourly-slow-l2-test/39998649-1be4-4103-af4a-6f18f1f19aea-AE85iO/logs/logs.txt",
133 },
134 }
135
136 for i, tc := range tfe {
137 t.Run(fmt.Sprintf("%s_%d", t.Name(), i), func(t *testing.T) {
138 path := buildArgoResultPath(tc.job)
139 assert.Equal(t, tc.path, path)
140 })
141 }
142 }
143
144 func TestGatherFinished(t *testing.T) {
145 fjson := []byte(`{"timestamp":1635278696, "passed": true,"metadata":{"job_name":"build","workflow_name":"CI", "run_id": "2053035745", "platform": "actions"}}`)
146
147 mc := MiddleChild{}
148 mcj := &Job{}
149
150 mcj.files.JSON = make(map[string][]byte)
151 mcj.files.JSON["finished.json"] = fjson
152
153 assert.NilError(t, mc.gatherFinished(mcj), "failed to gather finished data")
154
155 t.Logf("%+v", mcj.Metadata)
156
157 b := []byte{
158 0x0, 0x0, 0x0, 0x0,
159 0x0, 0x0, 0x0, 0x0,
160 0x0, 0x0, 0x0, 0x0,
161 0x0, 0x0, 0x0, 0x0,
162 }
163
164 uuid, err := uuid.FromBytes(b)
165 assert.NilError(t, err, "failed to create uuid")
166
167 meta := []model.EdgeJobMetadata{
168 {
169 EdgeJob: uuid,
170 Key: "job_name",
171 Value: "build",
172 },
173 {
174 EdgeJob: uuid,
175 Key: "workflow_name",
176 Value: "CI",
177 },
178 {
179 EdgeJob: uuid,
180 Key: "run_id",
181 Value: "2053035745",
182 },
183 {
184 EdgeJob: uuid,
185 Key: "platform",
186 Value: "actions",
187 },
188 }
189
190
191 sort.Slice(meta, func(i, j int) bool {
192 return meta[i].Key < meta[j].Key
193 })
194
195 sort.Slice(mcj.Metadata, func(i, j int) bool {
196 return mcj.Metadata[i].Key < mcj.Metadata[j].Key
197 })
198
199 assert.DeepEqual(t, meta, mcj.Metadata)
200 }
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272 func TestParseJUnit(t *testing.T) {
273 tfe := []struct {
274 path string
275 tests int
276 failed int
277 checkJob bool
278 job []model.EdgeJobTest
279 }{
280 {
281 "testdata/unit2558878622.xml", 47, 3, false, []model.EdgeJobTest{},
282 },
283 {
284 "testdata/unit2598106678.xml", 82, 5, false, []model.EdgeJobTest{},
285 },
286 {
287 "testdata/unit1027837568.xml", 18, 0, false, []model.EdgeJobTest{},
288 },
289 {
290 "testdata/unit540587877.xml", 21, 0, false, []model.EdgeJobTest{},
291 },
292 {
293 "testdata/passed.xml", 3, 0, true, []model.EdgeJobTest{
294 {Name: "TestDeploymentToLivenessReadinessResponse", Time: 0, Failed: false, FailureText: "", Suite: "edge-infra.dev/pkg/edge/api/graph/mapper"},
295 {Name: "TestDeploymentToLivenessReadinessResponseDaemonSet", Time: 0, Failed: false, FailureText: "", Suite: "edge-infra.dev/pkg/edge/api/graph/mapper"},
296 {Name: "TestIsClusterReadyForDeletedCluster", Time: 0, Failed: false, FailureText: "", Suite: "edge-infra.dev/pkg/edge/api/graph/mapper"},
297 },
298 },
299 {
300 "testdata/failed.xml", 3, 1, true, []model.EdgeJobTest{
301 {Name: "TestDeploymentToLivenessReadinessResponse", Time: 0, Failed: true, FailureText: "Assertion failed", Suite: "edge-infra.dev/pkg/edge/api/graph/mapper"},
302 {Name: "TestDeploymentToLivenessReadinessResponseDaemonSet", Time: 0, Failed: false, FailureText: "", Suite: "edge-infra.dev/pkg/edge/api/graph/mapper"},
303 {Name: "TestIsClusterReadyForDeletedCluster", Time: 0, Failed: false, FailureText: "", Suite: "edge-infra.dev/pkg/edge/api/graph/mapper"},
304 },
305 },
306 }
307
308 for i, tc := range tfe {
309 t.Run(fmt.Sprintf("%s_%d", t.Name(), i), func(t *testing.T) {
310 dat, err := os.ReadFile(tc.path)
311 assert.NilError(t, err, "failed to read xml file")
312
313 job, tests, failed, err := parseJUnit(dat)
314 assert.NilError(t, err, "failed to parse junit")
315 assert.Equal(t, tc.tests, tests)
316 assert.Equal(t, tc.failed, failed)
317
318 if tc.checkJob {
319 assert.DeepEqual(t, tc.job, job)
320 }
321 })
322 }
323 }
324
View as plain text