Skip to content

Commit

Permalink
addressed nits
Browse files Browse the repository at this point in the history
  • Loading branch information
killianmuldoon committed Dec 7, 2021
1 parent f004801 commit f57e591
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 37 deletions.
28 changes: 8 additions & 20 deletions internal/webhooks/test/clusterclass_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"testing"

. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
utilfeature "k8s.io/component-base/featuregate/testing"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/feature"
Expand All @@ -33,7 +32,7 @@ func TestClusterClassWebhook_Succeed_Create(t *testing.T) {
defer utilfeature.SetFeatureGateDuringTest(t, feature.Gates, feature.ClusterTopology, true)()
g := NewWithT(t)

ns, err := env.CreateNamespace(ctx, "test-topology-cluster-reconcile")
ns, err := env.CreateNamespace(ctx, "test-topology-clusterclass-webhook")
g.Expect(err).ToNot(HaveOccurred())

// Create the objects needed for the integration test:
Expand Down Expand Up @@ -75,7 +74,7 @@ func TestClusterClassWebhook_Fail_Create(t *testing.T) {
defer utilfeature.SetFeatureGateDuringTest(t, feature.Gates, feature.ClusterTopology, true)()
g := NewWithT(t)

ns, err := env.CreateNamespace(ctx, "test-topology-cluster-reconcile")
ns, err := env.CreateNamespace(ctx, "test-topology-clusterclass-webhook")
g.Expect(err).ToNot(HaveOccurred())

// Create the objects needed for the integration test:
Expand Down Expand Up @@ -114,7 +113,7 @@ func TestClusterWebhook_Succeed_Update(t *testing.T) {
defer utilfeature.SetFeatureGateDuringTest(t, feature.Gates, feature.ClusterTopology, true)()
g := NewWithT(t)

ns, err := env.CreateNamespace(ctx, "test-topology-cluster-reconcile")
ns, err := env.CreateNamespace(ctx, "test-topology-clusterclass-webhook")
g.Expect(err).ToNot(HaveOccurred())

// Create the objects needed for the integration test:
Expand Down Expand Up @@ -177,7 +176,7 @@ func TestClusterWebhook_Succeed_Update(t *testing.T) {
actualCluster.Spec.Workers.MachineDeployments[1],
}
// Change the template used in the ClusterClass to a compatible alternative (Only name is changed).
actualCluster.Spec.Infrastructure.Ref = objToRef(builder.InfrastructureClusterTemplate(ns.Name, "NEW_INFRA").Build())
actualCluster.Spec.Infrastructure.Ref.Name = "NEW_INFRA"

// Attempt to update the ClusterClass with the above changes.
// Expect no error here as the updates are compatible with the current Clusters using the ClusterClass.
Expand All @@ -190,7 +189,7 @@ func TestClusterWebhook_Fail_Update(t *testing.T) {
defer utilfeature.SetFeatureGateDuringTest(t, feature.Gates, feature.ClusterTopology, true)()
g := NewWithT(t)

ns, err := env.CreateNamespace(ctx, "test-topology-cluster-reconcile")
ns, err := env.CreateNamespace(ctx, "test-topology-clusterclass-webhook")
g.Expect(err).ToNot(HaveOccurred())

// Create the objects needed for the integration test:
Expand Down Expand Up @@ -264,7 +263,7 @@ func TestClusterClassWebhook_Succeed_Delete(t *testing.T) {
defer utilfeature.SetFeatureGateDuringTest(t, feature.Gates, feature.ClusterTopology, true)()
g := NewWithT(t)

ns, err := env.CreateNamespace(ctx, "test-topology-cluster-reconcile")
ns, err := env.CreateNamespace(ctx, "test-topology-clusterclass-webhook")
g.Expect(err).ToNot(HaveOccurred())

// Create the objects needed for the integration test:
Expand Down Expand Up @@ -351,7 +350,7 @@ func TestClusterClassWebhook_Fail_Delete(t *testing.T) {
defer utilfeature.SetFeatureGateDuringTest(t, feature.Gates, feature.ClusterTopology, true)()
g := NewWithT(t)

ns, err := env.CreateNamespace(ctx, "test-topology-cluster-reconcile")
ns, err := env.CreateNamespace(ctx, "test-topology-clusterclass-webhook")
g.Expect(err).ToNot(HaveOccurred())

// Create the objects needed for the integration test:
Expand Down Expand Up @@ -392,7 +391,7 @@ func TestClusterClassWebhook_Fail_Delete(t *testing.T) {
Build()).
Build()

// Create the two ClusterClasses in the API server.
// Create the ClusterClass in the API server.
g.Expect(env.Create(ctx, clusterClass1)).To(Succeed())

// Create a clusters.
Expand All @@ -408,14 +407,3 @@ func TestClusterClassWebhook_Fail_Delete(t *testing.T) {
// Expect no error here as the webhook should allow the deletion of an existing ClusterClass.
g.Expect(env.Delete(ctx, clusterClass1)).NotTo(Succeed())
}

// objToRef returns a reference to the given object.
func objToRef(obj client.Object) *corev1.ObjectReference {
gvk := obj.GetObjectKind().GroupVersionKind()
return &corev1.ObjectReference{
Kind: gvk.Kind,
APIVersion: gvk.GroupVersion().String(),
Namespace: obj.GetNamespace(),
Name: obj.GetName(),
}
}
22 changes: 5 additions & 17 deletions internal/webhooks/test/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,16 @@ import (
"os"
"testing"

apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/api/v1beta1/index"
"sigs.k8s.io/cluster-api/internal/envtest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

var (
ctx = ctrl.SetupSignalHandler()
fakeScheme = runtime.NewScheme()
env *envtest.Environment
ctx = ctrl.SetupSignalHandler()
env *envtest.Environment
)

func init() {
_ = clientgoscheme.AddToScheme(fakeScheme)
_ = clusterv1.AddToScheme(fakeScheme)
_ = apiextensionsv1.AddToScheme(fakeScheme)
}
func TestMain(m *testing.M) {
setupIndexes := func(ctx context.Context, mgr ctrl.Manager) {
if err := index.AddDefaultIndexes(ctx, mgr); err != nil {
Expand All @@ -51,9 +40,8 @@ func TestMain(m *testing.M) {
}

os.Exit(envtest.Run(ctx, envtest.RunInput{
M: m,
ManagerUncachedObjs: []client.Object{},
SetupEnv: func(e *envtest.Environment) { env = e },
SetupIndexes: setupIndexes,
M: m,
SetupEnv: func(e *envtest.Environment) { env = e },
SetupIndexes: setupIndexes,
}))
}

0 comments on commit f57e591

Please sign in to comment.