1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/pkg/mock/dockerRunner_test.go
Jk1484 ffc931aad1
feat(golangBuild): use 'unit' build tag to include tests during test execution (#4345)
* Added unit tag as argument. Added description to runTests command. Changed code generator to have unit build tag in generated unit test files.

* Added unit build tag to all unit test files.

* added to new unit test unit build tag

* Update verify-go.yml

* small fix

---------

Co-authored-by: Muhammadali Nazarov <Muhammadali.Nazarov@acronis.com>
Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
2023-05-03 21:02:11 +05:00

79 lines
2.6 KiB
Go

//go:build unit
// +build unit
package mock
import (
"errors"
"fmt"
"github.com/stretchr/testify/assert"
"os"
"testing"
)
func TestDockerExecRunnerAddExecConfig(t *testing.T) {
t.Run("no executable provided results in error", func(t *testing.T) {
dockerRunner := DockerExecRunner{}
err := dockerRunner.AddExecConfig("", DockerExecConfig{})
assert.Error(t, err, "'executable' needs to be provided")
assert.Nil(t, dockerRunner.executablesToWrap)
})
t.Run("no image provided results in error", func(t *testing.T) {
dockerRunner := DockerExecRunner{}
err := dockerRunner.AddExecConfig("useful-tool", DockerExecConfig{})
assert.Error(t, err, "the DockerExecConfig must specify a docker image")
})
t.Run("success case", func(t *testing.T) {
dockerRunner := DockerExecRunner{}
config := DockerExecConfig{Image: "image", Workspace: "/var/home"}
err := dockerRunner.AddExecConfig("useful-tool", config)
assert.NoError(t, err)
if assert.NotNil(t, dockerRunner.executablesToWrap) {
assert.Len(t, dockerRunner.executablesToWrap, 1)
assert.Equal(t, config, dockerRunner.executablesToWrap["useful-tool"])
}
})
}
func TestDockerExecRunnerRunExecutable(t *testing.T) {
dockerRunner := DockerExecRunner{}
_ = dockerRunner.AddExecConfig("useful-tool", DockerExecConfig{
Image: "image",
Workspace: "some/path",
})
t.Run("tool execution is wrapped", func(t *testing.T) {
mockRunner := ExecMockRunner{}
dockerRunner.Runner = &mockRunner
currentDir, err := os.Getwd()
assert.NoError(t, err)
err = dockerRunner.RunExecutable("useful-tool", "param", "--flag")
assert.NoError(t, err)
if assert.Equal(t, 1, len(mockRunner.Calls)) {
assert.Equal(t, ExecCall{
Exec: "docker",
Params: []string{"run", "--entrypoint=useful-tool", "-v", fmt.Sprintf("%s:some/path", currentDir), "image", "param", "--flag"},
}, mockRunner.Calls[0])
}
})
t.Run("tool execution is not wrapped", func(t *testing.T) {
mockRunner := ExecMockRunner{}
dockerRunner.Runner = &mockRunner
err := dockerRunner.RunExecutable("another-tool", "param", "--flag")
assert.NoError(t, err)
if assert.Equal(t, 1, len(mockRunner.Calls)) {
assert.Equal(t, ExecCall{
Exec: "another-tool",
Params: []string{"param", "--flag"},
}, mockRunner.Calls[0])
}
})
t.Run("error case", func(t *testing.T) {
mockRunner := ExecMockRunner{}
mockRunner.ShouldFailOnCommand = map[string]error{}
mockRunner.ShouldFailOnCommand["some-tool"] = errors.New("failed")
dockerRunner.Runner = &mockRunner
err := dockerRunner.RunExecutable("some-tool")
assert.Error(t, err, "failed")
})
}