testutil

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2026 License: Apache-2.0 Imports: 22 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package testutil provides testing utilities for the cloudflare-operator.

Index

Constants

View Source
const DefaultAccountID = "test-account-id"

DefaultAccountID is the default test account ID.

View Source
const DefaultZoneID = "test-zone-id"

DefaultZoneID is the default test zone ID.

View Source
const DefaultZoneName = "example.com"

DefaultZoneName is the default test zone name.

View Source
const SystemNamespace = "cloudflare-operator-system"

SystemNamespace is the operator system namespace.

View Source
const TestNamespace = "test-namespace"

TestNamespace is the default namespace for tests.

Variables

This section is empty.

Functions

func AssertConditionFalse

func AssertConditionFalse(t *testing.T, conditions []metav1.Condition, conditionType string)

AssertConditionFalse asserts that a condition with the given type is False.

func AssertConditionTrue

func AssertConditionTrue(t *testing.T, conditions []metav1.Condition, conditionType string)

AssertConditionTrue asserts that a condition with the given type is True.

func AssertConditionWithMessage

func AssertConditionWithMessage(t *testing.T, conditions []metav1.Condition, conditionType, expectedMessage string)

AssertConditionWithMessage asserts that a condition message contains the expected substring.

func AssertConditionWithReason

func AssertConditionWithReason(t *testing.T, conditions []metav1.Condition, conditionType, expectedReason string)

AssertConditionWithReason asserts that a condition has the expected reason.

func AssertEventually

func AssertEventually(t *testing.T, condition func() bool, msgAndArgs ...interface{}) bool

AssertEventually is a helper that runs assert.Eventually with common defaults.

func AssertHasFinalizer

func AssertHasFinalizer(t *testing.T, finalizers []string, finalizerName string)

AssertHasFinalizer asserts that the object has the given finalizer.

func AssertNoCondition

func AssertNoCondition(t *testing.T, conditions []metav1.Condition, conditionType string)

AssertNoCondition asserts that a condition with the given type does not exist.

func AssertNoFinalizer

func AssertNoFinalizer(t *testing.T, finalizers []string, finalizerName string)

AssertNoFinalizer asserts that the object does not have the given finalizer.

func AssertResourceNotReady

func AssertResourceNotReady(t *testing.T, conditions []metav1.Condition)

AssertResourceNotReady asserts that the Ready condition is False.

func AssertResourceReady

func AssertResourceReady(t *testing.T, conditions []metav1.Condition)

AssertResourceReady asserts that the Ready condition is True.

func NewMockCloudflareAPI

func NewMockCloudflareAPI(config *MockAPIClientConfig) (*cloudflare.API, error)

NewMockCloudflareAPI creates a new Cloudflare API client pointing to the mock server.

func NewMockCloudflareAPIWithToken

func NewMockCloudflareAPIWithToken(config *MockAPIClientConfig) (*cloudflare.API, error)

NewMockCloudflareAPIWithToken creates a new Cloudflare API client using API token.

func RequireEventually

func RequireEventually(t *testing.T, condition func() bool, msgAndArgs ...interface{})

RequireEventually is a helper that runs require.Eventually with common defaults.

func WaitForMockServer

func WaitForMockServer(url string, timeout time.Duration) error

WaitForMockServer waits for the mock server to be ready.

Types

type AccessApplicationBuilder

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

AccessApplicationBuilder builds AccessApplication resources for testing.

func NewAccessApplicationBuilder

func NewAccessApplicationBuilder(name string) *AccessApplicationBuilder

NewAccessApplicationBuilder creates a new AccessApplicationBuilder.

func (*AccessApplicationBuilder) Build

Build returns the constructed AccessApplication.

func (*AccessApplicationBuilder) WithCredentialsRef

func (b *AccessApplicationBuilder) WithCredentialsRef(name string) *AccessApplicationBuilder

WithCredentialsRef sets the credentials reference.

func (*AccessApplicationBuilder) WithDeletionTimestamp

func (b *AccessApplicationBuilder) WithDeletionTimestamp() *AccessApplicationBuilder

WithDeletionTimestamp marks the resource for deletion.

func (*AccessApplicationBuilder) WithDomain

WithDomain sets the application domain.

func (*AccessApplicationBuilder) WithFinalizer

WithFinalizer adds a finalizer.

func (*AccessApplicationBuilder) WithSessionDuration

func (b *AccessApplicationBuilder) WithSessionDuration(duration string) *AccessApplicationBuilder

WithSessionDuration sets the session duration.

func (*AccessApplicationBuilder) WithType

WithType sets the application type.

type CloudflareCredentialsBuilder

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

CloudflareCredentialsBuilder builds CloudflareCredentials resources for testing.

func NewCloudflareCredentialsBuilder

func NewCloudflareCredentialsBuilder(name string) *CloudflareCredentialsBuilder

NewCloudflareCredentialsBuilder creates a new CloudflareCredentialsBuilder.

func (*CloudflareCredentialsBuilder) Build

Build returns the constructed CloudflareCredentials.

func (*CloudflareCredentialsBuilder) WithAPIToken

func (b *CloudflareCredentialsBuilder) WithAPIToken(secretName, secretKey string) *CloudflareCredentialsBuilder

WithAPIToken is a convenience method that sets up secret ref and auth type for API token.

func (*CloudflareCredentialsBuilder) WithAccountID

WithAccountID sets the account ID.

func (*CloudflareCredentialsBuilder) WithSecretRef

func (b *CloudflareCredentialsBuilder) WithSecretRef(secretName, namespace string) *CloudflareCredentialsBuilder

WithSecretRef sets the secret reference for API credentials.

type ClusterTunnelBuilder

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

ClusterTunnelBuilder builds ClusterTunnel resources for testing.

func NewClusterTunnelBuilder

func NewClusterTunnelBuilder(name string) *ClusterTunnelBuilder

NewClusterTunnelBuilder creates a new ClusterTunnelBuilder.

func (*ClusterTunnelBuilder) Build

Build returns the constructed ClusterTunnel.

func (*ClusterTunnelBuilder) WithCloudflareID

func (b *ClusterTunnelBuilder) WithCloudflareID(id string) *ClusterTunnelBuilder

WithCloudflareID sets the Cloudflare tunnel ID.

func (*ClusterTunnelBuilder) WithCredentialsRef

func (b *ClusterTunnelBuilder) WithCredentialsRef(name string) *ClusterTunnelBuilder

WithCredentialsRef sets the credentials reference.

func (*ClusterTunnelBuilder) WithDeletionTimestamp

func (b *ClusterTunnelBuilder) WithDeletionTimestamp() *ClusterTunnelBuilder

WithDeletionTimestamp marks the resource for deletion.

func (*ClusterTunnelBuilder) WithDomain

func (b *ClusterTunnelBuilder) WithDomain(domain string) *ClusterTunnelBuilder

WithDomain sets the Cloudflare domain.

func (*ClusterTunnelBuilder) WithFinalizer

func (b *ClusterTunnelBuilder) WithFinalizer(name string) *ClusterTunnelBuilder

WithFinalizer adds a finalizer.

type ConditionsAccessor

type ConditionsAccessor interface {
	GetConditions() []metav1.Condition
}

ConditionsAccessor is an interface for resources that have conditions.

type DNSRecordBuilder

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

DNSRecordBuilder builds DNSRecord resources for testing.

func NewDNSRecordBuilder

func NewDNSRecordBuilder(name, namespace string) *DNSRecordBuilder

NewDNSRecordBuilder creates a new DNSRecordBuilder.

func (*DNSRecordBuilder) Build

func (b *DNSRecordBuilder) Build() *v1alpha2.DNSRecord

Build returns the constructed DNSRecord.

func (*DNSRecordBuilder) WithContent

func (b *DNSRecordBuilder) WithContent(content string) *DNSRecordBuilder

WithContent sets the record content.

func (*DNSRecordBuilder) WithCredentialsRef

func (b *DNSRecordBuilder) WithCredentialsRef(name string) *DNSRecordBuilder

WithCredentialsRef sets the credentials reference.

func (*DNSRecordBuilder) WithDeletionTimestamp

func (b *DNSRecordBuilder) WithDeletionTimestamp() *DNSRecordBuilder

WithDeletionTimestamp marks the resource for deletion.

func (*DNSRecordBuilder) WithFinalizer

func (b *DNSRecordBuilder) WithFinalizer(name string) *DNSRecordBuilder

WithFinalizer adds a finalizer.

func (*DNSRecordBuilder) WithName

func (b *DNSRecordBuilder) WithName(name string) *DNSRecordBuilder

WithName sets the DNS name.

func (*DNSRecordBuilder) WithProxied

func (b *DNSRecordBuilder) WithProxied(proxied bool) *DNSRecordBuilder

WithProxied sets whether the record is proxied.

func (*DNSRecordBuilder) WithTTL

func (b *DNSRecordBuilder) WithTTL(ttl int) *DNSRecordBuilder

WithTTL sets the TTL.

func (*DNSRecordBuilder) WithType

func (b *DNSRecordBuilder) WithType(recordType string) *DNSRecordBuilder

WithType sets the record type.

type Fixtures

type Fixtures struct {
	Namespace string
}

Fixtures provides pre-built test resources.

func NewFixtures

func NewFixtures() *Fixtures

NewFixtures creates a new Fixtures instance.

func (*Fixtures) CNAMERecord

func (f *Fixtures) CNAMERecord(name, hostname, tunnelID string) *v1alpha2.DNSRecord

CNAMERecord returns a CNAME DNS record pointing to a tunnel.

func (*Fixtures) DefaultCredentials

func (f *Fixtures) DefaultCredentials() *v1alpha2.CloudflareCredentials

DefaultCredentials returns default CloudflareCredentials for testing.

func (*Fixtures) DefaultCredentialsSecret

func (f *Fixtures) DefaultCredentialsSecret() *corev1.Secret

DefaultCredentialsSecret returns the secret for default credentials.

func (*Fixtures) DefaultVirtualNetwork

func (f *Fixtures) DefaultVirtualNetwork(name string) *v1alpha2.VirtualNetwork

DefaultVirtualNetwork returns a default VirtualNetwork resource.

func (*Fixtures) NetworkRouteWithVNet

func (f *Fixtures) NetworkRouteWithVNet(name, network, tunnelName, vnetName string) *v1alpha2.NetworkRoute

NetworkRouteWithVNet returns a NetworkRoute with virtual network reference.

func (*Fixtures) Service

func (f *Fixtures) Service(name string, port int32) *corev1.Service

Service returns a Kubernetes Service for testing.

func (*Fixtures) ServiceWithSelector

func (f *Fixtures) ServiceWithSelector(name string, port int32, selector map[string]string) *corev1.Service

ServiceWithSelector returns a Kubernetes Service with selector for testing.

func (*Fixtures) SimpleAccessApplication

func (f *Fixtures) SimpleAccessApplication(name, domain string) *v1alpha2.AccessApplication

SimpleAccessApplication returns a simple AccessApplication resource.

func (*Fixtures) SimpleClusterTunnel

func (f *Fixtures) SimpleClusterTunnel(name string) *v1alpha2.ClusterTunnel

SimpleClusterTunnel returns a simple ClusterTunnel resource.

func (*Fixtures) SimpleDNSRecord

func (f *Fixtures) SimpleDNSRecord(name, dnsName, content string) *v1alpha2.DNSRecord

SimpleDNSRecord returns a simple DNS record.

func (*Fixtures) SimpleNetworkRoute

func (f *Fixtures) SimpleNetworkRoute(name, network, tunnelName string) *v1alpha2.NetworkRoute

SimpleNetworkRoute returns a simple NetworkRoute resource.

func (*Fixtures) SimpleTunnel

func (f *Fixtures) SimpleTunnel(name string) *v1alpha2.Tunnel

SimpleTunnel returns a simple Tunnel resource.

func (*Fixtures) SimpleTunnelBinding

func (f *Fixtures) SimpleTunnelBinding(name, tunnelName, serviceName, fqdn string) *v1alpha1.TunnelBinding

SimpleTunnelBinding returns a simple TunnelBinding resource (v1alpha1).

func (*Fixtures) SimpleVirtualNetwork

func (f *Fixtures) SimpleVirtualNetwork(name string) *v1alpha2.VirtualNetwork

SimpleVirtualNetwork returns a simple VirtualNetwork resource.

func (*Fixtures) SystemNamespaceObj

func (f *Fixtures) SystemNamespaceObj() *corev1.Namespace

SystemNamespaceObj returns the system namespace object.

func (*Fixtures) TestNamespaceObj

func (f *Fixtures) TestNamespaceObj() *corev1.Namespace

TestNamespaceObj returns a test namespace object.

func (*Fixtures) WithNamespace

func (f *Fixtures) WithNamespace(ns string) *Fixtures

WithNamespace sets the namespace for fixtures.

type MockAPIClientConfig

type MockAPIClientConfig struct {
	// BaseURL is the mock server URL
	BaseURL string
	// AccountID is the test account ID
	AccountID string
	// ZoneID is the test zone ID
	ZoneID string
}

MockAPIClientConfig holds configuration for creating mock API clients.

func DefaultMockConfig

func DefaultMockConfig() *MockAPIClientConfig

DefaultMockConfig returns a default mock configuration.

type MockServerTestEnv

type MockServerTestEnv struct {
	MockServer *mockserver.Server
	TestEnv    *TestEnv
	APIConfig  *MockAPIClientConfig
}

MockServerTestEnv combines a mock Cloudflare server with a test environment.

func NewMockServerTestEnv

func NewMockServerTestEnv(opts *MockServerTestEnvOptions) (*MockServerTestEnv, error)

NewMockServerTestEnv creates a new test environment with a mock Cloudflare server.

func (*MockServerTestEnv) Reset

func (m *MockServerTestEnv) Reset()

Reset resets the mock server state.

func (*MockServerTestEnv) Stop

func (m *MockServerTestEnv) Stop() error

Stop stops both the mock server and test environment.

type MockServerTestEnvOptions

type MockServerTestEnvOptions struct {
	MockServerPort int
	TestEnvOptions *TestEnvOptions
}

MockServerTestEnvOptions configures the mock server test environment.

func DefaultMockServerTestEnvOptions

func DefaultMockServerTestEnvOptions() *MockServerTestEnvOptions

DefaultMockServerTestEnvOptions returns default options.

type NetworkRouteBuilder

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

NetworkRouteBuilder builds NetworkRoute resources for testing.

func NewNetworkRouteBuilder

func NewNetworkRouteBuilder(name string) *NetworkRouteBuilder

NewNetworkRouteBuilder creates a new NetworkRouteBuilder.

func (*NetworkRouteBuilder) Build

Build returns the constructed NetworkRoute.

func (*NetworkRouteBuilder) WithComment

func (b *NetworkRouteBuilder) WithComment(comment string) *NetworkRouteBuilder

WithComment sets the comment.

func (*NetworkRouteBuilder) WithCredentialsRef

func (b *NetworkRouteBuilder) WithCredentialsRef(name string) *NetworkRouteBuilder

WithCredentialsRef sets the credentials reference.

func (*NetworkRouteBuilder) WithDeletionTimestamp

func (b *NetworkRouteBuilder) WithDeletionTimestamp() *NetworkRouteBuilder

WithDeletionTimestamp marks the resource for deletion.

func (*NetworkRouteBuilder) WithFinalizer

func (b *NetworkRouteBuilder) WithFinalizer(name string) *NetworkRouteBuilder

WithFinalizer adds a finalizer.

func (*NetworkRouteBuilder) WithNetwork

func (b *NetworkRouteBuilder) WithNetwork(network string) *NetworkRouteBuilder

WithNetwork sets the CIDR network.

func (*NetworkRouteBuilder) WithTunnelRef

func (b *NetworkRouteBuilder) WithTunnelRef(name string) *NetworkRouteBuilder

WithTunnelRef sets the tunnel reference.

func (*NetworkRouteBuilder) WithTunnelRefKind

func (b *NetworkRouteBuilder) WithTunnelRefKind(name, kind string) *NetworkRouteBuilder

WithTunnelRefKind sets the tunnel reference with kind.

func (*NetworkRouteBuilder) WithVirtualNetworkRef

func (b *NetworkRouteBuilder) WithVirtualNetworkRef(name string) *NetworkRouteBuilder

WithVirtualNetworkRef sets the virtual network reference.

type ResourceContext

type ResourceContext struct {
	Ctx       context.Context
	AccountID string
	ZoneID    string
	API       *cloudflare.API
}

ResourceContext provides context for creating test resources.

func NewResourceContext

func NewResourceContext(api *cloudflare.API, config *MockAPIClientConfig) *ResourceContext

NewResourceContext creates a new resource context for testing.

type SecretBuilder

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

SecretBuilder builds Secret resources for testing.

func NewSecretBuilder

func NewSecretBuilder(name, namespace string) *SecretBuilder

NewSecretBuilder creates a new SecretBuilder.

func (*SecretBuilder) Build

func (b *SecretBuilder) Build() *corev1.Secret

Build returns the constructed Secret.

func (*SecretBuilder) WithData

func (b *SecretBuilder) WithData(key string, value []byte) *SecretBuilder

WithData adds data to the secret.

func (*SecretBuilder) WithStringData

func (b *SecretBuilder) WithStringData(key, value string) *SecretBuilder

WithStringData adds string data to the secret.

func (*SecretBuilder) WithType

func (b *SecretBuilder) WithType(secretType corev1.SecretType) *SecretBuilder

WithType sets the secret type.

type TestEnv

type TestEnv struct {
	Env        *envtest.Environment
	Cfg        *rest.Config
	K8sClient  client.Client
	Ctx        context.Context
	Cancel     context.CancelFunc
	Mgr        ctrl.Manager
	MgrStarted bool
}

TestEnv wraps envtest for controller testing.

func NewTestEnv

func NewTestEnv(opts *TestEnvOptions) (*TestEnv, error)

NewTestEnv creates a new test environment.

func (*TestEnv) CleanupResources

func (te *TestEnv) CleanupResources(template client.Object, namespace string) error

CleanupResources deletes all resources of a type in a namespace. The template parameter should be an empty object of the type to delete.

func (*TestEnv) CreateNamespace

func (te *TestEnv) CreateNamespace(name string) error

CreateNamespace creates a namespace in the test cluster.

func (*TestEnv) DeleteNamespace

func (te *TestEnv) DeleteNamespace(name string) error

DeleteNamespace deletes a namespace from the test cluster.

func (*TestEnv) EnsureNamespace

func (te *TestEnv) EnsureNamespace(name string) error

EnsureNamespace ensures a namespace exists.

func (*TestEnv) Stop

func (te *TestEnv) Stop() error

Stop stops the test environment.

type TestEnvOptions

type TestEnvOptions struct {
	// CRDPaths are paths to CRD manifests
	CRDPaths []string
	// UseExistingCluster connects to an existing cluster instead of starting envtest
	UseExistingCluster bool
	// StartManager whether to start a controller manager
	StartManager bool
}

TestEnvOptions configures the test environment.

func DefaultTestEnvOptions

func DefaultTestEnvOptions() *TestEnvOptions

DefaultTestEnvOptions returns default options.

type TunnelBindingBuilder

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

TunnelBindingBuilder builds TunnelBinding resources for testing (v1alpha1).

func NewTunnelBindingBuilder

func NewTunnelBindingBuilder(name, namespace string) *TunnelBindingBuilder

NewTunnelBindingBuilder creates a new TunnelBindingBuilder.

func (*TunnelBindingBuilder) Build

Build returns the constructed TunnelBinding.

func (*TunnelBindingBuilder) WithDeletionTimestamp

func (b *TunnelBindingBuilder) WithDeletionTimestamp() *TunnelBindingBuilder

WithDeletionTimestamp marks the resource for deletion.

func (*TunnelBindingBuilder) WithFinalizer

func (b *TunnelBindingBuilder) WithFinalizer(name string) *TunnelBindingBuilder

WithFinalizer adds a finalizer.

func (*TunnelBindingBuilder) WithSubject

func (b *TunnelBindingBuilder) WithSubject(serviceName, fqdn string) *TunnelBindingBuilder

WithSubject adds a subject.

func (*TunnelBindingBuilder) WithTunnelRef

func (b *TunnelBindingBuilder) WithTunnelRef(name, kind string) *TunnelBindingBuilder

WithTunnelRef sets the tunnel reference.

type TunnelBuilder

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

TunnelBuilder builds Tunnel resources for testing.

func NewTunnelBuilder

func NewTunnelBuilder(name, namespace string) *TunnelBuilder

NewTunnelBuilder creates a new TunnelBuilder.

func (*TunnelBuilder) Build

func (b *TunnelBuilder) Build() *v1alpha2.Tunnel

Build returns the constructed Tunnel.

func (*TunnelBuilder) WithCloudflareID

func (b *TunnelBuilder) WithCloudflareID(id string) *TunnelBuilder

WithCloudflareID sets the Cloudflare tunnel ID.

func (*TunnelBuilder) WithCredentialsRef

func (b *TunnelBuilder) WithCredentialsRef(name string) *TunnelBuilder

WithCredentialsRef sets the credentials reference.

func (*TunnelBuilder) WithDeletionTimestamp

func (b *TunnelBuilder) WithDeletionTimestamp() *TunnelBuilder

WithDeletionTimestamp marks the resource for deletion.

func (*TunnelBuilder) WithDomain

func (b *TunnelBuilder) WithDomain(domain string) *TunnelBuilder

WithDomain sets the Cloudflare domain.

func (*TunnelBuilder) WithFinalizer

func (b *TunnelBuilder) WithFinalizer(name string) *TunnelBuilder

WithFinalizer adds a finalizer.

type VirtualNetworkBuilder

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

VirtualNetworkBuilder builds VirtualNetwork resources for testing.

func NewVirtualNetworkBuilder

func NewVirtualNetworkBuilder(name string) *VirtualNetworkBuilder

NewVirtualNetworkBuilder creates a new VirtualNetworkBuilder.

func (*VirtualNetworkBuilder) Build

Build returns the constructed VirtualNetwork.

func (*VirtualNetworkBuilder) WithCloudflareID

func (b *VirtualNetworkBuilder) WithCloudflareID(id string) *VirtualNetworkBuilder

WithCloudflareID sets the Cloudflare virtual network ID.

func (*VirtualNetworkBuilder) WithComment

func (b *VirtualNetworkBuilder) WithComment(comment string) *VirtualNetworkBuilder

WithComment sets the comment.

func (*VirtualNetworkBuilder) WithCredentialsRef

func (b *VirtualNetworkBuilder) WithCredentialsRef(name string) *VirtualNetworkBuilder

WithCredentialsRef sets the credentials reference.

func (*VirtualNetworkBuilder) WithDeletionTimestamp

func (b *VirtualNetworkBuilder) WithDeletionTimestamp() *VirtualNetworkBuilder

WithDeletionTimestamp marks the resource for deletion.

func (*VirtualNetworkBuilder) WithFinalizer

func (b *VirtualNetworkBuilder) WithFinalizer(name string) *VirtualNetworkBuilder

WithFinalizer adds a finalizer.

func (*VirtualNetworkBuilder) WithIsDefault

func (b *VirtualNetworkBuilder) WithIsDefault(isDefault bool) *VirtualNetworkBuilder

WithIsDefault sets whether this is the default network.

Jump to

Keyboard shortcuts

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