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 ¶
- func NewBasicAuthSecret(user GitUser, name, namespace string) *corev1.Secret
- type AccessLevel
- type GitServer
- func (gs *GitServer) AddUser(user GitUser)
- func (gs *GitServer) CACert() []byte
- func (gs *GitServer) CACertX509() *x509.Certificate
- func (gs *GitServer) CommitFile(repo RepoRef, branch, path string, content []byte, commitMsg string) error
- func (gs *GitServer) CommitFiles(repo RepoRef, branch string, files map[string][]byte, commitMsg string) error
- func (gs *GitServer) CommitObject(repo RepoRef, branch, path string, obj runtime.Object, commitMsg string) error
- func (gs *GitServer) CreateBranch(repo RepoRef, branchName, sourceBranch string) error
- func (gs *GitServer) CreateRepo(repo RepoRef, defaultBranch string) error
- func (gs *GitServer) DeleteBranch(repo RepoRef, branchName string) error
- func (gs *GitServer) FQDN() string
- func (gs *GitServer) FileExists(repo RepoRef, branch, path string) (bool, error)
- func (gs *GitServer) GetLatestCommit(repo RepoRef, branch string) (hash, message string, err error)
- func (gs *GitServer) GetPermission(username string, repo RepoRef) AccessLevel
- func (gs *GitServer) GetUser(username string) (GitUser, bool)
- func (gs *GitServer) IsFieldDefined(repo RepoRef, branch string, obj runtime.Object, yamlPath string) (bool, error)
- func (gs *GitServer) IsObjectInRepo(repo RepoRef, branch string, obj runtime.Object) (bool, error)
- func (gs *GitServer) ListFiles(repo RepoRef, branch string) ([]string, error)
- func (gs *GitServer) MergeBranch(repo RepoRef, sourceBranch, targetBranch string) error
- func (gs *GitServer) NewTLSSecret(name, namespace string) *corev1.Secret
- func (gs *GitServer) PushAttempts(repo RepoRef) int
- func (gs *GitServer) ReadFile(repo RepoRef, branch, path string) ([]byte, error)
- func (gs *GitServer) RepoURL(repo RepoRef) string
- func (gs *GitServer) ResetPushAttempts()
- func (gs *GitServer) SearchForObjectInRepo(repo RepoRef, branch string, obj runtime.Object) ([]ObjectFile, error)
- func (gs *GitServer) SetPermission(username string, repo RepoRef, level AccessLevel)
- func (gs *GitServer) Stop()
- func (gs *GitServer) TLSFQDN() string
- func (gs *GitServer) TLSRepoURL(repo RepoRef) string
- func (gs *GitServer) TLSURL() string
- func (gs *GitServer) URL() string
- type GitUser
- type ObjectFile
- type RepoRef
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 ¶
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 ¶
AddUser registers a git user. The user starts with no access to any repository; call SetPermission to grant access.
func (*GitServer) CACert ¶
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 ¶
CreateBranch creates a new branch pointing at the current HEAD of sourceBranch. Fails if the branch already exists.
func (*GitServer) CreateRepo ¶
CreateRepo creates a bare repository with an initial empty-tree commit on defaultBranch. Returns an error if the repository already exists.
func (*GitServer) DeleteBranch ¶
DeleteBranch removes a branch reference from the repository.
func (*GitServer) FQDN ¶
FQDN returns the host:port of the HTTP listener, suitable for RemoteUser.Spec.GitBaseDomainFQDN.
func (*GitServer) FileExists ¶
FileExists returns true if the file exists at path on the given branch.
func (*GitServer) GetLatestCommit ¶
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) 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 ¶
IsObjectInRepo reports whether any YAML file on branch contains a K8s object whose apiVersion, kind, name, and namespace match obj.
func (*GitServer) MergeBranch ¶
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 ¶
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
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 ¶
ReadFile reads the file at path on the given branch. Returns an error if the branch or file does not exist.
func (*GitServer) RepoURL ¶
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) TLSRepoURL ¶
TLSRepoURL returns the HTTPS clone URL for a repository.
func (*GitServer) TLSURL ¶
TLSURL returns the HTTPS base URL of the server, e.g. "https://127.0.0.1:PORT".
func (*GitServer) URL ¶
URL returns the plain-HTTP base URL of the server, e.g. "http://127.0.0.1:PORT".
type ObjectFile ¶
ObjectFile describes a file in a repository that contains a matching K8s object manifest.