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

     1  package helpers
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  
     7  	. "github.com/onsi/ginkgo/v2"
     8  	. "github.com/onsi/gomega"
     9  
    10  	"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/vars"
    11  	"github.com/k8snetworkplumbingwg/sriov-network-operator/test/util/fakefilesystem"
    12  )
    13  
    14  // GinkgoConfigureFakeFS configure fake filesystem by setting vars.FilesystemRoot
    15  // and register Ginkgo DeferCleanup handler to clean the fs when test completed
    16  func GinkgoConfigureFakeFS(f *fakefilesystem.FS) {
    17  	var (
    18  		cleanFakeFs func()
    19  		err         error
    20  	)
    21  	vars.FilesystemRoot, cleanFakeFs, err = f.Use()
    22  	Expect(err).ToNot(HaveOccurred())
    23  	DeferCleanup(cleanFakeFs)
    24  }
    25  
    26  // GinkgoAssertFileContentsEquals check that content of the file
    27  // match the expected value.
    28  // prepends vars.FilesystemRoot to the file path to be compatible with
    29  // GinkgoConfigureFakeFS function
    30  func GinkgoAssertFileContentsEquals(path, expectedContent string) {
    31  	d, err := os.ReadFile(filepath.Join(vars.FilesystemRoot, path))
    32  	ExpectWithOffset(1, err).NotTo(HaveOccurred())
    33  	ExpectWithOffset(1, string(d)).To(Equal(expectedContent))
    34  }