1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2025-11-23 21:44:44 +02:00

Refactor docker backend and add more test coverage (#2700)

collection of some smal nit's and additions of tests
This commit is contained in:
6543
2024-07-21 21:28:10 +02:00
committed by GitHub
parent 720a076dd4
commit b2970dbf0d
5 changed files with 109 additions and 25 deletions

View File

@@ -91,6 +91,80 @@ func TestSplitVolumeParts(t *testing.T) {
}
}
// dummy vars to test against.
var (
testCmdStep = &backend.Step{
Name: "hello",
UUID: "f51821af-4cb8-435e-a3c2-3a684185d828",
Type: backend.StepTypeCommands,
Commands: []string{"echo \"hello world\"", "ls"},
Image: "alpine",
Environment: map[string]string{"SHELL": "/bin/zsh"},
}
testPluginStep = &backend.Step{
Name: "lint",
UUID: "d841ee40-e66e-4275-bb3f-55bf89744b21",
Type: backend.StepTypePlugin,
Image: "mstruebing/editorconfig-checker",
Environment: make(map[string]string),
}
testEngine = &docker{
info: types.Info{
Architecture: "x86_64",
OSType: "linux",
DefaultRuntime: "runc",
DockerRootDir: "/var/lib/docker",
OperatingSystem: "Archlinux",
Name: "SOME_HOSTNAME",
},
}
)
func TestToContainerName(t *testing.T) {
assert.EqualValues(t, "wp_f51821af-4cb8-435e-a3c2-3a684185d828", toContainerName(testCmdStep))
assert.EqualValues(t, "wp_d841ee40-e66e-4275-bb3f-55bf89744b21", toContainerName(testPluginStep))
}
func TestStepToConfig(t *testing.T) {
// StepTypeCommands
conf := testEngine.toConfig(testCmdStep)
if assert.NotNil(t, conf) {
assert.EqualValues(t, []string{"/bin/sh", "-c", "echo $CI_SCRIPT | base64 -d | /bin/sh -e"}, conf.Entrypoint)
assert.Nil(t, conf.Cmd)
assert.EqualValues(t, testCmdStep.UUID, conf.Labels["wp_uuid"])
}
// StepTypePlugin
conf = testEngine.toConfig(testPluginStep)
if assert.NotNil(t, conf) {
assert.Nil(t, conf.Cmd)
assert.EqualValues(t, testPluginStep.UUID, conf.Labels["wp_uuid"])
}
}
func TestToEnv(t *testing.T) {
assert.Nil(t, toEnv(nil))
assert.EqualValues(t, []string{"A=B"}, toEnv(map[string]string{"A": "B"}))
assert.ElementsMatch(t, []string{"A=B=C", "T=T"}, toEnv(map[string]string{"A": "B=C", "": "Z", "T": "T"}))
}
func TestToVol(t *testing.T) {
assert.Nil(t, toVol(nil))
assert.EqualValues(t, map[string]struct{}{"/test": {}}, toVol([]string{"test:/test"}))
}
func TestEncodeAuthToBase64(t *testing.T) {
res, err := encodeAuthToBase64(backend.Auth{})
assert.NoError(t, err)
assert.EqualValues(t, "e30=", res)
res, err = encodeAuthToBase64(backend.Auth{Username: "user", Password: "pwd"})
assert.NoError(t, err)
assert.EqualValues(t, "eyJ1c2VybmFtZSI6InVzZXIiLCJwYXNzd29yZCI6InB3ZCJ9", res)
}
func TestToConfigSmall(t *testing.T) {
engine := docker{info: types.Info{OSType: "linux/riscv64"}}