testing

package
v0.0.0-...-54d739a Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2023 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RequestTransformer

func RequestTransformer(
	fn func(*http.Request) *http.Request) func(http.RoundTripper) http.RoundTripper

RequestTransformer returns a transport wrapper that creates transports that transform the request using the given function.

func ResponseTransformer

func ResponseTransformer(
	fn func(*http.Response, error) (*http.Response, error)) func(http.RoundTripper) http.RoundTripper

ResponseTransformer returns a transport wrapper that creates transports that transform the response using the given function.

func Template

func Template(source string, args ...any) string

Templates generates a string from the given templlate source and name value pairs.

func TmpFS

func TmpFS(args ...any) (dir string, fsys fs.FS)

TmpFS creates a temporary directory containing the given files, and then creates a fs.FS object that can be used to access it.

The files are specified as pairs of full path names and content. For example, to create a file named `mydir/myfile.yaml` containig some YAML text and a file `yourdir/yourfile.json` containing some JSON text:

dir, fsys = TmpFS(
	"mydir/myfile.yaml",
	`
		name: Joe
		age: 52
	`,
	"yourdir/yourfile.json",
	`{
		"name": "Mary",
		"age": 59
	}`
)

Directories are created automatically when they contain at least one file or subdirectory.

The caller is responsible for removing the directory once it is no longer needed.

Types

type DNSServer

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

DNSServer is a simple DNS server intendef for use in tests.

func NewDNSServer

func NewDNSServer() *DNSServer

func (*DNSServer) AddHost

func (s *DNSServer) AddHost(name, address string)

AddHost adds an A record and the corresponding PTR record to the server.

func (*DNSServer) AddZone

func (s *DNSServer) AddZone(name string)

AddZone adds a zone to the server. This must be done before adding any record for that zone.

func (*DNSServer) Address

func (s *DNSServer) Address() string

Address returns the host and port number of the server.

func (*DNSServer) Close

func (s *DNSServer) Close()

Close stops the server.

type Manager

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

Manager is a simplified controller manager intended for tests. Instead of creating types that implement the reconcile.Reconciler the user creates simple functions of the Reconciler type that can be registered during the execution of the test. These functions will be called with the object already retrieved, and only if that object hasn't been already marked for deletion. For example, to write a simple reconciler that moves bare metal hosts to the 'provisioned' state:

var manager *Manager

BeforeEach(func() {
	manager = NewManager().
		SetLogger(logger).
		AddGVK(gvk).
		Build()
})

AfterEach(func() {
	manager.Close()
})

It("Does something", func() {
	// Add the reconciler that changes the state of bare metal hosts:
	manager.Add(gvk, func(ctx context.Context, object *unstructured.Unstructured) {
		update := object.DeepCopy()
		err := mergo.Map(&update.Object, map[string]any{
			"status": map[string]any{
				"provisioning": map[string]any{
					"state": "provisioned",
				},
			},
		})
		Expect(err).ToNot(HaveOccurred())
		err = client.Status().Patch(ctx, update, clnt.MergeFrom(object))
		Expect(err).ToNot(HaveOccurred())
	})
})

func (*Manager) AddReconciler

func (m *Manager) AddReconciler(gvk schema.GroupVersionKind, reconciler Reconciler)

AddReconciler adds a reconciler function for the given GKV. It will be executed after all previous reconciler for the same GVK.

func (*Manager) Close

func (m *Manager) Close()

Close stops all the controller and releases all the resources used by this manager.

func (*Manager) SetReconciler

func (m *Manager) SetReconciler(gvk schema.GroupVersionKind, reconciler Reconciler)

SetReconciler sets a reconciler function for the given GKV. All previously existing reconciler functions will be removed.

type ManagerBuilder

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

ManagerBuilder contains the data and logic needs to build a fake controller manager intended for tests. Don't create instances of this type directly, use the NewManager function instead.

func NewManager

func NewManager() *ManagerBuilder

NewManager creates a simplified controller manager intended for tests.

func (*ManagerBuilder) AddGVK

AddGVK adds a GVK that the manager will watch.

func (*ManagerBuilder) Build

func (b *ManagerBuilder) Build() *Manager

Build uses the data stored in the builder to configure and create a new controller manager.

func (*ManagerBuilder) SetLogger

func (b *ManagerBuilder) SetLogger(value logr.Logger) *ManagerBuilder

SetLogger sets the logger that will be used by this manager. This is mandatory.

type Reconciler

type Reconciler func(context.Context, *unstructured.Unstructured)

Reconciler is a simplified version of the controller runtime reconciler interface intended for use in tests. The function will be called passing the object already loaded.

Jump to

Keyboard shortcuts

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