Files
ingress-parser/parser/labels_decode_test.go
zeekay fb0e4ce424 fork: traefik/paerser -> hanzoai/ingress-parser
Rebrand module path, DefaultNamePrefix TRAEFIK_ -> INGRESS_,
DefaultRootName traefik -> ingress, config paths /etc/traefik -> /etc/ingress.
All 7 packages pass tests.
2026-03-19 02:57:11 -07:00

260 lines
5.4 KiB
Go

package parser
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDecodeToNode(t *testing.T) {
type expected struct {
error bool
node *Node
}
testCases := []struct {
desc string
in map[string]string
filters []string
expected expected
}{
{
desc: "no label",
in: map[string]string{},
expected: expected{node: nil},
},
{
desc: "invalid label, ending by a dot",
in: map[string]string{
"ingress.http.": "bar",
},
expected: expected{
error: true,
},
},
{
desc: "level 1",
in: map[string]string{
"ingress.foo": "bar",
},
expected: expected{node: &Node{
Name: "ingress",
Children: []*Node{
{Name: "foo", Value: "bar"},
},
}},
},
{
desc: "level 1 empty value",
in: map[string]string{
"ingress.foo": "",
},
expected: expected{node: &Node{
Name: "ingress",
Children: []*Node{
{Name: "foo", Value: ""},
},
}},
},
{
desc: "level 2",
in: map[string]string{
"ingress.foo.bar": "bar",
},
expected: expected{node: &Node{
Name: "ingress",
Children: []*Node{{
Name: "foo",
Children: []*Node{
{Name: "bar", Value: "bar"},
},
}},
}},
},
{
desc: "several entries, level 0",
in: map[string]string{
"ingress": "bar",
"traefic": "bur",
},
expected: expected{error: true},
},
{
desc: "several entries, prefix filter",
in: map[string]string{
"ingress.foo": "bar",
"ingress.fii": "bir",
},
filters: []string{"ingress.Foo"},
expected: expected{node: &Node{
Name: "ingress",
Children: []*Node{
{Name: "foo", Value: "bar"},
},
}},
},
{
desc: "several entries, level 1",
in: map[string]string{
"ingress.foo": "bar",
"ingress.fii": "bur",
},
expected: expected{node: &Node{
Name: "ingress",
Children: []*Node{
{Name: "fii", Value: "bur"},
{Name: "foo", Value: "bar"},
},
}},
},
{
desc: "several entries, level 2",
in: map[string]string{
"ingress.foo.aaa": "bar",
"ingress.foo.bbb": "bur",
},
expected: expected{node: &Node{
Name: "ingress",
Children: []*Node{
{Name: "foo", Children: []*Node{
{Name: "aaa", Value: "bar"},
{Name: "bbb", Value: "bur"},
}},
},
}},
},
{
desc: "several entries, level 2, case-insensitive",
in: map[string]string{
"ingress.foo.aaa": "bar",
"ingress.Foo.bbb": "bur",
},
expected: expected{node: &Node{
Name: "ingress",
Children: []*Node{
{Name: "Foo", Children: []*Node{
{Name: "bbb", Value: "bur"},
{Name: "aaa", Value: "bar"},
}},
},
}},
},
{
desc: "several entries, level 2, 3 children",
in: map[string]string{
"ingress.foo.aaa": "bar",
"ingress.foo.bbb": "bur",
"ingress.foo.ccc": "bir",
},
expected: expected{node: &Node{
Name: "ingress",
Children: []*Node{
{Name: "foo", Children: []*Node{
{Name: "aaa", Value: "bar"},
{Name: "bbb", Value: "bur"},
{Name: "ccc", Value: "bir"},
}},
},
}},
},
{
desc: "several entries, level 3",
in: map[string]string{
"ingress.foo.bar.aaa": "bar",
"ingress.foo.bar.bbb": "bur",
},
expected: expected{node: &Node{
Name: "ingress",
Children: []*Node{
{Name: "foo", Children: []*Node{
{Name: "bar", Children: []*Node{
{Name: "aaa", Value: "bar"},
{Name: "bbb", Value: "bur"},
}},
}},
},
}},
},
{
desc: "several entries, level 3, 2 children level 1",
in: map[string]string{
"ingress.foo.bar.aaa": "bar",
"ingress.foo.bar.bbb": "bur",
"ingress.bar.foo.bbb": "bir",
},
expected: expected{node: &Node{
Name: "ingress",
Children: []*Node{
{Name: "bar", Children: []*Node{
{Name: "foo", Children: []*Node{
{Name: "bbb", Value: "bir"},
}},
}},
{Name: "foo", Children: []*Node{
{Name: "bar", Children: []*Node{
{Name: "aaa", Value: "bar"},
{Name: "bbb", Value: "bur"},
}},
}},
},
}},
},
{
desc: "several entries, slice syntax",
in: map[string]string{
"ingress.foo[0].aaa": "bar0",
"ingress.foo[0].bbb": "bur0",
"ingress.foo[1].aaa": "bar1",
"ingress.foo[1].bbb": "bur1",
},
expected: expected{node: &Node{
Name: "ingress",
Children: []*Node{
{Name: "foo", Children: []*Node{
{Name: "[0]", Children: []*Node{
{Name: "aaa", Value: "bar0"},
{Name: "bbb", Value: "bur0"},
}},
{Name: "[1]", Children: []*Node{
{Name: "aaa", Value: "bar1"},
{Name: "bbb", Value: "bur1"},
}},
}},
},
}},
},
{
desc: "several entries, invalid slice syntax",
in: map[string]string{
"ingress.foo.[0].aaa": "bar0",
"ingress.foo.[0].bbb": "bur0",
"ingress.foo.[1].aaa": "bar1",
"ingress.foo.[1].bbb": "bur1",
},
expected: expected{error: true},
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
out, err := DecodeToNode(test.in, DefaultRootName, test.filters...)
if test.expected.error {
require.Error(t, err)
} else {
require.NoError(t, err)
if !assert.Equal(t, test.expected.node, out) {
bytes, err := json.MarshalIndent(out, "", " ")
require.NoError(t, err)
t.Log(string(bytes))
}
}
})
}
}