framework

package
v2.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2026 License: MIT Imports: 42 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// NodeUtilsContainer is the name of the node-utils sidecar container
	NodeUtilsContainer = "node-utils"

	// NodeUtilsBinary is the path to the node-utils binary in the container
	NodeUtilsBinary = "/nodeutils"
)
View Source
const (
	// VaultNamespace is the namespace where Vault is deployed
	VaultNamespace = "vault-system"

	// VaultTokenSecretName is the name of the secret containing the Vault token for TMKMS
	VaultTokenSecretName = "vault-tmkms-token"

	// VaultCASecretName is the name of the secret containing the Vault CA certificate
	VaultCASecretName = "vault-tls"

	// VaultAddress returns the in-cluster Vault address
	VaultAddress = "https://vault.vault-system.svc.cluster.local:8200"
)
View Source
const (
	// DefaultKindNodeImage is the default Kind node image
	DefaultKindNodeImage = "kindest/node:v1.32.0"
)
View Source
const (
	// RandomNamespacePrefix is the prefix for randomly generated namespace names
	RandomNamespacePrefix = "cosmopilot-test-"
)
View Source
const VaultCredsSecretName = "vault-creds"

VaultCredsSecretName is the name of the secret containing Vault root credentials

Variables

View Source
var (
	// CRDsPath is the path to the CRD files
	CRDsPath = filepath.Join("..", "..", "helm", "cosmopilot", "crds")

	// ExternalCRDsPath is the path to external CRD files (cert-manager, volumesnapshot)
	ExternalCRDsPath = filepath.Join("..", "..", "test", "testdata", "crds")

	// WebhooksPath is the path to webhook configuration files for envtest
	WebhooksPath = filepath.Join("..", "..", "test", "testdata", "webhooks")
)

Functions

func RandString

func RandString(n int) string

RandString generates a random string of the specified length

Types

type BaseFramework

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

BaseFramework contains common functionality for all framework implementations

func (*BaseFramework) Cancel

func (b *BaseFramework) Cancel()

Cancel cancels the context

func (*BaseFramework) Client

func (b *BaseFramework) Client() client.Client

Client returns the controller-runtime client

func (*BaseFramework) Config

func (b *BaseFramework) Config() *Config

Config returns the framework configuration

func (*BaseFramework) Context

func (b *BaseFramework) Context() context.Context

Context returns the framework context

func (*BaseFramework) CreateRandomNamespace

func (b *BaseFramework) CreateRandomNamespace() (*corev1.Namespace, error)

CreateRandomNamespace creates a namespace with a random name

func (*BaseFramework) DeleteNamespace

func (b *BaseFramework) DeleteNamespace(ns *corev1.Namespace) error

DeleteNamespace deletes the given namespace

func (*BaseFramework) KubeClient

func (b *BaseFramework) KubeClient() *kubernetes.Clientset

KubeClient returns the kubernetes clientset

func (*BaseFramework) PodExec

func (b *BaseFramework) PodExec(namespace, podName, container string, command ...string) (string, error)

PodExec executes a command in a pod container and returns stdout

func (*BaseFramework) RestConfig

func (b *BaseFramework) RestConfig() *rest.Config

RestConfig returns the REST config

func (*BaseFramework) RunAppCommand

func (b *BaseFramework) RunAppCommand(namespace, image, appBinary, accountSecretName string, args []string) (string, error)

RunAppCommand creates a temporary pod with the account secret mounted and runs a command. This is useful for running app CLI commands that require the validator account (e.g., submitting transactions). The mnemonic is imported into the keyring before running the command using the app binary only (no shell required).

func (*BaseFramework) SetClient

func (b *BaseFramework) SetClient(c client.Client)

SetClient sets the controller-runtime client

func (*BaseFramework) SetConfig

func (b *BaseFramework) SetConfig(cfg *Config)

SetConfig sets the configuration

func (*BaseFramework) SetContext

func (b *BaseFramework) SetContext(ctx context.Context, cancel context.CancelFunc)

SetContext sets the context and cancel function

func (*BaseFramework) SetKubeClient

func (b *BaseFramework) SetKubeClient(c *kubernetes.Clientset)

SetKubeClient sets the kubernetes clientset

func (*BaseFramework) SetRestConfig

func (b *BaseFramework) SetRestConfig(cfg *rest.Config)

SetRestConfig sets the REST config

type Config

type Config struct {
	// CertsDir is the directory containing webhook certificates
	CertsDir string

	// IssuerName is the cert-manager ClusterIssuer name
	IssuerName string

	// WorkerCount is the number of controller workers
	WorkerCount int

	// NodeUtilsImage is the node-utils image to use
	NodeUtilsImage string

	// ControllerImage is the controller image (for e2e only)
	ControllerImage string

	// ChartVersion is the helm chart version to deploy (for e2e release mode)
	// If set, deploys from OCI registry instead of local chart
	ChartVersion string

	// ClusterName is the Kind cluster name (for e2e only)
	ClusterName string

	// ReuseCluster indicates whether to reuse an existing cluster (for e2e only)
	ReuseCluster bool

	// InstallCertManager indicates whether to install cert-manager (for e2e only)
	InstallCertManager bool

	// InstallCSIDriver indicates whether to install CSI hostpath driver (for e2e only)
	InstallCSIDriver bool

	// InstallIngressNginx indicates whether to install ingress-nginx (for e2e only)
	InstallIngressNginx bool

	// InstallVault indicates whether to install HashiCorp Vault (for e2e only)
	InstallVault bool
}

Config holds configuration for test frameworks

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns a Config with default values

type EnvTestFramework

type EnvTestFramework struct {
	BaseFramework
	// contains filtered or unexported fields
}

EnvTestFramework implements Framework using controller-runtime's envtest

func NewEnvTestFramework

func NewEnvTestFramework(opts ...Option) *EnvTestFramework

NewEnvTestFramework creates a new envtest-based framework

func (*EnvTestFramework) ChainNodeReconciler added in v2.2.0

func (f *EnvTestFramework) ChainNodeReconciler() *chainnode.Reconciler

ChainNodeReconciler returns the ChainNode reconciler. This can be used to directly access or configure the reconciler in tests.

func (*EnvTestFramework) EnableMockStats added in v2.2.0

func (f *EnvTestFramework) EnableMockStats() *MockStatsClientRegistry

EnableMockStats enables mock stats for the framework. This is a convenience method that creates a registry and ensures it will be injected when StartManager() is called. Returns the registry for configuration.

func (*EnvTestFramework) Env

Env returns the underlying envtest environment

func (*EnvTestFramework) MockStatsRegistry added in v2.2.0

func (f *EnvTestFramework) MockStatsRegistry() *MockStatsClientRegistry

MockStatsRegistry returns the mock stats client registry, creating one if needed. This provides access to configure mock CPU/memory stats for testing VPA.

func (*EnvTestFramework) SetMockStatsRegistry added in v2.2.0

func (f *EnvTestFramework) SetMockStatsRegistry(registry *MockStatsClientRegistry)

SetMockStatsRegistry sets the mock stats client registry. This must be called before StartManager() to have effect.

func (*EnvTestFramework) Setup

func (f *EnvTestFramework) Setup(ctx context.Context) error

Setup initializes the envtest environment

func (*EnvTestFramework) StartManager

func (f *EnvTestFramework) StartManager() error

StartManager starts the controller manager

func (*EnvTestFramework) StopManager

func (f *EnvTestFramework) StopManager()

StopManager stops the controller manager

func (*EnvTestFramework) TearDown

func (f *EnvTestFramework) TearDown() error

TearDown cleans up the envtest environment

func (*EnvTestFramework) Type

func (f *EnvTestFramework) Type() FrameworkType

Type returns the framework type

type Framework

type Framework interface {
	// Setup initializes the test framework
	Setup(ctx context.Context) error

	// TearDown cleans up the test framework
	TearDown() error

	// Context returns the context used by the framework
	Context() context.Context

	// Client returns the controller-runtime client
	Client() client.Client

	// KubeClient returns the kubernetes clientset
	KubeClient() *kubernetes.Clientset

	// RestConfig returns the REST config for the cluster
	RestConfig() *rest.Config

	// Config returns the framework configuration
	Config() *Config

	// CreateRandomNamespace creates a namespace with a random name
	CreateRandomNamespace() (*corev1.Namespace, error)

	// DeleteNamespace deletes the given namespace
	DeleteNamespace(ns *corev1.Namespace) error

	// PodExec executes a command in a pod container and returns stdout
	PodExec(namespace, podName, container string, command ...string) (string, error)

	// RunAppCommand creates a temporary pod with the account secret mounted and runs a command
	RunAppCommand(namespace, image, appBinary, accountSecretName, command string) (string, error)

	// Type returns the type of framework (integration or e2e)
	Type() FrameworkType
}

Framework defines the interface for test frameworks. Both integration (envtest) and e2e (Kind) frameworks implement this interface.

type FrameworkType

type FrameworkType string

FrameworkType identifies the type of test framework

const (
	// FrameworkTypeIntegration is for envtest-based integration tests
	FrameworkTypeIntegration FrameworkType = "integration"

	// FrameworkTypeE2E is for Kind-based e2e tests
	FrameworkTypeE2E FrameworkType = "e2e"
)

type KindFramework

type KindFramework struct {
	BaseFramework
	// contains filtered or unexported fields
}

KindFramework implements Framework using Kind clusters

func NewKindFramework

func NewKindFramework(opts ...Option) *KindFramework

NewKindFramework creates a new Kind-based framework

func (*KindFramework) CreateClusterIssuer

func (f *KindFramework) CreateClusterIssuer(name string) error

CreateClusterIssuer creates a self-signed ClusterIssuer

func (*KindFramework) DeployController

func (f *KindFramework) DeployController() error

DeployController deploys the controller to the cluster using Helm. If ChartVersion is set, deploys from OCI registry (release mode). Otherwise, deploys from local chart with ControllerImage (dev mode).

func (*KindFramework) LoadImage

func (f *KindFramework) LoadImage(image string) error

LoadImage loads a Docker image into the Kind cluster

func (*KindFramework) Provider

func (f *KindFramework) Provider() *cluster.Provider

Provider returns the Kind provider

func (*KindFramework) Setup

func (f *KindFramework) Setup(ctx context.Context) error

Setup initializes the Kind cluster and installs dependencies

func (*KindFramework) TearDown

func (f *KindFramework) TearDown() error

TearDown cleans up the Kind cluster

func (*KindFramework) Type

func (f *KindFramework) Type() FrameworkType

Type returns the framework type

func (*KindFramework) UndeployController

func (f *KindFramework) UndeployController() error

UndeployController removes the controller from the cluster

type MockNodeUtilsHelper added in v2.2.0

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

MockNodeUtilsHelper provides methods to control node-utils mock mode via kubectl exec. This is useful for E2E testing VPA functionality where we need to simulate different CPU/memory usage levels.

func NewMockNodeUtilsHelper added in v2.2.0

func NewMockNodeUtilsHelper(execer PodExecer, namespace, podName string) *MockNodeUtilsHelper

NewMockNodeUtilsHelper creates a helper for controlling mock mode on a specific pod.

func NewMockNodeUtilsHelperFromChainNode added in v2.2.0

func NewMockNodeUtilsHelperFromChainNode(f *KindFramework, cn *cosmopilotv1.ChainNode) (*MockNodeUtilsHelper, error)

NewMockNodeUtilsHelperFromChainNode creates a helper from a ChainNode. It retrieves the pod name from the ChainNode status.

func NewMockNodeUtilsHelperFromPod added in v2.2.0

func NewMockNodeUtilsHelperFromPod(execer PodExecer, pod *corev1.Pod) *MockNodeUtilsHelper

NewMockNodeUtilsHelperFromPod creates a helper from a Pod.

func (*MockNodeUtilsHelper) GetStats added in v2.2.0

func (h *MockNodeUtilsHelper) GetStats() (cpuCores float64, memoryBytes uint64, err error)

GetStats returns the current mock stats as a map.

func (*MockNodeUtilsHelper) SetCPUMillicores added in v2.2.0

func (h *MockNodeUtilsHelper) SetCPUMillicores(millicores int64) error

SetCPUMillicores sets the mock CPU usage in millicores. Example: SetCPUMillicores(500) sets CPU to 500m (0.5 cores).

func (*MockNodeUtilsHelper) SetMemoryMiB added in v2.2.0

func (h *MockNodeUtilsHelper) SetMemoryMiB(mib int64) error

SetMemoryMiB sets the mock memory usage in MiB. Example: SetMemoryMiB(512) sets memory to 512MiB.

func (*MockNodeUtilsHelper) SetUsagePercentOfRequest added in v2.2.0

func (h *MockNodeUtilsHelper) SetUsagePercentOfRequest(percent int, cpuRequestMillicores, memoryRequestMiB int64) error

SetUsagePercentOfRequest sets mock usage as a percentage of the current request. This is useful for testing VPA scale-up/scale-down thresholds. Example: SetUsagePercentOfRequest(80, 1000, 512) sets CPU to 80% of 1000m (800m) and memory to 80% of 512MiB.

func (*MockNodeUtilsHelper) SimulateHighUsage added in v2.2.0

func (h *MockNodeUtilsHelper) SimulateHighUsage(cpuRequestMillicores, memoryRequestMiB int64) error

SimulateHighUsage sets mock usage to trigger scale-up (85% of request).

func (*MockNodeUtilsHelper) SimulateLowUsage added in v2.2.0

func (h *MockNodeUtilsHelper) SimulateLowUsage(cpuRequestMillicores, memoryRequestMiB int64) error

SimulateLowUsage sets mock usage to trigger scale-down (30% of request).

func (*MockNodeUtilsHelper) SimulateNormalUsage added in v2.2.0

func (h *MockNodeUtilsHelper) SimulateNormalUsage(cpuRequestMillicores, memoryRequestMiB int64) error

SimulateNormalUsage sets mock usage to a stable level (60% of request).

type MockStatsClient added in v2.2.0

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

MockStatsClient implements nodeutils.StatsClient for testing. It allows setting expected return values for CPU and memory stats.

func NewMockStatsClient added in v2.2.0

func NewMockStatsClient() *MockStatsClient

NewMockStatsClient creates a new mock stats client with default values.

func (*MockStatsClient) GetCPUStats added in v2.2.0

func (m *MockStatsClient) GetCPUStats(ctx context.Context, since time.Duration) (float64, error)

GetCPUStats returns the configured CPU usage.

func (*MockStatsClient) GetCPUStatsCalls added in v2.2.0

func (m *MockStatsClient) GetCPUStatsCalls() []time.Duration

GetCPUStatsCalls returns the durations passed to GetCPUStats calls.

func (*MockStatsClient) GetMemoryStats added in v2.2.0

func (m *MockStatsClient) GetMemoryStats(ctx context.Context, since time.Duration) (uint64, error)

GetMemoryStats returns the configured memory usage.

func (*MockStatsClient) GetMemoryStatsCalls added in v2.2.0

func (m *MockStatsClient) GetMemoryStatsCalls() []time.Duration

GetMemoryStatsCalls returns the durations passed to GetMemoryStats calls.

func (*MockStatsClient) Reset added in v2.2.0

func (m *MockStatsClient) Reset()

Reset resets all configuration to defaults.

func (*MockStatsClient) ResetCalls added in v2.2.0

func (m *MockStatsClient) ResetCalls()

ResetCalls clears the call tracking.

func (*MockStatsClient) SetCPUError added in v2.2.0

func (m *MockStatsClient) SetCPUError(err error)

SetCPUError sets an error to be returned by GetCPUStats.

func (*MockStatsClient) SetCPUUsage added in v2.2.0

func (m *MockStatsClient) SetCPUUsage(usage float64)

SetCPUUsage sets the CPU usage that will be returned by GetCPUStats. Usage is in cores (e.g., 0.5 = 500m, 1.0 = 1000m).

func (*MockStatsClient) SetCPUUsageMillicores added in v2.2.0

func (m *MockStatsClient) SetCPUUsageMillicores(millicores int64)

SetCPUUsageMillicores sets the CPU usage in millicores. Convenience method that converts millicores to cores.

func (*MockStatsClient) SetCPUUsagePercent added in v2.2.0

func (m *MockStatsClient) SetCPUUsagePercent(percent int, requestMillicores int64)

SetCPUUsagePercent sets CPU usage as a percentage of a given request. Example: SetCPUUsagePercent(80, 1000) sets usage to 80% of 1000m = 800m.

func (*MockStatsClient) SetMemoryError added in v2.2.0

func (m *MockStatsClient) SetMemoryError(err error)

SetMemoryError sets an error to be returned by GetMemoryStats.

func (*MockStatsClient) SetMemoryUsage added in v2.2.0

func (m *MockStatsClient) SetMemoryUsage(bytes uint64)

SetMemoryUsage sets the memory usage in bytes.

func (*MockStatsClient) SetMemoryUsageGiB added in v2.2.0

func (m *MockStatsClient) SetMemoryUsageGiB(gib float64)

SetMemoryUsageGiB sets the memory usage in GiB.

func (*MockStatsClient) SetMemoryUsageMiB added in v2.2.0

func (m *MockStatsClient) SetMemoryUsageMiB(mib int64)

SetMemoryUsageMiB sets the memory usage in MiB.

func (*MockStatsClient) SetMemoryUsagePercent added in v2.2.0

func (m *MockStatsClient) SetMemoryUsagePercent(percent int, requestBytes int64)

SetMemoryUsagePercent sets memory usage as a percentage of a given request. Example: SetMemoryUsagePercent(80, 1*1024*1024*1024) sets usage to 80% of 1Gi.

type MockStatsClientRegistry added in v2.2.0

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

MockStatsClientRegistry manages mock stats clients for different hosts. This allows setting up different mock behaviors for different ChainNodes.

func NewMockStatsClientRegistry added in v2.2.0

func NewMockStatsClientRegistry() *MockStatsClientRegistry

NewMockStatsClientRegistry creates a new registry with a default mock client.

func (*MockStatsClientRegistry) Default added in v2.2.0

Default returns the default mock client.

func (*MockStatsClientRegistry) Factory added in v2.2.0

func (r *MockStatsClientRegistry) Factory() func(host string) interface{}

Factory returns a StatsClientFactory that uses this registry. This factory can be injected into the Reconciler for testing.

func (*MockStatsClientRegistry) Get added in v2.2.0

Get returns the mock client for the given host, or the default if none exists.

func (*MockStatsClientRegistry) GetOrCreate added in v2.2.0

func (r *MockStatsClientRegistry) GetOrCreate(host string) *MockStatsClient

GetOrCreate returns the mock client for the given host, creating one if it doesn't exist.

func (*MockStatsClientRegistry) Reset added in v2.2.0

func (r *MockStatsClientRegistry) Reset()

Reset clears all registered clients and resets the default.

func (*MockStatsClientRegistry) SetDefault added in v2.2.0

func (r *MockStatsClientRegistry) SetDefault(client *MockStatsClient)

SetDefault sets the default mock client used for unknown hosts.

type Option

type Option func(*Config)

Option is a function that modifies the Config

func WithCSIDriver

func WithCSIDriver(install bool) Option

WithCSIDriver sets whether to install CSI hostpath driver (for e2e)

func WithCertManager

func WithCertManager(install bool) Option

WithCertManager sets whether to install cert-manager (for e2e)

func WithCertsDir

func WithCertsDir(dir string) Option

WithCertsDir sets the certificates directory

func WithChartVersion

func WithChartVersion(version string) Option

WithChartVersion sets the helm chart version (for e2e release mode)

func WithClusterName

func WithClusterName(name string) Option

WithClusterName sets the Kind cluster name (for e2e)

func WithControllerImage

func WithControllerImage(image string) Option

WithControllerImage sets the controller image (for e2e)

func WithIngressNginx

func WithIngressNginx(install bool) Option

WithIngressNginx sets whether to install ingress-nginx (for e2e)

func WithIssuerName

func WithIssuerName(name string) Option

WithIssuerName sets the cert-manager issuer name

func WithNodeUtilsImage

func WithNodeUtilsImage(image string) Option

WithNodeUtilsImage sets the node-utils image

func WithReuseCluster

func WithReuseCluster(reuse bool) Option

WithReuseCluster sets whether to reuse an existing cluster (for e2e)

func WithVault

func WithVault(install bool) Option

WithVault sets whether to install HashiCorp Vault (for e2e)

func WithWorkerCount

func WithWorkerCount(count int) Option

WithWorkerCount sets the number of controller workers

type PodExecer added in v2.2.0

type PodExecer interface {
	PodExec(namespace, podName, container string, command ...string) (string, error)
}

PodExecer is an interface for executing commands in pods. Both Framework and *KindFramework implement this interface.

type TestHelpers

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

TestHelpers provides helper methods for testing

func NewTestHelpers

func NewTestHelpers(c client.Client, ctx context.Context) *TestHelpers

NewTestHelpers creates a new TestHelpers instance

func (*TestHelpers) CreateConfigMap

func (h *TestHelpers) CreateConfigMap(namespace, name string, data map[string]string) error

CreateConfigMap creates a ConfigMap

func (*TestHelpers) CreateSecret

func (h *TestHelpers) CreateSecret(namespace, name string, data map[string][]byte) error

CreateSecret creates a generic secret

func (*TestHelpers) CreateVolumeSnapshotClass

func (h *TestHelpers) CreateVolumeSnapshotClass(name, driver string) error

CreateVolumeSnapshotClass creates a VolumeSnapshotClass for testing

func (*TestHelpers) SimulateCertificateReady

func (h *TestHelpers) SimulateCertificateReady(namespace, secretName string, dnsNames []string) error

SimulateCertificateReady simulates cert-manager creating a TLS secret for a Certificate This is useful for integration tests where cert-manager is not running

func (*TestHelpers) SimulateSnapshotReady

func (h *TestHelpers) SimulateSnapshotReady(namespace, snapshotName string, restoreSize string) error

SimulateSnapshotReady simulates a VolumeSnapshot becoming ready This is useful for integration tests where the CSI driver is not running

func (*TestHelpers) WaitForPodReady

func (h *TestHelpers) WaitForPodReady(namespace, name string, timeout time.Duration) error

WaitForPodReady waits for a pod to be ready

Jump to

Keyboard shortcuts

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