...
1 package testutils
2
3 import (
4 "errors"
5 "fmt"
6 "os"
7 "path/filepath"
8 "runtime"
9 "strings"
10 "testing"
11
12 "github.com/onsi/gomega/format"
13
14 . "github.com/onsi/ginkgo/v2"
15 "github.com/onsi/ginkgo/v2/reporters"
16 "github.com/onsi/gomega"
17
18 "kubevirt.io/client-go/log"
19 v1reporter "kubevirt.io/client-go/reporter"
20 )
21
22 var afterSuiteReporters = []reporters.DeprecatedReporter{}
23
24
25
26
27
28 func KubeVirtTestSuiteSetup(t *testing.T) {
29 _, description, _, _ := runtime.Caller(1)
30 projectRoot := findRoot()
31 description = strings.TrimPrefix(description, projectRoot)
32
33
34 log.Log.SetIOWriter(GinkgoWriter)
35
36 gomega.RegisterFailHandler(Fail)
37
38
39
40 testsWrapped := os.Getenv("GO_TEST_WRAP")
41 outputFile := os.Getenv("XML_OUTPUT_FILE")
42
43 suiteConfig, _ := GinkgoConfiguration()
44 format.TruncatedDiff = false
45 format.MaxLength = 8192
46
47
48
49
50 if testsWrapped == "0" && outputFile != "" {
51 testTarget := os.Getenv("TEST_TARGET")
52 if suiteConfig.ParallelTotal > 1 {
53 outputFile = fmt.Sprintf("%s-%d", outputFile, GinkgoParallelProcess())
54 }
55
56 afterSuiteReporters = append(afterSuiteReporters, v1reporter.NewV1JUnitReporter(outputFile))
57
58 RunSpecs(t, testTarget)
59 } else {
60 RunSpecs(t, description)
61 }
62 }
63
64 func findRoot() string {
65 _, current, _, _ := runtime.Caller(0)
66 for {
67 current = filepath.Dir(current)
68 if current == "/" || current == "." {
69 return current
70 }
71 if _, err := os.Stat(filepath.Join(current, "WORKSPACE")); err == nil {
72 return strings.TrimSuffix(current, "/") + "/"
73 } else if errors.Is(err, os.ErrNotExist) {
74 continue
75 } else if err != nil {
76 panic(err)
77 }
78 }
79 }
80
81 var _ = ReportAfterSuite("KubeVirtTest", func(report Report) {
82 for _, reporter := range afterSuiteReporters {
83 reporters.ReportViaDeprecatedReporter(reporter, report)
84 }
85 })
86
View as plain text