testing

package
v0.0.42 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2026 License: Apache-2.0 Imports: 47 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultJWKS

func DefaultJWKS() []byte

DefaultJWKS generates the JSON web key set used for tests.

func LocalhostCertificate

func LocalhostCertificate() tls.Certificate

LocalhostCertificate returns a self signed TLS certificate valid for the name `localhost` DNS name, for the `127.0.0.1` IPv4 address and for the `::1` IPv6 address.

A similar certificate can be generated with the following command:

openssl req \
-x509 \
-newkey rsa:4096 \
-nodes \
-keyout tls.key \
-out tls.crt \
-subj '/CN=localhost' \
-addext 'subjectAltName=DNS:localhost,IP:127.0.0.1,IP:::1' \
-days 1

func LocalhostCertificateFiles

func LocalhostCertificateFiles() (certFile, keyFile, caFile string)

LocalhostCertificateFiles returns the paths to three temporary files containing the certificate, private key and CA certificate returned by LocalhostCertificate. All files are PEM encoded. Since the certificate is self-signed, the CA file contains the same certificate. It is the responsibility of the caller to delete these files when they are no longer needed.

func MakeClaims

func MakeClaims() jwt.MapClaims

MakeClaims generates a default set of claims to be used to issue a token.

func MakeTCPServer

func MakeTCPServer() *ghttp.Server

MakeTCPServer creates a test server that listens in a TCP socket and configured so that it sends log messages to the Ginkgo writer.

func MakeTCPTLSServer

func MakeTCPTLSServer() (server *ghttp.Server, ca string)

MakeTCPTLSServer creates a test server configured so that it sends log messages to the Ginkgo writer. It returns the created server and the name of a temporary file that contains the CA certificate that the client should trust in order to connect to the server. It is the responsibility of the caller to delete this temporary file when it is no longer needed.

func MakeTokenObject

func MakeTokenObject(claims jwt.MapClaims) *jwt.Token

MakeTokenObject generates a token with the claims resulting from merging the default claims and the claims explicitly given.

func MakeTokenString

func MakeTokenString(typ string, life time.Duration) string

MakeTokenString generates a token issued by the default OpenID server and with the given type and with the given life. If the life is zero the token will never expire. If the life is positive the token will be valid, and expire after that time. If the life is negative the token will be already expired that time ago.

func MatchLine

func MatchLine(regexp string, args ...any) OmegaMatcher

MatchLine succeeds if actual is a slice of strings that contains at least one item that matches the passed regular expression.

func RespondWithContent

func RespondWithContent(status int, contentType, body string) http.HandlerFunc

RespondeWithContent responds with the given status code, content type and body.

func RespondWithJSON

func RespondWithJSON(status int, body string) http.HandlerFunc

RespondWithJSON responds with the given status code and JSON body.

func RespondWithObject

func RespondWithObject(object any) http.HandlerFunc

RespondWithObject returns an HTTP handler that responds with a single object.

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 Command

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

Command helps manage execute a command line tool. Don't create instances of this type directly, use the NewCommand function instead.

func (*Command) Evaluate

func (c *Command) Evaluate(ctx context.Context) (stdoutBytes, stderrBytes []byte, err error)

Evaluate evalutes the command and returns the standard output and error as byte slices. It returns an error if the command fails.

func (*Command) Execute

func (c *Command) Execute(ctx context.Context) error

Execute executes the commands. It returns an error if the command fails.

type CommandBuilder

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

CommandBuilder contains the data and logic needed to create an object that helps execute a command line tool. Don't create instances of this type directly, use the NewCommand function instead.

func NewCommand

func NewCommand() *CommandBuilder

NewCommand creates a builder that can then be used to configure and create a new command.

func (*CommandBuilder) AddArgs

func (b *CommandBuilder) AddArgs(args ...string) *CommandBuilder

AddArgs adds the arguments for the command. Note that this appends to any previously set arguments with the SetArgs method. Arguments are optional, if not set no arguments will be used.

func (*CommandBuilder) Build

func (b *CommandBuilder) Build() (result *Command, err error)

func (*CommandBuilder) SetArgs

func (b *CommandBuilder) SetArgs(args ...string) *CommandBuilder

SetArgs sets the arguments for the command. Note that this replaces any previously arguments added with the AddArgs /method. Arguments are optional, if not set no arguments will be used.

func (*CommandBuilder) SetDir

func (b *CommandBuilder) SetDir(value string) *CommandBuilder

SetDir sets the directory where the command will be executed. This is optional, if not set the current working directory will be used.

func (*CommandBuilder) SetHome

func (b *CommandBuilder) SetHome(value string) *CommandBuilder

SetHome sets the project home directory. This is optional, and it is used to shorten the directory in log messages when it is a subdirectory of the project home directory, replacing it with '~'. This is used to make log messages more readable.

func (*CommandBuilder) SetLogger

func (b *CommandBuilder) SetLogger(value *slog.Logger) *CommandBuilder

SetLogger sets the logger. This is mandatory.

func (*CommandBuilder) SetName

func (b *CommandBuilder) SetName(name string) *CommandBuilder

SetName sets the name of the command. This is mandatory.

func (*CommandBuilder) SetQuiet

func (b *CommandBuilder) SetQuiet(value bool) *CommandBuilder

SetQuiet sets whether the command output should be quiet. When quiet is true (the default), the command output is buffered and only logged if the command fails. When quiet is false, each line of output is logged as it happens. This is useful to avoid flooding the logs with output that is not of interest.

type Database

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

Database is a PostgreSQL database.

func (*Database) Close

func (d *Database) Close()

Close deletes the database.

func (*Database) MakeHandle

func (d *Database) MakeHandle() *sql.DB

MakeHandle creates a new database handle for this database.

func (*Database) MakeURL

func (d *Database) MakeURL() string

MakeURL calculates the URL for this database.

type DatabaseServer

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

DatabaseServer knows how to start a PostgreSQL database server inside a container, and how to create databases to be used for tests.

func MakeDatabaseServer

func MakeDatabaseServer() *DatabaseServer

MakeDatabaseServer creates a new database server.

func (*DatabaseServer) Close

func (s *DatabaseServer) Close()

Close stops the database server.

func (*DatabaseServer) MakeDatabase

func (s *DatabaseServer) MakeDatabase() *Database

MakeDatabase creates a new database.

type Kind

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

Kind helps manage a Kind cluster used for integration tests. Don't create instances of this type directly, use the NewKind function instead.

func (*Kind) Client

func (k *Kind) Client() crclient.WithWatch

Client returns the controller runtime client for the cluster.

func (*Kind) ClientSet

func (k *Kind) ClientSet() *kubernetes.Clientset

ClientSet returns the Kubernetes set client for the cluster.

func (*Kind) Dump

func (k *Kind) Dump(ctx context.Context, dir string) error

Dump uses 'kubectl cluster-info dump' to dump the state and logs of all namespaces to the specified directory. If the directory already exists, all its contents will be removed before dumping. If it doesn't exist, it will be created.

func (*Kind) Exists

func (k *Kind) Exists(ctx context.Context) (result bool, err error)

Exists checks whether the cluster already exists.

func (*Kind) Kubeconfig

func (k *Kind) Kubeconfig() []byte

Kubeconfig returns the kubeconfig bytes.

func (*Kind) LoadArchive

func (k *Kind) LoadArchive(ctx context.Context, archive string) error

LoadArchive loads the specified image archive into the Kind cluster.

func (*Kind) LoadImage

func (k *Kind) LoadImage(ctx context.Context, image string) error

LoadImage loads the specified image into the Kind cluster.

Note that this will take the image from the local Docker daemon, regardless of what provider was used to create the cluster. For example, if the cluster was created with the Podman provider this wills still load the image from Docker.

func (*Kind) Start

func (k *Kind) Start(ctx context.Context) error

Start makes sure that the cluster is created and ready to use. If the cluster already exists, it will use the existing one. If it doesn't exist, it will create a new one.

func (*Kind) Stop

func (k *Kind) Stop(ctx context.Context) error

Stop removes the Kind cluster.

type KindBuilder

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

KindBuilder contains the data and logic needed to create an object that helps manage a Kind cluster used for integration tests. Don't create instances of this type directly, use the NewKind function instead.

func NewKind

func NewKind() *KindBuilder

NewKind creates a builder that can then be used to configure and create a new Kind cluster used for integration tests.

func (*KindBuilder) AddCrdFile

func (b *KindBuilder) AddCrdFile(file string) *KindBuilder

AddCrdFile adds a file containing custom resource definition to be installed in the cluster.

func (*KindBuilder) Build

func (b *KindBuilder) Build() (result *Kind, err error)

Build uses the configuration stored in the builder to create a new Kind cluster

func (*KindBuilder) SetHome

func (b *KindBuilder) SetHome(value string) *KindBuilder

SetHome sets the project home directory. This is optional, and it is used to shorten the directory in log messages when it is a subdirectory of the project home directory, replacing it with '~'. This is used to make log messages more readable.

func (*KindBuilder) SetLogger

func (b *KindBuilder) SetLogger(value *slog.Logger) *KindBuilder

SetLogger sets the logger. This is mandatory.

func (*KindBuilder) SetName

func (b *KindBuilder) SetName(name string) *KindBuilder

SetName sets the name of the Kind cluster. This is mandatory.

func (*KindBuilder) SetQuiet

func (b *KindBuilder) SetQuiet(value bool) *KindBuilder

SetQuiet sets whether the output of the command executed to manage the cluster should be quiet. When quiet is true (the default), the command output is buffered and only logged if the command fails. When quiet is false, each line of output is logged as it happens. This is useful to avoid flooding the logs with output that is not of interest.

type MetricsServer

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

MetricsServer is an HTTP server configured to return Prometheus metrics. Don't create objects of this type directly, use the NewMetricsServer function instead.

func NewMetricsServer

func NewMetricsServer() *MetricsServer

NewMetricsServer creates a metrics server.

func (*MetricsServer) Close

func (s *MetricsServer) Close()

Close stops the server and releases the resources it uses.

func (*MetricsServer) Metrics

func (s *MetricsServer) Metrics() []string

Metrics returns an array of strings containing the metrics available in this server. Each item in this array is a line in the Prometheus exposition format. This is intended to be used together with the MatchLine matcher.

func (*MetricsServer) Registry

func (s *MetricsServer) Registry() prometheus.Registerer

Registry returns the registry that should be used to register metrics for this server.

Jump to

Keyboard shortcuts

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