2020-02-27 16:01:38 +02:00
|
|
|
// +build !release
|
|
|
|
|
2020-02-27 13:11:22 +02:00
|
|
|
package mock
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
2020-06-30 12:57:27 +02:00
|
|
|
|
|
|
|
"github.com/SAP/jenkins-library/pkg/command"
|
2020-02-27 13:11:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type ExecMockRunner struct {
|
|
|
|
Dir []string
|
2020-03-03 10:26:32 +02:00
|
|
|
Env []string
|
2020-06-30 12:57:27 +02:00
|
|
|
ExitCode int
|
2020-02-27 13:11:22 +02:00
|
|
|
Calls []ExecCall
|
2021-04-29 16:50:23 +02:00
|
|
|
stdin io.Reader
|
2020-02-27 13:11:22 +02:00
|
|
|
stdout io.Writer
|
|
|
|
stderr io.Writer
|
|
|
|
StdoutReturn map[string]string
|
|
|
|
ShouldFailOnCommand map[string]error
|
|
|
|
}
|
|
|
|
|
|
|
|
type ExecCall struct {
|
2020-06-16 11:42:51 +02:00
|
|
|
Execution *Execution
|
|
|
|
Async bool
|
|
|
|
Exec string
|
|
|
|
Params []string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Execution struct {
|
|
|
|
Killed bool
|
2020-02-27 13:11:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type ShellMockRunner struct {
|
|
|
|
Dir string
|
2020-03-03 10:26:32 +02:00
|
|
|
Env []string
|
2020-06-30 12:57:27 +02:00
|
|
|
ExitCode int
|
2020-02-27 13:11:22 +02:00
|
|
|
Calls []string
|
|
|
|
Shell []string
|
2021-04-29 16:50:23 +02:00
|
|
|
stdin io.Reader
|
2020-02-27 13:11:22 +02:00
|
|
|
stdout io.Writer
|
|
|
|
stderr io.Writer
|
|
|
|
StdoutReturn map[string]string
|
|
|
|
ShouldFailOnCommand map[string]error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *ExecMockRunner) SetDir(d string) {
|
|
|
|
m.Dir = append(m.Dir, d)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *ExecMockRunner) SetEnv(e []string) {
|
2020-07-28 17:06:17 +02:00
|
|
|
m.Env = e
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *ExecMockRunner) AppendEnv(e []string) {
|
2020-03-03 10:26:32 +02:00
|
|
|
m.Env = append(m.Env, e...)
|
2020-02-27 13:11:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *ExecMockRunner) RunExecutable(e string, p ...string) error {
|
|
|
|
|
|
|
|
exec := ExecCall{Exec: e, Params: p}
|
|
|
|
m.Calls = append(m.Calls, exec)
|
|
|
|
|
|
|
|
c := strings.Join(append([]string{e}, p...), " ")
|
|
|
|
|
|
|
|
return handleCall(c, m.StdoutReturn, m.ShouldFailOnCommand, m.stdout)
|
|
|
|
}
|
|
|
|
|
2020-06-30 12:57:27 +02:00
|
|
|
func (m *ExecMockRunner) GetExitCode() int {
|
|
|
|
return m.ExitCode
|
|
|
|
}
|
|
|
|
|
2020-06-16 11:42:51 +02:00
|
|
|
func (m *ExecMockRunner) RunExecutableInBackground(e string, p ...string) (command.Execution, error) {
|
|
|
|
|
|
|
|
execution := Execution{}
|
|
|
|
exec := ExecCall{Exec: e, Params: p, Async: true, Execution: &execution}
|
|
|
|
m.Calls = append(m.Calls, exec)
|
|
|
|
|
|
|
|
c := strings.Join(append([]string{e}, p...), " ")
|
|
|
|
|
|
|
|
err := handleCall(c, m.StdoutReturn, m.ShouldFailOnCommand, m.stdout)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &execution, nil
|
|
|
|
}
|
|
|
|
|
2021-04-29 16:50:23 +02:00
|
|
|
func (m *ExecMockRunner) Stdin(in io.Reader) {
|
|
|
|
m.stdin = in
|
|
|
|
}
|
|
|
|
|
2020-02-27 13:11:22 +02:00
|
|
|
func (m *ExecMockRunner) Stdout(out io.Writer) {
|
|
|
|
m.stdout = out
|
|
|
|
}
|
|
|
|
|
2021-02-05 11:54:38 +02:00
|
|
|
func (m *ExecMockRunner) GetStdout() io.Writer {
|
|
|
|
return m.stdout
|
|
|
|
}
|
|
|
|
|
2020-02-27 13:11:22 +02:00
|
|
|
func (m *ExecMockRunner) Stderr(err io.Writer) {
|
|
|
|
m.stderr = err
|
|
|
|
}
|
|
|
|
|
2021-02-05 11:54:38 +02:00
|
|
|
func (m *ExecMockRunner) GetStderr() io.Writer {
|
|
|
|
return m.stderr
|
|
|
|
}
|
|
|
|
|
2020-02-27 13:11:22 +02:00
|
|
|
func (m *ShellMockRunner) SetDir(d string) {
|
|
|
|
m.Dir = d
|
|
|
|
}
|
|
|
|
|
2020-03-03 11:26:34 +02:00
|
|
|
func (m *ShellMockRunner) SetEnv(e []string) {
|
|
|
|
m.Env = append(m.Env, e...)
|
|
|
|
}
|
|
|
|
|
2020-07-28 17:06:17 +02:00
|
|
|
func (m *ShellMockRunner) AppendEnv(e []string) {
|
2020-03-03 10:26:32 +02:00
|
|
|
m.Env = append(m.Env, e...)
|
2020-02-27 13:11:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *ShellMockRunner) RunShell(s string, c string) error {
|
|
|
|
|
|
|
|
m.Shell = append(m.Shell, s)
|
|
|
|
m.Calls = append(m.Calls, c)
|
|
|
|
|
|
|
|
return handleCall(c, m.StdoutReturn, m.ShouldFailOnCommand, m.stdout)
|
|
|
|
}
|
|
|
|
|
2020-06-30 12:57:27 +02:00
|
|
|
func (m *ShellMockRunner) GetExitCode() int {
|
|
|
|
return m.ExitCode
|
|
|
|
}
|
|
|
|
|
2020-06-16 11:42:51 +02:00
|
|
|
func (e *Execution) Kill() error {
|
|
|
|
e.Killed = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Execution) Wait() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-27 13:11:22 +02:00
|
|
|
func handleCall(call string, stdoutReturn map[string]string, shouldFailOnCommand map[string]error, stdout io.Writer) error {
|
|
|
|
|
|
|
|
if stdoutReturn != nil {
|
|
|
|
for k, v := range stdoutReturn {
|
|
|
|
|
|
|
|
found := k == call
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
|
|
|
|
r, e := regexp.Compile(k)
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
// we don't distinguish here between an error returned
|
|
|
|
// since it was configured or returning this error here
|
|
|
|
// indicating an invalid regex. Anyway: when running the
|
|
|
|
// test we will see it ...
|
|
|
|
}
|
|
|
|
if r.MatchString(call) {
|
|
|
|
found = true
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if found {
|
|
|
|
stdout.Write([]byte(v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if shouldFailOnCommand != nil {
|
|
|
|
for k, v := range shouldFailOnCommand {
|
|
|
|
|
|
|
|
found := k == call
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
r, e := regexp.Compile(k)
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
// we don't distinguish here between an error returned
|
|
|
|
// since it was configured or returning this error here
|
|
|
|
// indicating an invalid regex. Anyway: when running the
|
|
|
|
// test we will see it ...
|
|
|
|
}
|
|
|
|
if r.MatchString(call) {
|
|
|
|
found = true
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if found {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-29 16:50:23 +02:00
|
|
|
func (m *ShellMockRunner) Stdin(in io.Reader) {
|
|
|
|
m.stdin = in
|
|
|
|
}
|
|
|
|
|
2020-02-27 13:11:22 +02:00
|
|
|
func (m *ShellMockRunner) Stdout(out io.Writer) {
|
|
|
|
m.stdout = out
|
|
|
|
}
|
|
|
|
|
2021-02-05 11:54:38 +02:00
|
|
|
func (m *ShellMockRunner) GetStdout() io.Writer {
|
|
|
|
return m.stdout
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *ShellMockRunner) GetStderr() io.Writer {
|
|
|
|
return m.stderr
|
|
|
|
}
|
|
|
|
|
2020-02-27 13:11:22 +02:00
|
|
|
func (m *ShellMockRunner) Stderr(err io.Writer) {
|
|
|
|
m.stderr = err
|
|
|
|
}
|
|
|
|
|
|
|
|
type StepOptions struct {
|
|
|
|
TestParam string `json:"testParam,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func OpenFileMock(name string) (io.ReadCloser, error) {
|
|
|
|
var r string
|
|
|
|
switch name {
|
|
|
|
case "testDefaults.yml":
|
|
|
|
r = "general:\n testParam: testValue"
|
|
|
|
case "testDefaultsInvalid.yml":
|
|
|
|
r = "invalid yaml"
|
|
|
|
default:
|
|
|
|
r = ""
|
|
|
|
}
|
|
|
|
return ioutil.NopCloser(strings.NewReader(r)), nil
|
|
|
|
}
|