Documentation
¶
Overview ¶
Package servertest starts a Packhorse with a fake upstream behind it, so tests can run real git fetches against it.
Index ¶
- Constants
- func Fsck(ctx context.Context, t *testing.T, dir string)
- func GitErr(ctx context.Context, dir string, args ...string) error
- func InitClientRepo(ctx context.Context, repoDir, remoteURL string) error
- func RequireCommit(ctx context.Context, t *testing.T, dir, commit string)
- func RunGit(ctx context.Context, t *testing.T, dir string, args ...string) string
- func RunGitCombined(ctx context.Context, t *testing.T, dir string, args ...string) string
- func SkipIfShort(t *testing.T)
- type Option
- type TestPackhorse
- func (p *TestPackhorse) ClientRepo(ctx context.Context, t *testing.T) string
- func (p *TestPackhorse) CommitContent(t *testing.T, commit string) string
- func (p *TestPackhorse) Fetch(ctx context.Context, t *testing.T, repoDir string, args ...string)
- func (p *TestPackhorse) FetchInNewRepo(ctx context.Context, t *testing.T, args ...string) string
- func (p *TestPackhorse) RemoteURL() string
- func (p *TestPackhorse) RequireCommitContent(ctx context.Context, t *testing.T, repoDir, commit string)
Constants ¶
const ( // RepoPath is the path the upstream repository is served from. RepoPath = "/test/repo.git" // RepoName is RepoPath as the allowlist sees it. RepoName = "test/repo" )
Variables ¶
This section is empty.
Functions ¶
func Fsck ¶
Fsck checks every object in dir is well-formed. fsck re-hashes each object, so a truncated or corrupt pack fails here.
--no-dangling is needed because fetching a bare SHA leaves the new objects reachable only from FETCH_HEAD.
func GitErr ¶
GitErr is RunGit for goroutines, where require would be invalid. It reports the failure instead of ending the test.
func InitClientRepo ¶
InitClientRepo points a new repository at remoteURL. It returns an error instead of failing the test so the parallel tests can call it from a goroutine, where require would be invalid.
func RequireCommit ¶
RequireCommit checks commit arrived in dir as a commit object.
func RunGit ¶
RunGit runs a git command in dir and returns stdout, failing the test with stderr attached if git exits non-zero. It goes through testgitserver.GitCommand so the local git config can't move the request off the path under test.
func RunGitCombined ¶
RunGitCombined is RunGit but returns stdout and stderr interleaved, for tests that assert on what git printed rather than only on its exit status.
func SkipIfShort ¶
SkipIfShort skips a test that shells out to git and starts a server.
Types ¶
type Option ¶
type Option func(*settings)
Option changes how NewTestPackhorse builds the Packhorse.
func WithAllowlist ¶
WithAllowlist replaces the default allowlist, which holds only the test repository. Pass one that excludes it to exercise the bypass path.
func WithCommits ¶
WithCommits sets how many commits the upstream repository holds.
func WithIdleAge ¶
WithIdleAge turns on the sliding TTL.
type TestPackhorse ¶
type TestPackhorse struct {
Packhorse *server.Server
Upstream *testgitserver.Server
Repo *testgitserver.TestRepo
Metrics *observability.MockAppMetrics
}
TestPackhorse is a running Packhorse, the upstream Git server it proxies to, and the repository they both serve. NewTestPackhorse shuts all of it down when the test ends.
func NewTestPackhorse ¶
func NewTestPackhorse(t *testing.T, opts ...Option) *TestPackhorse
NewTestPackhorse starts an upstream Git server holding a repository, and a Packhorse pointed at it. Both listen on random ports.
The allowlist holds only that repository, which is how production runs. It also makes Packhorse label cache metrics by repository rather than collapsing them onto a sentinel, so tests can assert against RepoName.
func (*TestPackhorse) ClientRepo ¶
ClientRepo returns a fresh empty repository pointed at Packhorse.
func (*TestPackhorse) CommitContent ¶
func (p *TestPackhorse) CommitContent(t *testing.T, commit string) string
CommitContent returns the content of test.txt at commit. CreateTestRepo writes "Commit <n>\n" on its nth commit.
func (*TestPackhorse) FetchInNewRepo ¶
FetchInNewRepo fetches into a fresh client repository and returns its path. Calling it twice for the same commit gives the miss-then-hit pattern the cache tests need.
func (*TestPackhorse) RemoteURL ¶
func (p *TestPackhorse) RemoteURL() string
RemoteURL is the repository's URL through Packhorse.
func (*TestPackhorse) RequireCommitContent ¶
func (p *TestPackhorse) RequireCommitContent(ctx context.Context, t *testing.T, repoDir, commit string)
RequireCommitContent checks commit arrived in repoDir and its blob matches upstream. The blob check catches a pack that has the commit but not the objects under it.