katatestutils

package
v0.0.0-...-f04a7a5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 1, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

README

Kata test utilities

This package provides a small set of test utilities. See the GoDoc for full details.

Test Constraints

This package provides helper functions that accept user-specified constraints that allow you to skip tests.

Usage

Create a TestConstraint object using the NewTestConstraint() constructor. This takes a single boolean parameter that specifies if debug output is generated.

In each test that has particular test constraints, call the NotValid() method on the TestConstraint object, passing one or more constraints that you want to be valid.

The NotValid() function returns true if any of the specified constraints are not available. This allows for a more natural way to code an arbitrarily complex test skip as shown in the following example.

The main object is created in the init() function to make it available for all tests:


import ktu "katatestutils"

var tc ktu.TestConstraint

func init() {
    tc = NewTestConstraint(true)
}

func TestFoo(t *testing.T) {

    // Specify one or more constraint functions. If not satisfied, the test
    // will be skipped.
    if tc.NotValid(...) {
        t.Skip("skipping test")
    }

    // Test code ...
}
Displaying the TestConstraint

Note that you could add the TestConstraint object to the Skip() call as it will provide details of why the skip occurred:

if tc.NotValid(...) {
    t.Skipf("skipping test as requirements not met: %v", tc)
}
Associating an issue with a constraint

You can add a constraint which specifies an issue URL for the skip. No checking is performed on the issue but if specified, it will be added to the TestConstraint and recorded in error messages and when that object is displayed:

if tc.NotValid(WithIssue("https://github.com/kata-containers/runtime/issues/1586"), ...) {
    t.Skipf("skipping test as requirements not met: %v", tc)
}
Examples
Skip tests based on user

Use the NeedRoot() constraint to skip a test unless running as root:

func TestOnlyRunWhenRoot(t *testing.T) {

    if tc.NotValid(ktu.NeedRoot()) {
        t.Skip("skipping test as not running as root user")
    }

    // Test code to run as root user ...
}

Use the NeedNonRoot() constraint to skip a test unless running as a non-root user:

func TestOnlyRunWhenNotRoot(t *testing.T) {

    if tc.NotValid(ktu.NeedNonRoot()) {
        t.Skip("skipping test as running as root user")
    }

    // Test code to run as non-root user ...
}
Skip tests based on kernel version

Use the NeedKernelVersionGE() constraint to skip a test unless running on a system with at least the specified kernel version:

func TestNewKernelVersion(t *testing.T) {

    if tc.NotValid(ktu.NeedKernelVersionGE("5.0.10")) {
        t.Skip("skipping test as kernel is too old")
    }

    // Test code to run on specified kernel version (or newer) ...
}

Use the NeedKernelVersionLT() constraint to skip a test unless running on a system whose kernel is older than the specified kernel version:

func TestOldKernelVersion(t *testing.T) {

    if tc.NotValid(ktu.NeedKernelVersionLT("4.14.114")) {
        t.Skip("skipping test as kernel is too new")
    }

    // Test code to run on specified kernel version (or newer) ...
}
Full details

The public API is shown in constraints_api.go or the GoDoc.

Documentation

Index

Constants

View Source
const (
	TestDisabledNeedRoot    = "Test disabled as requires root user"
	TestDisabledNeedNonRoot = "Test disabled as requires non-root user"
)

Variables

View Source
var ContainerIDTestData = []ContainerIDTestDataType{
	{"", false},
	{" ", false},
	{".", false},
	{"-", false},
	{"_", false},
	{" a", false},
	{".a", false},
	{"-a", false},
	{"_a", false},
	{"..", false},
	{"a", false},
	{"z", false},
	{"A", false},
	{"Z", false},
	{"0", false},
	{"9", false},
	{"-1", false},
	{"/", false},
	{"a/", false},
	{"a/../", false},
	{"../a", false},
	{"../../a", false},
	{"../../../a", false},
	{"foo/../bar", false},
	{"foo bar", false},
	{"a.", true},
	{"a..", true},
	{"aa", true},
	{"aa.", true},
	{"hello..world", true},
	{"hello/../world", false},
	{"aa1245124sadfasdfgasdga.", true},
	{"aAzZ0123456789_.-", true},
	{"abcdefghijklmnopqrstuvwxyz0123456789.-_", true},
	{"0123456789abcdefghijklmnopqrstuvwxyz.-_", true},
	{" abcdefghijklmnopqrstuvwxyz0123456789.-_", false},
	{".abcdefghijklmnopqrstuvwxyz0123456789.-_", false},
	{"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_", true},
	{"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ.-_", true},
	{" ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_", false},
	{".ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_", false},
	{"/a/b/c", false},
	{"a/b/c", false},
	{"foo/../../../etc/passwd", false},
	{"../../../../../../etc/motd", false},
	{"/etc/passwd", false},
}

ContainerIDTestData is a set of test data that lists valid and invalid Container IDs

Functions

func IsInGitHubActions

func IsInGitHubActions() bool

func MakeRuntimeConfigFileData

func MakeRuntimeConfigFileData(config RuntimeConfigOptions) string

func SetupOCIConfigFile

func SetupOCIConfigFile(t *testing.T) (rootPath string, bundlePath, ociConfigFile string)

func WriteOCIConfigFile

func WriteOCIConfigFile(spec specs.Spec, configPath string) error

WriteOCIConfigFile using spec to update OCI config file by path configPath

Types

type Constraint

type Constraint func(c *Constraints)

Constraint is a function that operates on a Constraints object to set particular values.

func NeedKernelVersion

func NeedKernelVersion(version string) Constraint

NeedKernelVersion will skip the test unless the kernel version is same as the specified version.

func NeedKernelVersionEquals

func NeedKernelVersionEquals(version string) Constraint

NeedKernelVersionEquals will skip the test unless the kernel version is same as the specified version.

func NeedKernelVersionGE

func NeedKernelVersionGE(version string) Constraint

NeedKernelVersionGE will skip the test unless the kernel version is newer than or the same as the specified version.

func NeedKernelVersionGT

func NeedKernelVersionGT(version string) Constraint

NeedKernelVersionGT will skip the test unless the kernel version is newer than the specified version.

func NeedKernelVersionLE

func NeedKernelVersionLE(version string) Constraint

NeedKernelVersionLE will skip the test unless the kernel version is older than or the same as the specified version.

func NeedKernelVersionLT

func NeedKernelVersionLT(version string) Constraint

NeedKernelVersionLT will skip the test unless the kernel version is older than the specified version.

func NeedKernelVersionNotEquals

func NeedKernelVersionNotEquals(version string) Constraint

NeedKernelVersionNotEquals will skip the test unless the kernel version is different to the specified version.

func NeedKernelVersionWithOp

func NeedKernelVersionWithOp(version string, op Operator) Constraint

NeedKernelVersionWithOp skips the test unless the kernel version constraint specified by the arguments is true.

func NeedNonRoot

func NeedNonRoot() Constraint

NeedNonRoot skips the test if running as the root user.

func NeedRoot

func NeedRoot() Constraint

NeedNonRoot skips the test unless running as root.

func NeedUID

func NeedUID(uid int, op Operator) Constraint

NeedUID skips the test unless running as a user with the specified user ID.

func WithIssue

func WithIssue(issue string) Constraint

WithIssue allows the specification of an issue URL.

Note that the issue is not checked for validity.

type Constraints

type Constraints struct {
	Issue string

	// KernelVersion is the version of a particular kernel.
	KernelVersion string

	UID int

	// Operator is the operator to apply to one of the constraints.
	Operator Operator

	// Not ideal: set when UID needs to be checked. This allows
	// a test for UID 0 to be detected.
	UIDSet bool
}

Constraints encapsulates all information about a test constraint.

type ContainerIDTestDataType

type ContainerIDTestDataType struct {
	ID    string
	Valid bool
}

ContainerIDTestDataType is a type used to test Container and Sandbox ID's.

type Operator

type Operator int

Operator represents an operator to apply to a test constraint value.

func (Operator) String

func (o Operator) String() (s string)

String converts the operator to a human-readable value.

type Result

type Result struct {
	// Details of the constraint
	// (human-readable result of testing for a Constraint).
	Description string

	// true if constraint was valid
	Success bool
}

Result is the outcome of a Constraint test

type RuntimeConfigOptions

type RuntimeConfigOptions struct {
	Hypervisor           string
	HypervisorPath       string
	DefaultGuestHookPath string
	KernelPath           string
	ImagePath            string
	RootfsType           string
	KernelParams         string
	MachineType          string
	LogPath              string
	BlockDeviceDriver    string
	BlockDeviceAIO       string
	SharedFS             string
	VirtioFSDaemon       string
	JaegerEndpoint       string
	JaegerUser           string
	JaegerPassword       string
	PFlash               []string
	HotPlugVFIO          config.PCIePort
	ColdPlugVFIO         config.PCIePort
	DefaultVCPUCount     uint32
	DefaultMaxVCPUCount  uint32
	DefaultMemSize       uint32
	DefaultMaxMemorySize uint64
	DefaultMsize9p       uint32
	DisableBlock         bool
	EnableIOThreads      bool
	DisableNewNetNs      bool
	HypervisorDebug      bool
	RuntimeDebug         bool
	RuntimeTrace         bool
	AgentDebug           bool
	AgentTrace           bool
	EnablePprof          bool
}

type TestConstraint

type TestConstraint struct {
	KernelVersion string

	// Optionally used to record an issue number that relates to the
	// constraint.
	Issue string

	// Used to record all passed and failed constraints in
	// human-readable form.
	Passed []Result
	Failed []Result

	Debug bool

	// Effective user ID of running test
	ActualEUID int
}

TestConstraint records details about test constraints.

func NewTestConstraint

func NewTestConstraint(debug bool) TestConstraint

NewKataTest creates a new TestConstraint object and is the main interface to the test constraints feature.

func (*TestConstraint) NotValid

func (tc *TestConstraint) NotValid(constraints ...Constraint) bool

NotValid checks if the specified list of constraints are all valid, returning true if any _fail_.

Notes:

  • Constraints are applied in the order specified.
  • A constraint type (user, kernel) can only be specified once.
  • If the function fails to determine whether it can check the constraints, it will panic. Since this is facility is used for testing, this seems like the best approach as it unburdens the caller from checking for an error (which should never be ignored).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL