Skip to content
This repository was archived by the owner on Apr 11, 2024. It is now read-only.

Commit 30cb4d9

Browse files
committed
test: move users unit tests
1 parent 0e0f010 commit 30cb4d9

File tree

4 files changed

+97
-109
lines changed

4 files changed

+97
-109
lines changed

pkg/handlers/aws/mutation/metapatch_handler_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ import (
1010

1111
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation"
1212
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig"
13+
<<<<<<< HEAD
1314
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/users"
1415
userstests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/users/tests"
16+
=======
17+
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/workerconfig"
18+
>>>>>>> 4571fcb (test: move users unit tests)
1519
)
1620

1721
func metaPatchGeneratorFunc(mgr manager.Manager) func() mutation.GeneratePatches {
@@ -25,10 +29,4 @@ func TestGeneratePatches(t *testing.T) {
2529

2630
mgr := testEnv.Manager
2731

28-
userstests.TestGeneratePatches(
29-
t,
30-
metaPatchGeneratorFunc(mgr),
31-
clusterconfig.MetaVariableName,
32-
users.VariableName,
33-
)
3432
}

pkg/handlers/docker/mutation/metapatch_handler_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ import (
1010

1111
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation"
1212
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig"
13+
<<<<<<< HEAD
1314
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/users"
1415
userstests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/users/tests"
16+
=======
17+
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/workerconfig"
18+
>>>>>>> 4571fcb (test: move users unit tests)
1519
)
1620

1721
func metaPatchGeneratorFunc(mgr manager.Manager) func() mutation.GeneratePatches {
@@ -25,10 +29,5 @@ func TestGeneratePatches(t *testing.T) {
2529

2630
mgr := testEnv.Manager
2731

28-
userstests.TestGeneratePatches(
29-
t,
30-
metaPatchGeneratorFunc(mgr),
31-
clusterconfig.MetaVariableName,
32-
users.VariableName,
33-
)
32+
3433
}

pkg/handlers/generic/mutation/users/inject_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,18 @@ import (
77
"testing"
88

99
"github.com/google/go-cmp/cmp"
10+
. "github.com/onsi/ginkgo/v2"
11+
"github.com/onsi/gomega"
12+
"k8s.io/apiserver/pkg/storage/names"
1013
"k8s.io/utils/ptr"
1114
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
15+
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
1216

1317
"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"
1422
)
1523

1624
func Test_generateBootstrapUser(t *testing.T) {
@@ -114,3 +122,83 @@ func Test_generateBootstrapUser(t *testing.T) {
114122
})
115123
}
116124
}
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+
})

pkg/handlers/generic/mutation/users/tests/generate_patches.go

Lines changed: 0 additions & 97 deletions
This file was deleted.

0 commit comments

Comments
 (0)