Files
zeekayandHanzo Dev cbf668a980 Build on Go 1.27
go fix applied the modernizers: promoted-field keys in struct literals,
strings.Cut, SplitSeq, new(expr), slices and maps helpers, wg.Go, reflect.TypeFor
and errors.AsType. omitzero is left out, since omitting a zero struct such as
time.Time{} is a per-field wire decision and not a mechanical one.

Two changes the modernizers could not make for themselves. AsType needs its type
argument to be an error, so the git exit-status reader embeds error in the
interface it asks for. And an error built by concatenating a tag list into its
own format string now says so with a verb, which is what vet was pointing at.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-17 01:47:46 -07:00

82 lines
2.2 KiB
Go

package generators
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"github.com/hanzoai/cd/pkg/apis/application/v1alpha1"
)
func TestGenerateListParams(t *testing.T) {
t.Parallel()
testCases := []struct {
elements []apiextensionsv1.JSON
expected []map[string]any
}{
{
elements: []apiextensionsv1.JSON{{Raw: []byte(`{"cluster": "cluster","url": "url"}`)}},
expected: []map[string]any{{"cluster": "cluster", "url": "url"}},
}, {
elements: []apiextensionsv1.JSON{{Raw: []byte(`{"cluster": "cluster","url": "url","values":{"foo":"bar"}}`)}},
expected: []map[string]any{{"cluster": "cluster", "url": "url", "values.foo": "bar"}},
},
}
for _, testCase := range testCases {
listGenerator := NewListGenerator()
applicationSetInfo := v1alpha1.ApplicationSet{
Name: "set",
Spec: v1alpha1.ApplicationSetSpec{},
}
got, err := listGenerator.GenerateParams(&v1alpha1.ApplicationSetGenerator{
List: &v1alpha1.ListGenerator{
Elements: testCase.elements,
},
}, &applicationSetInfo, nil)
require.NoError(t, err)
assert.ElementsMatch(t, testCase.expected, got)
}
}
func TestGenerateListParamsGoTemplate(t *testing.T) {
t.Parallel()
testCases := []struct {
elements []apiextensionsv1.JSON
expected []map[string]any
}{
{
elements: []apiextensionsv1.JSON{{Raw: []byte(`{"cluster": "cluster","url": "url"}`)}},
expected: []map[string]any{{"cluster": "cluster", "url": "url"}},
}, {
elements: []apiextensionsv1.JSON{{Raw: []byte(`{"cluster": "cluster","url": "url","values":{"foo":"bar"}}`)}},
expected: []map[string]any{{"cluster": "cluster", "url": "url", "values": map[string]any{"foo": "bar"}}},
},
}
for _, testCase := range testCases {
listGenerator := NewListGenerator()
applicationSetInfo := v1alpha1.ApplicationSet{
Name: "set",
Spec: v1alpha1.ApplicationSetSpec{
GoTemplate: true,
},
}
got, err := listGenerator.GenerateParams(&v1alpha1.ApplicationSetGenerator{
List: &v1alpha1.ListGenerator{
Elements: testCase.elements,
},
}, &applicationSetInfo, nil)
require.NoError(t, err)
assert.ElementsMatch(t, testCase.expected, got)
}
}