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

test: move docker unit tests to their own package #30

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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