Help us shape the future of Go! Take the 2026 Go Developer Survey

gitlabtest

package
v1.1.0 Latest Latest
Warning

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

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

Documentation

Overview

Package gitlabtest is an in-memory GitLab behind httptest, for tests.

It serves the REST paths the client uses and models the platform facts of docs/architecture.md §2 the server depends on: 404 for a private project, the catch-all route shape, offset and keyset pagination with their edges, 429 in both body shapes, a moved project, OAuth with immediately rotating refresh tokens, and quick actions run from a note. Everything in it is generated (§9.1).

Index

Constants

View Source
const (
	// PipelineFailed is a failed pipeline on ProjectAlpha's main.
	PipelineFailed = 61001
	// JobPassed passed; JobFailed failed its script and has a long log
	// with the synthetic token in it; JobAllowed failed and was allowed
	// to; JobManual waits for a person.
	JobPassed  = 70001
	JobFailed  = 70002
	JobAllowed = 70003
	JobManual  = 70004
	// BridgeFailed is a trigger job on PipelineFailed whose downstream
	// pipeline, DownstreamFailed, failed.
	BridgeFailed     = 79001
	DownstreamFailed = 62001
	// FakeToken is the synthetic token JobFailed's log prints.
	FakeToken = "glpat-" + "EXAMPLE0000000000000000000"
	// LogFiller is how many lines of test output JobFailed's log holds,
	// enough to be longer than one default window.
	LogFiller = 2500
)

Fixture CI ids tests address.

View Source
const (
	// ProjectAlpha is public; alice maintains it. It has issues, merge
	// requests, discussions, files, branches and commits.
	ProjectAlpha = "example-group/alpha"
	// ProjectBeta is private under a subgroup; alice is a member.
	ProjectBeta = "example-group/sub/beta"
	// ProjectSecret is private and alice is not a member, so it answers
	// 404 exactly as a missing project does.
	ProjectSecret = "example-group/secret"
	// ProjectMoved is the old path of ProjectAlpha: a GET redirects, any
	// other method is 405.
	ProjectMoved = "example-group/old-alpha"
	// GroupTop and GroupSub are the two groups.
	GroupTop = "example-group"
	GroupSub = "example-group/sub"
	// DefaultUser is who a default token and an authorization act as.
	DefaultUser = "alice"
)

Fixture names tests address.

View Source
const (
	// MilestoneActive and MilestoneClosed are ProjectAlpha's; the group
	// milestone is GroupTop's.
	MilestoneActive = 90001
	MilestoneClosed = 90002
	MilestoneGroup  = 90003
	// GroupLabel is a label GroupTop defines for every project under it.
	GroupLabel = "group-wide"
	// TodoAssigned, TodoReview and TodoDone are alice's to-do items.
	TodoAssigned = 95001
	TodoReview   = 95002
	TodoDone     = 95003
)

Fixture ids tests address.

View Source
const (
	TagRelease = "v1.0"
	TagPlain   = "v0.9"
)

Tags on ProjectAlpha: an annotated, protected tag with a release on release/1.0's head, and a lightweight one on main's head.

View Source
const (
	// SnippetAlpha is bob's snippet in ProjectAlpha, two files long.
	SnippetAlpha = 80001
	// SnippetPersonal is alice's private personal snippet.
	SnippetPersonal = 80002
	// WikiNested is a wiki page whose slug holds a slash.
	WikiNested = "guides/setup"
)

Fixture ids and names tests address.

View Source
const ClientID = "gitlabtest-application-id"

ClientID is the OAuth application's id.

View Source
const ConflictSHA = "c0ffee0000000000000000000000000000000000"

ConflictSHA is a commit whose cherry-pick or revert conflicts on every branch.

Variables

View Source
var Users = []string{"alice", "bob", "carol", "dave"}

Users is the synthetic user list, in id order from 1001.

Functions

This section is empty.

Types

type Fault

type Fault struct {
	Method string
	Path   string
	Times  int
	Status int
	Header http.Header
	Body   string
	// Delay holds the answer before its headers are written.
	Delay time.Duration
	// StallAfter writes the headers and part of Body, then waits this
	// long before the rest.
	StallAfter time.Duration
	// AfterApply serves the request first, so a write lands, then
	// discards that answer and sends the fault's: the answer to a write
	// that succeeded was lost.
	AfterApply bool
}

Fault is an injected answer. It replaces the next Times requests whose method (empty for any) and escaped path under /api/v4 (prefix match, "/projects" matches "/projects/2001/issues") match.

func Application429

func Application429(path string, retryAfter, times int) Fault

Application429 is an application limit's 429: JSON, with Retry-After and no RateLimit-* headers.

func RackAttack429

func RackAttack429(path string, retryAfter, times int) Fault

RackAttack429 is the instance throttle's 429: a plain-text body with Retry-After and RateLimit-* headers.

type Options

type Options struct {
	// TotalLimit is the count past which X-Total and X-Total-Pages are
	// dropped. GitLab's is 10,000; a test sets a small one.
	TotalLimit int
	// OffsetCap is the offset past which an endpoint with keyset
	// pagination answers 405. GitLab's is 50,000.
	OffsetCap int
	// ExtraProjects adds public projects example-group/bulk-0001… for
	// paging tests.
	ExtraProjects int
	// AlphaIssues, AlphaMergeRequests and AlphaCommits size ProjectAlpha.
	// Defaults 25, 3 and 30.
	AlphaIssues        int
	AlphaMergeRequests int
	AlphaCommits       int
	// Version and Enterprise are what /metadata reports. Default
	// "19.4.0", Community Edition.
	Version    string
	Enterprise bool

	// Confidential makes the OAuth application confidential: the token
	// endpoint then refuses a request without ClientSecret.
	Confidential bool
	ClientSecret string
	// RedirectURIs are the application's registered redirect URIs.
	// Default http://127.0.0.1/callback.
	RedirectURIs []string
	// AuthorizeAs is the user an authorization signs in as. Default alice.
	AuthorizeAs string
	// AccessTokenTTL is expires_in. Default 7,200 s.
	AccessTokenTTL time.Duration
	// Now is the clock. Default time.Now.
	Now func() time.Time
	// MoveDropsQuery makes the redirect for a moved project carry the
	// new path without the request's query string.
	MoveDropsQuery bool
}

Options shape the instance. Zero values take the defaults.

type Request

type Request struct {
	Method        string
	EscapedPath   string
	RawQuery      string
	Authorization string
	UserAgent     string
	// IfUnmodifiedSince is the header a conditional delete sends.
	IfUnmodifiedSince string
}

Request is one request the instance received.

type Server

type Server struct {
	*httptest.Server
	// contains filtered or unexported fields
}

Server is the in-memory instance. URL is its base, and the instance to configure is that URL.

func New

func New(t testing.TB, opts Options) *Server

New starts an instance and stops it when the test ends.

func (*Server) Approvals

func (s *Server) Approvals(projectPath string, iid int64, user string) (gitlab.Approvals, bool)

Approvals is a merge request's approvals as user sees them.

func (*Server) BranchHead

func (s *Server) BranchHead(projectPath, branch string) (gitlab.Commit, bool)

BranchHead returns a branch's head commit.

func (*Server) Comment

func (s *Server) Comment(projectPath, kind string, iid int64, user, body string) (gitlab.Note, bool)

Comment plants a comment by user on an issue (kind "issue") or a merge request (kind "mr"), as content someone else wrote. It runs no quick action: it is a fixture, not a write through the API.

func (*Server) Discussions

func (s *Server) Discussions(projectPath, kind string, iid int64) []gitlab.Discussion

Discussions returns a copy of an issue's (kind "issue") or a merge request's (kind "mr") discussions.

func (*Server) Drafts

func (s *Server) Drafts(projectPath string, iid int64) []gitlab.DraftNote

Drafts returns a copy of every draft on a merge request, whoever wrote it.

func (*Server) EditWikiPage

func (s *Server) EditWikiPage(projectPath, slug, content string) bool

EditWikiPage changes a page's content, as an edit made elsewhere does.

func (*Server) FileAt

func (s *Server) FileAt(projectPath, branch, path string) (string, bool)

FileAt returns a file's content on a branch.

func (*Server) Inject

func (s *Server) Inject(f Fault)

Inject adds a fault.

func (*Server) Issue

func (s *Server) Issue(projectPath string, iid int64) (gitlab.Issue, bool)

Issue returns a copy of an issue, for a test to check what a write did.

func (s *Server) IssueLinks() int

IssueLinks counts the links the instance holds.

func (*Server) Job

func (s *Server) Job(projectPath string, id int64) (gitlab.Job, bool)

Job is a job as the instance holds it.

func (*Server) JobInputsSent

func (s *Server) JobInputsSent() map[string]any

JobInputsSent are the input values the last job retry or play sent.

func (*Server) MergeRequest

func (s *Server) MergeRequest(projectPath string, iid int64) (gitlab.MergeRequest, bool)

MergeRequest returns a copy of a merge request, for a test to check what a write did.

func (*Server) Pipeline

func (s *Server) Pipeline(projectPath string, id int64) (gitlab.PipelineDetail, bool)

Pipeline is a pipeline as the instance holds it.

func (*Server) ProjectID

func (s *Server) ProjectID(path string) int64

ProjectID returns a fixture project's numeric id.

func (*Server) PushTo

func (s *Server) PushTo(projectPath, branch string) (string, bool)

PushTo adds a commit to a branch, as someone else's push does.

func (*Server) Rebases

func (s *Server) Rebases() int

Rebases counts the rebases asked for.

func (*Server) Refreshes

func (s *Server) Refreshes() int

Refreshes counts refresh grants that succeeded, for a test of the cross-process lock.

func (*Server) Release

func (s *Server) Release(projectPath, tag string) (gitlab.Release, bool)

Release is a project's release of a tag.

func (*Server) Requests

func (s *Server) Requests() []Request

Requests returns what the instance received so far.

func (*Server) ResetRequests

func (s *Server) ResetRequests()

ResetRequests forgets the recorded requests.

func (*Server) ReviewerState

func (s *Server) ReviewerState(projectPath string, iid int64, user string) string

ReviewerState is the reviewer state a user last submitted with a review of a merge request, "" for none.

func (*Server) Revoke

func (s *Server) Revoke(token string)

Revoke revokes an access or refresh token.

func (*Server) SetHeadPipelineStatus

func (s *Server) SetHeadPipelineStatus(projectPath string, iid int64, status string) bool

SetHeadPipelineStatus sets the status of a merge request's head pipeline, as CI running or failing does.

func (*Server) SetIssuesAccess

func (s *Server) SetIssuesAccess(projectPath, level string) bool

SetIssuesAccess sets who sees a project's issues: enabled, private or disabled.

func (*Server) SetJobLog

func (s *Server) SetJobLog(projectPath string, id int64, status, log string, archived bool) bool

SetJobLog replaces a job's log and state. An archived log is also a trace artifact of its size, as GitLab lists one once it archives the log.

func (*Server) SetMRDiffs

func (s *Server) SetMRDiffs(projectPath string, iid int64, diffs []gitlab.Diff) bool

SetMRDiffs replaces the files a merge request changes.

func (*Server) SetMergeStatus

func (s *Server) SetMergeStatus(projectPath string, iid int64, status string) bool

SetMergeStatus sets a merge request's detailed_merge_status, as GitLab's checks do.

func (*Server) SnippetVisibility

func (s *Server) SnippetVisibility(id int64) string

SnippetVisibility is a snippet's visibility, "" when there is none.

func (*Server) TodoState

func (s *Server) TodoState(id int64) string

TodoState returns a to-do item's state, "" for an unknown one.

func (*Server) Token

func (s *Server) Token() string

Token issues an access token for alice with the api scope.

func (*Server) TokenFor

func (s *Server) TokenFor(user string, scopes ...string) string

TokenFor issues an access token for a user with the given scopes.

func (*Server) TouchNote

func (s *Server) TouchNote(projectPath, kind string, iid, noteID int64, at time.Time) bool

TouchNote moves a note's updated_at, as an edit made elsewhere does.

func (*Server) WikiPage

func (s *Server) WikiPage(projectPath, slug string) (gitlab.WikiPage, bool)

WikiPage is a wiki page as the instance holds it.

Jump to

Keyboard shortcuts

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