katatestutils

package
v0.0.0-...-5cef3e7 Latest Latest
Warning

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

Go to latest
Published: May 21, 2020 License: Apache-2.0 Imports: 7 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 distro

Use the NeedDistro() constraint to skip a test unless running on a particular Linux distribution:

func TestOnlyRunOnUbuntu(t *testing.T) {

    if tc.NotValid(ktu.NeedDistro("ubuntu")) {
        t.Skip("skipping test as not running on ubuntu")
    }

    // Test code to run on Ubuntu only ...
}

Use the NeedDistroNotEquals() constraint to skip a test unless running on a Linux distribution other than the one specified:

func TestDontRunOnFedora(t *testing.T) {

    if tc.NotValid(ktu.NeedDistroNotEquals("fedora")) {
        t.Skip("skipping test as running on fedora")
    }

    // Test code to run on any distro apart from Fedora ...
}
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

This section is empty.

Functions

func MakeRuntimeConfigFileData

func MakeRuntimeConfigFileData(config RuntimeConfigOptions) string

Types

type Constraint

type Constraint func(c *Constraints)

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

func NeedDistro

func NeedDistro(distro string) Constraint

NeedDistro will skip the test unless running on the specified distro.

func NeedDistroEquals

func NeedDistroEquals(distro string) Constraint

NeedDistroEquals will skip the test unless running on the specified distro.

func NeedDistroNotEquals

func NeedDistroNotEquals(distro string) Constraint

NeedDistroNotEquals will skip the test unless run a distro that does not match the specified name.

func NeedDistroVersion

func NeedDistroVersion(version string) Constraint

NeedDistroVersion will skip the test unless running on the specified (exact) version of some distro.

Note: distro versions vary in format.

func NeedDistroVersionEquals

func NeedDistroVersionEquals(version string) Constraint

NeedDistroVersionEquals will skip the test unless the distro version is the same as the specified version.

Note: distro versions vary in format.

func NeedDistroVersionGE

func NeedDistroVersionGE(version string) Constraint

NeedDistroVersionGE will skip the test unless the distro version is newer than or the same as the specified version.

Note: distro versions vary in format.

func NeedDistroVersionGT

func NeedDistroVersionGT(version string) Constraint

NeedDistroVersionGT will skip the test unless the distro version is newer than the specified version.

Note: distro versions vary in format.

func NeedDistroVersionLE

func NeedDistroVersionLE(version string) Constraint

NeedDistroVersionLE will skip the test unless the distro version is older than or the same as the specified version.

Note: distro versions vary in format.

func NeedDistroVersionLT

func NeedDistroVersionLT(version string) Constraint

NeedDistroVersionLT will skip the test unless the distro version is older than the specified version.

Note: distro versions vary in format.

func NeedDistroVersionNotEquals

func NeedDistroVersionNotEquals(version string) Constraint

NeedDistroVersionNotEquals will skip the test unless the distro version is different to the specified version.

Note: distro versions vary in format.

func NeedDistroVersionWithOp

func NeedDistroVersionWithOp(version string, op Operator) Constraint

NeedDistroVersionWithOp skips the test unless the distro version constraint specified by the arguments is true.

Note: distro versions vary in format.

func NeedDistroWithOp

func NeedDistroWithOp(distro string, op Operator) Constraint

NeedDistroWithOp skips the test unless the distro constraint specified by the arguments is true.

func NeedKernelVersion

func NeedKernelVersion(version string) Constraint

NeedKernelVersion will skip the test unless running on the specified (exact) version of some distro.

Note: distro versions vary in format.

func NeedKernelVersionEquals

func NeedKernelVersionEquals(version string) Constraint

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

func NeedKernelVersionGE

func NeedKernelVersionGE(version string) Constraint

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

Note: distro versions vary in format.

func NeedKernelVersionGT

func NeedKernelVersionGT(version string) Constraint

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

Note: distro versions vary in format.

func NeedKernelVersionLE

func NeedKernelVersionLE(version string) Constraint

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

Note: distro versions vary in format.

func NeedKernelVersionLT

func NeedKernelVersionLT(version string) Constraint

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

Note: distro versions vary in format.

func NeedKernelVersionNotEquals

func NeedKernelVersionNotEquals(version string) Constraint

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

func NeedKernelVersionWithOp

func NeedKernelVersionWithOp(version string, op Operator) Constraint

NeedKernelVersionWithOp skips the test unless the distro 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

	UID int

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

	// DistroName is the name of a distro in all lower-case letters.
	DistroName string

	// DistroVersion is the version of the particular distro in string
	// format. It may contain periods and dashes.
	DistroVersion string

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

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

Constraints encapsulates all information about a test constraint.

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
	DefaultVCPUCount     uint32
	DefaultMaxVCPUCount  uint32
	DefaultMemSize       uint32
	DefaultMsize9p       uint32
	DefaultGuestHookPath string
	KernelPath           string
	ImagePath            string
	KernelParams         string
	MachineType          string
	ShimPath             string
	ProxyPath            string
	NetmonPath           string
	LogPath              string
	BlockDeviceDriver    string
	AgentTraceMode       string
	AgentTraceType       string
	SharedFS             string
	PCIeRootPort         uint32
	DisableBlock         bool
	EnableIOThreads      bool
	HotplugVFIOOnRootBus bool
	DisableNewNetNs      bool
	HypervisorDebug      bool
	RuntimeDebug         bool
	RuntimeTrace         bool
	ProxyDebug           bool
	ShimDebug            bool
	NetmonDebug          bool
	AgentDebug           bool
	AgentTrace           bool
}

type TestConstraint

type TestConstraint struct {
	Debug bool

	// Effective user ID of running test
	ActualEUID int

	DistroName    string
	DistroVersion string
	KernelVersion string

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

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

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, distro) 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