Documentation
¶
Overview ¶
Package testutil provides a minimal envtest wrapper and golden-file test harness for integration tests, replacing the dependency on sigs.k8s.io/usage-metrics-collector/pkg/testutil (which dragged in containerd → runc as transitive dependencies).
Index ¶
Constants ¶
const ( // SuffixJSON is the default golden-file suffix for JSON expected output. SuffixJSON = ".json" // SuffixTXT is the golden-file suffix for plain-text expected output. SuffixTXT = ".txt" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type IntegrationTestSuite ¶
type IntegrationTestSuite struct {
Environment envtest.Environment
Config *rest.Config
Client client.Client
}
IntegrationTestSuite wraps envtest.Environment and exposes a ready-to-use Config and Client after SetupTestSuite is called.
The client is initialised with k8s.io/client-go/kubernetes/scheme.Scheme. Register any additional types into that scheme in the test's init() function before calling SetupTestSuite.
func (*IntegrationTestSuite) SetupTestSuite ¶
func (s *IntegrationTestSuite) SetupTestSuite(t *testing.T)
SetupTestSuite starts the embedded envtest control plane and populates Config and Client.
func (*IntegrationTestSuite) TearDownTestSuite ¶
func (s *IntegrationTestSuite) TearDownTestSuite(t *testing.T)
TearDownTestSuite stops the envtest control plane.
type TestCase ¶
type TestCase struct {
T testing.TB
Name string
// Inputs maps every non-expected file in the scenario directory to its
// raw content string, keyed by base filename (e.g. "input_config.yaml").
Inputs map[string]string
// Actual is populated by the test body and compared against the expected
// file after fn returns (or written to it when TESTUTIL_UPDATE_EXPECTED=true).
Actual string
}
TestCase is one golden-file test scenario (a testdata subdirectory).
func (*TestCase) GetObjects ¶
GetObjects decodes all YAML/YML documents from the scenario's input files into registered k8s objects. Unrecognised documents (e.g. custom config structs) are returned as raw bytes in the second slice and can be ignored.
type TestCaseParser ¶
type TestCaseParser struct {
// Subdir is the subdirectory of testdata/ that contains the scenario dirs.
Subdir string
// ExpectedSuffix is the suffix of the golden file (default: ".json").
// The expected file is named "expected" + ExpectedSuffix.
ExpectedSuffix string
// NameSuffix is unused; retained for API compatibility.
NameSuffix string
}
TestCaseParser discovers and drives golden-file tests under testdata/<Subdir>.
func (*TestCaseParser) TestDir ¶
func (p *TestCaseParser) TestDir(t *testing.T, fn func(tc *TestCase) error)
TestDir discovers all first-level subdirectories of testdata/<p.Subdir>, builds a TestCase for each, calls fn, then either compares tc.Actual to the golden file or (when TESTUTIL_UPDATE_EXPECTED=true) overwrites it.