Skip to content

Commit

Permalink
Refactor tests to ensure proper cache sync using custom context
Browse files Browse the repository at this point in the history
  • Loading branch information
sujeet01 committed Nov 22, 2024
1 parent 7c8e465 commit 48e58cb
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 51 deletions.
6 changes: 3 additions & 3 deletions broker/bucketbroker/server/bucket_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ import (
iri "github.com/ironcore-dev/ironcore/iri/apis/bucket/v1alpha1"
irimeta "github.com/ironcore-dev/ironcore/iri/apis/meta/v1alpha1"
bucketpoolletv1alpha1 "github.com/ironcore-dev/ironcore/poollet/bucketpoollet/api/v1alpha1"

. "github.com/ironcore-dev/ironcore/utils/testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"sigs.k8s.io/controller-runtime/pkg/client"
)

var _ = Describe("CreateBucket", func() {
ns, _, srv := SetupTest()
ctx := SetupContext()
ns, _, srv := SetupTest(ctx)
bucketClass := SetupBucketClass("250Mi", "1500")

It("should correctly create a bucket", func(ctx SpecContext) {
Expand All @@ -35,7 +36,6 @@ var _ = Describe("CreateBucket", func() {
},
},
})

Expect(err).NotTo(HaveOccurred())
Expect(res).NotTo(BeNil())

Expand Down
10 changes: 5 additions & 5 deletions broker/bucketbroker/server/bucket_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ import (
iri "github.com/ironcore-dev/ironcore/iri/apis/bucket/v1alpha1"
irimeta "github.com/ironcore-dev/ironcore/iri/apis/meta/v1alpha1"
bucketpoolletv1alpha1 "github.com/ironcore-dev/ironcore/poollet/bucketpoollet/api/v1alpha1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/client"

. "github.com/ironcore-dev/ironcore/utils/testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
)

var _ = Describe("DeleteBucket", func() {
ns, _, srv := SetupTest()
ctx := SetupContext()
ns, _, srv := SetupTest(ctx)
bucketClass := SetupBucketClass("250Mi", "1500")

It("should correctly delete a bucket", func(ctx SpecContext) {
Expand All @@ -33,7 +34,6 @@ var _ = Describe("DeleteBucket", func() {
},
},
})

Expect(err).NotTo(HaveOccurred())
Expect(createRes).NotTo(BeNil())

Expand Down
4 changes: 3 additions & 1 deletion broker/bucketbroker/server/bucket_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
iri "github.com/ironcore-dev/ironcore/iri/apis/bucket/v1alpha1"
irimeta "github.com/ironcore-dev/ironcore/iri/apis/meta/v1alpha1"
bucketpoolletv1alpha1 "github.com/ironcore-dev/ironcore/poollet/bucketpoollet/api/v1alpha1"
. "github.com/ironcore-dev/ironcore/utils/testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("ListBuckets", func() {
_, _, srv := SetupTest()
ctx := SetupContext()
_, _, srv := SetupTest(ctx)
bucketClass := SetupBucketClass("250Mi", "1500")

It("should correctly list buckets", func(ctx SpecContext) {
Expand Down
5 changes: 3 additions & 2 deletions broker/bucketbroker/server/bucketclass_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ package server_test

import (
iri "github.com/ironcore-dev/ironcore/iri/apis/bucket/v1alpha1"
. "github.com/ironcore-dev/ironcore/utils/testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
)

var _ = Describe("ListBucketClasses", func() {

_, bucketPool, srv := SetupTest()
ctx := SetupContext()
_, bucketPool, srv := SetupTest(ctx)
bucketClass1 := SetupBucketClass("100Mi", "100")
bucketClass2 := SetupBucketClass("200Mi", "200")

Expand Down
13 changes: 6 additions & 7 deletions broker/bucketbroker/server/event_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ import (
"github.com/ironcore-dev/ironcore/iri/apis/event/v1alpha1"
irimeta "github.com/ironcore-dev/ironcore/iri/apis/meta/v1alpha1"
bucketpoolletv1alpha1 "github.com/ironcore-dev/ironcore/poollet/bucketpoollet/api/v1alpha1"

. "github.com/ironcore-dev/ironcore/utils/testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

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

var _ = Describe("ListEvents", func() {
ns, _, srv := SetupTest()
ctx := SetupContext()
ns, _, srv := SetupTest(ctx)
bucketClass := SetupBucketClass("250Mi", "1500")

FIt("should correctly list events", func(ctx SpecContext) {
It("should correctly list events", func(ctx SpecContext) {
Expect(storagev1alpha1.AddToScheme(scheme.Scheme)).To(Succeed())
k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{
Scheme: scheme.Scheme,
Expand All @@ -45,7 +45,6 @@ var _ = Describe("ListEvents", func() {
},
},
})

Expect(err).NotTo(HaveOccurred())
Expect(res).NotTo(BeNil())

Expand Down
13 changes: 1 addition & 12 deletions broker/bucketbroker/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"sigs.k8s.io/controller-runtime/pkg/log"
)

var scheme = runtime.NewScheme()
Expand Down Expand Up @@ -112,10 +111,8 @@ var _ iri.BucketRuntimeServer = (*Server)(nil)
//+kubebuilder:rbac:groups=storage.ironcore.dev,resources=buckets,verbs=get;list;watch;create;update;patch;delete

func New(ctx context.Context, cfg *rest.Config, opts Options) (*Server, error) {
log := log.FromContext(ctx)
log.Info("entered server.New")
setOptionsDefaults(&opts)
log.Info("create cache")

readCache, err := cache.New(cfg, cache.Options{
Scheme: scheme,
DefaultNamespaces: map[string]cache.Config{
Expand All @@ -126,22 +123,15 @@ func New(ctx context.Context, cfg *rest.Config, opts Options) (*Server, error) {
return nil, fmt.Errorf("error creating cache: %w", err)
}

log.Info("start cache")
go func() {
if err := readCache.Start(ctx); err != nil {
log.Info("error starting cache")
fmt.Printf("Error starting cache: %v\n", err)
}
log.Info("started cache")
}()

log.Info("syncing cache")
if !readCache.WaitForCacheSync(ctx) {
log.Info("timeout waiting for cache sync")
return nil, fmt.Errorf("failed to sync cache")
}

log.Info("creating client")
c, err := client.New(cfg, client.Options{
Scheme: scheme,
Cache: &client.CacheOptions{
Expand All @@ -153,7 +143,6 @@ func New(ctx context.Context, cfg *rest.Config, opts Options) (*Server, error) {
return nil, fmt.Errorf("error creating client: %w", err)
}

log.Info("returning server obj")
return &Server{
client: c,
namespace: opts.Namespace,
Expand Down
4 changes: 2 additions & 2 deletions broker/bucketbroker/server/server_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,14 @@ var _ = BeforeSuite(func() {
Expect(utilsenvtest.WaitUntilAPIServicesReadyWithTimeout(apiServiceTimeout, testEnvExt, k8sClient, scheme.Scheme)).To(Succeed())
})

func SetupTest() (*corev1.Namespace, *storagev1alpha1.BucketPool, *server.Server) {
func SetupTest(ctx context.Context) (*corev1.Namespace, *storagev1alpha1.BucketPool, *server.Server) {
var (
ns = &corev1.Namespace{}
srv = &server.Server{}
bucketPool = &storagev1alpha1.BucketPool{}
)

BeforeEach(func(ctx SpecContext) {
BeforeEach(func() {
*ns = corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-ns-",
Expand Down
6 changes: 3 additions & 3 deletions broker/volumebroker/server/event_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ import (

irimeta "github.com/ironcore-dev/ironcore/iri/apis/meta/v1alpha1"
volumepoolletv1alpha1 "github.com/ironcore-dev/ironcore/poollet/volumepoollet/api/v1alpha1"
. "github.com/ironcore-dev/ironcore/utils/testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

var _ = Describe("ListEvents", func() {
ns, srv := SetupTest()
ctx := SetupContext()
ns, srv := SetupTest(ctx)
volumeClass := SetupVolumeClass()

It("should correctly list events", func(ctx SpecContext) {
Expand All @@ -49,7 +50,6 @@ var _ = Describe("ListEvents", func() {
},
},
})

Expect(err).NotTo(HaveOccurred())
Expect(res).NotTo(BeNil())

Expand Down
4 changes: 2 additions & 2 deletions broker/volumebroker/server/server_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ var _ = BeforeSuite(func() {
Expect(utilsenvtest.WaitUntilAPIServicesReadyWithTimeout(apiServiceTimeout, testEnvExt, k8sClient, scheme.Scheme)).To(Succeed())
})

func SetupTest() (*corev1.Namespace, *server.Server) {
func SetupTest(ctx context.Context) (*corev1.Namespace, *server.Server) {
var (
ns = &corev1.Namespace{}
srv = &server.Server{}
volumePool = &storagev1alpha1.VolumePool{}
)

BeforeEach(func(ctx SpecContext) {
BeforeEach(func() {
*ns = corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-ns-",
Expand Down
6 changes: 3 additions & 3 deletions broker/volumebroker/server/volume_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ import (
irimeta "github.com/ironcore-dev/ironcore/iri/apis/meta/v1alpha1"
iri "github.com/ironcore-dev/ironcore/iri/apis/volume/v1alpha1"
volumepoolletv1alpha1 "github.com/ironcore-dev/ironcore/poollet/volumepoollet/api/v1alpha1"

. "github.com/ironcore-dev/ironcore/utils/testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"sigs.k8s.io/controller-runtime/pkg/client"
)

var _ = Describe("CreateVolume", func() {
ns, srv := SetupTest()
ctx := SetupContext()
ns, srv := SetupTest(ctx)
volumeClass := SetupVolumeClass()

It("should correctly create a volume", func(ctx SpecContext) {
Expand All @@ -38,7 +39,6 @@ var _ = Describe("CreateVolume", func() {
},
},
})

Expect(err).NotTo(HaveOccurred())
Expect(res).NotTo(BeNil())

Expand Down
10 changes: 5 additions & 5 deletions broker/volumebroker/server/volume_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ import (
irimeta "github.com/ironcore-dev/ironcore/iri/apis/meta/v1alpha1"
iri "github.com/ironcore-dev/ironcore/iri/apis/volume/v1alpha1"
volumepoolletv1alpha1 "github.com/ironcore-dev/ironcore/poollet/volumepoollet/api/v1alpha1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/client"

. "github.com/ironcore-dev/ironcore/utils/testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
)

var _ = Describe("DeleteVolume", func() {
ns, srv := SetupTest()
ctx := SetupContext()
ns, srv := SetupTest(ctx)
volumeClass := SetupVolumeClass()

It("should correctly delete a volume", func(ctx SpecContext) {
Expand All @@ -36,7 +37,6 @@ var _ = Describe("DeleteVolume", func() {
},
},
})

Expect(err).NotTo(HaveOccurred())
Expect(createRes).NotTo(BeNil())

Expand Down
10 changes: 5 additions & 5 deletions broker/volumebroker/server/volume_expand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ import (
irimeta "github.com/ironcore-dev/ironcore/iri/apis/meta/v1alpha1"
iri "github.com/ironcore-dev/ironcore/iri/apis/volume/v1alpha1"
volumepoolletv1alpha1 "github.com/ironcore-dev/ironcore/poollet/volumepoollet/api/v1alpha1"
. "github.com/ironcore-dev/ironcore/utils/testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

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

var _ = Describe("ExpandVolume", func() {
ns, srv := SetupTest()
ctx := SetupContext()
ns, srv := SetupTest(ctx)

It("should correctly Expand a volume", func(ctx SpecContext) {
By("creating a volume class with resize policy expandonly")
Expand All @@ -32,7 +33,6 @@ var _ = Describe("ExpandVolume", func() {
},
ResizePolicy: storagev1alpha1.ResizePolicyExpandOnly,
}

Expect(k8sClient.Create(ctx, volumeClass)).To(Succeed(), "failed to create test volume class")
DeferCleanup(k8sClient.Delete, volumeClass)

Expand Down
4 changes: 3 additions & 1 deletion broker/volumebroker/server/volume_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
irimeta "github.com/ironcore-dev/ironcore/iri/apis/meta/v1alpha1"
iri "github.com/ironcore-dev/ironcore/iri/apis/volume/v1alpha1"
volumepoolletv1alpha1 "github.com/ironcore-dev/ironcore/poollet/volumepoollet/api/v1alpha1"
. "github.com/ironcore-dev/ironcore/utils/testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("ListVolumes", func() {
_, srv := SetupTest()
ctx := SetupContext()
_, srv := SetupTest(ctx)
volumeClass := SetupVolumeClass()

It("should correctly list volumes", func(ctx SpecContext) {
Expand Down

0 comments on commit 48e58cb

Please sign in to comment.