test

package
v0.0.0-...-331d270 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddReadyNode

func AddReadyNode(t *testing.T, clientset *fake.Clientset, name, ip string)

AddReadyNode adds a new Node with status "Ready" and the given name and IP.

func AddSomeReadyNodes

func AddSomeReadyNodes(t *testing.T, clientset *fake.Clientset, count int)

AddSomeReadyNodes create a number of new, ready Nodes.

  • name from 0 to count-1
  • ip from 0.0.0.0 to <count-1>.<count-1>.<count-1>.<count-1>

func ArgumentsMatchExpected

func ArgumentsMatchExpected(actualArgs []string, expectedArgs [][]string) error

ArgumentsMatchExpected returns a descriptive error if any of the expected arguments do not exist. This supports arguments in which flags appear multiple times with different values but does not support multiple instances of the same flag-value. This test is designed to fail if the list of actual arguments contains extra arguments not specified in the expected args. The expected arguments are given as an array of string arrays. This is to support flags which may have multiple values. Examples:

expectedArgs := [][]string{
    {"-h"},                              // test for a short flag
    {"-vvv"},                            // test for a short flag with value(s) specified
    {"-d", "3"},                         // test for a short flag with a value specified
    {"--verbose"},                       // test for a --bool flag
    {"--name=alex"},                     // test for a --flag=value flag
    {"--name", "sam"},                   // test for a --flag with a value after a space
    {"--full-name", "sam", "goodhuman"}, // test for a --flag with 2 values separated by spaces
}

func AssertLabelsContainRookRequirements

func AssertLabelsContainRookRequirements(t *testing.T, labels map[string]string, appName string)

AssertLabelsContainRookRequirements asserts that the labels under test contain the labels which all Rook pods should have. This can be used with labels for Kubernetes Deployments, DaemonSets, etc.

func FakeCustomisePodCreate

func FakeCustomisePodCreate(t *testing.T, clientset *fake.Clientset, name, ns string, label map[string]string)

func FakeOperatorPod

func FakeOperatorPod(ns string) *corev1.Pod

func FakeReplicaSet

func FakeReplicaSet(ns string) *appsv1.ReplicaSet

func GetEnv

func GetEnv(name string, envs []v1.EnvVar) (*v1.EnvVar, error)

GetEnv finds returns the env var with the given name from a list of env vars.

func HumanReadableVolume

func HumanReadableVolume(v *v1.Volume) string

HumanReadableVolume returns a string representation of a Kubernetes volume which is more compact and human readable than the default string go prints.

func HumanReadableVolumeMount

func HumanReadableVolumeMount(m *v1.VolumeMount) string

HumanReadableVolumeMount returns a string representation of a Kubernetes volume mount which is more compact and human readable than the default string go prints.

func HumanReadableVolumeMounts

func HumanReadableVolumeMounts(mounts []v1.VolumeMount) string

HumanReadableVolumeMounts returns a string representation of a list of Kubernetes volume mounts which is more compact and human readable than the default string go prints.

func HumanReadableVolumes

func HumanReadableVolumes(volumes []v1.Volume) string

HumanReadableVolumes returns a string representation of a list of Kubernetes volumes which is more compact and human readable than the default string go prints.

func New

func New(t *testing.T, nodes int) *fake.Clientset

New creates a fake K8s cluster with some nodes added.

func NewComplexClientset

func NewComplexClientset(t *testing.T) *fake.Clientset

NewComplexClientset is a reusable clientset for Rook unit tests that adds some complex behavior to the clientset to mimic more of what K8s does in the real world.

  • Generate a name for resources that have 'generateName' set and 'name' unset.

func PrependComplexJobReactor

func PrependComplexJobReactor(t *testing.T, clientset *fake.Clientset, assignPodToNode bool)

PrependComplexJobReactor adds a Job reactor with the below behavior. If more or different functionality than this is needed for a test, either make a custom Job reactor or add more optional behavior to this reactor.

  • When a Job is created, create the Pod for the Job based on the Job's Pod template
  • Created pod.Name = "[job name]-[pod name in job template]"
  • When a Job is deleted, delete the Pod for the Job (Pod delete will not be handled by reactors)
  • Pod create/delete is done to the clientset tracker, so no Pod watch events will register.
  • Optionally look through the clientset Nodes to randomly assign created Pods to a node.

func PrependFailReactor

func PrependFailReactor(t *testing.T, clientset *fake.Clientset, verb, resource string)

PrependFailReactor adds a reactor with the desired verb and resource that will report a failure.

func SetFakeKubernetesVersion

func SetFakeKubernetesVersion(clientset *fake.Clientset, semver string)

SetFakeKubernetesVersion sets a fake K8s version on the clientset. Version must be in semver format with a preceding "v" (e.g., "v1.13.2").

func VolumeExists

func VolumeExists(volumeName string, volumes []v1.Volume) error

VolumeExists returns a descriptive error if the volume does not exist.

func VolumeIsEmptyDir

func VolumeIsEmptyDir(volumeName string, volumes []v1.Volume) error

VolumeIsEmptyDir returns a descriptive error if the volume does not exist or is not an empty dir

func VolumeIsHostPath

func VolumeIsHostPath(volumeName, path string, volumes []v1.Volume) error

VolumeIsHostPath returns a descriptive error if the volume does not exist, is not a HostPath volume, or if the volume's path is not as expected.

func VolumeMountExists

func VolumeMountExists(mountName string, mounts []v1.VolumeMount) error

VolumeMountExists returns a descriptive error if the volume mount does not exist.

Types

type ContainersTester

type ContainersTester struct {
	// contains filtered or unexported fields
}

A ContainersTester is a helper exposing methods for testing required Rook specifications common for all Rook containers.

func NewContainersSpecTester

func NewContainersSpecTester(t *testing.T, cc []v1.Container) *ContainersTester

NewContainersSpecTester creates a new tester for the given container spec.

func (*ContainersTester) AssertArgReferencesMatchEnvVars

func (ct *ContainersTester) AssertArgReferencesMatchEnvVars()

AssertArgReferencesMatchEnvVars asserts that for each container under test, any references to Kubernetes environment variables (e.g., $(POD_NAME)), have an environment variable set to source the value.

func (*ContainersTester) AssertResourceSpec

func (ct *ContainersTester) AssertResourceSpec(expectations ResourceLimitExpectations)

AssertResourceSpec asserts that the container under test's resource limits/requests match the given (in string format) resource limits/requests.

func (*ContainersTester) RunFullSuite

func (ct *ContainersTester) RunFullSuite(resourceExpectations ResourceLimitExpectations)

RunFullSuite runs all assertion tests for the Containers under test.

type MountsSpec

type MountsSpec struct {
	// Moniker is a name given to the list to help identify it
	Moniker string
	Mounts  []v1.VolumeMount
}

MountsSpec is a struct which includes a list of Kubernetes volume mounts as well as additional metadata about the volume mount list for better identification during tests.

type PodSpecTester

type PodSpecTester struct {
	// contains filtered or unexported fields
}

A PodSpecTester is a helper exposing methods for testing required Ceph specifications common for all Ceph PodSpecs.

func NewPodSpecTester

func NewPodSpecTester(t *testing.T, spec *v1.PodSpec) *PodSpecTester

NewPodSpecTester creates a new tester to test the given PodSpec.

func (*PodSpecTester) AssertVolumesAndMountsMatch

func (ps *PodSpecTester) AssertVolumesAndMountsMatch()

AssertVolumesAndMountsMatch asserts that all of the volume mounts in the pod spec under test's containers have a volume which sources them. It also asserts that each volume is used at least once by any of the mounts in containers.

func (*PodSpecTester) Containers

func (ps *PodSpecTester) Containers() *ContainersTester

Containers creates a ContainersTester from a parent PodSpecTester. Because ContainersTester is intended to test the full list of containers (both init and run containers) in a PodSpec, this method is the only way of creating a ContainersTester.

func (*PodSpecTester) RunFullSuite

func (ps *PodSpecTester) RunFullSuite(resourceExpectations ResourceLimitExpectations)

RunFullSuite runs all assertion tests for the PodSpec under test and its sub-resources.

type PodTemplateSpecTester

type PodTemplateSpecTester struct {
	// contains filtered or unexported fields
}

A PodTemplateSpecTester is a helper exposing methods for testing required Rook specifications common for all Rook PodTemplateSpecs.

func NewPodTemplateSpecTester

func NewPodTemplateSpecTester(t *testing.T, template *v1.PodTemplateSpec) *PodTemplateSpecTester

NewPodTemplateSpecTester creates a new tester to test the given PodTemplateSpec

func (*PodTemplateSpecTester) AssertLabelsContainRookRequirements

func (pt *PodTemplateSpecTester) AssertLabelsContainRookRequirements(appName string)

AssertLabelsContainRookRequirements asserts that the PodTemplateSpec under test contains labels which all Rook pods should have.

func (*PodTemplateSpecTester) RunFullSuite

func (pt *PodTemplateSpecTester) RunFullSuite(
	appName string,
	resourceExpectations ResourceLimitExpectations,
)

RunFullSuite runs all assertion tests for the PodTemplateSpec under test and its sub-resources.

func (*PodTemplateSpecTester) Spec

Spec creates a PodSpecTester from a parent PodTemplateSpecTester.

type ResourceLimitExpectations

type ResourceLimitExpectations struct {
	CPUResourceLimit      string
	CPUResourceRequest    string
	MemoryResourceLimit   string
	MemoryResourceRequest string
}

ResourceLimitExpectations allows a test to define expectations for resource limits on containers. If any field is left as an empty string, that field will not be tested.

type VolumesAndMountsTestDefinition

type VolumesAndMountsTestDefinition struct {
	VolumesSpec     *VolumesSpec
	MountsSpecItems []*MountsSpec
}

VolumesAndMountsTestDefinition defines which volumes and mounts to test and what those values should be. The test is intended to be defined with VolumesSpec defined as a pod's volumes, and the list of MountsSpec items defined as the volume mounts from every container in the pod.

func (*VolumesAndMountsTestDefinition) TestMountsMatchVolumes

func (d *VolumesAndMountsTestDefinition) TestMountsMatchVolumes(t *testing.T)

TestMountsMatchVolumes tests two things: 1. That each volume mount in each every MountsSpec has a corresponding volume to source it in the VolumesSpec 2. That there are no extraneous volumes defined in the VolumesSpec that do not have a corresponding volume mount in any of the MountsSpec items

type VolumesSpec

type VolumesSpec struct {
	// Moniker is a name given to the list to help identify it
	Moniker string
	Volumes []v1.Volume
}

VolumesSpec is a struct which includes a list of Kubernetes volumes as well as additional metadata about the volume list for better identification during tests.

Jump to

Keyboard shortcuts

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