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.
|
|
|
|
|
2019-04-06 13:44:04 +00:00
|
|
|
package yaml
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/franela/goblin"
|
2023-11-02 14:31:49 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-10-30 15:52:02 +00:00
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/metadata"
|
|
|
|
yaml_base_types "go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/yaml/types/base"
|
2019-04-06 13:44:04 +00:00
|
|
|
)
|
|
|
|
|
2019-06-12 09:03:59 +00:00
|
|
|
func TestParse(t *testing.T) {
|
2019-04-06 13:44:04 +00:00
|
|
|
g := goblin.Goblin(t)
|
|
|
|
|
|
|
|
g.Describe("Parser", func() {
|
|
|
|
g.Describe("Given a yaml file", func() {
|
|
|
|
g.It("Should unmarshal a string", func() {
|
|
|
|
out, err := ParseString(sampleYaml)
|
|
|
|
if err != nil {
|
|
|
|
g.Fail(err)
|
|
|
|
}
|
|
|
|
|
2022-09-26 07:27:20 +00:00
|
|
|
g.Assert(out.When.Constraints[0].Event.Match("tester")).Equal(true)
|
|
|
|
|
2019-04-06 13:44:04 +00:00
|
|
|
g.Assert(out.Workspace.Base).Equal("/go")
|
|
|
|
g.Assert(out.Workspace.Path).Equal("src/github.com/octocat/hello-world")
|
2023-06-06 07:14:21 +00:00
|
|
|
g.Assert(out.Volumes.WorkflowVolumes[0].Name).Equal("custom")
|
|
|
|
g.Assert(out.Volumes.WorkflowVolumes[0].Driver).Equal("blockbridge")
|
|
|
|
g.Assert(out.Networks.WorkflowNetworks[0].Name).Equal("custom")
|
|
|
|
g.Assert(out.Networks.WorkflowNetworks[0].Driver).Equal("overlay")
|
|
|
|
g.Assert(out.Services.ContainerList[0].Name).Equal("database")
|
|
|
|
g.Assert(out.Services.ContainerList[0].Image).Equal("mysql")
|
|
|
|
g.Assert(out.Steps.ContainerList[0].Name).Equal("test")
|
|
|
|
g.Assert(out.Steps.ContainerList[0].Image).Equal("golang")
|
|
|
|
g.Assert(out.Steps.ContainerList[0].Commands).Equal(yaml_base_types.StringOrSlice{"go install", "go test"})
|
|
|
|
g.Assert(out.Steps.ContainerList[1].Name).Equal("build")
|
|
|
|
g.Assert(out.Steps.ContainerList[1].Image).Equal("golang")
|
|
|
|
g.Assert(out.Steps.ContainerList[1].Commands).Equal(yaml_base_types.StringOrSlice{"go build"})
|
|
|
|
g.Assert(out.Steps.ContainerList[2].Name).Equal("notify")
|
|
|
|
g.Assert(out.Steps.ContainerList[2].Image).Equal("slack")
|
|
|
|
// g.Assert(out.Steps.ContainerList[2].NetworkMode).Equal("container:name")
|
2019-04-06 13:44:04 +00:00
|
|
|
g.Assert(out.Labels["com.example.team"]).Equal("frontend")
|
|
|
|
g.Assert(out.Labels["com.example.type"]).Equal("build")
|
2019-06-12 09:03:59 +00:00
|
|
|
g.Assert(out.DependsOn[0]).Equal("lint")
|
|
|
|
g.Assert(out.DependsOn[1]).Equal("test")
|
2019-06-17 07:06:36 +00:00
|
|
|
g.Assert(out.RunsOn[0]).Equal("success")
|
|
|
|
g.Assert(out.RunsOn[1]).Equal("failure")
|
2019-06-24 09:19:12 +00:00
|
|
|
g.Assert(out.SkipClone).Equal(false)
|
2019-04-06 13:44:04 +00:00
|
|
|
})
|
2019-11-14 20:03:07 +00:00
|
|
|
|
2019-11-14 19:16:03 +00:00
|
|
|
g.It("Should handle simple yaml anchors", func() {
|
|
|
|
out, err := ParseString(simpleYamlAnchors)
|
|
|
|
if err != nil {
|
|
|
|
g.Fail(err)
|
|
|
|
}
|
2023-06-06 07:14:21 +00:00
|
|
|
g.Assert(out.Steps.ContainerList[0].Name).Equal("notify_success")
|
|
|
|
g.Assert(out.Steps.ContainerList[0].Image).Equal("plugins/slack")
|
2019-11-14 19:16:03 +00:00
|
|
|
})
|
|
|
|
|
2019-11-14 20:03:07 +00:00
|
|
|
g.It("Should unmarshal variables", func() {
|
|
|
|
out, err := ParseString(sampleVarYaml)
|
|
|
|
if err != nil {
|
|
|
|
g.Fail(err)
|
|
|
|
}
|
2023-06-06 07:14:21 +00:00
|
|
|
g.Assert(out.Steps.ContainerList[0].Name).Equal("notify_fail")
|
|
|
|
g.Assert(out.Steps.ContainerList[0].Image).Equal("plugins/slack")
|
|
|
|
g.Assert(out.Steps.ContainerList[1].Name).Equal("notify_success")
|
|
|
|
g.Assert(out.Steps.ContainerList[1].Image).Equal("plugins/slack")
|
2022-09-26 07:27:20 +00:00
|
|
|
|
2023-06-06 07:14:21 +00:00
|
|
|
g.Assert(len(out.Steps.ContainerList[0].When.Constraints)).Equal(0)
|
|
|
|
g.Assert(out.Steps.ContainerList[1].Name).Equal("notify_success")
|
|
|
|
g.Assert(out.Steps.ContainerList[1].Image).Equal("plugins/slack")
|
|
|
|
g.Assert(out.Steps.ContainerList[1].When.Constraints[0].Event.Include).Equal([]string{"success"})
|
2019-11-14 20:03:07 +00:00
|
|
|
})
|
2022-09-26 07:27:20 +00:00
|
|
|
|
|
|
|
matchConfig, err := ParseString(sampleYaml)
|
|
|
|
if err != nil {
|
|
|
|
g.Fail(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
g.It("Should match event tester", func() {
|
2023-06-04 22:15:07 +00:00
|
|
|
match, err := matchConfig.When.Match(metadata.Metadata{
|
|
|
|
Curr: metadata.Pipeline{
|
2022-09-26 07:27:20 +00:00
|
|
|
Event: "tester",
|
|
|
|
},
|
2023-07-02 21:45:22 +00:00
|
|
|
}, false, nil)
|
2022-10-05 23:49:23 +00:00
|
|
|
g.Assert(match).Equal(true)
|
|
|
|
g.Assert(err).IsNil()
|
2022-09-26 07:27:20 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should match event tester2", func() {
|
2023-06-04 22:15:07 +00:00
|
|
|
match, err := matchConfig.When.Match(metadata.Metadata{
|
|
|
|
Curr: metadata.Pipeline{
|
2022-09-26 07:27:20 +00:00
|
|
|
Event: "tester2",
|
|
|
|
},
|
2023-07-02 21:45:22 +00:00
|
|
|
}, false, nil)
|
2022-10-05 23:49:23 +00:00
|
|
|
g.Assert(match).Equal(true)
|
|
|
|
g.Assert(err).IsNil()
|
2022-09-26 07:27:20 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should match branch tester", func() {
|
2023-06-04 22:15:07 +00:00
|
|
|
match, err := matchConfig.When.Match(metadata.Metadata{
|
|
|
|
Curr: metadata.Pipeline{
|
|
|
|
Commit: metadata.Commit{
|
2022-09-26 07:27:20 +00:00
|
|
|
Branch: "tester",
|
|
|
|
},
|
|
|
|
},
|
2023-07-02 21:45:22 +00:00
|
|
|
}, true, nil)
|
2022-10-05 23:49:23 +00:00
|
|
|
g.Assert(match).Equal(true)
|
|
|
|
g.Assert(err).IsNil()
|
2022-09-26 07:27:20 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should not match event push", func() {
|
2023-06-04 22:15:07 +00:00
|
|
|
match, err := matchConfig.When.Match(metadata.Metadata{
|
|
|
|
Curr: metadata.Pipeline{
|
2022-09-26 07:27:20 +00:00
|
|
|
Event: "push",
|
|
|
|
},
|
2023-07-02 21:45:22 +00:00
|
|
|
}, false, nil)
|
2022-10-05 23:49:23 +00:00
|
|
|
g.Assert(match).Equal(false)
|
|
|
|
g.Assert(err).IsNil()
|
2022-09-26 07:27:20 +00:00
|
|
|
})
|
2019-04-06 13:44:04 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-11-02 14:31:49 +00:00
|
|
|
func TestParseLegacy(t *testing.T) {
|
|
|
|
sampleYamlPipelineLegacy := `
|
|
|
|
platform: linux/amd64
|
|
|
|
|
|
|
|
pipeline:
|
|
|
|
say hello:
|
|
|
|
image: bash
|
|
|
|
commands: echo hello
|
|
|
|
`
|
|
|
|
|
|
|
|
sampleYamlPipelineLegacyIgnore := `
|
|
|
|
platform: windows/amd64
|
|
|
|
labels:
|
|
|
|
platform: linux/amd64
|
|
|
|
|
|
|
|
steps:
|
|
|
|
say hello:
|
|
|
|
image: bash
|
|
|
|
commands: echo hello
|
|
|
|
|
|
|
|
pipeline:
|
|
|
|
old crap:
|
|
|
|
image: bash
|
|
|
|
commands: meh!
|
|
|
|
`
|
|
|
|
|
|
|
|
workflow1, err := ParseString(sampleYamlPipelineLegacy)
|
|
|
|
if !assert.NoError(t, err) {
|
2024-01-14 18:33:58 +00:00
|
|
|
return
|
2023-11-02 14:31:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
workflow2, err := ParseString(sampleYamlPipelineLegacyIgnore)
|
|
|
|
if !assert.NoError(t, err) {
|
2024-01-14 18:33:58 +00:00
|
|
|
return
|
2023-11-02 14:31:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.EqualValues(t, workflow1, workflow2)
|
|
|
|
assert.Len(t, workflow1.Steps.ContainerList, 1)
|
|
|
|
assert.EqualValues(t, "say hello", workflow1.Steps.ContainerList[0].Name)
|
|
|
|
}
|
|
|
|
|
2019-04-06 13:44:04 +00:00
|
|
|
var sampleYaml = `
|
|
|
|
image: hello-world
|
2022-09-26 07:27:20 +00:00
|
|
|
when:
|
|
|
|
- event:
|
|
|
|
- tester
|
|
|
|
- tester2
|
|
|
|
- branch:
|
|
|
|
- tester
|
2019-04-06 13:44:04 +00:00
|
|
|
build:
|
|
|
|
context: .
|
|
|
|
dockerfile: Dockerfile
|
|
|
|
workspace:
|
|
|
|
path: src/github.com/octocat/hello-world
|
|
|
|
base: /go
|
2023-06-07 10:04:37 +00:00
|
|
|
steps:
|
2019-04-06 13:44:04 +00:00
|
|
|
test:
|
|
|
|
image: golang
|
|
|
|
commands:
|
|
|
|
- go install
|
|
|
|
- go test
|
|
|
|
build:
|
|
|
|
image: golang
|
|
|
|
network_mode: container:name
|
|
|
|
commands:
|
|
|
|
- go build
|
|
|
|
when:
|
|
|
|
event: push
|
|
|
|
notify:
|
|
|
|
image: slack
|
|
|
|
channel: dev
|
|
|
|
when:
|
|
|
|
event: failure
|
|
|
|
services:
|
|
|
|
database:
|
|
|
|
image: mysql
|
|
|
|
networks:
|
|
|
|
custom:
|
|
|
|
driver: overlay
|
|
|
|
volumes:
|
|
|
|
custom:
|
|
|
|
driver: blockbridge
|
|
|
|
labels:
|
|
|
|
com.example.type: "build"
|
|
|
|
com.example.team: "frontend"
|
2019-06-12 09:03:59 +00:00
|
|
|
depends_on:
|
|
|
|
- lint
|
|
|
|
- test
|
2019-06-17 07:06:36 +00:00
|
|
|
runs_on:
|
|
|
|
- success
|
|
|
|
- failure
|
2019-04-06 13:44:04 +00:00
|
|
|
`
|
|
|
|
|
2019-11-14 19:16:03 +00:00
|
|
|
var simpleYamlAnchors = `
|
|
|
|
vars:
|
|
|
|
image: &image plugins/slack
|
2023-06-07 10:04:37 +00:00
|
|
|
steps:
|
2019-11-14 19:16:03 +00:00
|
|
|
notify_success:
|
|
|
|
image: *image
|
|
|
|
`
|
|
|
|
|
2019-04-06 13:44:04 +00:00
|
|
|
var sampleVarYaml = `
|
|
|
|
_slack: &SLACK
|
|
|
|
image: plugins/slack
|
2023-06-07 10:04:37 +00:00
|
|
|
steps:
|
2019-04-06 13:44:04 +00:00
|
|
|
notify_fail: *SLACK
|
|
|
|
notify_success:
|
|
|
|
<< : *SLACK
|
|
|
|
when:
|
|
|
|
event: success
|
|
|
|
`
|
2024-01-01 10:03:31 +00:00
|
|
|
|
|
|
|
var sampleSliceYaml = `
|
|
|
|
steps:
|
|
|
|
nil_slice:
|
|
|
|
image: plugins/slack
|
|
|
|
empty_slice:
|
|
|
|
image: plugins/slack
|
|
|
|
depends_on: []
|
|
|
|
`
|
|
|
|
|
|
|
|
func TestSlice(t *testing.T) {
|
|
|
|
g := goblin.Goblin(t)
|
|
|
|
|
|
|
|
g.Describe("Parser", func() {
|
|
|
|
g.It("should marshal a not set slice to nil", func() {
|
|
|
|
out, err := ParseString(sampleSliceYaml)
|
|
|
|
if err != nil {
|
|
|
|
g.Fail(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
g.Assert(out.Steps.ContainerList[0].DependsOn).IsNil()
|
|
|
|
g.Assert(len(out.Steps.ContainerList[0].DependsOn)).Equal(0)
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("should marshal an empty slice", func() {
|
|
|
|
out, err := ParseString(sampleSliceYaml)
|
|
|
|
if err != nil {
|
|
|
|
g.Fail(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
g.Assert(out.Steps.ContainerList[1].DependsOn).IsNotNil()
|
|
|
|
g.Assert(len(out.Steps.ContainerList[1].DependsOn)).Equal(0)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|