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.
|
|
|
|
|
2023-06-06 07:14:21 +00:00
|
|
|
package types
|
2019-04-06 13:44:04 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-12-04 12:23:33 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-11-14 19:16:03 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
2019-04-06 13:44:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestUnmarshalNetwork(t *testing.T) {
|
|
|
|
testdata := []struct {
|
|
|
|
from string
|
2023-06-06 07:14:21 +00:00
|
|
|
want WorkflowNetwork
|
2019-04-06 13:44:04 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
from: "{ name: foo, driver: bar }",
|
2023-06-06 07:14:21 +00:00
|
|
|
want: WorkflowNetwork{
|
2019-04-06 13:44:04 +00:00
|
|
|
Name: "foo",
|
|
|
|
Driver: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
from: "{ name: foo, driver: bar, driver_opts: { baz: qux } }",
|
2023-06-06 07:14:21 +00:00
|
|
|
want: WorkflowNetwork{
|
2019-04-06 13:44:04 +00:00
|
|
|
Name: "foo",
|
|
|
|
Driver: "bar",
|
|
|
|
DriverOpts: map[string]string{
|
|
|
|
"baz": "qux",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testdata {
|
|
|
|
in := []byte(test.from)
|
2023-06-06 07:14:21 +00:00
|
|
|
got := WorkflowNetwork{}
|
2019-04-06 13:44:04 +00:00
|
|
|
err := yaml.Unmarshal(in, &got)
|
2021-12-04 12:23:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, test.want, got, "problem parsing network %q", test.from)
|
2019-04-06 13:44:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-06 07:14:21 +00:00
|
|
|
func TestUnmarshalWorkflowNetworks(t *testing.T) {
|
2019-04-06 13:44:04 +00:00
|
|
|
testdata := []struct {
|
|
|
|
from string
|
2023-06-06 07:14:21 +00:00
|
|
|
want []*WorkflowNetwork
|
2019-04-06 13:44:04 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
from: "foo: { driver: bar }",
|
2023-06-06 07:14:21 +00:00
|
|
|
want: []*WorkflowNetwork{
|
2019-04-06 13:44:04 +00:00
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
Driver: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
from: "foo: { name: baz }",
|
2023-06-06 07:14:21 +00:00
|
|
|
want: []*WorkflowNetwork{
|
2019-04-06 13:44:04 +00:00
|
|
|
{
|
|
|
|
Name: "baz",
|
|
|
|
Driver: "bridge",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
from: "foo: { name: baz, driver: bar }",
|
2023-06-06 07:14:21 +00:00
|
|
|
want: []*WorkflowNetwork{
|
2019-04-06 13:44:04 +00:00
|
|
|
{
|
|
|
|
Name: "baz",
|
|
|
|
Driver: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testdata {
|
|
|
|
in := []byte(test.from)
|
2023-06-06 07:14:21 +00:00
|
|
|
got := WorkflowNetworks{}
|
2019-04-06 13:44:04 +00:00
|
|
|
err := yaml.Unmarshal(in, &got)
|
2021-12-04 12:23:33 +00:00
|
|
|
assert.NoError(t, err)
|
2023-06-06 07:14:21 +00:00
|
|
|
assert.EqualValues(t, test.want, got.WorkflowNetworks, "problem parsing network %q", test.from)
|
2019-04-06 13:44:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalNetworkErr(t *testing.T) {
|
|
|
|
testdata := []string{
|
|
|
|
"foo: { name: [ foo, bar] }",
|
|
|
|
"- foo",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testdata {
|
|
|
|
in := []byte(test)
|
2023-06-06 07:14:21 +00:00
|
|
|
err := yaml.Unmarshal(in, new(WorkflowNetworks))
|
2021-12-04 12:23:33 +00:00
|
|
|
assert.Error(t, err, "wanted error for networks %q", test)
|
2019-04-06 13:44:04 +00:00
|
|
|
}
|
|
|
|
}
|