2022-02-26 21:54:28 +00:00
|
|
|
// 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 grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline/rpc"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
2022-02-26 21:54:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCreateFilterFunc(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tests := []struct {
|
2022-05-30 23:12:18 +00:00
|
|
|
name string
|
|
|
|
agentLabels map[string]string
|
2023-03-20 03:50:56 +00:00
|
|
|
task model.Task
|
2022-05-30 23:12:18 +00:00
|
|
|
exp bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "agent with missing labels",
|
|
|
|
agentLabels: map[string]string{"repo": "test/woodpecker"},
|
2023-03-20 03:50:56 +00:00
|
|
|
task: model.Task{
|
2022-05-30 23:12:18 +00:00
|
|
|
Labels: map[string]string{"platform": "linux/amd64", "repo": "test/woodpecker"},
|
2022-02-26 21:54:28 +00:00
|
|
|
},
|
2022-05-30 23:12:18 +00:00
|
|
|
exp: false,
|
2022-02-26 21:54:28 +00:00
|
|
|
},
|
2022-05-30 23:12:18 +00:00
|
|
|
{
|
|
|
|
name: "agent with wrong labels",
|
|
|
|
agentLabels: map[string]string{"platform": "linux/arm64"},
|
2023-03-20 03:50:56 +00:00
|
|
|
task: model.Task{
|
2022-05-30 23:12:18 +00:00
|
|
|
Labels: map[string]string{"platform": "linux/amd64"},
|
2022-02-26 21:54:28 +00:00
|
|
|
},
|
|
|
|
exp: false,
|
2022-05-30 23:12:18 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "agent with correct labels",
|
|
|
|
agentLabels: map[string]string{"platform": "linux/amd64", "location": "europe"},
|
2023-03-20 03:50:56 +00:00
|
|
|
task: model.Task{
|
2022-05-30 23:12:18 +00:00
|
|
|
Labels: map[string]string{"platform": "linux/amd64", "location": "europe"},
|
2022-02-26 21:54:28 +00:00
|
|
|
},
|
|
|
|
exp: true,
|
|
|
|
},
|
2022-05-30 23:12:18 +00:00
|
|
|
{
|
|
|
|
name: "agent with additional labels",
|
|
|
|
agentLabels: map[string]string{"platform": "linux/amd64", "location": "europe"},
|
2023-03-20 03:50:56 +00:00
|
|
|
task: model.Task{
|
2022-05-30 23:12:18 +00:00
|
|
|
Labels: map[string]string{"platform": "linux/amd64"},
|
2022-02-26 21:54:28 +00:00
|
|
|
},
|
|
|
|
exp: true,
|
2022-05-30 23:12:18 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "agent with wildcard label",
|
|
|
|
agentLabels: map[string]string{"platform": "linux/amd64", "location": "*"},
|
2023-03-20 03:50:56 +00:00
|
|
|
task: model.Task{
|
2022-05-30 23:12:18 +00:00
|
|
|
Labels: map[string]string{"platform": "linux/amd64", "location": "america"},
|
2022-02-26 21:54:28 +00:00
|
|
|
},
|
|
|
|
exp: true,
|
2022-05-30 23:12:18 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "agent with platform label and task without",
|
|
|
|
agentLabels: map[string]string{"platform": "linux/amd64"},
|
2023-03-20 03:50:56 +00:00
|
|
|
task: model.Task{
|
2022-05-30 23:12:18 +00:00
|
|
|
Labels: map[string]string{"platform": ""},
|
2022-02-26 21:54:28 +00:00
|
|
|
},
|
2022-05-30 23:12:18 +00:00
|
|
|
exp: true,
|
|
|
|
},
|
|
|
|
}
|
2022-02-26 21:54:28 +00:00
|
|
|
|
|
|
|
for _, test := range tests {
|
2022-05-30 23:12:18 +00:00
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2024-01-09 20:35:37 +00:00
|
|
|
fn := createFilterFunc(rpc.Filter{Labels: test.agentLabels})
|
2022-05-30 23:12:18 +00:00
|
|
|
assert.EqualValues(t, test.exp, fn(&test.task))
|
|
|
|
})
|
2022-02-26 21:54:28 +00:00
|
|
|
}
|
|
|
|
}
|