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 25, 2024
1 parent 7c8e465 commit 1a2aef0
Show file tree
Hide file tree
Showing 13 changed files with 19 additions and 43 deletions.
2 changes: 0 additions & 2 deletions broker/bucketbroker/server/bucket_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ 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/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

Expand All @@ -35,7 +34,6 @@ var _ = Describe("CreateBucket", func() {
},
},
})

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

Expand Down
6 changes: 2 additions & 4 deletions broker/bucketbroker/server/bucket_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ 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/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() {
Expand All @@ -33,7 +32,6 @@ var _ = Describe("DeleteBucket", func() {
},
},
})

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

Expand Down
1 change: 0 additions & 1 deletion broker/bucketbroker/server/bucketclass_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
)

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

_, bucketPool, srv := SetupTest()
bucketClass1 := SetupBucketClass("100Mi", "100")
bucketClass2 := SetupBucketClass("200Mi", "200")
Expand Down
9 changes: 3 additions & 6 deletions broker/bucketbroker/server/event_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,19 @@ 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/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()
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 +43,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: 3 additions & 1 deletion broker/bucketbroker/server/server_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ func SetupTest() (*corev1.Namespace, *storagev1alpha1.BucketPool, *server.Server
Expect(k8sClient.Create(ctx, bucketPool)).To(Succeed())
DeferCleanup(k8sClient.Delete, bucketPool)

newSrv, err := server.New(ctx, cfg, server.Options{
srvCtx, cancel := context.WithCancel(context.Background())
DeferCleanup(cancel)
newSrv, err := server.New(srvCtx, cfg, server.Options{
Namespace: ns.Name,
BucketPoolName: bucketPool.Name,
BucketPoolSelector: map[string]string{
Expand Down
1 change: 0 additions & 1 deletion broker/machinebroker/server/event_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
machinepoolletv1alpha1 "github.com/ironcore-dev/ironcore/poollet/machinepoollet/api/v1alpha1"
. "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"
Expand Down
6 changes: 3 additions & 3 deletions broker/machinebroker/server/server_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,15 @@ func SetupTest() (*corev1.Namespace, *server.Server) {
Expect(k8sClient.Create(ctx, ns)).To(Succeed(), "failed to create test namespace")
DeferCleanup(k8sClient.Delete, ns)

newSrv, err := server.New(ctx, cfg, ns.Name, server.Options{
srvCtx, cancel := context.WithCancel(context.Background())
DeferCleanup(cancel)
newSrv, err := server.New(srvCtx, cfg, ns.Name, server.Options{
BaseURL: baseURL,
BrokerDownwardAPILabels: map[string]string{
"root-machine-uid": machinepoolletv1alpha1.MachineUIDLabel,
},
})
Expect(err).NotTo(HaveOccurred())
srvCtx, cancel := context.WithCancel(context.Background())
DeferCleanup(cancel)
go func() {
defer GinkgoRecover()
Expect(newSrv.Start(srvCtx)).To(Succeed())
Expand Down
2 changes: 0 additions & 2 deletions broker/volumebroker/server/event_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
volumepoolletv1alpha1 "github.com/ironcore-dev/ironcore/poollet/volumepoollet/api/v1alpha1"
. "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"
Expand Down Expand Up @@ -49,7 +48,6 @@ var _ = Describe("ListEvents", func() {
},
},
})

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

Expand Down
4 changes: 3 additions & 1 deletion broker/volumebroker/server/server_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ func SetupTest() (*corev1.Namespace, *server.Server) {
Expect(k8sClient.Create(ctx, volumePool)).To(Succeed(), "failed to create test volume pool")
DeferCleanup(k8sClient.Delete, volumePool)

newSrv, err := server.New(ctx, cfg, server.Options{
srvCtx, cancel := context.WithCancel(context.Background())
DeferCleanup(cancel)
newSrv, err := server.New(srvCtx, cfg, server.Options{
Namespace: ns.Name,
VolumePoolName: volumePool.Name,
VolumePoolSelector: map[string]string{
Expand Down
2 changes: 0 additions & 2 deletions broker/volumebroker/server/volume_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ 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/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

Expand Down Expand Up @@ -38,7 +37,6 @@ var _ = Describe("CreateVolume", func() {
},
},
})

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

Expand Down
6 changes: 2 additions & 4 deletions broker/volumebroker/server/volume_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ 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/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() {
Expand All @@ -36,7 +35,6 @@ var _ = Describe("DeleteVolume", func() {
},
},
})

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

Expand Down
6 changes: 2 additions & 4 deletions broker/volumebroker/server/volume_expand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ 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/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() {
Expand All @@ -32,7 +31,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

0 comments on commit 1a2aef0

Please sign in to comment.