framework

package
v0.3.8 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2023 License: Apache-2.0 Imports: 61 Imported by: 15

Documentation

Overview

Provides builders and helper methods for preparing Pods and nested Containers

Index

Constants

View Source
const (
	NamespaceCleanupTimeout = 2 * time.Minute
)
View Source
const (
	// Poll is how often to Poll pods
	Poll = 2 * time.Second
)

Variables

View Source
var (
	RetryInterval        = time.Second * 5
	Timeout              = time.Second * 600
	CleanupRetryInterval = time.Second * 1
	CleanupTimeout       = time.Second * 5
)
View Source
var (
	RunID = uuid.NewUUID()
)

Functions

func AfterReadingAllFlags

func AfterReadingAllFlags(t *TestContextType)

AfterReadingAllFlags makes changes to the context after all flags have been read.

func ContainsAll

func ContainsAll(model, target []interface{}) bool

ContainsAll can be used to compare two sorted slices and validate both are not nil and all elements from given model are present on the target instance.

func DeleteNamespaces

func DeleteNamespaces(c clientset.Interface, deleteFilter, skipFilter []string) ([]string, error)

DeleteNamespaces deletes all namespaces that match the given delete and skip filters. Filter is by simple strings.Contains; first skip filter, then delete filter. Returns the list of deleted namespaces or an error.

func ExpectError

func ExpectError(err error, explain ...interface{})

ExpectError expects an error happens, otherwise an exception raises

func ExpectNoError

func ExpectNoError(err error, explain ...interface{})

ExpectNoError checks if "err" is set, and if so, fails assertion while logging the error.

func ExpectNoErrorWithOffset

func ExpectNoErrorWithOffset(offset int, err error, explain ...interface{})

ExpectNoErrorWithOffset checks if "err" is set, and if so, fails assertion while logging the error at "offset" levels above its caller (for example, for call chain f -> g -> ExpectNoErrorWithOffset(1, ...) error would be logged for "f").

func ExpectNoErrorWithRetries

func ExpectNoErrorWithRetries(fn func() error, maxRetries int, explain ...interface{})

ExpectNoErrorWithRetries checks if an error occurs with the given retry count.

func FromInts

func FromInts(m []int) []interface{}

FromInts returns an interface array with sorted elements from int array

func FromStrings

func FromStrings(m []string) []interface{}

FromStrings returns an interface array with sorted elements from string array

func GetPorts

func GetPorts(service corev1.Service) []int

GetPorts returns an int slice with all ports exposed by the provided corev1.Service object

func HandleFlags

func HandleFlags()

HandleFlags sets up all flags and parses the command line.

func Int32Ptr

func Int32Ptr(i int32) *int32

func IsRetryFailure

func IsRetryFailure(err error) bool

func KubectlCmd

func KubectlCmd(ctxData ContextData, args ...string) *exec.Cmd

KubectlCmd runs the kubectl executable through the wrapper script.

func LoadClientset

func LoadClientset() (*clientset.Clientset, error)

LoadClientset returns clientset for connecting to kubernetes clusters.

func LoadConfig

func LoadConfig() (*restclient.Config, error)

LoadConfig returns a config for a rest client.

func LoadYamlFromUrl added in v0.1.2

func LoadYamlFromUrl(url string) (*unstructured.Unstructured, error)

func LookForRegexp

func LookForRegexp(expectedRegexp string, timeout time.Duration, fn func() string) (result string, err error)

LookForRegexp looks for the given regexp in results from given "func() string"

func LookForRegexpInLog

func LookForRegexpInLog(ctxData ContextData, podName, container, expectedRegexp string, timeout time.Duration) (result string, err error)

LookForRegexpInLog looks for the given regexp in the log of a specific pod container

func LookForString

func LookForString(expectedString string, timeout time.Duration, fn func() string) (result string, err error)

LookForString looks for the given string in the output of fn, repeatedly calling fn until the timeout is reached or the string is found. Returns last log and possibly error if the string was not found. TODO(alejandrox1): move to pod/ subpkg once kubectl methods are refactored.

func LookForStringInLog

func LookForStringInLog(ctxData ContextData, podName, container, expectedString string, timeout time.Duration) (result string, err error)

LookForStringInLog looks for the given string in the log of a specific pod container

func RegisterFlags

func RegisterFlags()

RegisterFlags registers flags for e2e test suites.

func RemoveCleanupAction

func RemoveCleanupAction(action ActionType, p CleanupActionHandle)

RemoveCleanupAction removes a function that was installed by AddCleanupAction.

func RestclientConfig

func RestclientConfig(kubeContext string) (*clientcmdapi.Config, error)

RestclientConfig returns a config holds the information needed to build connection to kubernetes clusters.

func Retry

func Retry(interval time.Duration, maxRetries int, f ConditionFunc) error

Retry retries f every interval until after maxRetries. The interval won't be affected by how long f takes. For example, if interval is 3s, f takes 1s, another f will be called 2s later. However, if f takes longer than interval, it will be delayed.

func RetryWithContext

func RetryWithContext(ctx context.Context, interval time.Duration, f ConditionFunc) error

RetryWithContext retries f every interval until the specified context times out.

func RunCleanupActions

func RunCleanupActions(action ActionType)

RunCleanupActions runs all functions installed by AddCleanupAction. It does not remove them (see RemoveCleanupAction) but it does run unlocked, so they may remove themselves.

func RunKubectl

func RunKubectl(ctxData ContextData, args ...string) (string, error)

RunKubectl is a convenience wrapper over kubectlBuilder

func RunKubectlOrDie

func RunKubectlOrDie(ctxData ContextData, args ...string) string

RunKubectlOrDie is a convenience wrapper over kubectlBuilder

func WaitForDaemonSet

func WaitForDaemonSet(kubeclient kubernetes.Interface, namespace, name string, count int, retryInterval, timeout time.Duration) error

func WaitForDeletion

func WaitForDeletion(dynclient client.Client, obj client.Object, retryInterval, timeout time.Duration) error

func WaitForDeployment

func WaitForDeployment(kubeclient kubernetes.Interface, namespace, name string, replicas int, retryInterval, timeout time.Duration) error

func WaitForDeploymentDeleted

func WaitForDeploymentDeleted(ctx context.Context, kubeclient kubernetes.Interface, namespace, name string) error

func WaitForNamespacesDeleted

func WaitForNamespacesDeleted(c clientset.Interface, namespaces []string, timeout time.Duration) error

WaitForNamespacesDeleted waits for the namespaces to be deleted.

func WaitForStatefulSet

func WaitForStatefulSet(kubeclient kubernetes.Interface, namespace, name string, count int, retryInterval, timeout time.Duration) error

func WaitForStatefulSetCreation added in v0.2.6

func WaitForStatefulSetCreation(kubeclient kubernetes.Interface, namespace, name string, retryInterval, timeout time.Duration) error

func WaitForStatefulSetReady added in v0.2.6

func WaitForStatefulSetReady(kubeclient kubernetes.Interface, namespace, name string, count int, retryInterval, timeout time.Duration) error

Types

type ActionType

type ActionType int

Action type to be enqueued

const (
	AfterEach ActionType = iota
	AfterSuite
)

type Builder

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

Framework Builder type

func NewFrameworkBuilder

func NewFrameworkBuilder(baseName string) Builder

Helper for building frameworks with possible customizations

func (Builder) Build

func (b Builder) Build() *Framework

Generates and initialize the Framework

func (Builder) GetContexts added in v0.2.4

func (b Builder) GetContexts() []string

func (Builder) IsOpenshift added in v0.1.6

func (b Builder) IsOpenshift(isIt bool) Builder

Sets if the framework would create namespaces or projects

func (Builder) WithBuilders

func (b Builder) WithBuilders(builders ...operators.OperatorSetupBuilder) Builder

Customize builders, by default when "BeforeEach" runs, the Framework iterates through all supported operators (from SupportedOperators map) and initializes all the default builder instances.

func (Builder) WithContexts

func (b Builder) WithContexts(contexts ...string) Builder

Customize contexts to use (default is the current-context only)

func (Builder) WithGlobalOperator added in v0.2.4

func (b Builder) WithGlobalOperator(globalOperator bool) Builder

type CleanupActionHandle

type CleanupActionHandle *int

CleanupActionHandle is an integer pointer type for handling cleanup action

func AddCleanupAction

func AddCleanupAction(action ActionType, fn func()) CleanupActionHandle

AddCleanupAction installs a function that will be called in the event of completion of a test Spec or a test Suite. This allows arbitrary pieces of the overall test to hook into AfterEach() and SynchronizedAfterSuite().

type ClientSet

type ClientSet struct {
	KubeClient clientset.Interface
	ExtClient  apiextension.Interface
	DynClient  dynamic.Interface
	OcpClient  ocpClient
}

type ConditionFunc

type ConditionFunc func() (bool, error)

type ConfigMapData

type ConfigMapData struct {
	Name string
	Data string
}

ConfigMapData makes it simpler to provide multiple data elements for a new ConfigMap

type ContainerBuilder

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

ContainerBuilder

func NewContainerBuilder

func NewContainerBuilder(name string, image string) *ContainerBuilder

NewContainerBuilder creates an instance of a ContainerBuilder helper

func (*ContainerBuilder) AddArgs

func (cb *ContainerBuilder) AddArgs(args ...string) *ContainerBuilder

AddArgs appends a given list of arguments to the existing

func (*ContainerBuilder) AddVolumeMountConfigMapData

func (cb *ContainerBuilder) AddVolumeMountConfigMapData(volumeName string, mountPath string, readOnly bool) *ContainerBuilder

AddVolumeMountConfigMapData add a VolumeMount entry to the container that must be related with a valid Volume defined in the Pod Spec.

func (*ContainerBuilder) Build

func (cb *ContainerBuilder) Build() v1.Container

Build returns the prepared Container to be used within a Pod

func (*ContainerBuilder) EnvVar

func (cb *ContainerBuilder) EnvVar(variable, value string) *ContainerBuilder

EnvVar sets an environment variable into the container

func (*ContainerBuilder) ImagePullPolicy

func (cb *ContainerBuilder) ImagePullPolicy(policy string) *ContainerBuilder

ImagePullPolicy sets the ImagePullPolicy for the given container. Default is PullAlways.

func (*ContainerBuilder) WithCommands

func (cb *ContainerBuilder) WithCommands(commands ...string) *ContainerBuilder

WithCommands set the list of commands to use with the new container

type ContextData

type ContextData struct {
	Id        string
	Clients   ClientSet
	Namespace string

	// Set together with creating the ClientSet and the namespace.
	// Guaranteed to be unique in the cluster even when running the same
	// test multiple times in parallel.
	UniqueName         string
	CertManagerPresent bool // if crd is detected
	OperatorMap        map[operators.OperatorType]operators.OperatorSetup

	EventHandler  events.EventHandler
	ServerVersion string
	// contains filtered or unexported fields
}

ContextData holds clients and data related with namespaces

created within

func (*ContextData) AddNamespacesToDelete

func (c *ContextData) AddNamespacesToDelete(namespaces ...*corev1.Namespace)

func (*ContextData) AddProjectsToDelete added in v0.1.6

func (c *ContextData) AddProjectsToDelete(projects ...*openapiv1.Project)

func (*ContextData) CreateConfigMapData

func (c *ContextData) CreateConfigMapData(name string, data ...ConfigMapData) (*v1.ConfigMap, error)

CreateConfigMapData helper method to generate a ConfiMap using configuration Data

func (*ContextData) CreateNamespace

func (c *ContextData) CreateNamespace(clientSet *clientset.Clientset,
	baseName string, labels map[string]string) *corev1.Namespace

CreateNamespace creates a namespace for e2e testing.

func (*ContextData) CreateProject added in v0.1.6

func (c *ContextData) CreateProject(clientSet *projectv1.Clientset,
	baseName string, labels map[string]string) *openapiv1.Project

func (*ContextData) CreateResource added in v0.1.2

func (c *ContextData) CreateResource(resourceType ResourceType, obj *unstructured.Unstructured, options v1.CreateOptions, subresources ...string) (*unstructured.Unstructured, error)

CreateResource creates a resource based on provided (known) resource type and unstructured data

func (*ContextData) CreateResourceGroupVersion added in v0.1.2

func (c *ContextData) CreateResourceGroupVersion(gv schema.GroupVersionResource, obj *unstructured.Unstructured, options v1.CreateOptions, subresources ...string) (*unstructured.Unstructured, error)

func (*ContextData) DeleteNamespace

func (c *ContextData) DeleteNamespace(ns *corev1.Namespace) []error

func (*ContextData) DeleteProject added in v0.1.6

func (c *ContextData) DeleteProject(prj *openapiv1.Project) []error

func (*ContextData) DeleteResource added in v0.1.2

func (c *ContextData) DeleteResource(resourceType ResourceType, name string, options v1.DeleteOptions, subresources ...string) error

DeleteResource deletes a resource based on provided (known) resource type and name

func (*ContextData) DeleteResourceGroupVersion added in v0.1.2

func (c *ContextData) DeleteResourceGroupVersion(gv schema.GroupVersionResource, name string, options v1.DeleteOptions, subresources ...string) error

func (*ContextData) GenerateNamespace

func (c *ContextData) GenerateNamespace() (*corev1.Namespace, error)

GenerateNamespace creates a namespace with a random name.

func (*ContextData) GenerateProject added in v0.1.6

func (c *ContextData) GenerateProject() (*openapiv1.Project, error)

func (*ContextData) GetDaemonSet

func (c *ContextData) GetDaemonSet(name string) (*appsv1.DaemonSet, error)

func (*ContextData) GetDeployment

func (c *ContextData) GetDeployment(name string) (*appsv1.Deployment, error)

func (*ContextData) GetLogs

func (c *ContextData) GetLogs(podName string) (string, error)

func (*ContextData) GetLogsFromNamespace added in v0.3.5

func (c *ContextData) GetLogsFromNamespace(podName string, ns string) (string, error)

returns whole pod log as a (meaty) string

func (*ContextData) GetPodName added in v0.1.4

func (c *ContextData) GetPodName(label string) (string, error)

func (*ContextData) GetPodNameFromNamespace added in v0.3.5

func (c *ContextData) GetPodNameFromNamespace(label string, ns string) (string, error)

func (*ContextData) GetResource

func (c *ContextData) GetResource(resourceType ResourceType, name string) (*unstructured.Unstructured, error)

GetResource returns the given resource type, identified by its given name

func (*ContextData) GetResourceGroupVersion added in v0.1.2

func (c *ContextData) GetResourceGroupVersion(gv schema.GroupVersionResource, name string) (*unstructured.Unstructured, error)

func (*ContextData) GetSecret

func (c *ContextData) GetSecret(name string) (*corev1.Secret, error)

func (*ContextData) GetService

func (c *ContextData) GetService(name string) (*corev1.Service, error)

func (*ContextData) IsOpenShift

func (c *ContextData) IsOpenShift() bool

func (*ContextData) ListPodsForDeployment

func (c *ContextData) ListPodsForDeployment(deployment *appsv1.Deployment) (*corev1.PodList, error)

func (*ContextData) ListPodsForDeploymentName

func (c *ContextData) ListPodsForDeploymentName(name string) (*corev1.PodList, error)

func (*ContextData) ListResources

func (c *ContextData) ListResources(resourceType ResourceType) (*unstructured.UnstructuredList, error)

ListResources returns a list of resources found in the related Framework's namespace, for the given resource type

func (*ContextData) ListResourcesGroupVersion added in v0.1.2

func (c *ContextData) ListResourcesGroupVersion(gv schema.GroupVersionResource) (*unstructured.UnstructuredList, error)

func (*ContextData) WaitForPodStatus added in v0.1.2

func (c *ContextData) WaitForPodStatus(podName string, status v1.PodPhase, timeout time.Duration, interval time.Duration) (*v1.Pod, error)

func (*ContextData) WaitForService added in v0.1.2

func (c *ContextData) WaitForService(name string, timeout time.Duration, interval time.Duration) (*corev1.Service, error)

type Framework

type Framework struct {
	BaseName string

	// Map that ties clients and namespaces for each available context
	ContextMap            map[string]*ContextData
	IsOpenshift           bool // Namespace/Project
	SkipNamespaceCreation bool // Whether to skip creating a namespace
	// contains filtered or unexported fields
}

func (*Framework) AfterEach

func (f *Framework) AfterEach()

AfterEach deletes the namespace, after reading its events.

func (*Framework) AfterSuite

func (f *Framework) AfterSuite()

AfterSuite deletes the cluster level resources

func (*Framework) BeforeEach

func (f *Framework) BeforeEach(contexts ...string)

BeforeEach gets clients and makes a namespace

func (*Framework) Execute added in v0.3.1

func (f *Framework) Execute(ctx1 *ContextData, command string, arguments []string, podname string) (string, string, error)

func (*Framework) GetConfig added in v0.2.4

func (f *Framework) GetConfig() rest.Config

func (*Framework) GetFirstContext

func (f *Framework) GetFirstContext() *ContextData

GetFirstContext returns the first entry in the ContextMap or nil if none

func (*Framework) SetOperatorBuilders

func (f *Framework) SetOperatorBuilders(builders ...operators.OperatorSetupBuilder)

Defines a custom set of builders for the given Framework instance

func (*Framework) Setup

func (f *Framework) Setup() error

func (*Framework) TeardownEach

func (f *Framework) TeardownEach() error

func (*Framework) TeardownSuite

func (f *Framework) TeardownSuite() error

type KubectlBuilder

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

KubectlBuilder is used to build, customize and execute a kubectl Command. Add more functions to customize the builder as needed.

func NewKubectlCommand

func NewKubectlCommand(ctxData ContextData, args ...string) *KubectlBuilder

NewKubectlCommand returns a KubectlBuilder for running kubectl.

func NewKubectlCommandTimeout

func NewKubectlCommandTimeout(ctxData ContextData, timeout time.Duration, args ...string) *KubectlBuilder

NewKubectlCommandTimeout returns a KubectlBuilder with a timeout defined, for running kubectl.

func NewKubectlExecCommand

func NewKubectlExecCommand(ctxData ContextData, pod string, timeout time.Duration, commandArgs ...string) *KubectlBuilder

NewKubectlExecCommand returns a KubectlBuilder prepared to execute a given command in a running pod.

func (KubectlBuilder) Exec

func (b KubectlBuilder) Exec() (string, error)

Exec runs the kubectl executable.

func (KubectlBuilder) ExecOrDie

func (b KubectlBuilder) ExecOrDie() string

ExecOrDie runs the kubectl executable or dies if error occurs.

type PodBuilder

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

PodBuilder

func NewPodBuilder

func NewPodBuilder(name string, namespace string, serverVersion string) *PodBuilder

NewPodBuilder Creates an instance of a PodBuilder helper

func (*PodBuilder) AddConfigMapVolumeSource

func (p *PodBuilder) AddConfigMapVolumeSource(name string, configMapName string) *PodBuilder

AddConfigMapVolumeSource append a Volume with a local reference to a ConfigMap into the Pod Spec

func (*PodBuilder) AddContainer

func (p *PodBuilder) AddContainer(c v1.Container) *PodBuilder

AddContainer adds a container to the Pod being prepared

func (*PodBuilder) AddLabel

func (p *PodBuilder) AddLabel(key, value string) *PodBuilder

AddLabel Adds or replaces the given label key and value to Pod

func (*PodBuilder) Build

func (p *PodBuilder) Build() *v1.Pod

Build returns the prepared Pod instance

func (*PodBuilder) RestartPolicy

func (p *PodBuilder) RestartPolicy(policy string) *PodBuilder

RestartPolicy defines the RestartPolicy of the Pod. Default is Never.

type ResourceType

type ResourceType int
const (
	Issuers ResourceType = iota
	Certificates
	Deployments
)

type RetryError

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

func (*RetryError) Error

func (e *RetryError) Error() string

type TestContextType

type TestContextType struct {
	KubeConfig               string
	KubeContexts             contextNames
	CertDir                  string
	Host                     string
	RepoRoot                 string
	KubectlPath              string
	OutputDir                string
	ReportDir                string
	ReportPrefix             string
	Prefix                   string
	DeleteNamespace          bool
	DeleteNamespaceOnFailure bool
	CleanStart               bool
}

TestContextType contains test settings and global state.

var TestContext TestContextType

TestContext should be used by all tests to access validation context data.

func (TestContextType) ContextsAvailable

func (t TestContextType) ContextsAvailable() int

ContextsAvailable returns the number of contexts available after parsing command line arguments.

func (TestContextType) GetContexts

func (t TestContextType) GetContexts() []string

GetContexts returns a list of contexts from provided flags or the current-context if none. If KubeConfig not provided or not generated, it returns nil.

Directories

Path Synopsis
Package ginkgowrapper wraps Ginkgo Fail and Skip functions to panic with structured data instead of a constant string.
Package ginkgowrapper wraps Ginkgo Fail and Skip functions to panic with structured data instead of a constant string.

Jump to

Keyboard shortcuts

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