Documentation ¶
Index ¶
- func OpenFileMock(name string) (io.ReadCloser, error)
- type DockerExecConfig
- type DockerExecRunner
- func (d *DockerExecRunner) AddExecConfig(executable string, config DockerExecConfig) error
- func (d *DockerExecRunner) RunExecutable(executable string, parameters ...string) error
- func (d *DockerExecRunner) SetDir(dir string)
- func (d *DockerExecRunner) SetEnv(env []string)
- func (d *DockerExecRunner) Stderr(err io.Writer)
- func (d *DockerExecRunner) Stdout(out io.Writer)
- type ExecCall
- type ExecMockRunner
- func (m *ExecMockRunner) GetExitCode() int
- func (m *ExecMockRunner) RunExecutable(e string, p ...string) error
- func (m *ExecMockRunner) RunExecutableInBackground(e string, p ...string) (command.Execution, error)
- func (m *ExecMockRunner) SetDir(d string)
- func (m *ExecMockRunner) SetEnv(e []string)
- func (m *ExecMockRunner) Stderr(err io.Writer)
- func (m *ExecMockRunner) Stdout(out io.Writer)
- type Execution
- type FilesMock
- func (f *FilesMock) AddDir(path string)
- func (f *FilesMock) AddDirWithMode(path string, mode os.FileMode)
- func (f *FilesMock) AddFile(path string, contents []byte)
- func (f *FilesMock) AddFileWithMode(path string, contents []byte, mode os.FileMode)
- func (f *FilesMock) Chdir(path string) error
- func (f *FilesMock) Chmod(path string, mode os.FileMode) error
- func (f *FilesMock) Copy(src, dst string) (int64, error)
- func (f *FilesMock) DirExists(path string) (bool, error)
- func (f *FilesMock) FileExists(path string) (bool, error)
- func (f *FilesMock) FileRead(path string) ([]byte, error)
- func (f *FilesMock) FileRemove(path string) error
- func (f *FilesMock) FileWrite(path string, content []byte, mode os.FileMode) error
- func (f *FilesMock) Getwd() (string, error)
- func (f *FilesMock) Glob(pattern string) ([]string, error)
- func (f *FilesMock) HasFile(path string) bool
- func (f *FilesMock) HasRemovedFile(path string) bool
- func (f *FilesMock) HasWrittenFile(path string) bool
- func (f *FilesMock) MkdirAll(path string, mode os.FileMode) error
- func (f *FilesMock) Stat(name string) (os.FileInfo, error)
- type ShellMockRunner
- func (m *ShellMockRunner) AddToEnv(e []string)
- func (m *ShellMockRunner) GetExitCode() int
- func (m *ShellMockRunner) RunShell(s string, c string) error
- func (m *ShellMockRunner) SetDir(d string)
- func (m *ShellMockRunner) SetEnv(e []string)
- func (m *ShellMockRunner) Stderr(err io.Writer)
- func (m *ShellMockRunner) Stdout(out io.Writer)
- type StepOptions
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func OpenFileMock ¶
func OpenFileMock(name string) (io.ReadCloser, error)
Types ¶
type DockerExecConfig ¶ added in v1.40.0
type DockerExecConfig struct { // Image is the fully qualified docker image name that is passed to docker for this tool. Image string // Workspace is the (optional) directory to which the current working directory is mapped // within the docker container. Workspace string }
DockerExecConfig is the configuration for an individual tool that shall be executed in docker.
type DockerExecRunner ¶ added in v1.40.0
type DockerExecRunner struct { // Runner is the ExecRunner to which all executions are forwarded in the end. Runner baseRunner // contains filtered or unexported fields }
DockerExecRunner can be used "in place" of another ExecRunner in order to transparently execute commands within a docker container. One use-case is to test locally with tools that are not available on the current platform. When entering the run*() function of a step implementation, a DockerExecRunner can be wrapped around a command.Command{} an be configured to run certain executables within docker.
func (*DockerExecRunner) AddExecConfig ¶ added in v1.40.0
func (d *DockerExecRunner) AddExecConfig(executable string, config DockerExecConfig) error
AddExecConfig needs to be called to store a configuration for a specific executable, in order to run this executable within docker.
func (*DockerExecRunner) RunExecutable ¶ added in v1.40.0
func (d *DockerExecRunner) RunExecutable(executable string, parameters ...string) error
RunExecutable runs the provided executable within docker, if a DockerExecConfig has been previously associated with this executable via AddExecConfig(). Otherwise runs it directly. The BaseRunner is used for execution in any case.
Example ¶
package main import ( "bytes" "fmt" "github.com/SAP/jenkins-library/pkg/command" "github.com/SAP/jenkins-library/pkg/mock" "io" "strings" ) type ExecRunner interface { SetDir(d string) SetEnv(e []string) Stdout(out io.Writer) Stderr(err io.Writer) RunExecutable(e string, p ...string) error } func getMavenVersion(runner ExecRunner) (string, error) { output := bytes.Buffer{} runner.Stdout(&output) err := runner.RunExecutable("mvn", "--version") if err != nil { return "", fmt.Errorf("failed to run maven: %w", err) } logLines := strings.Split(output.String(), "\n") if len(logLines) < 1 { return "", fmt.Errorf("failed to obtain maven output") } return logLines[0], nil } func main() { // getMavenVersion(runner ExecRunner) executes the command "mvn --version" // and returns the command output as string runner := command.Command{} localMavenVersion, _ := getMavenVersion(&runner) dockerRunner := mock.DockerExecRunner{Runner: &runner} _ = dockerRunner.AddExecConfig("mvn", mock.DockerExecConfig{ Image: "maven:3.6.1-jdk-8", }) dockerMavenVersion, _ := getMavenVersion(&dockerRunner) fmt.Printf("Your local mvn version is %v, while the version in docker is %v", localMavenVersion, dockerMavenVersion) }
Output:
func (*DockerExecRunner) SetDir ¶ added in v1.40.0
func (d *DockerExecRunner) SetDir(dir string)
SetDir directly forwards to the provided BaseRunner.
func (*DockerExecRunner) SetEnv ¶ added in v1.40.0
func (d *DockerExecRunner) SetEnv(env []string)
SetEnv directly forwards to the provided BaseRunner.
func (*DockerExecRunner) Stderr ¶ added in v1.40.0
func (d *DockerExecRunner) Stderr(err io.Writer)
Stderr directly forwards to the provided BaseRunner.
func (*DockerExecRunner) Stdout ¶ added in v1.40.0
func (d *DockerExecRunner) Stdout(out io.Writer)
Stdout directly forwards to the provided BaseRunner.
type ExecMockRunner ¶
type ExecMockRunner struct { Dir []string Env []string ExitCode int Calls []ExecCall StdoutReturn map[string]string ShouldFailOnCommand map[string]error // contains filtered or unexported fields }
func (*ExecMockRunner) GetExitCode ¶ added in v1.51.0
func (m *ExecMockRunner) GetExitCode() int
func (*ExecMockRunner) RunExecutable ¶
func (m *ExecMockRunner) RunExecutable(e string, p ...string) error
func (*ExecMockRunner) RunExecutableInBackground ¶ added in v1.45.0
func (*ExecMockRunner) SetDir ¶
func (m *ExecMockRunner) SetDir(d string)
func (*ExecMockRunner) SetEnv ¶
func (m *ExecMockRunner) SetEnv(e []string)
func (*ExecMockRunner) Stderr ¶
func (m *ExecMockRunner) Stderr(err io.Writer)
func (*ExecMockRunner) Stdout ¶
func (m *ExecMockRunner) Stdout(out io.Writer)
type FilesMock ¶ added in v1.19.0
type FilesMock struct { Separator string // contains filtered or unexported fields }
FilesMock implements the functions from piperutils.Files with an in-memory file system.
func (*FilesMock) AddDir ¶ added in v1.43.0
AddDir establishes the existence of a virtual directory. The directory is add with default mode 755
func (*FilesMock) AddDirWithMode ¶ added in v1.50.0
AddDirWithMode establishes the existence of a virtual directory.
func (*FilesMock) AddFile ¶ added in v1.43.0
AddFile establishes the existence of a virtual file. The file is added with mode 644
func (*FilesMock) AddFileWithMode ¶ added in v1.50.0
AddFileWithMode establishes the existence of a virtual file.
func (*FilesMock) Chdir ¶ added in v1.43.0
Chdir changes virtually in to the given directory. The directory needs to exist according to the files and directories via AddFile() and AddDirectory(). The implementation does not support relative path components such as "..".
func (*FilesMock) Copy ¶ added in v1.19.0
Copy checks if content has been associated with the given src path, and if so copies it under the given path dst.
func (*FilesMock) DirExists ¶ added in v1.43.0
DirExists returns true, if the given path is a previously added directory, or a parent directory for any of the previously added files.
func (*FilesMock) FileExists ¶ added in v1.19.0
FileExists returns true if file content has been associated with the given path, false otherwise. Only relative paths are supported.
func (*FilesMock) FileRead ¶ added in v1.19.0
FileRead returns the content previously associated with the given path via AddFile(), or an error if no content has been associated.
func (*FilesMock) FileRemove ¶ added in v1.43.0
FileRemove deletes the association of the given path with any content and records the removal of the file. If the path has not been registered before, it returns an error.
func (*FilesMock) FileWrite ¶ added in v1.19.0
FileWrite just forwards to AddFile(), i.e. the content is associated with the given path.
func (*FilesMock) Getwd ¶ added in v1.43.0
Getwd returns the rooted current virtual working directory
func (*FilesMock) Glob ¶ added in v1.43.0
Glob returns an array of path strings which match the given glob-pattern. Double star matching is supported.
func (*FilesMock) HasFile ¶ added in v1.43.0
HasFile returns true if the virtual file system contains an entry for the given path.
func (*FilesMock) HasRemovedFile ¶ added in v1.43.0
HasRemovedFile returns true if the virtual file system at one point contained an entry for the given path, and it was removed via FileRemove().
func (*FilesMock) HasWrittenFile ¶ added in v1.49.0
HasWrittenFile returns true if the virtual file system at one point contained an entry for the given path, and it was written via FileWrite().
type ShellMockRunner ¶
type ShellMockRunner struct { Dir string Env []string ExitCode int Calls []string Shell []string StdoutReturn map[string]string ShouldFailOnCommand map[string]error // contains filtered or unexported fields }
func (*ShellMockRunner) AddToEnv ¶
func (m *ShellMockRunner) AddToEnv(e []string)
func (*ShellMockRunner) GetExitCode ¶ added in v1.51.0
func (m *ShellMockRunner) GetExitCode() int
func (*ShellMockRunner) SetDir ¶
func (m *ShellMockRunner) SetDir(d string)
func (*ShellMockRunner) SetEnv ¶ added in v1.16.0
func (m *ShellMockRunner) SetEnv(e []string)
func (*ShellMockRunner) Stderr ¶
func (m *ShellMockRunner) Stderr(err io.Writer)
func (*ShellMockRunner) Stdout ¶
func (m *ShellMockRunner) Stdout(out io.Writer)
type StepOptions ¶
type StepOptions struct {
TestParam string `json:"testParam,omitempty"`
}