woodpecker/pipeline/backend/docker/convert_test.go
2024-01-09 06:01:34 +01:00

115 lines
2.7 KiB
Go

// Copyright 2022 Woodpecker Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package docker
import (
"reflect"
"testing"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/stretchr/testify/assert"
backend "go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/types"
)
func TestSplitVolumeParts(t *testing.T) {
testdata := []struct {
from string
to []string
success bool
}{
{
from: `Z::Z::rw`,
to: []string{`Z:`, `Z:`, `rw`},
success: true,
},
{
from: `Z:\:Z:\:rw`,
to: []string{`Z:\`, `Z:\`, `rw`},
success: true,
},
{
from: `Z:\git\refs:Z:\git\refs:rw`,
to: []string{`Z:\git\refs`, `Z:\git\refs`, `rw`},
success: true,
},
{
from: `Z:\git\refs:Z:\git\refs`,
to: []string{`Z:\git\refs`, `Z:\git\refs`},
success: true,
},
{
from: `Z:/:Z:/:rw`,
to: []string{`Z:/`, `Z:/`, `rw`},
success: true,
},
{
from: `Z:/git/refs:Z:/git/refs:rw`,
to: []string{`Z:/git/refs`, `Z:/git/refs`, `rw`},
success: true,
},
{
from: `Z:/git/refs:Z:/git/refs`,
to: []string{`Z:/git/refs`, `Z:/git/refs`},
success: true,
},
{
from: `/test:/test`,
to: []string{`/test`, `/test`},
success: true,
},
{
from: `test:/test`,
to: []string{`test`, `/test`},
success: true,
},
{
from: `test:test`,
to: []string{`test`, `test`},
success: true,
},
}
for _, test := range testdata {
results, err := splitVolumeParts(test.from)
if test.success != (err == nil) {
if reflect.DeepEqual(results, test.to) != test.success {
t.Errorf("Expect %q matches %q is %v", test.from, results, test.to)
}
}
}
}
func TestToConfigSmall(t *testing.T) {
engine := docker{info: types.Info{OSType: "linux/riscv64"}}
conf := engine.toConfig(&backend.Step{
Name: "test",
UUID: "09238932",
Commands: []string{"go test"},
})
assert.NotNil(t, conf)
assert.EqualValues(t, &container.Config{
AttachStdout: true,
AttachStderr: true,
Cmd: []string{"echo $CI_SCRIPT | base64 -d | /bin/sh -e"},
Entrypoint: []string{"/bin/sh", "-c"},
Labels: map[string]string{
"wp_step": "test",
"wp_uuid": "09238932",
},
}, conf)
}