...

Source file src/kubevirt.io/client-go/testutils/setup.go

Documentation: kubevirt.io/client-go/testutils

     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  // KubeVirtTestSuiteSetup is the default setup function for kubevirts unittests.
    25  // If tests are executed through bazel, the provided description is ignored. Instead
    26  // the TEST_TARGET environment variable will be used to synchronize the output
    27  // with bazels test output and make test navigation and detection consistent.
    28  func KubeVirtTestSuiteSetup(t *testing.T) {
    29  	_, description, _, _ := runtime.Caller(1)
    30  	projectRoot := findRoot()
    31  	description = strings.TrimPrefix(description, projectRoot)
    32  	// Redirect writes to ginkgo writer to keep tests quiet when
    33  	// they succeed
    34  	log.Log.SetIOWriter(GinkgoWriter)
    35  	// setup the connection between ginkgo and gomega
    36  	gomega.RegisterFailHandler(Fail)
    37  
    38  	// See https://github.com/bazelbuild/rules_go/blob/197699822e081dad064835a09825448a3e4cc2a2/go/core.rst#go_test
    39  	// for context.
    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  	// if run on bazel (XML_OUTPUT_FILE is not empty)
    48  	// and rules_go is configured to not produce the junit xml
    49  	// produce it here. Otherwise just run the default RunSpec
    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