Files
ingress-parser/parser/labels_encode_test.go
zeekayandClaude Opus 5 7b23b7e68d Go 1.27.1, and the code it can say more simply
Modules declare go 1.27.1 and the build images follow. go fix applied the
modernizers; omitzero is not applied, since it changes what the JSON says.

Co-Authored-By: Claude Opus 5 <[email protected]>
2026-09-16 19:25:40 -07:00

234 lines
4.6 KiB
Go

package parser
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestEncodeNode(t *testing.T) {
testCases := []struct {
desc string
node *Node
expected map[string]string
}{
{
desc: "1 label",
node: &Node{
Name: "ingress",
Children: []*Node{
{Name: "aaa", Value: "bar"},
},
},
expected: map[string]string{
"ingress.aaa": "bar",
},
},
{
desc: "2 labels",
node: &Node{
Name: "ingress",
Children: []*Node{
{Name: "aaa", Value: "bar"},
{Name: "bbb", Value: "bur"},
},
},
expected: map[string]string{
"ingress.aaa": "bar",
"ingress.bbb": "bur",
},
},
{
desc: "2 labels, 1 disabled",
node: &Node{
Name: "ingress",
Children: []*Node{
{Name: "aaa", Value: "bar"},
{Name: "bbb", Value: "bur", Disabled: true},
},
},
expected: map[string]string{
"ingress.aaa": "bar",
},
},
{
desc: "2 levels",
node: &Node{
Name: "ingress",
Children: []*Node{
{Name: "foo", Children: []*Node{
{Name: "aaa", Value: "bar"},
}},
},
},
expected: map[string]string{
"ingress.foo.aaa": "bar",
},
},
{
desc: "3 levels",
node: &Node{
Name: "ingress",
Children: []*Node{
{Name: "foo", Children: []*Node{
{Name: "bar", Children: []*Node{
{Name: "aaa", Value: "bar"},
}},
}},
},
},
expected: map[string]string{
"ingress.foo.bar.aaa": "bar",
},
},
{
desc: "2 levels, same root",
node: &Node{
Name: "ingress",
Children: []*Node{
{Name: "foo", Children: []*Node{
{Name: "bar", Children: []*Node{
{Name: "aaa", Value: "bar"},
{Name: "bbb", Value: "bur"},
}},
}},
},
},
expected: map[string]string{
"ingress.foo.bar.aaa": "bar",
"ingress.foo.bar.bbb": "bur",
},
},
{
desc: "several levels, different root",
node: &Node{
Name: "ingress",
Children: []*Node{
{Name: "bar", Children: []*Node{
{Name: "ccc", Value: "bir"},
}},
{Name: "foo", Children: []*Node{
{Name: "bar", Children: []*Node{
{Name: "aaa", Value: "bar"},
}},
}},
},
},
expected: map[string]string{
"ingress.foo.bar.aaa": "bar",
"ingress.bar.ccc": "bir",
},
},
{
desc: "multiple labels, multiple levels",
node: &Node{
Name: "ingress",
Children: []*Node{
{Name: "bar", Children: []*Node{
{Name: "ccc", Value: "bir"},
}},
{Name: "foo", Children: []*Node{
{Name: "bar", Children: []*Node{
{Name: "aaa", Value: "bar"},
{Name: "bbb", Value: "bur"},
}},
}},
},
},
expected: map[string]string{
"ingress.foo.bar.aaa": "bar",
"ingress.foo.bar.bbb": "bur",
"ingress.bar.ccc": "bir",
},
},
{
desc: "slice of struct syntax",
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"},
}},
}},
},
},
expected: map[string]string{
"ingress.foo[0].aaa": "bar0",
"ingress.foo[0].bbb": "bur0",
"ingress.foo[1].aaa": "bar1",
"ingress.foo[1].bbb": "bur1",
},
},
{
desc: "raw value, level 1",
node: &Node{
Name: "ingress",
Children: []*Node{
{Name: "aaa", RawValue: map[string]any{
"bbb": "test1",
"ccc": "test2",
}},
},
},
expected: map[string]string{
"ingress.aaa.bbb": "test1",
"ingress.aaa.ccc": "test2",
},
},
{
desc: "raw value, level 2",
node: &Node{
Name: "ingress",
Children: []*Node{
{Name: "aaa", RawValue: map[string]any{
"bbb": "test1",
"ccc": map[string]any{
"ddd": "test2",
},
}},
},
},
expected: map[string]string{
"ingress.aaa.bbb": "test1",
"ingress.aaa.ccc.ddd": "test2",
},
},
{
desc: "raw value, slice of struct",
node: &Node{
Name: "ingress",
Children: []*Node{
{Name: "aaa", RawValue: map[string]any{
"bbb": []any{
map[string]any{
"ccc": "test1",
"ddd": "test2",
},
},
}},
},
},
expected: map[string]string{
"ingress.aaa.bbb[0].ccc": "test1",
"ingress.aaa.bbb[0].ddd": "test2",
},
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
labels := EncodeNode(test.node)
assert.Equal(t, test.expected, labels)
})
}
}