Skip to content

Commit 2ad7965

Browse files
committed
feat(test): orderMap for makeValidator
1 parent 75e9e35 commit 2ad7965

File tree

1 file changed

+102
-2
lines changed

1 file changed

+102
-2
lines changed

api/internal/generators/utils_test.go

Lines changed: 102 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
// Copyright 2020 The Kubernetes Authors.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
package generators_test
4+
package generators
55

66
import (
77
"testing"
88

99
"github.com/stretchr/testify/require"
10-
. "sigs.k8s.io/kustomize/api/internal/generators"
10+
"sigs.k8s.io/kustomize/api/ifc"
11+
valtest_test "sigs.k8s.io/kustomize/api/testutils/valtest"
12+
"sigs.k8s.io/kustomize/api/types"
1113
)
1214

1315
func TestParseFileSource(t *testing.T) {
@@ -49,3 +51,101 @@ func TestParseFileSource(t *testing.T) {
4951
})
5052
}
5153
}
54+
55+
func TestMakeValidateDataMapOrder(t *testing.T) {
56+
tests := []struct {
57+
name string
58+
pairs []types.Pair
59+
wantKeys []string
60+
wantValues map[string]string
61+
wantErr bool
62+
validatorFn func() ifc.Validator
63+
}{
64+
{
65+
name: "single pair",
66+
pairs: []types.Pair{
67+
{Key: "key", Value: "value"},
68+
},
69+
wantKeys: []string{"key"},
70+
wantValues: map[string]string{"key": "value"},
71+
},
72+
{
73+
name: "multiple pairs preserve order",
74+
pairs: []types.Pair{
75+
{Key: "A", Value: "1"},
76+
{Key: "B", Value: "2"},
77+
{Key: "C", Value: "3"},
78+
},
79+
wantKeys: []string{"A", "B", "C"},
80+
wantValues: map[string]string{"A": "1", "B": "2", "C": "3"},
81+
},
82+
{
83+
name: "multiple pairs out of order - loader order wins",
84+
pairs: []types.Pair{
85+
{Key: "third", Value: "3"},
86+
{Key: "first", Value: "1"},
87+
{Key: "second", Value: "2"},
88+
},
89+
wantKeys: []string{"third", "first", "second"},
90+
wantValues: map[string]string{"third": "3", "first": "1", "second": "2"},
91+
},
92+
{
93+
name: "duplicate key returns error",
94+
pairs: []types.Pair{
95+
{Key: "dup", Value: "1"},
96+
{Key: "dup", Value: "2"},
97+
},
98+
wantErr: true,
99+
},
100+
}
101+
102+
for _, tt := range tests {
103+
t.Run(tt.name, func(t *testing.T) {
104+
loader := &fakeKvLoader{
105+
Pairs: tt.pairs,
106+
Val: func() ifc.Validator {
107+
if tt.validatorFn != nil {
108+
return tt.validatorFn()
109+
}
110+
return valtest_test.MakeFakeValidator()
111+
}(),
112+
}
113+
114+
args := types.ConfigMapArgs{
115+
GeneratorArgs: types.GeneratorArgs{
116+
Name: "envConfigMap",
117+
KvPairSources: types.KvPairSources{
118+
EnvSources: []string{"app.env"},
119+
},
120+
},
121+
}
122+
123+
res, err := makeValidatedDataMap(loader, args.Name, args.KvPairSources)
124+
125+
if tt.wantErr {
126+
require.Error(t, err)
127+
return
128+
}
129+
130+
require.NoError(t, err)
131+
require.Exactly(t, tt.wantKeys, res.keys)
132+
require.Exactly(t, tt.wantValues, res.values)
133+
})
134+
}
135+
}
136+
137+
type fakeKvLoader struct {
138+
Pairs []types.Pair
139+
Val ifc.Validator
140+
}
141+
142+
func (f *fakeKvLoader) Load(_ types.KvPairSources) ([]types.Pair, error) {
143+
return f.Pairs, nil
144+
}
145+
146+
func (f *fakeKvLoader) Validator() ifc.Validator {
147+
if f.Val != nil {
148+
return f.Val
149+
}
150+
return valtest_test.MakeFakeValidator()
151+
}

0 commit comments

Comments
 (0)