envtest

package
v0.9.3 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: Apache-2.0 Imports: 29 Imported by: 0

Documentation

Overview

Package envtest provides an in-process git server toolkit for integration tests that run against a controller-runtime envtest environment.

A GitServer wraps github.com/sosedoff/gitkit and exposes both HTTP and HTTPS listeners simultaneously. All repositories, users, and permissions are shared between the two listeners. Tests pick the protocol they need: plain HTTP (URL, RepoURL, FQDN) for most tests, and HTTPS (TLSURL, TLSRepoURL, TLSFQDN, CACert) for tests that exercise CA bundle handling.

Per-user, per-repository access levels (NoAccess, ReadOnly, ReadWrite) are enforced in the auth function by inspecting the git smart HTTP service in the URL: git-upload-pack is a read operation, git-receive-pack is a write operation.

Push attempts are counted per repository (PushAttempts, ResetPushAttempts), including the ones the access levels reject, so tests can assert on how many times a client retried a failing push.

Two GitServers are intended to be started once in BeforeSuite and shared across parallel tests. Some are testing the target against two different url. Test isolation comes from each test creating uniquely-named repositories; the users/permissions maps are protected by a sync.RWMutex for safe concurrent access.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewBasicAuthSecret

func NewBasicAuthSecret(user GitUser, name, namespace string) *corev1.Secret

NewBasicAuthSecret builds a kubernetes.io/basic-auth Secret for a GitUser, suitable for RemoteUser.Spec.SecretRef.

Types

type AccessLevel

type AccessLevel int

AccessLevel defines the git access for a user on a repository.

const (
	// NoAccess denies all git operations (clone and push).
	NoAccess AccessLevel = iota
	// ReadOnly allows clone/fetch but denies push.
	ReadOnly
	// ReadWrite allows clone/fetch and push.
	ReadWrite
)

type GitServer

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

GitServer is an in-process git server for integration tests. It starts both an HTTP and an HTTPS listener backed by a single gitkit handler, sharing the same repositories, users, and permissions between the two.

func NewGitServer

func NewGitServer() (*GitServer, error)

NewGitServer creates, starts, and returns a GitServer ready to serve git operations over both HTTP and HTTPS. Callers must invoke Stop to release resources.

func (*GitServer) AddUser

func (gs *GitServer) AddUser(user GitUser)

AddUser registers a git user. The user starts with no access to any repository; call SetPermission to grant access.

func (*GitServer) CACert

func (gs *GitServer) CACert() []byte

CACert returns the PEM-encoded CA certificate that signed the TLS listener's server certificate. Use for CABundle in TLS tests.

func (*GitServer) CACertX509

func (gs *GitServer) CACertX509() *x509.Certificate

CACertX509 returns the server certificate as an *x509.Certificate.

func (*GitServer) CommitFile

func (gs *GitServer) CommitFile(repo RepoRef, branch, path string, content []byte, commitMsg string) error

CommitFile writes content at path on the given branch and commits it. The branch must already exist. Directories in path are created as needed.

func (*GitServer) CommitFiles

func (gs *GitServer) CommitFiles(repo RepoRef, branch string, files map[string][]byte, commitMsg string) error

CommitFiles writes the given files atomically in a single commit on the given branch. Paths are relative to the repo root and may contain slashes.

func (*GitServer) CommitObject

func (gs *GitServer) CommitObject(repo RepoRef, branch, path string, obj runtime.Object, commitMsg string) error

CommitObject marshals a runtime.Object to YAML and commits it as a file.

func (*GitServer) CreateBranch

func (gs *GitServer) CreateBranch(repo RepoRef, branchName, sourceBranch string) error

CreateBranch creates a new branch pointing at the current HEAD of sourceBranch. Fails if the branch already exists.

func (*GitServer) CreateRepo

func (gs *GitServer) CreateRepo(repo RepoRef, defaultBranch string) error

CreateRepo creates a bare repository with an initial empty-tree commit on defaultBranch. Returns an error if the repository already exists.

func (*GitServer) DeleteBranch

func (gs *GitServer) DeleteBranch(repo RepoRef, branchName string) error

DeleteBranch removes a branch reference from the repository.

func (*GitServer) FQDN

func (gs *GitServer) FQDN() string

FQDN returns the host:port of the HTTP listener, suitable for RemoteUser.Spec.GitBaseDomainFQDN.

func (*GitServer) FileExists

func (gs *GitServer) FileExists(repo RepoRef, branch, path string) (bool, error)

FileExists returns true if the file exists at path on the given branch.

func (*GitServer) GetLatestCommit

func (gs *GitServer) GetLatestCommit(repo RepoRef, branch string) (hash, message string, err error)

GetLatestCommit returns the hash and message of the latest commit on branch.

func (*GitServer) GetPermission

func (gs *GitServer) GetPermission(username string, repo RepoRef) AccessLevel

GetPermission returns the access level for a user on a repository. Returns NoAccess if no explicit permission was set.

func (*GitServer) GetUser

func (gs *GitServer) GetUser(username string) (GitUser, bool)

GetUser returns the GitUser registered for username.

func (*GitServer) IsFieldDefined

func (gs *GitServer) IsFieldDefined(repo RepoRef, branch string, obj runtime.Object, yamlPath string) (bool, error)

IsFieldDefined reports whether yamlPath (dot-separated) is defined in the YAML file that contains obj on branch.

func (*GitServer) IsObjectInRepo

func (gs *GitServer) IsObjectInRepo(repo RepoRef, branch string, obj runtime.Object) (bool, error)

IsObjectInRepo reports whether any YAML file on branch contains a K8s object whose apiVersion, kind, name, and namespace match obj.

func (*GitServer) ListFiles

func (gs *GitServer) ListFiles(repo RepoRef, branch string) ([]string, error)

ListFiles returns all file paths (blobs) on the given branch.

func (*GitServer) MergeBranch

func (gs *GitServer) MergeBranch(repo RepoRef, sourceBranch, targetBranch string) error

MergeBranch merges sourceBranch into targetBranch. Performs a fast-forward when possible; otherwise creates a merge commit whose tree is the union of both branches (source wins for conflicting paths, subtrees are merged recursively).

func (*GitServer) NewTLSSecret

func (gs *GitServer) NewTLSSecret(name, namespace string) *corev1.Secret

NewTLSSecret builds a kubernetes.io/tls Secret containing the server's CA certificate as tls.crt, suitable for RemoteSyncer.Spec.CABundleSecretRef. Only the CA certificate is populated; tls.key is left empty because the consumer only needs to verify the server identity.

func (*GitServer) PushAttempts added in v0.9.1

func (gs *GitServer) PushAttempts(repo RepoRef) int

PushAttempts returns how many pushes have been attempted on repo since the server started or since the last ResetPushAttempts. Attempts are counted before authorization is decided, so a push rejected by the permission matrix counts just like an accepted one. Use it to assert on retry behavior.

func (*GitServer) ReadFile

func (gs *GitServer) ReadFile(repo RepoRef, branch, path string) ([]byte, error)

ReadFile reads the file at path on the given branch. Returns an error if the branch or file does not exist.

func (*GitServer) RepoURL

func (gs *GitServer) RepoURL(repo RepoRef) string

RepoURL returns the plain-HTTP clone URL for a repository, e.g. "http://127.0.0.1:PORT/{owner}/{name}.git".

func (*GitServer) ResetPushAttempts added in v0.9.1

func (gs *GitServer) ResetPushAttempts()

ResetPushAttempts zeroes the push attempt counters of every repository.

func (*GitServer) SearchForObjectInRepo

func (gs *GitServer) SearchForObjectInRepo(repo RepoRef, branch string, obj runtime.Object) ([]ObjectFile, error)

SearchForObjectInRepo returns all files on branch whose YAML content matches obj's identity and whose spec/data match obj's spec/data.

func (*GitServer) SetPermission

func (gs *GitServer) SetPermission(username string, repo RepoRef, level AccessLevel)

SetPermission sets the access level for a user on a specific repository.

func (*GitServer) Stop

func (gs *GitServer) Stop()

Stop shuts down both HTTP servers and removes the on-disk repository directory. Safe to call multiple times.

func (*GitServer) TLSFQDN

func (gs *GitServer) TLSFQDN() string

TLSFQDN returns the host:port of the HTTPS listener.

func (*GitServer) TLSRepoURL

func (gs *GitServer) TLSRepoURL(repo RepoRef) string

TLSRepoURL returns the HTTPS clone URL for a repository.

func (*GitServer) TLSURL

func (gs *GitServer) TLSURL() string

TLSURL returns the HTTPS base URL of the server, e.g. "https://127.0.0.1:PORT".

func (*GitServer) URL

func (gs *GitServer) URL() string

URL returns the plain-HTTP base URL of the server, e.g. "http://127.0.0.1:PORT".

type GitUser

type GitUser struct {
	Username string
	Password string
	Email    string
}

GitUser represents a git user with basic-auth credentials.

type ObjectFile

type ObjectFile struct {
	Path    string
	Content []byte
}

ObjectFile describes a file in a repository that contains a matching K8s object manifest.

type RepoRef

type RepoRef struct {
	Owner string
	Name  string
}

RepoRef identifies a repository by owner and name.

func (RepoRef) String

func (r RepoRef) String() string

String returns "owner/name".

Jump to

Keyboard shortcuts

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