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