github.com/k8snetworkplumbingwg/sriov-network-operator@v1.2.1-0.20240408194816-2d2e5a45d453/test/conformance/test_suite_test.go (about)

     1  package conformance
     2  
     3  import (
     4  	"flag"
     5  	"log"
     6  	"path"
     7  	"testing"
     8  	"time"
     9  
    10  	. "github.com/onsi/ginkgo/v2"
    11  	. "github.com/onsi/gomega"
    12  
    13  	kniK8sReporter "github.com/openshift-kni/k8sreporter"
    14  	logf "sigs.k8s.io/controller-runtime/pkg/log"
    15  	"sigs.k8s.io/controller-runtime/pkg/log/zap"
    16  
    17  	// Test files in this package must not end with `_test.go` suffix, as they are imported as go package
    18  	_ "github.com/k8snetworkplumbingwg/sriov-network-operator/test/conformance/tests"
    19  
    20  	"github.com/k8snetworkplumbingwg/sriov-network-operator/test/util/clean"
    21  	"github.com/k8snetworkplumbingwg/sriov-network-operator/test/util/k8sreporter"
    22  )
    23  
    24  var (
    25  	customReporter *kniK8sReporter.KubernetesReporter
    26  	err            error
    27  	reportPath     *string
    28  )
    29  
    30  func init() {
    31  	reportPath = flag.String("report", "", "the path of the report directory containing details for failed tests")
    32  }
    33  
    34  func TestTest(t *testing.T) {
    35  	// We want to collect logs before any resource is deleted in AfterEach, so we register the global fail handler
    36  	// in a way such that the reporter's Dump is always called before the default Fail.
    37  	RegisterFailHandler(
    38  		func(message string, callerSkip ...int) {
    39  			if customReporter != nil {
    40  				customReporter.Dump(10*time.Minute, CurrentSpecReport().FullText())
    41  			}
    42  
    43  			// Ensure failing line location is not affected by this wrapper
    44  			for i := range callerSkip {
    45  				callerSkip[i]++
    46  			}
    47  			Fail(message, callerSkip...)
    48  		})
    49  
    50  	if *reportPath != "" {
    51  		reportFile := path.Join(*reportPath, "sriov_failure_report.log")
    52  		customReporter, err = k8sreporter.New(reportFile)
    53  		if err != nil {
    54  			log.Fatalf("Failed to create the k8s reporter %s", err)
    55  		}
    56  	}
    57  
    58  	logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
    59  	RunSpecs(t, "SRIOV Operator conformance tests")
    60  }
    61  
    62  var _ = BeforeSuite(func() {
    63  	err := clean.All()
    64  	Expect(err).NotTo(HaveOccurred())
    65  })
    66  
    67  var _ = AfterSuite(func() {
    68  	err := clean.All()
    69  	Expect(err).NotTo(HaveOccurred())
    70  })