diff --git a/pkg/handlers/docker/mutation/customimage/inject_control_plane_test.go b/pkg/handlers/docker/mutation/customimage/inject_control_plane_test.go new file mode 100644 index 000000000..fa40cbd5d --- /dev/null +++ b/pkg/handlers/docker/mutation/customimage/inject_control_plane_test.go @@ -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, + ) + }) + } +}) diff --git a/pkg/handlers/docker/mutation/customimage/inject_suite_test.go b/pkg/handlers/docker/mutation/customimage/inject_suite_test.go new file mode 100644 index 000000000..9f17edd7c --- /dev/null +++ b/pkg/handlers/docker/mutation/customimage/inject_suite_test.go @@ -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") +} diff --git a/pkg/handlers/docker/mutation/customimage/inject_worker_test.go b/pkg/handlers/docker/mutation/customimage/inject_worker_test.go new file mode 100644 index 000000000..b13f582d7 --- /dev/null +++ b/pkg/handlers/docker/mutation/customimage/inject_worker_test.go @@ -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, + ) + }) + } +}) diff --git a/pkg/handlers/docker/mutation/customimage/tests/generate_patches.go b/pkg/handlers/docker/mutation/customimage/tests/generate_patches.go deleted file mode 100644 index 7b1b9b6e4..000000000 --- a/pkg/handlers/docker/mutation/customimage/tests/generate_patches.go +++ /dev/null @@ -1,122 +0,0 @@ -// Copyright 2023 D2iQ, Inc. All rights reserved. -// SPDX-License-Identifier: Apache-2.0 - -package tests - -import ( - "testing" - - "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" -) - -func TestControlPlaneGeneratePatches( - t *testing.T, - generatorFunc func() mutation.GeneratePatches, - variableName string, - variablePath ...string, -) { - t.Helper() - - capitest.ValidateGeneratePatches( - t, - generatorFunc, - 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"), - }}, - }, - capitest.PatchTestDef{ - Name: "image set for control plane", - Vars: []runtimehooksv1.Variable{ - capitest.VariableWithValue( - variableName, - "a-specific-image", - variablePath..., - ), - 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"), - }}, - }, - ) -} - -func TestWorkerGeneratePatches( - t *testing.T, - generatorFunc func() mutation.GeneratePatches, - variableName string, - variablePath ...string, -) { - t.Helper() - - capitest.ValidateGeneratePatches( - t, - generatorFunc, - 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"), - }}, - }, - capitest.PatchTestDef{ - Name: "image set for workers", - Vars: []runtimehooksv1.Variable{ - capitest.VariableWithValue( - variableName, - "a-specific-image", - variablePath..., - ), - 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"), - }}, - }, - ) -} diff --git a/pkg/handlers/docker/mutation/metapatch_handler_test.go b/pkg/handlers/docker/mutation/metapatch_handler_test.go index d8e97ebd9..7b709e945 100644 --- a/pkg/handlers/docker/mutation/metapatch_handler_test.go +++ b/pkg/handlers/docker/mutation/metapatch_handler_test.go @@ -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" @@ -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 { @@ -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),