Documentation ¶
Index ¶
- Constants
- Variables
- func GenerateRSAKey() (*rsa.PrivateKey, error)
- func GetDockerImage(image string) string
- func GetSSHCommand(gerrit *Gerrit) (string, error)
- func ReadSSHKeys(path string) (ssh.PublicKey, ssh.Signer, error)
- func WriteRSAKey(key *rsa.PrivateKey, file *os.File) error
- type Change
- func (c *Change) Abandon() (*gerrit.ChangeInfo, error)
- func (c *Change) Add(relative string, mode os.FileMode, content string) error
- func (c *Change) AddFileComment(revision string, path string, line int, comment string) (*gerrit.ReviewResult, error)
- func (c *Change) AddTopLevelComment(revision string, comment string) (*gerrit.ReviewResult, error)
- func (c *Change) ApplyLabel(revision string, label string, value int) (*gerrit.ReviewResult, error)
- func (c *Change) Destroy() error
- func (c *Change) Push() error
- func (c *Change) Remove(relative string) error
- func (c *Change) Submit() (*gerrit.ChangeInfo, error)
- type Config
- type Container
- type CookieJar
- type Gerrit
- type HTTPClient
- type Repository
- func (r *Repository) Add(path string, mode os.FileMode, content []byte) error
- func (r *Repository) AddOriginFromContainer(container *Container, project string) error
- func (r *Repository) AddRemote(name string, uri string) error
- func (r *Repository) Amend() error
- func (r *Repository) ChangeID() (string, error)
- func (r *Repository) Commit(message string) error
- func (r *Repository) Destroy() error
- func (r *Repository) GetRemote(name string) (string, error)
- func (r *Repository) Git(args []string) (string, string, error)
- func (r *Repository) Push(ref string) error
- func (r *Repository) Remove(path string) error
- func (r *Repository) Status() (string, error)
- type SSHClient
- type SSHKey
Examples ¶
Constants ¶
const ( // VerifiedLabel is a string representing the 'Verified' label VerifiedLabel = "Verified" // CodeReviewLabel is a string representing the 'Code-Review' label CodeReviewLabel = "Code-Review" )
const ( // DefaultImageEnvironmentVar defines the environment variable NewConfig() // and the tests should be using to locate the default image override. DefaultImageEnvironmentVar = "GERRITTEST_DOCKER_IMAGE" // ExportedHTTPPort is the port exported by the docker container // where the HTTP service is running. ExportedHTTPPort = 8080 // ExportedSSHPort is the port exported by the docker container // where the SSHPort service is running. ExportedSSHPort = 29418 )
const ProjectName = "gerrittest"
ProjectName is used anywhere we need a default value (temp files, default field values, etc.
Variables ¶
var ( // GitCommand is the command used to run git commands. GitCommand = "git" // DefaultGitCommands contains a mapping of git commands // to their arguments. This is used by Repository for running // git commands. DefaultGitCommands = map[string][]string{ "status": {"status", "--porcelain"}, "config": {"config", "--local"}, "add": {"add", "--force"}, "remote-add": {"remote", "add"}, "get-remote-url": {"remote", "get-url"}, "commit": {"commit", "--allow-empty", "--message"}, "push": {"push", "--porcelain"}, "last-commit-message": {"log", "-n", "1", "--format=medium"}, "amend": {"commit", "--amend", "--no-edit", "--allow-empty"}, } // ErrRemoteDoesNotExist is returned by GetRemote if the requested // remote does not appear to exist. ErrRemoteDoesNotExist = errors.New("requested remote does not exist") //ErrFailedToLocateChange is returned by functions, such as Push(), that // expect to find a change number in the output from git. ErrFailedToLocateChange = errors.New("failed to locate ChangeID") // ErrRemoteDiffers is returned by any function that adds a remote // where the named remote already exists but with a different url. ErrRemoteDiffers = errors.New( "the requested remote exists but with a different url") // ErrNoCommits is returned by ChangeID if there are not any commits // to the repository yet. ErrNoCommits = errors.New("no commits") // RegexChangeID is used to match the Change-Id for a commit. RegexChangeID = regexp.MustCompile(`(?m)^\s+Change-Id: (I[a-f0-9]{40}).*$`) )
var ( // DefaultImage defines the default docker image to use in // NewConfig(). This may be overridden with the $GERRITTEST_DOCKER_IMAGE // environment variable. DefaultImage = "opalmer/gerrittest:2.14.4" )
var ( // DefaultRevision is the revision to use in the Change struct when // no other revision is provided. DefaultRevision = "current" )
Functions ¶
func GenerateRSAKey ¶
func GenerateRSAKey() (*rsa.PrivateKey, error)
GenerateRSAKey will generate and return an SSH key pair.
func GetDockerImage ¶
GetDockerImage returns the docker image to use to run the container. If "" is provided as the docker then we'll check $GERRITTEST_DOCKER_IMAGE first before returning the value defined in DefaultImage.
func GetSSHCommand ¶ added in v0.4.1
GetSSHCommand returns a string representing the ssh command to run to access the Gerrit container over ssh.
func ReadSSHKeys ¶
ReadSSHKeys will read the provided private key and return the public and private portions.
func WriteRSAKey ¶
func WriteRSAKey(key *rsa.PrivateKey, file *os.File) error
WriteRSAKey will take a private key and write out the public and private portions to disk. nolint: interfacer
Types ¶
type Change ¶ added in v0.3.0
type Change struct { ChangeID string Repo *Repository // contains filtered or unexported fields }
Change is used to interact with an manipulate a single change.
func (*Change) Add ¶ added in v0.3.0
Add writes a file to the repository but does not commit it. The added or modified path will be staged for commit.
func (*Change) AddFileComment ¶ added in v0.3.0
func (c *Change) AddFileComment(revision string, path string, line int, comment string) (*gerrit.ReviewResult, error)
AddFileComment will apply a comment to a specific file in a specific location
func (*Change) AddTopLevelComment ¶ added in v0.3.0
AddTopLevelComment will a single top level comment to the current change.
func (*Change) ApplyLabel ¶ added in v0.3.0
ApplyLabel will apply the requested label to the current change. Examples of labels include 'Code-Review +2' or 'Verified +1'. If a specific revision is not provided then 'current' will be used.
func (*Change) Destroy ¶ added in v0.3.0
Destroy is responsible for removing any files on disk related to this change.
type Config ¶
type Config struct { // Image is the name of docker image to run. Image string `json:"image"` // PortSSH is the port to expose the SSH service on. PortSSH uint16 `json:"port_ssh"` // PortHTTP is the port to expose the HTTP service on. PortHTTP uint16 `json:"port_http"` // Timeout is used to timeout commands and other contextual operations. Timeout time.Duration `json:"timeout"` // GitConfig contains key/value pairs to pass to 'git config' GitConfig map[string]string `json:"git"` // Context is used internally when starting or managing // containers and processes. If no context is provided then // context.Background() will be used. Context context.Context `json:"-"` // SSHKeys store information about one or more ssh keys. SSHKeys []*SSHKey `json:"ssh_keys"` // Username is the name of the Gerrit admin account to create. By default // this will be 'admin' unless otherwise specified. Username string `json:"username"` // Password is the password to create for the Gerrit admin user. If not // provided one will be randomly generated for you after the container // starts. Password string `json:"password"` // SkipSetup when true will cause the container to be started but // none of the final setup steps will be performed. SkipSetup bool `json:"skip_setup"` // CleanupContainer when true will cause the cleanup steps to destroy // the container running Gerrit. This defaults to true. CleanupContainer bool `json:"cleanup_container"` }
Config is used to tell the *runner struct what setup steps to perform, where to listen for services, etc.
type Container ¶
type Container struct { Docker *dockertest.DockerClient `json:"-"` HTTP *dockertest.Port `json:"http"` SSH *dockertest.Port `json:"ssh"` Image string `json:"image"` ID string `json:"id"` // contains filtered or unexported fields }
Container stores information about a Gerrit instance running inside of a container.
func NewContainer ¶
func NewContainer(parent context.Context, http uint16, ssh uint16, image string) (*Container, error)
NewContainer will create a new container using dockertest and return it. If you prefer to use an existing container use one of the LoadContainer* functions instead. This function will not return until the container has started and is listening on the requested ports.
Example ¶
You can start the Gerrit container using the NewContainer() function. In this example an random port will be used for both http and the default image will be used. This kind of setup is useful if you don't want to gerrittest to perform any setup steps for you.
container, err := NewContainer( context.Background(), dockertest.RandomPort, dockertest.RandomPort, "") if err != nil { log.Fatal(err) } // Terminate the container when you're done. if err := container.Terminate(); err != nil { log.Fatal(err) }
Output:
type CookieJar ¶
type CookieJar struct {
// contains filtered or unexported fields
}
CookieJar is an implementation of a cookie jar similar to http/cookiejar. This implementation is only intended for local development.
func NewCookieJar ¶
func NewCookieJar() *CookieJar
NewCookieJar constructs and returns a *CookieJar struct.
type Gerrit ¶
type Gerrit struct { Config *Config `json:"config"` Container *Container `json:"container"` HTTP *HTTPClient `json:"-"` HTTPPort *dockertest.Port `json:"http"` SSH *SSHClient `json:"-"` SSHPort *dockertest.Port `json:"ssh"` // contains filtered or unexported fields }
Gerrit is the central struct which combines multiple components of the gerrittest project. Use New() to construct this struct.
func LoadJSON ¶ added in v0.4.1
LoadJSON strictly loads the json file from the provided path. It makes no attempts to verify that the docker container is running orr
func New ¶
New constructs and returns a *Gerrit struct after all setup steps have been completed. Once this function returns Gerrit will be running in a container, an admin user will be created and a git repository will be setup pointing at the service in the container.
func NewFromJSON ¶
NewFromJSON reads information from a json file and returns a *Gerrit struct.
func (*Gerrit) CreateChange ¶ added in v0.3.0
CreateChange will return a *Change struct. If a change has already been created then that change will be returned instead of creating a new one.
Example ¶
Once you've started the service you'll want to setup Gerrit inside the container. Running Setup.Init will cause the administrative user to be created, generate an http API password and insert a public key for ssh access.
cfg := NewConfig() gerrit, err := New(cfg) if err != nil { log.Fatal(err) } files := map[string]string{ "README.md": "# Hello", "scripts/foo.bash": "echo 'foo'", } change, err := gerrit.CreateChange("testing", "test") if err != nil { log.Fatal(err) } defer change.Destroy() // nolint: errcheck defer gerrit.Destroy() // nolint: errcheck for relative, content := range files { if err := change.Add(relative, 0600, content); err != nil { log.Fatal(err) } } if err := change.Push(); err != nil { log.Fatal(err) } if _, err := change.ApplyLabel("1", CodeReviewLabel, 2); err != nil { log.Fatal(err) } if _, err := change.ApplyLabel("1", VerifiedLabel, 1); err != nil { log.Fatal(err) } if _, err := change.Submit(); err != nil { log.Fatal(err) }
Output:
func (*Gerrit) Destroy ¶
Destroy will destroy the container and all associated resources. Custom private keys or repositories will not be cleaned up.
func (*Gerrit) WriteJSONFile ¶
WriteJSONFile takes the current struct and writes the data to disk as json.
type HTTPClient ¶
type HTTPClient struct { Prefix string // contains filtered or unexported fields }
HTTPClient is a simple client for talking to Gerrit within a container. This is not intended as a replacement for go-gerrit. Instead, it's intended to get validate that Gerrit is setup correctly and then perform the final steps to get it ready for testing.
func NewHTTPClient ¶
func NewHTTPClient(config *Config, port *dockertest.Port) (*HTTPClient, error)
NewHTTPClient takes a *Service struct and returns an *HTTPClient. No validation to ensure the service is actually running is performed.
func (*HTTPClient) Gerrit ¶
func (h *HTTPClient) Gerrit() (*gerrit.Client, error)
Gerrit will return a *gerrit.Gerrit client. Note, the username and password must already be set and basic validation to ensure the client is setup properly is performed.
type Repository ¶
type Repository struct { SSHCommand string Root string Username string // contains filtered or unexported fields }
Repository is used to store information about an interact with a git repository. In the end, this is a thin wrapper around GitConfig commands.
func NewRepository ¶
func NewRepository(config *Config) (*Repository, error)
NewRepository constructs and returns a *Repository struct. It will also ensure the repository is properly setup before returning.
func (*Repository) Add ¶
Add is similar to Add() except it allows content to be created in addition to be added to the repo.
func (*Repository) AddOriginFromContainer ¶ added in v0.3.0
func (r *Repository) AddOriginFromContainer(container *Container, project string) error
AddOriginFromContainer adds a new remote based on the provided container.
func (*Repository) AddRemote ¶
func (r *Repository) AddRemote(name string, uri string) error
AddRemote will add a remote with the given name so long as it does not already exist.
func (*Repository) ChangeID ¶
func (r *Repository) ChangeID() (string, error)
ChangeID returns a string representing the change id of the last commit.
func (*Repository) Commit ¶
func (r *Repository) Commit(message string) error
Commit will add a new commit to the repository with the given message.
func (*Repository) Destroy ¶ added in v0.3.0
func (r *Repository) Destroy() error
Destroy will remove the entire repository from disk, useful for temporary repositories. This cannot be reversed.
func (*Repository) GetRemote ¶ added in v0.3.0
func (r *Repository) GetRemote(name string) (string, error)
GetRemote will return the url for the given remote name. If the requested remote does not exist ErrRemoteDoesNotExist will be returned.
func (*Repository) Git ¶
func (r *Repository) Git(args []string) (string, string, error)
Git runs git with the provided arguments. This also ensures the proper working path and environment are set before calling git.
func (*Repository) Push ¶
func (r *Repository) Push(ref string) error
Push will push changes to the given remote and reference. `ref` will default to 'HEAD:refs/for/master' if not provided.
func (*Repository) Remove ¶
func (r *Repository) Remove(path string) error
Remove removes the requested content from the repository.
func (*Repository) Status ¶
func (r *Repository) Status() (string, error)
Status returns the current status of the repository.
type SSHClient ¶
SSHClient implements an SSH client for talking to Gerrit.
func NewSSHClient ¶
func NewSSHClient(config *Config, port *dockertest.Port) (*SSHClient, error)
NewSSHClient produces an *SSHClient struct and attempts to connect to Gerrrit.
type SSHKey ¶ added in v0.4.0
type SSHKey struct { Public ssh.PublicKey `json:"-"` Private ssh.Signer `json:"-"` Path string `json:"path"` Generated bool `json:"generated"` Default bool `json:"default"` }
SSHKey contains information about an ssh key.
func LoadSSHKey ¶ added in v0.4.0
LoadSSHKey loads an SSH key from disk and returns a *SSHKey struct.