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

Commit

Permalink
test: move customimage unit tests to their own package
Browse files Browse the repository at this point in the history
  • Loading branch information
supershal committed Apr 5, 2024
1 parent 0e3c0fb commit 9157b7f
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 150 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2023 D2iQ, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package customimage

import (
. "github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"

"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation"
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest"
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest/request"
dockerclusterconfig "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/docker/clusterconfig"
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig"
)

var _ = Describe("Docker CustomImage patches for ControlPlane", func() {
patchGenerator := func() mutation.GeneratePatches {
return mutation.NewMetaGeneratePatchesHandler("", NewControlPlanePatch()).(mutation.GeneratePatches)
}

testDefs := []capitest.PatchTestDef{
{
Name: "image unset for control plane",
Vars: []runtimehooksv1.Variable{
capitest.VariableWithValue(
"builtin",
apiextensionsv1.JSON{Raw: []byte(`{"controlPlane": {"version": "v1.2.3"}}`)},
),
},
RequestItem: request.NewCPDockerMachineTemplateRequestItem("1234"),
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{{
Operation: "add",
Path: "/spec/template/spec/customImage",
ValueMatcher: gomega.Equal("ghcr.io/mesosphere/kind-node:v1.2.3"),
}},
},
{
Name: "image set for control plane",
Vars: []runtimehooksv1.Variable{
capitest.VariableWithValue(
clusterconfig.MetaVariableName,
"a-specific-image",
clusterconfig.MetaControlPlaneConfigName,
dockerclusterconfig.DockerVariableName,
VariableName,
),
capitest.VariableWithValue(
"builtin",
apiextensionsv1.JSON{
Raw: []byte(`{"machineDeployment": {"class": "a-worker"}}`),
},
),
},
RequestItem: request.NewCPDockerMachineTemplateRequestItem("1234"),
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{{
Operation: "add",
Path: "/spec/template/spec/customImage",
ValueMatcher: gomega.Equal("a-specific-image"),
}},
},
}

// create test node for each case
for testIdx := range testDefs {
tt := testDefs[testIdx]
It(tt.Name, func() {
capitest.AssertGeneratePatches(
GinkgoT(),
patchGenerator,
&tt,
)
})
}
})
16 changes: 16 additions & 0 deletions pkg/handlers/docker/mutation/customimage/inject_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2023 D2iQ, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package customimage

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestCustomImagePatch(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Docker CustomImage patches for ControlPlane and Workers suite")
}
80 changes: 80 additions & 0 deletions pkg/handlers/docker/mutation/customimage/inject_worker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2023 D2iQ, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package customimage

import (
. "github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"

"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation"
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest"
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest/request"
dockerworkerconfig "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/docker/workerconfig"
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/workerconfig"
)

var _ = Describe("Docker CustomImage patches for workers", func() {
patchGenerator := func() mutation.GeneratePatches {
return mutation.NewMetaGeneratePatchesHandler("", NewWorkerPatch()).(mutation.GeneratePatches)
}

testDefs := []capitest.PatchTestDef{
{
Name: "image unset for workers",
Vars: []runtimehooksv1.Variable{
capitest.VariableWithValue(
"builtin",
apiextensionsv1.JSON{
Raw: []byte(
`{"machineDeployment": {"class": "a-worker", "version": "v1.2.3"}}`,
),
},
),
},
RequestItem: request.NewWorkerDockerMachineTemplateRequestItem("1234"),
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{{
Operation: "add",
Path: "/spec/template/spec/customImage",
ValueMatcher: gomega.Equal("ghcr.io/mesosphere/kind-node:v1.2.3"),
}},
},
{
Name: "image set for workers",
Vars: []runtimehooksv1.Variable{
capitest.VariableWithValue(
workerconfig.MetaVariableName,
"a-specific-image",
dockerworkerconfig.DockerVariableName,
VariableName,
),
capitest.VariableWithValue(
"builtin",
apiextensionsv1.JSON{
Raw: []byte(`{"machineDeployment": {"class": "a-worker"}}`),
},
),
},
RequestItem: request.NewWorkerDockerMachineTemplateRequestItem("1234"),
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{{
Operation: "add",
Path: "/spec/template/spec/customImage",
ValueMatcher: gomega.Equal("a-specific-image"),
}},
},
}

// create test node for each case
for testIdx := range testDefs {
tt := testDefs[testIdx]
It(tt.Name, func() {
capitest.AssertGeneratePatches(
GinkgoT(),
patchGenerator,
&tt,
)
})
}
})
122 changes: 0 additions & 122 deletions pkg/handlers/docker/mutation/customimage/tests/generate_patches.go

This file was deleted.

28 changes: 0 additions & 28 deletions pkg/handlers/docker/mutation/metapatch_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/manager"

"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation"
dockerclusterconfig "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/docker/clusterconfig"
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/docker/mutation/customimage"
customimagetests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/docker/mutation/customimage/tests"
dockerworkerconfig "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/docker/workerconfig"
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig"
auditpolicytests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/auditpolicy/tests"
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/etcd"
Expand All @@ -27,7 +23,6 @@ import (
globalimageregistrymirrortests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/mirrors/tests"
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/users"
userstests "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/users/tests"
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/workerconfig"
)

func metaPatchGeneratorFunc(mgr manager.Manager) func() mutation.GeneratePatches {
Expand All @@ -36,34 +31,11 @@ func metaPatchGeneratorFunc(mgr manager.Manager) func() mutation.GeneratePatches
}
}

func workerPatchGeneratorFunc() func() mutation.GeneratePatches {
return func() mutation.GeneratePatches {
return MetaWorkerPatchHandler().(mutation.GeneratePatches)
}
}

func TestGeneratePatches(t *testing.T) {
t.Parallel()

mgr := testEnv.Manager

customimagetests.TestControlPlaneGeneratePatches(
t,
metaPatchGeneratorFunc(mgr),
clusterconfig.MetaVariableName,
clusterconfig.MetaControlPlaneConfigName,
dockerclusterconfig.DockerVariableName,
customimage.VariableName,
)

customimagetests.TestWorkerGeneratePatches(
t,
workerPatchGeneratorFunc(),
workerconfig.MetaVariableName,
dockerworkerconfig.DockerVariableName,
customimage.VariableName,
)

auditpolicytests.TestGeneratePatches(
t,
metaPatchGeneratorFunc(mgr),
Expand Down

0 comments on commit 9157b7f

Please sign in to comment.