cluster

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2024 License: Apache-2.0 Imports: 38 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ControllerLabel base.TestCaseLabel = "controller"
	WebhookLabel    base.TestCaseLabel = "webhook"
	WebServiceLabel base.TestCaseLabel = "webservice"
	CliLabel        base.TestCaseLabel = "cli"
)

Variables

This section is empty.

Functions

func ConvertToAuthSecret

func ConvertToAuthSecret(auth client.Auth) (secret corev1.Secret)

ConvertToAuthSecret convert auth

func ExpectKubeObject added in v0.11.0

func ExpectKubeObject(ctx *TestContext, object ctrlclient.Object, cleanFuncs ...func(object interface{}) interface{}) Assertion

ExpectKubeObject expect kubernetes resource exists by object.name and object.namespace, clean some fields in object and return gomega.Expect for this object. the param object should contains Namespace and name. you can use cleanFuncs to clean some fileds for object that load from kubernetes eg.

ExpectKubeObject(&TestContext{Client: clt, Namespace: "default"}, &corev1.ConfigMap{
	ObjectMeta: metav1.ObjectMeta{
		Namespace: "default",
		Name:      "config-1",
	},
}).Should(testing.DiffEqualTo(&corev1.ConfigMap{
	ObjectMeta: metav1.ObjectMeta{
		Namespace: "default",
		Name:      "config-1",
	},
	Data: map[string]string{
		"a": "a",
		"b": "b",
	},
}))

func FromSharedScheme

func FromSharedScheme(ctx context.Context) *runtime.Scheme

FromSharedScheme get scheme from context

func GetConfigFromContext

func GetConfigFromContext(ctx context.Context) interface{}

GetConfigFromContext get the config information from context

func GetPluginAddress

func GetPluginAddress(client *resty.Client, plugin client.Interface) (address *duckv1.Addressable, err error)

GetPluginAddress get the base url of the plugin

func LoadKubeResourcesToCtx added in v0.11.0

func LoadKubeResourcesToCtx(ctx *TestContext, file string, converts ...testing.ConvertRuntimeObjctToClientObjectFunc) (err error)

LoadKubeResourcesToCtx will load kubernetes resources from file it will use client in ctx and set namespace as ctx.Namespace if namespace is empty in file if you set namespace in file, it will just use it and ignore ctx.Namespace you can set converts to change some extra fields before create in kubernetes but you must pay attention to that when you set more than one converts: just one converts will be executed when match if you set converts , you should set namespace to ctx.Namespace explicitly eg.

LoadKubeResources(ctx, file)// load resource from file and apply to kubernetes with ctx.Namespace and ctx.Client

// load resource from file and execute convert if resource type is configmap before apply to kubernetes with ctx.Namespace and ctx.Client

LoadKubeResources(ctx, file,func(object runtime.Object) (ctrlclient.Object, error) {
	configmap, ok := object.(*corev1.ConfigMap)
	if !ok {
		return nil, fmt.Errorf("Unsupported gvk: %s", object.GetObjectKind().GroupVersionKind())
	}
	configmap.Namespace = "default-e2e"
	configmap.Annotations = map[string]string{
		"a": "1",
	}
	return configmap, nil
})

func MustGetPluginAddress

func MustGetPluginAddress(client *resty.Client, plugin client.Interface) (address *duckv1.Addressable)

MustGetPluginAddress get the base url or panics if the parse fails.

func MustLoadKubeResourcesToCtx added in v0.11.0

func MustLoadKubeResourcesToCtx(ctx *TestContext, file string, converts ...testing.ConvertRuntimeObjctToClientObjectFunc)

MustLoadKubeResourcesToCtx similar with LoadKubeResources but panic if LoadKubeResources error

func MustRollback

func MustRollback(testCtx *TestContext, obj client.Object, opts ...client.DeleteOption)

MustRollback delete an object and wait for completion

func NewConfigCondition

func NewConfigCondition(configName string, obj interface{}) *configCondition

NewConfigCondition construct an configCondition object `obj` is a pointer which used to unmarshal configuration to

func NewTestConfigMap

func NewTestConfigMap(name, namespace string, data map[string]string) *v1.ConfigMap

NewTestConfigMap helper function for constructing a new configmap

func PluginService

func PluginService(ctx context.Context, clt client.Client, name string) (*corev1.Service, error)

PluginService Get plugin service by plugin name

func ReportObjectListWithOptions added in v0.12.0

func ReportObjectListWithOptions(ctx *TestContext, list ctrlclient.ObjectList, opts ...ctrlclient.ListOption)

ReportObjectListWithOptions dumps the provided object list into the current Ginkgo spec report if the test case has failed. It allows passing client list options. Example:

JustAfteEach(func() {
  ReportObjectListWithOptions(ctx, &corev1.ConfigMapList{}, client.InNamespace(ctx.Namespace))
})

When using testing.ListByGVK:

JustAfteEach(func() {
  ReportObjectListWithOptions(ctx, ListByGVK(schema.GroupVersionKind{
    Group:   "katanomi.dev",
    Version: "v1alpha1",
    Kind:    "ClusterRuleList",
  }), client.InNamespace(ctx.Namespace))
})

func ReportObjectsLists added in v0.12.0

func ReportObjectsLists(ctx *TestContext, objects ...ctrlclient.ObjectList)

ReportObjectsLists will dump objects into Current Ginkgo Report when test case failed. Example:

JustAfteEach(func() {
  ReportObjectsLists(ctx, &corev1.ConfigMapList{}, &corev1.PodList{})
})

func RequestWrapAuthMeta

func RequestWrapAuthMeta(req *resty.Request, auth client.Auth, meta client.Meta) *resty.Request

RequestWrapAuthMeta wrap auth and meta information into http request

func ResourceTopConditionIsReadyEventually

func ResourceTopConditionIsReadyEventually(ctx *TestContext, obj client.Object, readyFuncs ...IsReadyCheckFunc) func(g Gomega) error

ResourceTopConditionIsReadyEventually generic function to check if a resource is ready resource type must implement GetTopLevelCondition() *apis.Condition function

func ShareScheme

func ShareScheme(scheme *runtime.Scheme) base.SharedExtension

ShareScheme to construct extension to share scheme

func SharedClient added in v0.11.0

func SharedClient() base.SharedExtension

SharedClient to construct extension to share client

func WaitRollback

func WaitRollback(testCtx *TestContext, obj client.Object)

WaitRollback Wait for the delete object behavior to complete

func WithSharedScheme

func WithSharedScheme(ctx context.Context, scheme *runtime.Scheme) context.Context

WithSharedScheme wrap scheme into context

Types

type Condition

type Condition interface {
	Condition(testCtx *TestContext) error
}

func NewNamespaceCondition

func NewNamespaceCondition(ns string) Condition

NewNamespaceCondition construct a namespace condition

type ConditionFunc

type ConditionFunc func(testCtx *TestContext) error

ConditionFunc helper function to wrapping condition

func (ConditionFunc) Condition

func (c ConditionFunc) Condition(testCtx *TestContext) error

Condition implement the Condition interface

type IsReadyCheckFunc

type IsReadyCheckFunc func(g Gomega, obj client.Object) error

IsReadyCheckFunc additional function to check if object is ready in an Eventually context

 example:
   ```
   func(g Gomega, obj client.Object) error {
	  if obj.GetName() == "" {
		fmt.Errorf("should have a name")
	  }
      // or use g.Expect instead
      g.Expect(obj.GetName()).To(Equal(""))
	  return nil
   }
   ```

type NamespaceCondition

type NamespaceCondition struct {
	Namespace string
}

NamespaceCondition generate namespace for testing

func (*NamespaceCondition) Condition

func (t *NamespaceCondition) Condition(testCtx *TestContext) error

Condition implement the Condition interface Delete the namespace when it already exists, then create a new one. After the testing is completed, delete the namespace as well.

type Poller

type Poller struct {
	Interval time.Duration
	Timeout  time.Duration
}

Poller describe the configuration of Poller Interval: Indicates the inspection interval Timeout: Indicates the maximum time the check will last

func (Poller) Settings

func (p Poller) Settings() (interval, timeout time.Duration)

Settings get the configuration Default values will be used if no custom settings are made

type ReconcileCondition

type ReconcileCondition struct {
	*Poller
	// contains filtered or unexported fields
}

ReconcileCondition check if a controller is available

func NewReconcileCondition

func NewReconcileCondition(obj client.Object, objCheckFun func(obj client.Object) bool) *ReconcileCondition

NewReconcileCondition helper function for constructing a `ReconcileCondition` object

func (*ReconcileCondition) Condition

func (n *ReconcileCondition) Condition(testCtx *TestContext) error

Condition implement the Condition interface Apply an object and wait for reconciliation, then check the status via `objCheckFun`

func (*ReconcileCondition) WithPoller

func (n *ReconcileCondition) WithPoller(interval, timeout time.Duration) *ReconcileCondition

WithPoller customize poller settings

type ServicePortForward

type ServicePortForward struct {
	Namespace         string
	ServiceName       string
	ServicePortFilter func(ports []v1.ServicePort) int32
	// contains filtered or unexported fields
}

ServicePortForward describe the configuration of port forwarding

func NewServicePortForward

func NewServicePortForward(ns string, serviceName string) *ServicePortForward

NewServicePortForward construct a new ServicePortForward object

func (*ServicePortForward) Close

func (s *ServicePortForward) Close()

Close will stop the forwarder

func (*ServicePortForward) Forward

func (s *ServicePortForward) Forward(testCtx *TestContext) (restyClient *resty.Client, err error)

Forward setting a forwarder

type TestCaseBuilder

type TestCaseBuilder struct {

	// Conditions condition list which will be checked before test case execution
	Conditions []Condition

	// Scope defines what kind of permissions this test case needs
	Scope TestCaseScope

	// TestContextOptions used to setup TestContext
	TestContextOptions []TestContextOption

	// Scheme the scheme for initializing the k8s client
	Scheme *runtime.Scheme

	// ObjectLists the list of objects to be reported in the test case
	// whenever a test case fails
	ObjectLists []client.ObjectList
	// contains filtered or unexported fields
}

func NewTestCaseBuilder

func NewTestCaseBuilder(baseBuilder base.TestCaseBuilder) *TestCaseBuilder

func (*TestCaseBuilder) Do

func (b *TestCaseBuilder) Do() bool

Do build and return the test case

func (*TestCaseBuilder) DoFunc

func (b *TestCaseBuilder) DoFunc(f TestSpecFunc) bool

DoFunc build and return the test case, just like the Do function

func (*TestCaseBuilder) DoWithContext added in v0.11.0

func (b *TestCaseBuilder) DoWithContext(ctx context.Context) bool

DoWithContext runs the test case builder with the given context. It sets up the test context, checks test conditions, adds object reporting, and runs the test spec function if provided.

func (*TestCaseBuilder) WithClusterScope

func (b *TestCaseBuilder) WithClusterScope() *TestCaseBuilder

WithClusterScope set the scope of the testcase as a cluster scoped

func (*TestCaseBuilder) WithCondition

func (b *TestCaseBuilder) WithCondition(funcs ...Condition) *TestCaseBuilder

WithCondition sets conditions

func (*TestCaseBuilder) WithFunc

func (b *TestCaseBuilder) WithFunc(tc TestSpecFunc) *TestCaseBuilder

WithFunc replaces the function with another given function

func (*TestCaseBuilder) WithNamespacedScope

func (b *TestCaseBuilder) WithNamespacedScope() *TestCaseBuilder

WithNamespacedScope set the scope of the testcase as namespaced

func (*TestCaseBuilder) WithObjectListReport added in v0.12.0

func (b *TestCaseBuilder) WithObjectListReport(objectList ...client.ObjectList) *TestCaseBuilder

WithObjectListReport will add a list of objects to be reported just after each test case if the test case fails. Can be used with testing.ListByGVK() to quickly create an ObjectList. Example: var _ = P0Case("test case").

Cluster().
WithObjectListReport(testing.ListByGVK(schema.GroupVersionKind{
  Group:   "tekton.dev",
  Version: "v1beta1",
  Kind:    "PipelineRunList",
}, &corev1.ConfigMapList{}).DoWithContext(ctx)

Note that multiple ObjectList can be added to the same test case.

func (*TestCaseBuilder) WithScheme

func (b *TestCaseBuilder) WithScheme(scheme *runtime.Scheme) *TestCaseBuilder

WithScheme adds a scheme object to the framework

func (*TestCaseBuilder) WithTestContextOptions

func (b *TestCaseBuilder) WithTestContextOptions(options ...TestContextOption) *TestCaseBuilder

WithTestContextOptions sets options of TestContext

type TestCaseScope

type TestCaseScope string

TestCaseScope scope for test case

const (
	// ClusterScoped cluster test case scope
	ClusterScoped TestCaseScope = "Cluster"
	// NamespaceScoped test case scoped for a Namespace
	NamespaceScoped TestCaseScope = "Namespaced"
)

type TestConfig

type TestConfig struct {
	Namespace string
	Name      string
	// contains filtered or unexported fields
}

TestConfig Provide a unified way to get configuration from configmap

func NewE2EConfig

func NewE2EConfig(cmName string) *TestConfig

NewE2EConfig construct a `TestConfig` with special configmap name

func (*TestConfig) GetConfig

func (c *TestConfig) GetConfig(ctx context.Context, clt client.Client) (string, error)

GetConfig get configuration by specified client

type TestContext

type TestContext struct {
	base.TestContext

	Config    *rest.Config
	Scheme    *runtime.Scheme
	Client    client.Client
	Namespace string

	MultiClusterClient multicluster.Interface
}

TestContext context for cluster testing

func (*TestContext) GetClient added in v0.11.0

func (ctx *TestContext) GetClient() client.Client

GetClient returns a client to access kubernetes resources

type TestContextOption

type TestContextOption func(*TestContext)

TestContextOption options for building TestContext

func NamespaceOption

func NamespaceOption(ns string) TestContextOption

NamespaceOption customize the namespace name

func NamespacePrefixOption

func NamespacePrefixOption(prefix string) TestContextOption

NamespacePrefixOption customize the prefix of the namespace name

type TestNamespaceCondition

type TestNamespaceCondition struct{}

TestNamespaceCondition generate namespace for testing

func (*TestNamespaceCondition) Condition

func (t *TestNamespaceCondition) Condition(testCtx *TestContext) error

Condition implement the Condition interface Delete the namespace when it already exists, then create a new one. After the testing is completed, delete the namespace as well.

type TestSpecFunc

type TestSpecFunc func(*TestContext)

Jump to

Keyboard shortcuts

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