@@ -7,10 +7,18 @@ import (
7
7
"testing"
8
8
9
9
"github.com/google/go-cmp/cmp"
10
+ . "github.com/onsi/ginkgo/v2"
11
+ "github.com/onsi/gomega"
12
+ "k8s.io/apiserver/pkg/storage/names"
10
13
"k8s.io/utils/ptr"
11
14
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
15
+ runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
12
16
13
17
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
18
+ "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation"
19
+ "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest"
20
+ "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest/request"
21
+ "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig"
14
22
)
15
23
16
24
func Test_generateBootstrapUser (t * testing.T ) {
@@ -114,3 +122,83 @@ func Test_generateBootstrapUser(t *testing.T) {
114
122
})
115
123
}
116
124
}
125
+
126
+ var (
127
+ testUser1 = v1alpha1.User {
128
+ Name : "complete" ,
129
+ HashedPassword : "password" ,
130
+ SSHAuthorizedKeys : []string {
131
+ "key1" ,
132
+ "key2" ,
133
+ },
134
+ Sudo : "ALL=(ALL) NOPASSWD:ALL" ,
135
+ }
136
+ testUser2 = v1alpha1.User {
137
+ Name : "onlyname" ,
138
+ }
139
+ )
140
+
141
+ func TestUsersPatch (t * testing.T ) {
142
+ gomega .RegisterFailHandler (Fail )
143
+ RunSpecs (t , "Users mutator suite" )
144
+ }
145
+
146
+ var _ = Describe ("Generate Users patches" , func () {
147
+ patchGenerator := func () mutation.GeneratePatches {
148
+ return mutation .NewMetaGeneratePatchesHandler ("" , NewPatch ()).(mutation.GeneratePatches )
149
+ }
150
+
151
+ testDefs := []capitest.PatchTestDef {
152
+ {
153
+ Name : "unset variable" ,
154
+ },
155
+ {
156
+ Name : "users set for KubeadmControlPlaneTemplate" ,
157
+ Vars : []runtimehooksv1.Variable {
158
+ capitest .VariableWithValue (
159
+ clusterconfig .MetaVariableName ,
160
+ []v1alpha1.User {testUser1 , testUser2 },
161
+ VariableName ,
162
+ ),
163
+ },
164
+ RequestItem : request .NewKubeadmControlPlaneTemplateRequestItem ("" ),
165
+ ExpectedPatchMatchers : []capitest.JSONPatchMatcher {{
166
+ Operation : "add" ,
167
+ Path : "/spec/template/spec/kubeadmConfigSpec/users" ,
168
+ ValueMatcher : gomega .HaveLen (2 ),
169
+ }},
170
+ },
171
+ {
172
+ Name : "users set for KubeadmConfigTemplate generic worker" ,
173
+ Vars : []runtimehooksv1.Variable {
174
+ capitest .VariableWithValue (
175
+ clusterconfig .MetaVariableName ,
176
+ []v1alpha1.User {testUser1 , testUser2 },
177
+ VariableName ,
178
+ ),
179
+ capitest .VariableWithValue (
180
+ "builtin" ,
181
+ map [string ]any {
182
+ "machineDeployment" : map [string ]any {
183
+ "class" : names .SimpleNameGenerator .GenerateName ("worker-" ),
184
+ },
185
+ },
186
+ ),
187
+ },
188
+ RequestItem : request .NewKubeadmConfigTemplateRequestItem ("" ),
189
+ ExpectedPatchMatchers : []capitest.JSONPatchMatcher {{
190
+ Operation : "add" ,
191
+ Path : "/spec/template/spec/users" ,
192
+ ValueMatcher : gomega .HaveLen (2 ),
193
+ }},
194
+ },
195
+ }
196
+
197
+ // create test node for each case
198
+ for testIdx := range testDefs {
199
+ tt := testDefs [testIdx ]
200
+ It (tt .Name , func () {
201
+ capitest .AssertGeneratePatches (GinkgoT (), patchGenerator , & tt )
202
+ })
203
+ }
204
+ })
0 commit comments