framework

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2026 License: GPL-3.0 Imports: 22 Imported by: 0

Documentation

Overview

Package framework provides mock ArgoCD API server for integration tests. Simulates ArgoCD API responses for application status, sync status, health checks.

Package framework provides test infrastructure for integration tests. Manages Kubernetes test environments using controller-runtime envtest.

Package framework provides test fixtures for integration tests. Creates sample CRDs, manifests, and test data.

Package framework provides mock GitHub API server for integration tests. Simulates GitHub API responses for PR operations, labels, webhooks, etc.

Package framework provides common test utilities for integration tests. Helper functions for assertions, waiting, resource management, etc.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertEventuallyEqual

func AssertEventuallyEqual(t *testing.T, ctx context.Context, expectedFunc func() interface{}, actualFunc func() interface{}, timeout time.Duration, msgFormat string, args ...interface{})

AssertEventuallyEqual asserts that a value eventually equals the expected value within the timeout.

func AssertEventuallyTrue

func AssertEventuallyTrue(t *testing.T, ctx context.Context, conditionFunc func() bool, timeout time.Duration, msgFormat string, args ...interface{})

AssertEventuallyTrue asserts that a condition eventually becomes true within the timeout.

func AssertResourceExists

func AssertResourceExists(t *testing.T, ctx context.Context, c client.Client, key types.NamespacedName, obj client.Object)

AssertResourceExists asserts that a Kubernetes resource exists.

func AssertResourceNotFound

func AssertResourceNotFound(t *testing.T, ctx context.Context, c client.Client, key types.NamespacedName, obj client.Object)

AssertResourceNotFound asserts that a Kubernetes resource does not exist.

func CleanupNamespace

func CleanupNamespace(t *testing.T, ctx context.Context, c client.Client, name string)

CleanupNamespace deletes a namespace and all resources within it.

func CleanupResource

func CleanupResource(t *testing.T, ctx context.Context, c client.Client, obj client.Object)

CleanupResource cleans up a Kubernetes resource, ignoring NotFound errors.

func CreateAndWait

func CreateAndWait(ctx context.Context, t *testing.T, c client.Client, obj client.Object, timeout time.Duration) error

CreateAndWait creates a Kubernetes resource and waits for it to exist.

func CreateNamespace

func CreateNamespace(t *testing.T, ctx context.Context, c client.Client, name string) error

CreateNamespace creates a namespace for testing.

func DeleteAndWait

func DeleteAndWait(ctx context.Context, t *testing.T, c client.Client, obj client.Object, timeout time.Duration) error

DeleteAndWait deletes a Kubernetes resource and waits for deletion to complete.

func GenerateUniqueName

func GenerateUniqueName(prefix string) string

GenerateUniqueName generates a unique name for test resources.

func GetCRDBasePath

func GetCRDBasePath() (string, error)

GetCRDBasePath returns the path to the CRD bases directory. Resolves relative to the project root.

func LogResourceStatus

func LogResourceStatus(t *testing.T, resourceType, name string, status interface{})

LogResourceStatus logs the status of a resource for debugging.

func RetryOnConflict

func RetryOnConflict(ctx context.Context, fn func() error) error

RetryOnConflict retries an operation on conflict errors. Useful for update operations that may encounter optimistic concurrency conflicts.

func UpdateAndWait

func UpdateAndWait(ctx context.Context, t *testing.T, c client.Client, obj client.Object, timeout time.Duration) error

UpdateAndWait updates a Kubernetes resource and waits for the update to be reflected.

func WaitForCondition

func WaitForCondition(ctx context.Context, conditionFunc func() (bool, error), timeout time.Duration) error

WaitForCondition waits for a custom condition to be met. The condition function should return true when the condition is satisfied.

func WaitForResourceToBeDeleted

func WaitForResourceToBeDeleted(ctx context.Context, c client.Client, key types.NamespacedName, obj client.Object, timeout time.Duration) error

WaitForResourceToBeDeleted waits for a Kubernetes resource to be deleted. Uses polling with configurable interval and timeout.

func WaitForResourceToExist

func WaitForResourceToExist(ctx context.Context, c client.Client, key types.NamespacedName, obj client.Object, timeout time.Duration) error

WaitForResourceToExist waits for a Kubernetes resource to exist. Uses polling with configurable interval and timeout.

Types

type ArgoCDMockServer

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

ArgoCDMockServer provides a mock ArgoCD API server for testing. Simulates ArgoCD API endpoints for application queries and health checks.

func NewArgoCDMockServer

func NewArgoCDMockServer() *ArgoCDMockServer

NewArgoCDMockServer creates a new mock ArgoCD API server.

func (*ArgoCDMockServer) AddApplication

func (m *ArgoCDMockServer) AddApplication(app *MockApplication)

AddApplication adds a mock application to the server.

func (*ArgoCDMockServer) Close

func (m *ArgoCDMockServer) Close()

Close shuts down the mock server.

func (*ArgoCDMockServer) GetApplication

func (m *ArgoCDMockServer) GetApplication(name string) (*MockApplication, bool)

GetApplication retrieves a mock application.

func (*ArgoCDMockServer) SetHealthy

func (m *ArgoCDMockServer) SetHealthy(healthy bool)

SetHealthy sets the health status of the ArgoCD mock server.

func (*ArgoCDMockServer) URL

func (m *ArgoCDMockServer) URL() string

URL returns the mock server's base URL.

func (*ArgoCDMockServer) UpdateApplicationHealth

func (m *ArgoCDMockServer) UpdateApplicationHealth(name, healthStatus, message string)

UpdateApplicationHealth updates the health status of an application.

func (*ArgoCDMockServer) UpdateApplicationSync

func (m *ArgoCDMockServer) UpdateApplicationSync(name, syncStatus, revision string)

UpdateApplicationSync updates the sync status of an application.

type Fixtures

type Fixtures struct{}

Fixtures provides test data and sample resources for integration tests.

func NewFixtures

func NewFixtures() *Fixtures

NewFixtures creates a new Fixtures instance.

func (*Fixtures) DriftMonitorWithCondition

func (f *Fixtures) DriftMonitorWithCondition(namespace, name, trackerName string, conditionType, status, reason, message string) *cdv1alpha1.DriftMonitor

DriftMonitorWithCondition creates a DriftMonitor with a specific condition.

func (*Fixtures) DriftMonitorWithSync

func (f *Fixtures) DriftMonitorWithSync(namespace, name, trackerName string, synced bool, deployedSHA, expectedSHA string) *cdv1alpha1.DriftMonitor

DriftMonitorWithSync creates a DriftMonitor with specific sync status.

func (*Fixtures) NewCDOperatorConfig

func (f *Fixtures) NewCDOperatorConfig(name string) *cdv1alpha1.CDOperatorConfig

NewCDOperatorConfig creates a sample CDOperatorConfig for testing.

func (*Fixtures) NewClusterConfig

func (f *Fixtures) NewClusterConfig(name, argoCDURL string) *cdv1alpha1.ClusterConfig

NewClusterConfig creates a sample ClusterConfig for testing.

func (*Fixtures) NewDriftMonitor

func (f *Fixtures) NewDriftMonitor(namespace, name, trackerName string) *cdv1alpha1.DriftMonitor

NewDriftMonitor creates a sample DriftMonitor for testing.

func (*Fixtures) NewGitHubTokenSecret

func (f *Fixtures) NewGitHubTokenSecret(namespace string) *corev1.Secret

NewGitHubTokenSecret creates a GitHub token secret for testing.

func (*Fixtures) NewNamespace

func (f *Fixtures) NewNamespace(name string) *corev1.Namespace

NewNamespace creates a namespace for testing.

func (*Fixtures) NewPromotionPolicy

func (f *Fixtures) NewPromotionPolicy(namespace, name string) *cdv1alpha1.PromotionPolicy

NewPromotionPolicy creates a sample PromotionPolicy for testing.

func (*Fixtures) NewPullRequestTracker

func (f *Fixtures) NewPullRequestTracker(namespace, name string, prNumber int) *cdv1alpha1.PullRequestTracker

NewPullRequestTracker creates a sample PullRequestTracker for testing.

func (*Fixtures) NewSecret

func (f *Fixtures) NewSecret(namespace, name string, data map[string][]byte) *corev1.Secret

NewSecret creates a secret for testing.

func (*Fixtures) NewWebhookSecret

func (f *Fixtures) NewWebhookSecret(namespace string) *corev1.Secret

NewWebhookSecret creates a webhook secret for testing.

func (*Fixtures) PRTrackerWithCondition

func (f *Fixtures) PRTrackerWithCondition(namespace, name string, prNumber int, conditionType, status, reason, message string) *cdv1alpha1.PullRequestTracker

PRTrackerWithCondition creates a PullRequestTracker with a specific condition.

func (*Fixtures) PRTrackerWithState

func (f *Fixtures) PRTrackerWithState(namespace, name string, prNumber int, state string) *cdv1alpha1.PullRequestTracker

PRTrackerWithState creates a PullRequestTracker with a specific state.

type GitHubMockServer

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

GitHubMockServer provides a mock GitHub API server for testing. Simulates common GitHub API endpoints used by cd-operator.

func NewGitHubMockServer

func NewGitHubMockServer() *GitHubMockServer

NewGitHubMockServer creates a new mock GitHub API server.

func (*GitHubMockServer) AddPR

func (m *GitHubMockServer) AddPR(pr *MockPullRequest)

AddPR adds a mock pull request to the server.

func (*GitHubMockServer) AddReview

func (m *GitHubMockServer) AddReview(prNumber int, review MockReview)

AddReview adds a mock review to a PR.

func (*GitHubMockServer) AddStatus

func (m *GitHubMockServer) AddStatus(sha string, status MockStatus)

AddStatus adds a mock commit status.

func (*GitHubMockServer) Close

func (m *GitHubMockServer) Close()

Close shuts down the mock server.

func (*GitHubMockServer) GetLabels

func (m *GitHubMockServer) GetLabels(prNumber int) []string

GetLabels retrieves labels for a PR.

func (*GitHubMockServer) GetPR

func (m *GitHubMockServer) GetPR(prNumber int) (*MockPullRequest, bool)

GetPR retrieves a mock pull request.

func (*GitHubMockServer) URL

func (m *GitHubMockServer) URL() string

URL returns the mock server's base URL.

type MockApplication

type MockApplication struct {
	Name      string                `json:"name"`
	Namespace string                `json:"namespace"`
	Status    MockApplicationStatus `json:"status"`
	Spec      MockApplicationSpec   `json:"spec"`
}

MockApplication represents a mock ArgoCD application.

func NewMockApplicationOutOfSync

func NewMockApplicationOutOfSync(name, namespace, currentRevision, targetRevision string) *MockApplication

NewMockApplicationOutOfSync creates a mock application in out-of-sync state.

func NewMockApplicationSynced

func NewMockApplicationSynced(name, namespace, revision string) *MockApplication

NewMockApplicationSynced creates a mock application in synced state.

type MockApplicationDestination

type MockApplicationDestination struct {
	Server    string `json:"server"`
	Namespace string `json:"namespace"`
}

MockApplicationDestination represents the destination of a mock ArgoCD application.

type MockApplicationSource

type MockApplicationSource struct {
	RepoURL        string `json:"repoURL"`
	Path           string `json:"path"`
	TargetRevision string `json:"targetRevision"`
}

MockApplicationSource represents the source of a mock ArgoCD application.

type MockApplicationSpec

type MockApplicationSpec struct {
	Source      MockApplicationSource      `json:"source"`
	Destination MockApplicationDestination `json:"destination"`
}

MockApplicationSpec represents the spec of a mock ArgoCD application.

type MockApplicationStatus

type MockApplicationStatus struct {
	Sync   MockSyncStatus   `json:"sync"`
	Health MockHealthStatus `json:"health"`
}

MockApplicationStatus represents the status of a mock ArgoCD application.

type MockHealthStatus

type MockHealthStatus struct {
	Status  string `json:"status"` // Healthy, Progressing, Degraded, Suspended, Missing, Unknown
	Message string `json:"message,omitempty"`
}

MockHealthStatus represents the health status of a mock ArgoCD application.

type MockPullRequest

type MockPullRequest struct {
	Number     int       `json:"number"`
	Title      string    `json:"title"`
	State      string    `json:"state"`
	HeadSHA    string    `json:"head"`
	BaseBranch string    `json:"base"`
	Labels     []string  `json:"labels,omitempty"`
	Mergeable  bool      `json:"mergeable"`
	CreatedAt  time.Time `json:"created_at"`
	UpdatedAt  time.Time `json:"updated_at"`
}

MockPullRequest represents a mock GitHub pull request.

type MockReview

type MockReview struct {
	ID          int64     `json:"id"`
	User        string    `json:"user"`
	State       string    `json:"state"`
	Body        string    `json:"body"`
	SubmittedAt time.Time `json:"submitted_at"`
}

MockReview represents a mock GitHub PR review.

type MockStatus

type MockStatus struct {
	State       string    `json:"state"`
	Context     string    `json:"context"`
	Description string    `json:"description"`
	TargetURL   string    `json:"target_url,omitempty"`
	CreatedAt   time.Time `json:"created_at"`
	UpdatedAt   time.Time `json:"updated_at"`
}

MockStatus represents a mock GitHub commit status.

type MockSyncStatus

type MockSyncStatus struct {
	Status   string    `json:"status"` // Synced, OutOfSync, Unknown
	Revision string    `json:"revision"`
	SyncedAt time.Time `json:"syncedAt,omitempty"`
}

MockSyncStatus represents the sync status of a mock ArgoCD application.

type TestCluster

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

TestCluster represents a Kubernetes test cluster for integration testing. Uses controller-runtime's envtest to provision a control plane (etcd + kube-apiserver).

func NewTestCluster

func NewTestCluster(t *testing.T, opts TestClusterOptions) *TestCluster

NewTestCluster creates a new test cluster with envtest. Provisions a local Kubernetes control plane for testing. The cluster must be torn down with Teardown() after test completion.

func (*TestCluster) Client

func (tc *TestCluster) Client() client.Client

Client returns the Kubernetes client for the test cluster.

func (*TestCluster) Config

func (tc *TestCluster) Config() *rest.Config

Config returns the REST config for the test cluster.

func (*TestCluster) Context

func (tc *TestCluster) Context() context.Context

Context returns the context for the test cluster.

func (*TestCluster) Teardown

func (tc *TestCluster) Teardown()

Teardown cleans up the test cluster and all resources. Must be called after test completion, typically via defer.

type TestClusterOptions

type TestClusterOptions struct {
	// CRDDirectoryPaths are paths to CRD directories to install
	CRDDirectoryPaths []string

	// AttachControlPlaneOutput attaches control plane output to test logs
	AttachControlPlaneOutput bool

	// Timeout is the overall test timeout
	Timeout time.Duration
}

TestClusterOptions configures a TestCluster.

Jump to

Keyboard shortcuts

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