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-01-17 13:43:30 +00:00
|
|
|
package constraint
|
2019-04-06 13:44:04 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-11-23 14:36:52 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-11-14 19:16:03 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
2021-10-12 07:25:13 +00:00
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/metadata"
|
2019-04-06 13:44:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestConstraint(t *testing.T) {
|
|
|
|
testdata := []struct {
|
|
|
|
conf string
|
|
|
|
with string
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
// string value
|
|
|
|
{
|
2023-07-31 03:47:23 +00:00
|
|
|
conf: "main",
|
2019-04-06 13:44:04 +00:00
|
|
|
with: "develop",
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
2023-07-31 03:47:23 +00:00
|
|
|
conf: "main",
|
|
|
|
with: "main",
|
2019-04-06 13:44:04 +00:00
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conf: "feature/*",
|
|
|
|
with: "feature/foo",
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
// slice value
|
|
|
|
{
|
2023-07-31 03:47:23 +00:00
|
|
|
conf: "[ main, feature/* ]",
|
2019-04-06 13:44:04 +00:00
|
|
|
with: "develop",
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
2023-07-31 03:47:23 +00:00
|
|
|
conf: "[ main, feature/* ]",
|
|
|
|
with: "main",
|
2019-04-06 13:44:04 +00:00
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
2023-07-31 03:47:23 +00:00
|
|
|
conf: "[ main, feature/* ]",
|
2019-04-06 13:44:04 +00:00
|
|
|
with: "feature/foo",
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
// includes block
|
|
|
|
{
|
2023-07-31 03:47:23 +00:00
|
|
|
conf: "include: main",
|
2019-04-06 13:44:04 +00:00
|
|
|
with: "develop",
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
2023-07-31 03:47:23 +00:00
|
|
|
conf: "include: main",
|
|
|
|
with: "main",
|
2019-04-06 13:44:04 +00:00
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conf: "include: feature/*",
|
2023-07-31 03:47:23 +00:00
|
|
|
with: "main",
|
2019-04-06 13:44:04 +00:00
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conf: "include: feature/*",
|
|
|
|
with: "feature/foo",
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
2023-07-31 03:47:23 +00:00
|
|
|
conf: "include: [ main, feature/* ]",
|
2019-04-06 13:44:04 +00:00
|
|
|
with: "develop",
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
2023-07-31 03:47:23 +00:00
|
|
|
conf: "include: [ main, feature/* ]",
|
|
|
|
with: "main",
|
2019-04-06 13:44:04 +00:00
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
2023-07-31 03:47:23 +00:00
|
|
|
conf: "include: [ main, feature/* ]",
|
2019-04-06 13:44:04 +00:00
|
|
|
with: "feature/foo",
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
// excludes block
|
|
|
|
{
|
2023-07-31 03:47:23 +00:00
|
|
|
conf: "exclude: main",
|
2019-04-06 13:44:04 +00:00
|
|
|
with: "develop",
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
2023-07-31 03:47:23 +00:00
|
|
|
conf: "exclude: main",
|
|
|
|
with: "main",
|
2019-04-06 13:44:04 +00:00
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conf: "exclude: feature/*",
|
2023-07-31 03:47:23 +00:00
|
|
|
with: "main",
|
2019-04-06 13:44:04 +00:00
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conf: "exclude: feature/*",
|
|
|
|
with: "feature/foo",
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
2023-07-31 03:47:23 +00:00
|
|
|
conf: "exclude: [ main, develop ]",
|
|
|
|
with: "main",
|
2019-04-06 13:44:04 +00:00
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conf: "exclude: [ feature/*, bar ]",
|
2023-07-31 03:47:23 +00:00
|
|
|
with: "main",
|
2019-04-06 13:44:04 +00:00
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conf: "exclude: [ feature/*, bar ]",
|
|
|
|
with: "feature/foo",
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
// include and exclude blocks
|
|
|
|
{
|
2023-07-31 03:47:23 +00:00
|
|
|
conf: "{ include: [ main, feature/* ], exclude: [ develop ] }",
|
|
|
|
with: "main",
|
2019-04-06 13:44:04 +00:00
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
2023-07-31 03:47:23 +00:00
|
|
|
conf: "{ include: [ main, feature/* ], exclude: [ feature/bar ] }",
|
2019-04-06 13:44:04 +00:00
|
|
|
with: "feature/bar",
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
2023-07-31 03:47:23 +00:00
|
|
|
conf: "{ include: [ main, feature/* ], exclude: [ main, develop ] }",
|
|
|
|
with: "main",
|
2019-04-06 13:44:04 +00:00
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
// empty blocks
|
|
|
|
{
|
|
|
|
conf: "",
|
2023-07-31 03:47:23 +00:00
|
|
|
with: "main",
|
2019-04-06 13:44:04 +00:00
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range testdata {
|
2021-11-23 14:36:52 +00:00
|
|
|
c := parseConstraint(t, test.conf)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Equal(t, test.want, c.Match(test.with))
|
2019-04-06 13:44:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-28 21:50:35 +00:00
|
|
|
func TestConstraintList(t *testing.T) {
|
|
|
|
testdata := []struct {
|
|
|
|
conf string
|
|
|
|
with []string
|
|
|
|
message string
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
conf: "",
|
|
|
|
with: []string{"CHANGELOG.md", "README.md"},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conf: "CHANGELOG.md",
|
|
|
|
with: []string{"CHANGELOG.md", "README.md"},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conf: "'*.md'",
|
|
|
|
with: []string{"CHANGELOG.md", "README.md"},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conf: "['*.md']",
|
|
|
|
with: []string{"CHANGELOG.md", "README.md"},
|
|
|
|
want: true,
|
|
|
|
},
|
2021-09-21 04:18:43 +00:00
|
|
|
{
|
|
|
|
conf: "'docs/*'",
|
|
|
|
with: []string{"docs/README.md"},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conf: "'docs/*'",
|
|
|
|
with: []string{"docs/sub/README.md"},
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conf: "'docs/**'",
|
|
|
|
with: []string{"docs/README.md", "docs/sub/README.md", "docs/sub-sub/README.md"},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conf: "'docs/**'",
|
|
|
|
with: []string{"README.md"},
|
|
|
|
want: false,
|
|
|
|
},
|
2021-06-28 21:50:35 +00:00
|
|
|
{
|
|
|
|
conf: "{ include: [ README.md ] }",
|
|
|
|
with: []string{"CHANGELOG.md"},
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conf: "{ exclude: [ README.md ] }",
|
|
|
|
with: []string{"design.md"},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
// include and exclude blocks
|
|
|
|
{
|
|
|
|
conf: "{ include: [ '*.md', '*.ini' ], exclude: [ CHANGELOG.md ] }",
|
|
|
|
with: []string{"README.md"},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conf: "{ include: [ '*.md' ], exclude: [ CHANGELOG.md ] }",
|
|
|
|
with: []string{"CHANGELOG.md"},
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conf: "{ include: [ '*.md' ], exclude: [ CHANGELOG.md ] }",
|
|
|
|
with: []string{"README.md", "CHANGELOG.md"},
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
// commit message ignore matches
|
|
|
|
{
|
|
|
|
conf: "{ include: [ README.md ], ignore_message: '[ALL]' }",
|
|
|
|
with: []string{"CHANGELOG.md"},
|
|
|
|
message: "Build them [ALL]",
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conf: "{ exclude: [ '*.php' ], ignore_message: '[ALL]' }",
|
|
|
|
with: []string{"myfile.php"},
|
|
|
|
message: "Build them [ALL]",
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conf: "{ ignore_message: '[ALL]' }",
|
|
|
|
with: []string{},
|
|
|
|
message: "Build them [ALL]",
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
// empty commit
|
|
|
|
{
|
|
|
|
conf: "{ include: [ README.md ] }",
|
|
|
|
with: []string{},
|
|
|
|
want: true,
|
|
|
|
},
|
2024-05-14 14:28:14 +00:00
|
|
|
{
|
|
|
|
conf: "{ include: [ README.md ], on_empty: false }",
|
|
|
|
with: []string{},
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conf: "{ include: [ README.md ], on_empty: true }",
|
|
|
|
with: []string{},
|
|
|
|
want: true,
|
|
|
|
},
|
2021-06-28 21:50:35 +00:00
|
|
|
}
|
|
|
|
for _, test := range testdata {
|
2021-11-23 14:36:52 +00:00
|
|
|
c := parseConstraintPath(t, test.conf)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Equal(t, test.want, c.Match(test.with, test.message))
|
2021-06-28 21:50:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-06 13:44:04 +00:00
|
|
|
func TestConstraintMap(t *testing.T) {
|
|
|
|
testdata := []struct {
|
|
|
|
conf string
|
|
|
|
with map[string]string
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
conf: "GOLANG: 1.7",
|
|
|
|
with: map[string]string{"GOLANG": "1.7"},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conf: "GOLANG: tip",
|
|
|
|
with: map[string]string{"GOLANG": "1.7"},
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conf: "{ GOLANG: 1.7, REDIS: 3.1 }",
|
|
|
|
with: map[string]string{"GOLANG": "1.7", "REDIS": "3.1", "MYSQL": "5.6"},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conf: "{ GOLANG: 1.7, REDIS: 3.1 }",
|
|
|
|
with: map[string]string{"GOLANG": "1.7", "REDIS": "3.0"},
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conf: "{ GOLANG: 1.7, REDIS: 3.* }",
|
|
|
|
with: map[string]string{"GOLANG": "1.7", "REDIS": "3.0"},
|
2022-01-17 13:43:30 +00:00
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conf: "{ GOLANG: 1.7, BRANCH: release/**/test }",
|
|
|
|
with: map[string]string{"GOLANG": "1.7", "BRANCH": "release/v1.12.1//test"},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conf: "{ GOLANG: 1.7, BRANCH: release/**/test }",
|
|
|
|
with: map[string]string{"GOLANG": "1.7", "BRANCH": "release/v1.12.1/qest"},
|
2019-04-06 13:44:04 +00:00
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
// include syntax
|
|
|
|
{
|
|
|
|
conf: "include: { GOLANG: 1.7 }",
|
|
|
|
with: map[string]string{"GOLANG": "1.7"},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conf: "include: { GOLANG: tip }",
|
|
|
|
with: map[string]string{"GOLANG": "1.7"},
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conf: "include: { GOLANG: 1.7, REDIS: 3.1 }",
|
|
|
|
with: map[string]string{"GOLANG": "1.7", "REDIS": "3.1", "MYSQL": "5.6"},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conf: "include: { GOLANG: 1.7, REDIS: 3.1 }",
|
|
|
|
with: map[string]string{"GOLANG": "1.7", "REDIS": "3.0"},
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
// exclude syntax
|
|
|
|
{
|
|
|
|
conf: "exclude: { GOLANG: 1.7 }",
|
|
|
|
with: map[string]string{"GOLANG": "1.7"},
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conf: "exclude: { GOLANG: tip }",
|
|
|
|
with: map[string]string{"GOLANG": "1.7"},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conf: "exclude: { GOLANG: 1.7, REDIS: 3.1 }",
|
|
|
|
with: map[string]string{"GOLANG": "1.7", "REDIS": "3.1", "MYSQL": "5.6"},
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conf: "exclude: { GOLANG: 1.7, REDIS: 3.1 }",
|
|
|
|
with: map[string]string{"GOLANG": "1.7", "REDIS": "3.0"},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
// exclude AND include values
|
|
|
|
{
|
|
|
|
conf: "{ include: { GOLANG: 1.7 }, exclude: { GOLANG: 1.7 } }",
|
|
|
|
with: map[string]string{"GOLANG": "1.7"},
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
// blanks
|
|
|
|
{
|
|
|
|
conf: "",
|
|
|
|
with: map[string]string{"GOLANG": "1.7", "REDIS": "3.0"},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conf: "GOLANG: 1.7",
|
|
|
|
with: map[string]string{},
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conf: "{ GOLANG: 1.7, REDIS: 3.0 }",
|
|
|
|
with: map[string]string{},
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conf: "include: { GOLANG: 1.7, REDIS: 3.1 }",
|
|
|
|
with: map[string]string{},
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
conf: "exclude: { GOLANG: 1.7, REDIS: 3.1 }",
|
|
|
|
with: map[string]string{},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range testdata {
|
2021-11-23 14:36:52 +00:00
|
|
|
c := parseConstraintMap(t, test.conf)
|
2022-01-17 13:43:30 +00:00
|
|
|
assert.Equal(t, test.want, c.Match(test.with), "config: '%s', with: '%s'", test.conf, test.with)
|
2019-04-06 13:44:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-02 05:36:57 +00:00
|
|
|
func TestConstraintStatusSuccess(t *testing.T) {
|
|
|
|
testdata := []struct {
|
|
|
|
conf string
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{conf: "", want: true},
|
|
|
|
{conf: "{status: [failure]}", want: false},
|
|
|
|
{conf: "{status: [success]}", want: true},
|
|
|
|
{conf: "{status: [failure, success]}", want: true},
|
|
|
|
{conf: "{status: {exclude: [success], include: [failure]}}", want: false},
|
|
|
|
{conf: "{status: {exclude: [failure], include: [success]}}", want: true},
|
|
|
|
}
|
|
|
|
for _, test := range testdata {
|
|
|
|
c := parseConstraints(t, test.conf)
|
|
|
|
assert.Equal(t, test.want, c.IncludesStatusSuccess(), "when: '%s'", test.conf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-06 13:44:04 +00:00
|
|
|
func TestConstraints(t *testing.T) {
|
|
|
|
testdata := []struct {
|
2022-08-29 22:36:37 +00:00
|
|
|
desc string
|
2019-04-06 13:44:04 +00:00
|
|
|
conf string
|
2023-06-04 22:15:07 +00:00
|
|
|
with metadata.Metadata
|
2023-07-02 21:45:22 +00:00
|
|
|
env map[string]string
|
2019-04-06 13:44:04 +00:00
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{
|
2022-08-29 22:36:37 +00:00
|
|
|
desc: "no constraints, must match on default events",
|
2019-04-06 13:44:04 +00:00
|
|
|
conf: "",
|
2023-06-04 22:15:07 +00:00
|
|
|
with: metadata.Metadata{
|
|
|
|
Curr: metadata.Pipeline{
|
|
|
|
Event: metadata.EventPush,
|
2022-08-29 22:36:37 +00:00
|
|
|
},
|
|
|
|
},
|
2019-04-06 13:44:04 +00:00
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
2022-08-29 22:36:37 +00:00
|
|
|
desc: "global branch filter",
|
2019-04-06 13:44:04 +00:00
|
|
|
conf: "{ branch: develop }",
|
2023-07-31 03:47:23 +00:00
|
|
|
with: metadata.Metadata{Curr: metadata.Pipeline{Event: metadata.EventPush, Commit: metadata.Commit{Branch: "main"}}},
|
2019-04-06 13:44:04 +00:00
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
2022-08-29 22:36:37 +00:00
|
|
|
desc: "global branch filter",
|
2023-07-31 03:47:23 +00:00
|
|
|
conf: "{ branch: main }",
|
|
|
|
with: metadata.Metadata{Curr: metadata.Pipeline{Event: metadata.EventPush, Commit: metadata.Commit{Branch: "main"}}},
|
2019-04-06 13:44:04 +00:00
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
2022-08-29 22:36:37 +00:00
|
|
|
desc: "repo constraint",
|
2021-10-02 08:59:34 +00:00
|
|
|
conf: "{ repo: owner/* }",
|
2023-06-04 22:15:07 +00:00
|
|
|
with: metadata.Metadata{Curr: metadata.Pipeline{Event: metadata.EventPush}, Repo: metadata.Repo{Owner: "owner", Name: "repo"}},
|
2019-04-06 13:44:04 +00:00
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
2022-08-29 22:36:37 +00:00
|
|
|
desc: "repo constraint",
|
2019-04-06 13:44:04 +00:00
|
|
|
conf: "{ repo: octocat/* }",
|
2023-06-04 22:15:07 +00:00
|
|
|
with: metadata.Metadata{Curr: metadata.Pipeline{Event: metadata.EventPush}, Repo: metadata.Repo{Owner: "owner", Name: "repo"}},
|
2019-04-06 13:44:04 +00:00
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
2022-08-29 22:36:37 +00:00
|
|
|
desc: "ref constraint",
|
2019-04-06 13:44:04 +00:00
|
|
|
conf: "{ ref: refs/tags/* }",
|
2023-06-04 22:15:07 +00:00
|
|
|
with: metadata.Metadata{Curr: metadata.Pipeline{Commit: metadata.Commit{Ref: "refs/tags/v1.0.0"}, Event: metadata.EventPush}},
|
2019-04-06 13:44:04 +00:00
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
2022-08-29 22:36:37 +00:00
|
|
|
desc: "ref constraint",
|
2019-04-06 13:44:04 +00:00
|
|
|
conf: "{ ref: refs/tags/* }",
|
2023-07-31 03:47:23 +00:00
|
|
|
with: metadata.Metadata{Curr: metadata.Pipeline{Commit: metadata.Commit{Ref: "refs/heads/main"}, Event: metadata.EventPush}},
|
2019-04-06 13:44:04 +00:00
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
2022-08-29 22:36:37 +00:00
|
|
|
desc: "platform constraint",
|
2019-04-06 13:44:04 +00:00
|
|
|
conf: "{ platform: linux/amd64 }",
|
2023-06-04 22:15:07 +00:00
|
|
|
with: metadata.Metadata{Curr: metadata.Pipeline{Event: metadata.EventPush}, Sys: metadata.System{Platform: "linux/amd64"}},
|
2019-04-06 13:44:04 +00:00
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
2022-08-29 22:36:37 +00:00
|
|
|
desc: "platform constraint",
|
2019-04-06 13:44:04 +00:00
|
|
|
conf: "{ repo: linux/amd64 }",
|
2023-06-04 22:15:07 +00:00
|
|
|
with: metadata.Metadata{Curr: metadata.Pipeline{Event: metadata.EventPush}, Sys: metadata.System{Platform: "windows/amd64"}},
|
2019-04-06 13:44:04 +00:00
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
2022-08-29 22:36:37 +00:00
|
|
|
desc: "instance constraint",
|
2021-10-02 08:59:34 +00:00
|
|
|
conf: "{ instance: agent.tld }",
|
2023-06-04 22:15:07 +00:00
|
|
|
with: metadata.Metadata{Curr: metadata.Pipeline{Event: metadata.EventPush}, Sys: metadata.System{Host: "agent.tld"}},
|
2019-04-06 13:44:04 +00:00
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
2022-08-29 22:36:37 +00:00
|
|
|
desc: "instance constraint",
|
2021-10-02 08:59:34 +00:00
|
|
|
conf: "{ instance: agent.tld }",
|
2023-06-04 22:15:07 +00:00
|
|
|
with: metadata.Metadata{Curr: metadata.Pipeline{Event: metadata.EventPush}, Sys: metadata.System{Host: "beta.agent.tld"}},
|
2022-08-29 22:36:37 +00:00
|
|
|
want: false,
|
|
|
|
},
|
2022-08-31 22:36:32 +00:00
|
|
|
{
|
|
|
|
desc: "filter cron by matching name",
|
|
|
|
conf: "{ event: cron, cron: job1 }",
|
2023-06-04 22:15:07 +00:00
|
|
|
with: metadata.Metadata{Curr: metadata.Pipeline{Event: metadata.EventCron, Cron: "job1"}},
|
2022-08-31 22:36:32 +00:00
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "filter cron by name",
|
|
|
|
conf: "{ event: cron, cron: job2 }",
|
2023-06-04 22:15:07 +00:00
|
|
|
with: metadata.Metadata{Curr: metadata.Pipeline{Event: metadata.EventCron, Cron: "job1"}},
|
2022-08-31 22:36:32 +00:00
|
|
|
want: false,
|
|
|
|
},
|
2023-06-04 22:15:07 +00:00
|
|
|
{
|
|
|
|
desc: "filter with build-in env passes",
|
|
|
|
conf: "{ branch: ${CI_REPO_DEFAULT_BRANCH} }",
|
|
|
|
with: metadata.Metadata{
|
|
|
|
Curr: metadata.Pipeline{Event: metadata.EventPush, Commit: metadata.Commit{Branch: "stable"}},
|
|
|
|
Repo: metadata.Repo{Branch: "stable"},
|
|
|
|
},
|
|
|
|
want: true,
|
|
|
|
},
|
2022-10-05 23:49:23 +00:00
|
|
|
{
|
|
|
|
desc: "filter by eval based on event",
|
2022-10-18 01:24:12 +00:00
|
|
|
conf: `{ evaluate: 'CI_PIPELINE_EVENT == "push"' }`,
|
2023-06-04 22:15:07 +00:00
|
|
|
with: metadata.Metadata{Curr: metadata.Pipeline{Event: metadata.EventPush}},
|
2022-10-05 23:49:23 +00:00
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "filter by eval based on event and repo",
|
2022-10-18 01:24:12 +00:00
|
|
|
conf: `{ evaluate: 'CI_PIPELINE_EVENT == "push" && CI_REPO == "owner/repo"' }`,
|
2023-06-04 22:15:07 +00:00
|
|
|
with: metadata.Metadata{Curr: metadata.Pipeline{Event: metadata.EventPush}, Repo: metadata.Repo{Owner: "owner", Name: "repo"}},
|
2022-10-05 23:49:23 +00:00
|
|
|
want: true,
|
|
|
|
},
|
2023-07-02 21:45:22 +00:00
|
|
|
{
|
|
|
|
desc: "filter by eval based on custom variable",
|
|
|
|
conf: `{ evaluate: 'TESTVAR == "testval"' }`,
|
|
|
|
with: metadata.Metadata{Curr: metadata.Pipeline{Event: metadata.EventManual}},
|
|
|
|
env: map[string]string{"TESTVAR": "testval"},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "filter by eval based on custom variable",
|
|
|
|
conf: `{ evaluate: 'TESTVAR == "testval"' }`,
|
|
|
|
with: metadata.Metadata{Curr: metadata.Pipeline{Event: metadata.EventManual}},
|
|
|
|
env: map[string]string{"TESTVAR": "qwe"},
|
|
|
|
want: false,
|
|
|
|
},
|
2019-04-06 13:44:04 +00:00
|
|
|
}
|
2022-08-31 22:36:32 +00:00
|
|
|
|
2019-04-06 13:44:04 +00:00
|
|
|
for _, test := range testdata {
|
2022-08-29 22:36:37 +00:00
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
2023-12-29 20:19:42 +00:00
|
|
|
conf, err := metadata.EnvVarSubst(test.conf, test.with.Environ())
|
2023-06-04 22:15:07 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
c := parseConstraints(t, conf)
|
2023-07-02 21:45:22 +00:00
|
|
|
got, err := c.Match(test.with, false, test.env)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, test.want, got)
|
2022-08-29 22:36:37 +00:00
|
|
|
})
|
2019-04-06 13:44:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-14 17:32:49 +00:00
|
|
|
func parseConstraints(t *testing.T, s string) *When {
|
|
|
|
c := &When{}
|
2021-11-23 14:36:52 +00:00
|
|
|
assert.NoError(t, yaml.Unmarshal([]byte(s), c))
|
2019-04-06 13:44:04 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2022-01-17 13:43:30 +00:00
|
|
|
func parseConstraint(t *testing.T, s string) *List {
|
|
|
|
c := &List{}
|
2021-11-23 14:36:52 +00:00
|
|
|
assert.NoError(t, yaml.Unmarshal([]byte(s), c))
|
2019-04-06 13:44:04 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2022-01-17 13:43:30 +00:00
|
|
|
func parseConstraintMap(t *testing.T, s string) *Map {
|
|
|
|
c := &Map{}
|
2021-11-23 14:36:52 +00:00
|
|
|
assert.NoError(t, yaml.Unmarshal([]byte(s), c))
|
2019-04-06 13:44:04 +00:00
|
|
|
return c
|
|
|
|
}
|
2021-06-28 21:50:35 +00:00
|
|
|
|
2022-01-17 13:43:30 +00:00
|
|
|
func parseConstraintPath(t *testing.T, s string) *Path {
|
|
|
|
c := &Path{}
|
2021-11-23 14:36:52 +00:00
|
|
|
assert.NoError(t, yaml.Unmarshal([]byte(s), c))
|
2021-06-28 21:50:35 +00:00
|
|
|
return c
|
|
|
|
}
|