github.com/kubeshop/testkube@v1.17.23/pkg/mapper/cdevents/mapper_test.go (about) 1 package cdevents 2 3 import ( 4 "errors" 5 "testing" 6 7 cdevents "github.com/cdevents/sdk-go/pkg/api" 8 "github.com/stretchr/testify/assert" 9 10 "github.com/kubeshop/testkube/pkg/api/v1/testkube" 11 ) 12 13 func TestMapTestkubeEventQueuedTestToCDEvent(t *testing.T) { 14 t.Parallel() 15 16 event := testkube.Event{ 17 TestExecution: &testkube.Execution{ 18 Id: "1", 19 Name: "test-1", 20 TestName: "Test 1", 21 TestType: "ginkgo/test", 22 TestNamespace: "default", 23 RunningContext: &testkube.RunningContext{ 24 Type_: "scheduler", 25 }, 26 TestSuiteName: "Suite 1", 27 }, 28 } 29 clusterID := "cluster-1" 30 defaultNamespace := "default" 31 32 ev, err := MapTestkubeEventQueuedTestToCDEvent(event, clusterID, defaultNamespace, "") 33 if err != nil { 34 t.Errorf("Error mapping event: %v", err) 35 return 36 } 37 38 subjectID := ev.GetSubjectId() 39 if subjectID != "1" { 40 t.Errorf("Unexpected subject ID: %s", subjectID) 41 } 42 43 subjectSource := ev.GetSubjectSource() 44 if subjectSource != clusterID { 45 t.Errorf("Unexpected subject source: %s", subjectSource) 46 } 47 48 source := ev.GetSource() 49 if source != clusterID { 50 t.Errorf("Unexpected source: %s", source) 51 } 52 53 cde, ok := ev.(*cdevents.TestCaseRunQueuedEvent) 54 assert.Equal(t, true, ok) 55 56 testID := cde.Subject.Content.TestCase.Id 57 if testID != "Test 1" { 58 t.Errorf("Unexpected test case id: %s", testID) 59 } 60 61 testType := cde.Subject.Content.TestCase.Type 62 if testType != "unit" { 63 t.Errorf("Unexpected test case type: %s", testType) 64 } 65 66 testURI := cde.Subject.Content.TestCase.Uri 67 if testURI != "/tests/Test 1" { 68 t.Errorf("Unexpected test case uri: %s", testURI) 69 } 70 71 envID := cde.Subject.Content.Environment.Id 72 if envID != defaultNamespace { 73 t.Errorf("Unexpected environment id: %s", envID) 74 } 75 76 envSource := cde.Subject.Content.Environment.Source 77 if envSource != clusterID { 78 t.Errorf("Unexpected environment source: %s", envSource) 79 } 80 81 triggerType := cde.Subject.Content.Trigger.Type 82 if triggerType != "schedule" { 83 t.Errorf("Unexpected trigger type: %s", triggerType) 84 } 85 86 suiteID := cde.Subject.Content.TestSuiteRun.Id 87 if suiteID != "Suite 1" { 88 t.Errorf("Unexpected test suite id: %s", suiteID) 89 } 90 91 suiteSource := cde.Subject.Content.TestSuiteRun.Source 92 if suiteSource != clusterID { 93 t.Errorf("Unexpected test suite source: %s", suiteSource) 94 } 95 } 96 97 func TestMapTestkubeEventStatTestToCDEvent(t *testing.T) { 98 t.Parallel() 99 100 event := testkube.Event{ 101 TestExecution: &testkube.Execution{ 102 Id: "1", 103 Name: "test-1", 104 TestName: "Test 1", 105 TestType: "ginkgo/test", 106 TestNamespace: "default", 107 RunningContext: &testkube.RunningContext{ 108 Type_: "scheduler", 109 }, 110 TestSuiteName: "Suite 1", 111 }, 112 } 113 clusterID := "cluster-1" 114 defaultNamespace := "default" 115 116 ev, err := MapTestkubeEventStartTestToCDEvent(event, clusterID, defaultNamespace, "") 117 if err != nil { 118 t.Errorf("Error mapping event: %v", err) 119 return 120 } 121 122 subjectID := ev.GetSubjectId() 123 if subjectID != "1" { 124 t.Errorf("Unexpected subject ID: %s", subjectID) 125 } 126 127 subjectSource := ev.GetSubjectSource() 128 if subjectSource != clusterID { 129 t.Errorf("Unexpected subject source: %s", subjectSource) 130 } 131 132 source := ev.GetSource() 133 if source != clusterID { 134 t.Errorf("Unexpected source: %s", source) 135 } 136 137 cde, ok := ev.(*cdevents.TestCaseRunStartedEvent) 138 assert.Equal(t, true, ok) 139 140 testID := cde.Subject.Content.TestCase.Id 141 if testID != "Test 1" { 142 t.Errorf("Unexpected test case id: %s", testID) 143 } 144 145 testType := cde.Subject.Content.TestCase.Type 146 if testType != "unit" { 147 t.Errorf("Unexpected test case type: %s", testType) 148 } 149 150 testURI := cde.Subject.Content.TestCase.Uri 151 if testURI != "/tests/Test 1" { 152 t.Errorf("Unexpected test case uri: %s", testURI) 153 } 154 155 envID := cde.Subject.Content.Environment.Id 156 if envID != defaultNamespace { 157 t.Errorf("Unexpected environment id: %s", envID) 158 } 159 160 envSource := cde.Subject.Content.Environment.Source 161 if envSource != clusterID { 162 t.Errorf("Unexpected environment source: %s", envSource) 163 } 164 165 triggerType := cde.Subject.Content.Trigger.Type 166 if triggerType != "schedule" { 167 t.Errorf("Unexpected trigger type: %s", triggerType) 168 } 169 170 suiteID := cde.Subject.Content.TestSuiteRun.Id 171 if suiteID != "Suite 1" { 172 t.Errorf("Unexpected test suite id: %s", suiteID) 173 } 174 175 suiteSource := cde.Subject.Content.TestSuiteRun.Source 176 if suiteSource != clusterID { 177 t.Errorf("Unexpected test suite source: %s", suiteSource) 178 } 179 } 180 181 func TestMapTestkubeEventFinishTestToCDEvent(t *testing.T) { 182 t.Parallel() 183 184 result := testkube.NewErrorExecutionResult(errors.New("fake")) 185 event := testkube.Event{ 186 TestExecution: &testkube.Execution{ 187 Id: "1", 188 Name: "test-1", 189 TestName: "Test 1", 190 TestType: "ginkgo/test", 191 TestNamespace: "default", 192 RunningContext: &testkube.RunningContext{ 193 Type_: "scheduler", 194 }, 195 TestSuiteName: "Suite 1", 196 ExecutionResult: &result, 197 }, 198 } 199 clusterID := "cluster-1" 200 defaultNamespace := "default" 201 202 ev, err := MapTestkubeEventFinishTestToCDEvent(event, clusterID, defaultNamespace, "") 203 if err != nil { 204 t.Errorf("Error mapping event: %v", err) 205 return 206 } 207 208 subjectID := ev.GetSubjectId() 209 if subjectID != "1" { 210 t.Errorf("Unexpected subject ID: %s", subjectID) 211 } 212 213 subjectSource := ev.GetSubjectSource() 214 if subjectSource != clusterID { 215 t.Errorf("Unexpected subject source: %s", subjectSource) 216 } 217 218 source := ev.GetSource() 219 if source != clusterID { 220 t.Errorf("Unexpected source: %s", source) 221 } 222 223 cde, ok := ev.(*cdevents.TestCaseRunFinishedEvent) 224 assert.Equal(t, true, ok) 225 226 testID := cde.Subject.Content.TestCase.Id 227 if testID != "Test 1" { 228 t.Errorf("Unexpected test case id: %s", testID) 229 } 230 231 testType := cde.Subject.Content.TestCase.Type 232 if testType != "unit" { 233 t.Errorf("Unexpected test case type: %s", testType) 234 } 235 236 testURI := cde.Subject.Content.TestCase.Uri 237 if testURI != "/tests/Test 1" { 238 t.Errorf("Unexpected test case uri: %s", testURI) 239 } 240 241 envID := cde.Subject.Content.Environment.Id 242 if envID != defaultNamespace { 243 t.Errorf("Unexpected environment id: %s", envID) 244 } 245 246 envSource := cde.Subject.Content.Environment.Source 247 if envSource != clusterID { 248 t.Errorf("Unexpected environment source: %s", envSource) 249 } 250 251 suiteID := cde.Subject.Content.TestSuiteRun.Id 252 if suiteID != "Suite 1" { 253 t.Errorf("Unexpected test suite id: %s", suiteID) 254 } 255 256 suiteSource := cde.Subject.Content.TestSuiteRun.Source 257 if suiteSource != clusterID { 258 t.Errorf("Unexpected test suite source: %s", suiteSource) 259 } 260 261 outcome := cde.Subject.Content.Outcome 262 if outcome != "fail" { 263 t.Errorf("Unexpected outcome: %s", outcome) 264 } 265 266 reason := cde.Subject.Content.Reason 267 if reason != "fake" { 268 t.Errorf("Unexpected reason: %s", reason) 269 } 270 } 271 272 func TestMapTestkubeEventQueuedTestSuiteToCDEvent(t *testing.T) { 273 t.Parallel() 274 275 event := testkube.Event{ 276 TestSuiteExecution: &testkube.TestSuiteExecution{ 277 Id: "1", 278 Name: "suite-1", 279 TestSuite: &testkube.ObjectRef{ 280 Namespace: "default", 281 Name: "Suite 1", 282 }, 283 RunningContext: &testkube.RunningContext{ 284 Type_: "scheduler", 285 }, 286 }, 287 } 288 clusterID := "cluster-1" 289 290 ev, err := MapTestkubeEventQueuedTestSuiteToCDEvent(event, clusterID, "") 291 if err != nil { 292 t.Errorf("Error mapping event: %v", err) 293 return 294 } 295 296 subjectID := ev.GetSubjectId() 297 if subjectID != "1" { 298 t.Errorf("Unexpected subject ID: %s", subjectID) 299 } 300 301 subjectSource := ev.GetSubjectSource() 302 if subjectSource != clusterID { 303 t.Errorf("Unexpected subject source: %s", subjectSource) 304 } 305 306 source := ev.GetSource() 307 if source != clusterID { 308 t.Errorf("Unexpected source: %s", source) 309 } 310 311 cde, ok := ev.(*cdevents.TestSuiteRunQueuedEvent) 312 assert.Equal(t, true, ok) 313 314 suiteID := cde.Subject.Content.TestSuite.Id 315 if suiteID != "Suite 1" { 316 t.Errorf("Unexpected test suite id: %s", suiteID) 317 } 318 319 suiteURI := cde.Subject.Content.TestSuite.Url 320 if suiteURI != "/test-suites/executions/Suite 1" { 321 t.Errorf("Unexpected test case uri: %s", suiteURI) 322 } 323 324 envID := cde.Subject.Content.Environment.Id 325 if envID != "default" { 326 t.Errorf("Unexpected environment id: %s", envID) 327 } 328 329 envSource := cde.Subject.Content.Environment.Source 330 if envSource != clusterID { 331 t.Errorf("Unexpected environment source: %s", envSource) 332 } 333 334 triggerType := cde.Subject.Content.Trigger.Type 335 if triggerType != "schedule" { 336 t.Errorf("Unexpected trigger type: %s", triggerType) 337 } 338 } 339 340 func TestMapTestkubeEventStartTestSuiteToCDEvent(t *testing.T) { 341 t.Parallel() 342 343 event := testkube.Event{ 344 TestSuiteExecution: &testkube.TestSuiteExecution{ 345 Id: "1", 346 Name: "suite-1", 347 TestSuite: &testkube.ObjectRef{ 348 Namespace: "default", 349 Name: "Suite 1", 350 }, 351 RunningContext: &testkube.RunningContext{ 352 Type_: "scheduler", 353 }, 354 }, 355 } 356 clusterID := "cluster-1" 357 358 ev, err := MapTestkubeEventStartTestSuiteToCDEvent(event, clusterID, "") 359 if err != nil { 360 t.Errorf("Error mapping event: %v", err) 361 return 362 } 363 364 subjectID := ev.GetSubjectId() 365 if subjectID != "1" { 366 t.Errorf("Unexpected subject ID: %s", subjectID) 367 } 368 369 subjectSource := ev.GetSubjectSource() 370 if subjectSource != clusterID { 371 t.Errorf("Unexpected subject source: %s", subjectSource) 372 } 373 374 source := ev.GetSource() 375 if source != clusterID { 376 t.Errorf("Unexpected source: %s", source) 377 } 378 379 cde, ok := ev.(*cdevents.TestSuiteRunStartedEvent) 380 assert.Equal(t, true, ok) 381 382 suiteID := cde.Subject.Content.TestSuite.Id 383 if suiteID != "Suite 1" { 384 t.Errorf("Unexpected test suite id: %s", suiteID) 385 } 386 387 suiteURI := cde.Subject.Content.TestSuite.Uri 388 if suiteURI != "/test-suites/Suite 1" { 389 t.Errorf("Unexpected test case uri: %s", suiteURI) 390 } 391 392 envID := cde.Subject.Content.Environment.Id 393 if envID != "default" { 394 t.Errorf("Unexpected environment id: %s", envID) 395 } 396 397 envSource := cde.Subject.Content.Environment.Source 398 if envSource != clusterID { 399 t.Errorf("Unexpected environment source: %s", envSource) 400 } 401 402 triggerType := cde.Subject.Content.Trigger.Type 403 if triggerType != "schedule" { 404 t.Errorf("Unexpected trigger type: %s", triggerType) 405 } 406 } 407 408 func TestMapTestkubeEventFinishTestSuiteToCDEvent(t *testing.T) { 409 t.Parallel() 410 411 execution := testkube.NewFailedExecution(errors.New("fake")) 412 event := testkube.Event{ 413 TestSuiteExecution: &testkube.TestSuiteExecution{ 414 Id: "1", 415 Name: "suite-1", 416 TestSuite: &testkube.ObjectRef{ 417 Namespace: "default", 418 Name: "Suite 1", 419 }, 420 RunningContext: &testkube.RunningContext{ 421 Type_: "scheduler", 422 }, 423 Status: testkube.TestSuiteExecutionStatusFailed, 424 ExecuteStepResults: []testkube.TestSuiteBatchStepExecutionResult{ 425 { 426 Execute: []testkube.TestSuiteStepExecutionResult{ 427 { 428 Execution: &execution, 429 }, 430 }, 431 }, 432 }, 433 }, 434 } 435 clusterID := "cluster-1" 436 437 ev, err := MapTestkubeEventFinishTestSuiteToCDEvent(event, clusterID, "") 438 if err != nil { 439 t.Errorf("Error mapping event: %v", err) 440 return 441 } 442 443 subjectID := ev.GetSubjectId() 444 if subjectID != "1" { 445 t.Errorf("Unexpected subject ID: %s", subjectID) 446 } 447 448 subjectSource := ev.GetSubjectSource() 449 if subjectSource != clusterID { 450 t.Errorf("Unexpected subject source: %s", subjectSource) 451 } 452 453 source := ev.GetSource() 454 if source != clusterID { 455 t.Errorf("Unexpected source: %s", source) 456 } 457 458 cde, ok := ev.(*cdevents.TestSuiteRunFinishedEvent) 459 assert.Equal(t, true, ok) 460 461 suiteID := cde.Subject.Content.TestSuite.Id 462 if suiteID != "Suite 1" { 463 t.Errorf("Unexpected test suite id: %s", suiteID) 464 } 465 466 suiteURI := cde.Subject.Content.TestSuite.Uri 467 if suiteURI != "/test-suites/Suite 1" { 468 t.Errorf("Unexpected test case uri: %s", suiteURI) 469 } 470 471 envID := cde.Subject.Content.Environment.Id 472 if envID != "default" { 473 t.Errorf("Unexpected environment id: %s", envID) 474 } 475 476 envSource := cde.Subject.Content.Environment.Source 477 if envSource != clusterID { 478 t.Errorf("Unexpected environment source: %s", envSource) 479 } 480 481 outcome := cde.Subject.Content.Outcome 482 if outcome != "fail" { 483 t.Errorf("Unexpected outcome: %s", outcome) 484 } 485 486 reason := cde.Subject.Content.Reason 487 if reason != "fake" { 488 t.Errorf("Unexpected reason: %s", reason) 489 } 490 }