Skip to content

Commit

Permalink
simplify test
Browse files Browse the repository at this point in the history
  • Loading branch information
rschalo committed Sep 19, 2024
1 parent dad42cd commit e7117bd
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pkg/controllers/migration/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (c *Controller[T]) Reconcile(ctx context.Context) (reconcile.Result, error)
}
// should filter tighter than this
crds := lo.Filter(crdList.Items, func(crd apiextensionsv1.CustomResourceDefinition, _ int) bool {
return strings.Contains(crd.ObjectMeta.Name, strings.ToLower(o.GetObjectKind().GroupVersionKind().Kind))
return strings.Contains(crd.ObjectMeta.Name, strings.ToLower(object.GVK(o).Kind))
})
for _, crd := range crds {
stored := crd.DeepCopy()
Expand Down
16 changes: 10 additions & 6 deletions pkg/controllers/migration/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package migration_test

import (
"context"
"strings"
"testing"

operatorexpectations "github.com/awslabs/operatorpkg/test/expectations"
Expand All @@ -30,7 +31,6 @@ import (
"sigs.k8s.io/karpenter/pkg/test"

corev1 "k8s.io/api/core/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"

. "sigs.k8s.io/karpenter/pkg/utils/testing"

Expand Down Expand Up @@ -60,9 +60,11 @@ var _ = AfterSuite(func() {
var _ = Describe("Migration", func() {
var node *corev1.Node
var nodeClaim *v1.NodeClaim
var nodeClass *v1alpha1.TestNodeClass

BeforeEach(func() {
nodeClaim, node = test.NodeClaimAndNode()
nodeClass = test.NodeClass()
node.Labels[v1.NodePoolLabelKey] = test.NodePool().Name
})

Expand All @@ -77,13 +79,15 @@ var _ = Describe("Migration", func() {
nodeClaim = ExpectExists(ctx, env.Client, nodeClaim)
Expect(nodeClaim.Annotations).To(HaveKeyWithValue("stored-version", "v1"))
})
It("should patch CRD status", func() {
ExpectApplied(ctx, env.Client, node, nodeClaim)
It("should patch CRD status stored versions", func() {
ExpectApplied(ctx, env.Client, node, nodeClaim, nodeClass)
operatorexpectations.ExpectSingletonReconciled(ctx, migrationController)
nodeClaim = ExpectExists(ctx, env.Client, nodeClaim)
crds := ExpectListExists(ctx, env.Client, &apiextensionsv1.CustomResourceDefinition{})
for _, crd := range crds {
Expect(crd.Object["status"]).To(HaveKeyWithValue("storedVersions", []interface{}{"v1"}))
nodeClass = ExpectExists(ctx, env.Client, nodeClass)
for _, crd := range env.CRDs {
if strings.Contains(crd.Name, strings.ToLower(nodeClass.Name)) {
Expect(crd.Status.StoredVersions).To(HaveExactElements("v1"))
}
}
})
})
Expand Down
10 changes: 0 additions & 10 deletions pkg/test/expectations/expectations.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (

"sigs.k8s.io/karpenter/pkg/test/v1alpha1"

"github.com/awslabs/operatorpkg/object"
"github.com/awslabs/operatorpkg/singleton"
"github.com/awslabs/operatorpkg/status"
. "github.com/onsi/ginkgo/v2" //nolint:revive,stylecheck
Expand All @@ -44,7 +43,6 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
Expand Down Expand Up @@ -96,14 +94,6 @@ func ExpectExists[T client.Object](ctx context.Context, c client.Client, obj T)
return resp
}

func ExpectListExists[T client.Object](ctx context.Context, c client.Client, obj T) []unstructured.Unstructured {
GinkgoHelper()
list := &unstructured.UnstructuredList{}
list.SetGroupVersionKind(object.GVK(obj))
Expect(c.List(ctx, list)).To(Succeed())
return list.Items
}

func ExpectPodExists(ctx context.Context, c client.Client, name string, namespace string) *corev1.Pod {
GinkgoHelper()
return ExpectExists(ctx, c, &corev1.Pod{ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: namespace}})
Expand Down

0 comments on commit e7117bd

Please sign in to comment.