gentest

package
v0.0.0-...-8f52349 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2019 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package gentest contains tests for the client generator.

Index

Examples

Constants

View Source
const (
	// Kind contains the kind for the backing Kubernetes API.
	Kind = "Secret"

	// APIVersion contains the version for the backing Kubernetes API.
	APIVersion = "v1"
)

Variables

This section is empty.

Functions

func ConditionDeleted

func ConditionDeleted(_ *v1.Secret, apiErr error) (bool, error)

ConditionDeleted is a ConditionFuncE that succeeds if the error returned by the cluster was a not found error.

func FormatDiff

func FormatDiff(w io.Writer, leftName, rightName string, left, right *v1.Secret)

FormatDiff creates a diff between two v1.Secrets and writes it to the given writer.

Types

type Client

type Client interface {
	Create(namespace string, obj *v1.Secret, opts ...CreateOption) (*v1.Secret, error)
	Update(namespace string, obj *v1.Secret, opts ...UpdateOption) (*v1.Secret, error)
	Transform(namespace string, name string, transformer Mutator) (*v1.Secret, error)
	Get(namespace string, name string, opts ...GetOption) (*v1.Secret, error)
	Delete(namespace string, name string, opts ...DeleteOption) error
	List(namespace string, opts ...ListOption) ([]v1.Secret, error)
	Upsert(namespace string, newObj *v1.Secret, merge Merger) (*v1.Secret, error)
	WaitFor(ctx context.Context, namespace string, name string, interval time.Duration, condition Predicate) (*v1.Secret, error)
	WaitForE(ctx context.Context, namespace string, name string, interval time.Duration, condition ConditionFuncE) (*v1.Secret, error)

	// ClientExtension can be used by the developer to extend the client.
	ClientExtension
}

Client is the interface for interacting with v1.Secret types as OperatorConfig CF style objects.

func NewExampleClient

func NewExampleClient(mockK8s v1.SecretsGetter) Client

NewExampleClient creates an example client with a mutator and membership validator that filter based on a label.

type ClientExtension

type ClientExtension interface {
}

type ConditionFuncE

type ConditionFuncE func(instance *v1.Secret, apiErr error) (done bool, err error)

ConditionFuncE is a callback used by WaitForE. Done should be set to true once the condition succeeds and shouldn't be called anymore. The error will be passed back to the user.

This function MAY retrieve a nil instance and an apiErr. It's up to the function to decide how to handle the apiErr.

type CreateOption

type CreateOption func(*createConfig)

CreateOption is a single option for configuring a createConfig

type CreateOptions

type CreateOptions []CreateOption

CreateOptions is a configuration set defining a createConfig

func CreateOptionDefaults

func CreateOptionDefaults() CreateOptions

CreateOptionDefaults gets the default values for Create.

func (CreateOptions) Extend

func (opts CreateOptions) Extend(other CreateOptions) CreateOptions

Extend creates a new CreateOptions with the contents of other overriding the values set in this CreateOptions.

type DeleteOption

type DeleteOption func(*deleteConfig)

DeleteOption is a single option for configuring a deleteConfig

func WithDeleteDeleteImmediately

func WithDeleteDeleteImmediately(val bool) DeleteOption

WithDeleteDeleteImmediately creates an Option that sets If the resource should be deleted immediately.

func WithDeleteForegroundDeletion

func WithDeleteForegroundDeletion(val bool) DeleteOption

WithDeleteForegroundDeletion creates an Option that sets If the resource should be deleted in the foreground.

type DeleteOptions

type DeleteOptions []DeleteOption

DeleteOptions is a configuration set defining a deleteConfig

func DeleteOptionDefaults

func DeleteOptionDefaults() DeleteOptions

DeleteOptionDefaults gets the default values for Delete.

func (DeleteOptions) DeleteImmediately

func (opts DeleteOptions) DeleteImmediately() bool

DeleteImmediately returns the last set value for DeleteImmediately or the empty value if not set.

func (DeleteOptions) Extend

func (opts DeleteOptions) Extend(other DeleteOptions) DeleteOptions

Extend creates a new DeleteOptions with the contents of other overriding the values set in this DeleteOptions.

func (DeleteOptions) ForegroundDeletion

func (opts DeleteOptions) ForegroundDeletion() bool

ForegroundDeletion returns the last set value for ForegroundDeletion or the empty value if not set.

type GetOption

type GetOption func(*getConfig)

GetOption is a single option for configuring a getConfig

type GetOptions

type GetOptions []GetOption

GetOptions is a configuration set defining a getConfig

func GetOptionDefaults

func GetOptionDefaults() GetOptions

GetOptionDefaults gets the default values for Get.

func (GetOptions) Extend

func (opts GetOptions) Extend(other GetOptions) GetOptions

Extend creates a new GetOptions with the contents of other overriding the values set in this GetOptions.

type List

type List []v1.Secret

List represents a collection of v1.Secret.

func (List) Filter

func (list List) Filter(filter Predicate) (out List)

Filter returns a new list items for which the predicates fails removed.

Example
first := v1.Secret{}
first.Name = "ok"

second := v1.Secret{}
second.Name = "name-too-long-to-pass"

list := List{first, second}

filtered := list.Filter(func(s *v1.Secret) bool {
	return len(s.Name) < 8
})

fmt.Println("Results")
for _, v := range filtered {
	fmt.Println("-", v.Name)
}
Output:

Results
- ok

type ListOption

type ListOption func(*listConfig)

ListOption is a single option for configuring a listConfig

func WithListFieldSelector

func WithListFieldSelector(val map[string]string) ListOption

WithListFieldSelector creates an Option that sets A selector on the resource's fields.

func WithListFilters

func WithListFilters(val []Predicate) ListOption

WithListFilters creates an Option that sets Additional filters to apply.

func WithListLabelSelector

func WithListLabelSelector(val map[string]string) ListOption

WithListLabelSelector creates an Option that sets A label selector.

type ListOptions

type ListOptions []ListOption

ListOptions is a configuration set defining a listConfig

func ListOptionDefaults

func ListOptionDefaults() ListOptions

ListOptionDefaults gets the default values for List.

func (ListOptions) Extend

func (opts ListOptions) Extend(other ListOptions) ListOptions

Extend creates a new ListOptions with the contents of other overriding the values set in this ListOptions.

type Merger

type Merger func(newObj, oldObj *v1.Secret) *v1.Secret

Merger is a type to merge an existing value with a new one.

type Mutator

type Mutator func(*v1.Secret) error

Mutator is a function that changes v1.Secret.

func DiffWrapper

func DiffWrapper(w io.Writer, mutator Mutator) Mutator

DiffWrapper wraps a mutator and prints out the diff between the original object and the one it returns if there's no error.

Example (Changes)
secret := &v1.Secret{}
secret.Type = "opaque"

contents := &bytes.Buffer{}
wrapper := DiffWrapper(contents, func(s *v1.Secret) error {
	s.Type = "docker-creds"
	return nil
})

fmt.Println("Error:", wrapper(secret))
firstLine := strings.Split(contents.String(), "\n")[0]
fmt.Println("First line:", firstLine)
Output:

Error: <nil>
First line: OperatorConfig Diff (-old +new):
Example (Err)
secret := &v1.Secret{}

wrapper := DiffWrapper(os.Stdout, func(s *v1.Secret) error {
	return errors.New("some-error")
})

fmt.Println(wrapper(secret))
Output:

some-error
Example (NoDiff)
secret := &v1.Secret{}

wrapper := DiffWrapper(os.Stdout, func(s *v1.Secret) error {
	// don't mutate the secret
	return nil
})

wrapper(secret)
Output:

No changes

func LabelSetMutator

func LabelSetMutator(labels map[string]string) Mutator

LabelSetMutator creates a mutator that sets the given labels on the object.

Example
out := &v1.Secret{}
managedAdder := LabelSetMutator(map[string]string{"managed-by": "kf"})

managedAdder(out)
fmt.Printf("Labels: %v", out.Labels)
Output:

Labels: map[managed-by:kf]

type MutatorList

type MutatorList []Mutator

MutatorList is a list of mutators.

func (MutatorList) Apply

func (list MutatorList) Apply(svc *v1.Secret) error

Apply passes the given value to each of the mutators in the list failing if one of them returns an error.

Example
mutators := MutatorList{
	func(s *v1.Secret) error {
		s.Name = "Name"
		return nil
	},
	func(s *v1.Secret) error {
		return errors.New("some-error")
	},
}
res := v1.Secret{}
err := mutators.Apply(&res)

fmt.Println("Error:", err)
fmt.Println("Mutated name:", res.Name)
Output:

Error: some-error
Mutated name: Name

type Predicate

type Predicate func(*v1.Secret) bool

Predicate is a boolean function for a v1.Secret.

func AllPredicate

func AllPredicate(children ...Predicate) Predicate

AllPredicate is a predicate that passes if all children pass.

func LabelEqualsPredicate

func LabelEqualsPredicate(key, value string) Predicate

LabelEqualsPredicate validates that the given label exists exactly on the object.

Example
out := &v1.Secret{}
out.Labels = map[string]string{"managed-by": "not kf"}
pred := LabelEqualsPredicate("managed-by", "kf")

fmt.Printf("Not Equal: %v\n", pred(out))

out.Labels["managed-by"] = "kf"
fmt.Printf("Equal: %v\n", pred(out))
Output:

Not Equal: false
Equal: true

func LabelsContainsPredicate

func LabelsContainsPredicate(key string) Predicate

LabelsContainsPredicate validates that the given label exists on the object.

Example
out := &v1.Secret{}
out.Labels = map[string]string{"my-label": ""}

mylabelpred := LabelsContainsPredicate("my-label")
missinglabelpred := LabelsContainsPredicate("missing")

fmt.Printf("Contained: %v\n", mylabelpred(out))
fmt.Printf("Not Contained: %v\n", missinglabelpred(out))
Output:

Contained: true
Not Contained: false

type UpdateOption

type UpdateOption func(*updateConfig)

UpdateOption is a single option for configuring a updateConfig

type UpdateOptions

type UpdateOptions []UpdateOption

UpdateOptions is a configuration set defining a updateConfig

func UpdateOptionDefaults

func UpdateOptionDefaults() UpdateOptions

UpdateOptionDefaults gets the default values for Update.

func (UpdateOptions) Extend

func (opts UpdateOptions) Extend(other UpdateOptions) UpdateOptions

Extend creates a new UpdateOptions with the contents of other overriding the values set in this UpdateOptions.

Jump to

Keyboard shortcuts

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