2023-08-10 09:06:00 +00:00
|
|
|
// Copyright 2023 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.
|
|
|
|
|
2022-10-27 02:21:07 +00:00
|
|
|
package compiler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-06-06 07:14:21 +00:00
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
backend_types "go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/types"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/metadata"
|
|
|
|
yaml_types "go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/yaml/types"
|
|
|
|
yaml_base_types "go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/yaml/types/base"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/shared/constant"
|
2022-10-27 02:21:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSecretAvailable(t *testing.T) {
|
|
|
|
secret := Secret{
|
2023-10-24 18:38:47 +00:00
|
|
|
AllowedPlugins: []string{},
|
2022-10-27 02:21:07 +00:00
|
|
|
}
|
2023-06-06 07:14:21 +00:00
|
|
|
assert.True(t, secret.Available(&yaml_types.Container{
|
2022-10-27 02:21:07 +00:00
|
|
|
Image: "golang",
|
2023-06-06 07:14:21 +00:00
|
|
|
Commands: yaml_base_types.StringOrSlice{"echo 'this is not a plugin'"},
|
2022-10-27 02:21:07 +00:00
|
|
|
}))
|
2023-10-24 18:38:47 +00:00
|
|
|
|
2022-10-27 02:21:07 +00:00
|
|
|
// secret only available for "golang" plugin
|
|
|
|
secret = Secret{
|
2023-10-24 18:38:47 +00:00
|
|
|
AllowedPlugins: []string{"golang"},
|
2022-10-27 02:21:07 +00:00
|
|
|
}
|
2023-06-06 07:14:21 +00:00
|
|
|
assert.True(t, secret.Available(&yaml_types.Container{
|
2022-10-27 02:21:07 +00:00
|
|
|
Image: "golang",
|
2023-06-06 07:14:21 +00:00
|
|
|
Commands: yaml_base_types.StringOrSlice{},
|
2022-10-27 02:21:07 +00:00
|
|
|
}))
|
2023-06-06 07:14:21 +00:00
|
|
|
assert.False(t, secret.Available(&yaml_types.Container{
|
2023-10-24 18:38:47 +00:00
|
|
|
Image: "golang",
|
|
|
|
Commands: yaml_base_types.StringOrSlice{"echo 'this is not a plugin'"},
|
2022-10-27 02:21:07 +00:00
|
|
|
}))
|
2023-06-06 07:14:21 +00:00
|
|
|
assert.False(t, secret.Available(&yaml_types.Container{
|
2022-10-27 02:21:07 +00:00
|
|
|
Image: "not-golang",
|
2023-10-24 18:38:47 +00:00
|
|
|
Commands: yaml_base_types.StringOrSlice{},
|
2022-10-27 02:21:07 +00:00
|
|
|
}))
|
|
|
|
}
|
2023-08-10 07:52:52 +00:00
|
|
|
|
|
|
|
func TestCompilerCompile(t *testing.T) {
|
|
|
|
compiler := New(
|
|
|
|
WithMetadata(metadata.Metadata{
|
|
|
|
Repo: metadata.Repo{
|
|
|
|
Owner: "octacat",
|
|
|
|
Name: "hello-world",
|
|
|
|
Private: true,
|
2023-11-14 16:12:12 +00:00
|
|
|
ForgeURL: "https://github.com/octocat/hello-world",
|
2023-08-10 07:52:52 +00:00
|
|
|
CloneURL: "https://github.com/octocat/hello-world.git",
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
WithEnviron(map[string]string{
|
|
|
|
"VERBOSE": "true",
|
|
|
|
"COLORED": "true",
|
|
|
|
}),
|
|
|
|
WithPrefix("test"),
|
|
|
|
)
|
|
|
|
|
|
|
|
defaultNetworks := []*backend_types.Network{{
|
|
|
|
Name: "test_default",
|
|
|
|
}}
|
|
|
|
defaultVolumes := []*backend_types.Volume{{
|
|
|
|
Name: "test_default",
|
|
|
|
}}
|
|
|
|
|
|
|
|
defaultCloneStage := &backend_types.Stage{
|
|
|
|
Steps: []*backend_types.Step{{
|
2024-01-09 14:22:59 +00:00
|
|
|
Name: "clone",
|
2023-12-22 23:42:30 +00:00
|
|
|
Type: backend_types.StepTypeClone,
|
|
|
|
Image: constant.DefaultCloneImage,
|
|
|
|
OnSuccess: true,
|
|
|
|
Failure: "fail",
|
|
|
|
Volumes: []string{defaultVolumes[0].Name + ":"},
|
|
|
|
Networks: []backend_types.Conn{{Name: "test_default", Aliases: []string{"clone"}}},
|
|
|
|
ExtraHosts: []backend_types.HostAlias{},
|
2023-08-10 07:52:52 +00:00
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
2023-11-05 11:47:42 +00:00
|
|
|
name string
|
|
|
|
fronConf *yaml_types.Workflow
|
|
|
|
backConf *backend_types.Config
|
|
|
|
expectedErr string
|
2023-12-24 11:14:30 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "empty workflow, no clone",
|
|
|
|
fronConf: &yaml_types.Workflow{SkipClone: true},
|
|
|
|
backConf: &backend_types.Config{
|
|
|
|
Networks: defaultNetworks,
|
|
|
|
Volumes: defaultVolumes,
|
|
|
|
},
|
2023-08-10 07:52:52 +00:00
|
|
|
},
|
2023-12-24 11:14:30 +00:00
|
|
|
{
|
|
|
|
name: "empty workflow, default clone",
|
|
|
|
fronConf: &yaml_types.Workflow{},
|
|
|
|
backConf: &backend_types.Config{
|
|
|
|
Networks: defaultNetworks,
|
|
|
|
Volumes: defaultVolumes,
|
|
|
|
Stages: []*backend_types.Stage{defaultCloneStage},
|
|
|
|
},
|
2023-08-10 07:52:52 +00:00
|
|
|
},
|
2023-12-24 11:14:30 +00:00
|
|
|
{
|
|
|
|
name: "workflow with one dummy step",
|
|
|
|
fronConf: &yaml_types.Workflow{Steps: yaml_types.ContainerList{ContainerList: []*yaml_types.Container{{
|
|
|
|
Name: "dummy",
|
|
|
|
Image: "dummy_img",
|
|
|
|
}}}},
|
|
|
|
backConf: &backend_types.Config{
|
|
|
|
Networks: defaultNetworks,
|
|
|
|
Volumes: defaultVolumes,
|
|
|
|
Stages: []*backend_types.Stage{defaultCloneStage, {
|
|
|
|
Steps: []*backend_types.Step{{
|
2024-01-09 14:22:59 +00:00
|
|
|
Name: "dummy",
|
2023-12-24 11:14:30 +00:00
|
|
|
Type: backend_types.StepTypePlugin,
|
|
|
|
Image: "dummy_img",
|
|
|
|
OnSuccess: true,
|
|
|
|
Failure: "fail",
|
|
|
|
Volumes: []string{defaultVolumes[0].Name + ":"},
|
|
|
|
Networks: []backend_types.Conn{{Name: "test_default", Aliases: []string{"dummy"}}},
|
|
|
|
ExtraHosts: []backend_types.HostAlias{},
|
|
|
|
}},
|
2023-08-10 07:52:52 +00:00
|
|
|
}},
|
2023-12-24 11:14:30 +00:00
|
|
|
},
|
2023-08-10 07:52:52 +00:00
|
|
|
},
|
2023-12-24 11:14:30 +00:00
|
|
|
{
|
|
|
|
name: "workflow with three steps and one group",
|
|
|
|
fronConf: &yaml_types.Workflow{Steps: yaml_types.ContainerList{ContainerList: []*yaml_types.Container{{
|
|
|
|
Name: "echo env",
|
|
|
|
Image: "bash",
|
|
|
|
Commands: []string{"env"},
|
|
|
|
}, {
|
|
|
|
Name: "parallel echo 1",
|
|
|
|
Group: "parallel",
|
|
|
|
Image: "bash",
|
|
|
|
Commands: []string{"echo 1"},
|
|
|
|
}, {
|
|
|
|
Name: "parallel echo 2",
|
|
|
|
Group: "parallel",
|
|
|
|
Image: "bash",
|
|
|
|
Commands: []string{"echo 2"},
|
|
|
|
}}}},
|
|
|
|
backConf: &backend_types.Config{
|
|
|
|
Networks: defaultNetworks,
|
|
|
|
Volumes: defaultVolumes,
|
|
|
|
Stages: []*backend_types.Stage{defaultCloneStage, {
|
|
|
|
Steps: []*backend_types.Step{{
|
2024-01-09 14:22:59 +00:00
|
|
|
Name: "echo env",
|
2023-12-24 11:14:30 +00:00
|
|
|
Type: backend_types.StepTypeCommands,
|
|
|
|
Image: "bash",
|
|
|
|
Commands: []string{"env"},
|
|
|
|
OnSuccess: true,
|
|
|
|
Failure: "fail",
|
|
|
|
Volumes: []string{defaultVolumes[0].Name + ":"},
|
|
|
|
Networks: []backend_types.Conn{{Name: "test_default", Aliases: []string{"echo env"}}},
|
|
|
|
ExtraHosts: []backend_types.HostAlias{},
|
|
|
|
}},
|
|
|
|
}, {
|
|
|
|
Steps: []*backend_types.Step{{
|
2024-01-09 14:22:59 +00:00
|
|
|
Name: "parallel echo 1",
|
2023-12-24 11:14:30 +00:00
|
|
|
Type: backend_types.StepTypeCommands,
|
|
|
|
Image: "bash",
|
|
|
|
Commands: []string{"echo 1"},
|
|
|
|
OnSuccess: true,
|
|
|
|
Failure: "fail",
|
|
|
|
Volumes: []string{defaultVolumes[0].Name + ":"},
|
|
|
|
Networks: []backend_types.Conn{{Name: "test_default", Aliases: []string{"parallel echo 1"}}},
|
|
|
|
ExtraHosts: []backend_types.HostAlias{},
|
|
|
|
}, {
|
2024-01-09 14:22:59 +00:00
|
|
|
Name: "parallel echo 2",
|
2023-12-24 11:14:30 +00:00
|
|
|
Type: backend_types.StepTypeCommands,
|
|
|
|
Image: "bash",
|
|
|
|
Commands: []string{"echo 2"},
|
|
|
|
OnSuccess: true,
|
|
|
|
Failure: "fail",
|
|
|
|
Volumes: []string{defaultVolumes[0].Name + ":"},
|
|
|
|
Networks: []backend_types.Conn{{Name: "test_default", Aliases: []string{"parallel echo 2"}}},
|
|
|
|
ExtraHosts: []backend_types.HostAlias{},
|
|
|
|
}},
|
2023-08-10 07:52:52 +00:00
|
|
|
}},
|
2023-12-24 11:14:30 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "workflow with three steps and depends_on",
|
|
|
|
fronConf: &yaml_types.Workflow{Steps: yaml_types.ContainerList{ContainerList: []*yaml_types.Container{{
|
|
|
|
Name: "echo env",
|
|
|
|
Image: "bash",
|
|
|
|
Commands: []string{"env"},
|
|
|
|
}, {
|
|
|
|
Name: "echo 1",
|
|
|
|
Image: "bash",
|
|
|
|
Commands: []string{"echo 1"},
|
|
|
|
DependsOn: []string{"echo env", "echo 2"},
|
2023-08-10 07:52:52 +00:00
|
|
|
}, {
|
2023-12-24 11:14:30 +00:00
|
|
|
Name: "echo 2",
|
|
|
|
Image: "bash",
|
|
|
|
Commands: []string{"echo 2"},
|
|
|
|
}}}},
|
|
|
|
backConf: &backend_types.Config{
|
|
|
|
Networks: defaultNetworks,
|
|
|
|
Volumes: defaultVolumes,
|
|
|
|
Stages: []*backend_types.Stage{defaultCloneStage, {
|
|
|
|
Steps: []*backend_types.Step{{
|
2024-01-09 14:22:59 +00:00
|
|
|
Name: "echo env",
|
2023-12-24 11:14:30 +00:00
|
|
|
Type: backend_types.StepTypeCommands,
|
|
|
|
Image: "bash",
|
|
|
|
Commands: []string{"env"},
|
|
|
|
OnSuccess: true,
|
|
|
|
Failure: "fail",
|
|
|
|
Volumes: []string{defaultVolumes[0].Name + ":"},
|
|
|
|
Networks: []backend_types.Conn{{Name: "test_default", Aliases: []string{"echo env"}}},
|
|
|
|
ExtraHosts: []backend_types.HostAlias{},
|
|
|
|
}, {
|
2024-01-09 14:22:59 +00:00
|
|
|
Name: "echo 2",
|
2023-12-24 11:14:30 +00:00
|
|
|
Type: backend_types.StepTypeCommands,
|
|
|
|
Image: "bash",
|
|
|
|
Commands: []string{"echo 2"},
|
|
|
|
OnSuccess: true,
|
|
|
|
Failure: "fail",
|
|
|
|
Volumes: []string{defaultVolumes[0].Name + ":"},
|
|
|
|
Networks: []backend_types.Conn{{Name: "test_default", Aliases: []string{"echo 2"}}},
|
|
|
|
ExtraHosts: []backend_types.HostAlias{},
|
|
|
|
}},
|
2023-08-10 07:52:52 +00:00
|
|
|
}, {
|
2023-12-24 11:14:30 +00:00
|
|
|
Steps: []*backend_types.Step{{
|
2024-01-09 14:22:59 +00:00
|
|
|
Name: "echo 1",
|
2023-12-24 11:14:30 +00:00
|
|
|
Type: backend_types.StepTypeCommands,
|
|
|
|
Image: "bash",
|
|
|
|
Commands: []string{"echo 1"},
|
|
|
|
OnSuccess: true,
|
|
|
|
Failure: "fail",
|
|
|
|
Volumes: []string{defaultVolumes[0].Name + ":"},
|
|
|
|
Networks: []backend_types.Conn{{Name: "test_default", Aliases: []string{"echo 1"}}},
|
|
|
|
ExtraHosts: []backend_types.HostAlias{},
|
|
|
|
}},
|
2023-08-10 07:52:52 +00:00
|
|
|
}},
|
2023-12-24 11:14:30 +00:00
|
|
|
},
|
2023-08-10 07:52:52 +00:00
|
|
|
},
|
2023-12-24 11:14:30 +00:00
|
|
|
{
|
|
|
|
name: "workflow with missing secret",
|
|
|
|
fronConf: &yaml_types.Workflow{Steps: yaml_types.ContainerList{ContainerList: []*yaml_types.Container{{
|
|
|
|
Name: "step",
|
|
|
|
Image: "bash",
|
|
|
|
Commands: []string{"env"},
|
|
|
|
Secrets: yaml_types.Secrets{Secrets: []*yaml_types.Secret{{Source: "missing", Target: "missing"}}},
|
|
|
|
}}}},
|
|
|
|
backConf: nil,
|
|
|
|
expectedErr: "secret \"missing\" not found or not allowed to be used",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "workflow with broken step dependency",
|
|
|
|
fronConf: &yaml_types.Workflow{Steps: yaml_types.ContainerList{ContainerList: []*yaml_types.Container{{
|
|
|
|
Name: "dummy",
|
|
|
|
Image: "dummy_img",
|
|
|
|
DependsOn: []string{"not exist"},
|
|
|
|
}}}},
|
|
|
|
backConf: nil,
|
|
|
|
expectedErr: "step 'dummy' depends on unknown step 'not exist'",
|
|
|
|
},
|
|
|
|
}
|
2023-08-10 07:52:52 +00:00
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
backConf, err := compiler.Compile(test.fronConf)
|
2023-11-05 11:47:42 +00:00
|
|
|
if test.expectedErr != "" {
|
2023-08-10 07:52:52 +00:00
|
|
|
assert.Error(t, err)
|
2023-11-05 11:47:42 +00:00
|
|
|
assert.Equal(t, err.Error(), test.expectedErr)
|
2023-08-10 07:52:52 +00:00
|
|
|
} else {
|
|
|
|
// we ignore uuids in steps and only check if global env got set ...
|
|
|
|
for _, st := range backConf.Stages {
|
|
|
|
for _, s := range st.Steps {
|
|
|
|
s.UUID = ""
|
|
|
|
assert.Truef(t, s.Environment["VERBOSE"] == "true", "expect to get value of global set environment")
|
|
|
|
assert.Truef(t, len(s.Environment) > 50, "expect to have a lot of build in variables")
|
|
|
|
s.Environment = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// check if we get an expected backend config based on a frontend config
|
|
|
|
assert.EqualValues(t, *test.backConf, *backConf)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|