2019-04-06 15:44:04 +02:00
|
|
|
package yaml
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-12-04 14:23:33 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-11-14 21:16:03 +02:00
|
|
|
"gopkg.in/yaml.v3"
|
2021-10-30 17:52:02 +02:00
|
|
|
|
2022-01-17 15:43:30 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/frontend/yaml/constraint"
|
2021-10-30 17:52:02 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/frontend/yaml/types"
|
2019-04-06 15:44:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var containerYaml = []byte(`
|
|
|
|
image: golang:latest
|
|
|
|
auth_config:
|
|
|
|
username: janedoe
|
|
|
|
password: password
|
|
|
|
cap_add: [ ALL ]
|
|
|
|
cap_drop: [ NET_ADMIN, SYS_ADMIN ]
|
|
|
|
command: bundle exec thin -p 3000
|
|
|
|
commands:
|
|
|
|
- go build
|
|
|
|
- go test
|
|
|
|
cpu_quota: 11
|
|
|
|
cpuset: 1,2
|
|
|
|
cpu_shares: 99
|
|
|
|
detach: true
|
|
|
|
devices:
|
|
|
|
- /dev/ttyUSB0:/dev/ttyUSB0
|
|
|
|
dns: 8.8.8.8
|
|
|
|
dns_search: example.com
|
|
|
|
entrypoint: /code/entrypoint.sh
|
|
|
|
environment:
|
|
|
|
- RACK_ENV=development
|
|
|
|
- SHOW=true
|
|
|
|
extra_hosts:
|
|
|
|
- somehost:162.242.195.82
|
|
|
|
- otherhost:50.31.209.229
|
|
|
|
isolation: hyperv
|
|
|
|
name: my-build-container
|
|
|
|
network_mode: bridge
|
|
|
|
networks:
|
|
|
|
- some-network
|
|
|
|
- other-network
|
|
|
|
pull: true
|
|
|
|
privileged: true
|
|
|
|
labels:
|
|
|
|
com.example.type: build
|
|
|
|
com.example.team: frontend
|
|
|
|
shm_size: 1kb
|
|
|
|
mem_limit: 1kb
|
|
|
|
memswap_limit: 1kb
|
|
|
|
mem_swappiness: 1kb
|
|
|
|
volumes:
|
|
|
|
- /var/lib/mysql
|
|
|
|
- /opt/data:/var/lib/mysql
|
|
|
|
- /etc/configs:/etc/configs/:ro
|
|
|
|
tmpfs:
|
|
|
|
- /var/lib/test
|
|
|
|
when:
|
|
|
|
branch: master
|
2021-12-04 17:44:18 +02:00
|
|
|
settings:
|
|
|
|
foo: bar
|
|
|
|
baz: false
|
|
|
|
deprecated_setting: fallback
|
2019-04-06 15:44:04 +02:00
|
|
|
`)
|
|
|
|
|
|
|
|
func TestUnmarshalContainer(t *testing.T) {
|
|
|
|
want := Container{
|
|
|
|
AuthConfig: AuthConfig{
|
|
|
|
Username: "janedoe",
|
|
|
|
Password: "password",
|
|
|
|
},
|
|
|
|
CapAdd: []string{"ALL"},
|
|
|
|
CapDrop: []string{"NET_ADMIN", "SYS_ADMIN"},
|
2021-10-30 17:52:02 +02:00
|
|
|
Command: types.Command{"bundle", "exec", "thin", "-p", "3000"},
|
|
|
|
Commands: types.Stringorslice{"go build", "go test"},
|
|
|
|
CPUQuota: types.StringorInt(11),
|
2019-04-06 15:44:04 +02:00
|
|
|
CPUSet: "1,2",
|
2021-10-30 17:52:02 +02:00
|
|
|
CPUShares: types.StringorInt(99),
|
2019-04-06 15:44:04 +02:00
|
|
|
Detached: true,
|
|
|
|
Devices: []string{"/dev/ttyUSB0:/dev/ttyUSB0"},
|
2021-10-30 17:52:02 +02:00
|
|
|
DNS: types.Stringorslice{"8.8.8.8"},
|
|
|
|
DNSSearch: types.Stringorslice{"example.com"},
|
|
|
|
Entrypoint: types.Command{"/code/entrypoint.sh"},
|
|
|
|
Environment: types.SliceorMap{"RACK_ENV": "development", "SHOW": "true"},
|
2019-04-06 15:44:04 +02:00
|
|
|
ExtraHosts: []string{"somehost:162.242.195.82", "otherhost:50.31.209.229"},
|
|
|
|
Image: "golang:latest",
|
|
|
|
Isolation: "hyperv",
|
2021-10-30 17:52:02 +02:00
|
|
|
Labels: types.SliceorMap{"com.example.type": "build", "com.example.team": "frontend"},
|
|
|
|
MemLimit: types.MemStringorInt(1024),
|
|
|
|
MemSwapLimit: types.MemStringorInt(1024),
|
|
|
|
MemSwappiness: types.MemStringorInt(1024),
|
2019-04-06 15:44:04 +02:00
|
|
|
Name: "my-build-container",
|
2021-10-30 17:52:02 +02:00
|
|
|
Networks: types.Networks{
|
|
|
|
Networks: []*types.Network{
|
2019-04-06 15:44:04 +02:00
|
|
|
{Name: "some-network"},
|
|
|
|
{Name: "other-network"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
NetworkMode: "bridge",
|
|
|
|
Pull: true,
|
|
|
|
Privileged: true,
|
2021-10-30 17:52:02 +02:00
|
|
|
ShmSize: types.MemStringorInt(1024),
|
|
|
|
Tmpfs: types.Stringorslice{"/var/lib/test"},
|
|
|
|
Volumes: types.Volumes{
|
|
|
|
Volumes: []*types.Volume{
|
2019-04-06 15:44:04 +02:00
|
|
|
{Source: "", Destination: "/var/lib/mysql"},
|
|
|
|
{Source: "/opt/data", Destination: "/var/lib/mysql"},
|
|
|
|
{Source: "/etc/configs", Destination: "/etc/configs/", AccessMode: "ro"},
|
|
|
|
},
|
|
|
|
},
|
2022-01-17 15:43:30 +02:00
|
|
|
Constraints: constraint.Constraints{
|
|
|
|
Branch: constraint.List{
|
2019-04-06 15:44:04 +02:00
|
|
|
Include: []string{"master"},
|
|
|
|
},
|
|
|
|
},
|
2021-12-04 17:44:18 +02:00
|
|
|
Settings: map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
"baz": false,
|
|
|
|
},
|
|
|
|
Vargs: map[string]interface{}{
|
|
|
|
"deprecated_setting": "fallback",
|
|
|
|
},
|
2019-04-06 15:44:04 +02:00
|
|
|
}
|
|
|
|
got := Container{}
|
|
|
|
err := yaml.Unmarshal(containerYaml, &got)
|
2021-12-04 14:23:33 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, want, got, "problem parsing container")
|
2019-04-06 15:44:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestUnmarshalContainersErr unmarshals a map of containers. The order is
|
|
|
|
// retained and the container key may be used as the container name if a
|
|
|
|
// name is not explicitly provided.
|
|
|
|
func TestUnmarshalContainers(t *testing.T) {
|
|
|
|
testdata := []struct {
|
|
|
|
from string
|
|
|
|
want []*Container
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
from: "build: { image: golang }",
|
|
|
|
want: []*Container{
|
|
|
|
{
|
|
|
|
Name: "build",
|
|
|
|
Image: "golang",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-12-04 17:44:18 +02:00
|
|
|
from: "test: { name: unit_test, image: node, deprecated_setting: fallback, settings: { normal_setting: true } }",
|
2019-04-06 15:44:04 +02:00
|
|
|
want: []*Container{
|
|
|
|
{
|
|
|
|
Name: "unit_test",
|
|
|
|
Image: "node",
|
2021-12-04 17:44:18 +02:00
|
|
|
Settings: map[string]interface{}{
|
|
|
|
"deprecated_setting": "fallback",
|
|
|
|
"normal_setting": true,
|
|
|
|
},
|
2019-04-06 15:44:04 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-12-08 19:17:52 +02:00
|
|
|
{
|
|
|
|
from: `publish-agent:
|
|
|
|
group: bundle
|
|
|
|
image: print/env
|
|
|
|
repo: woodpeckerci/woodpecker-agent
|
|
|
|
dockerfile: docker/Dockerfile.agent
|
|
|
|
secrets: [docker_username, docker_password]
|
|
|
|
tag: [next, latest]
|
|
|
|
dry_run: true
|
|
|
|
when:
|
|
|
|
branch: ${CI_REPO_DEFAULT_BRANCH}
|
|
|
|
event: push`,
|
|
|
|
want: []*Container{
|
|
|
|
{
|
|
|
|
Name: "publish-agent",
|
|
|
|
Image: "print/env",
|
|
|
|
Group: "bundle",
|
|
|
|
Secrets: Secrets{Secrets: []*Secret{{
|
|
|
|
Source: "docker_username",
|
|
|
|
Target: "docker_username",
|
|
|
|
}, {
|
|
|
|
Source: "docker_password",
|
|
|
|
Target: "docker_password",
|
|
|
|
}}},
|
|
|
|
Settings: map[string]interface{}{
|
|
|
|
"repo": "woodpeckerci/woodpecker-agent",
|
|
|
|
"dockerfile": "docker/Dockerfile.agent",
|
|
|
|
"tag": stringsToInterface("next", "latest"),
|
|
|
|
"dry_run": true,
|
|
|
|
},
|
2022-01-17 15:43:30 +02:00
|
|
|
Constraints: constraint.Constraints{
|
|
|
|
Event: constraint.List{Include: []string{"push"}},
|
|
|
|
Branch: constraint.List{Include: []string{"${CI_REPO_DEFAULT_BRANCH}"}},
|
2021-12-08 19:17:52 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
from: `publish-cli:
|
|
|
|
group: docker
|
|
|
|
image: print/env
|
|
|
|
settings:
|
|
|
|
repo: woodpeckerci/woodpecker-cli
|
|
|
|
dockerfile: docker/Dockerfile.cli
|
|
|
|
tag: [next]
|
|
|
|
when:
|
|
|
|
branch: ${CI_REPO_DEFAULT_BRANCH}
|
|
|
|
event: push`,
|
|
|
|
want: []*Container{
|
|
|
|
{
|
|
|
|
Name: "publish-cli",
|
|
|
|
Image: "print/env",
|
|
|
|
Group: "docker",
|
|
|
|
Settings: map[string]interface{}{
|
|
|
|
"repo": "woodpeckerci/woodpecker-cli",
|
|
|
|
"dockerfile": "docker/Dockerfile.cli",
|
|
|
|
"tag": stringsToInterface("next"),
|
|
|
|
},
|
2022-01-17 15:43:30 +02:00
|
|
|
Constraints: constraint.Constraints{
|
|
|
|
Event: constraint.List{Include: []string{"push"}},
|
|
|
|
Branch: constraint.List{Include: []string{"${CI_REPO_DEFAULT_BRANCH}"}},
|
2021-12-08 19:17:52 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-04-06 15:44:04 +02:00
|
|
|
}
|
|
|
|
for _, test := range testdata {
|
|
|
|
in := []byte(test.from)
|
|
|
|
got := Containers{}
|
|
|
|
err := yaml.Unmarshal(in, &got)
|
2021-12-04 14:23:33 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, test.want, got.Containers, "problem parsing containers %q", test.from)
|
2019-04-06 15:44:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestUnmarshalContainersErr unmarshals a container map where invalid inputs
|
|
|
|
// are provided to verify error messages are returned.
|
|
|
|
func TestUnmarshalContainersErr(t *testing.T) {
|
|
|
|
testdata := []string{
|
|
|
|
"foo: { name: [ foo, bar] }",
|
|
|
|
"- foo",
|
|
|
|
}
|
|
|
|
for _, test := range testdata {
|
|
|
|
in := []byte(test)
|
2019-11-14 21:16:03 +02:00
|
|
|
containers := new(Containers)
|
|
|
|
err := yaml.Unmarshal(in, &containers)
|
2021-12-04 14:23:33 +02:00
|
|
|
assert.Error(t, err, "wanted error for containers %q", test)
|
2019-04-06 15:44:04 +02:00
|
|
|
}
|
|
|
|
}
|
2021-12-08 19:17:52 +02:00
|
|
|
|
|
|
|
func stringsToInterface(val ...string) []interface{} {
|
|
|
|
res := make([]interface{}, len(val))
|
|
|
|
for i := range val {
|
|
|
|
res[i] = val[i]
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|