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 13d7d0c
Show file tree
Hide file tree
Showing 23 changed files with 74 additions and 64 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
5 changes: 3 additions & 2 deletions broker/machinebroker/server/event_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ import (
iri "github.com/ironcore-dev/ironcore/iri/apis/machine/v1alpha1"
irimeta "github.com/ironcore-dev/ironcore/iri/apis/meta/v1alpha1"
machinepoolletv1alpha1 "github.com/ironcore-dev/ironcore/poollet/machinepoollet/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)
machineClass := SetupMachineClass()

It("should correctly list events", func(ctx SpecContext) {
Expand Down
4 changes: 3 additions & 1 deletion broker/machinebroker/server/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
"net/url"

iri "github.com/ironcore-dev/ironcore/iri/apis/machine/v1alpha1"
. "github.com/ironcore-dev/ironcore/utils/testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

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

It("should return an exec-url with a token", func(ctx SpecContext) {
By("issuing exec for an arbitrary machine id")
Expand Down
4 changes: 3 additions & 1 deletion broker/machinebroker/server/machine_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import (
iri "github.com/ironcore-dev/ironcore/iri/apis/machine/v1alpha1"
irimeta "github.com/ironcore-dev/ironcore/iri/apis/meta/v1alpha1"
machinepoolletv1alpha1 "github.com/ironcore-dev/ironcore/poollet/machinepoollet/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("CreateMachine", func() {
ns, srv := SetupTest()
ctx := SetupContext()
ns, srv := SetupTest(ctx)
machineClass := SetupMachineClass()

It("should correctly create a machine", func(ctx SpecContext) {
Expand Down
4 changes: 3 additions & 1 deletion broker/machinebroker/server/machine_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ import (
iri "github.com/ironcore-dev/ironcore/iri/apis/machine/v1alpha1"
irimeta "github.com/ironcore-dev/ironcore/iri/apis/meta/v1alpha1"
machinepoolletv1alpha1 "github.com/ironcore-dev/ironcore/poollet/machinepoollet/api/v1alpha1"
. "github.com/ironcore-dev/ironcore/utils/testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gstruct"
"sigs.k8s.io/controller-runtime/pkg/client"
)

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

It("should correctly delete a machine", func(ctx SpecContext) {
Expand Down
4 changes: 3 additions & 1 deletion broker/machinebroker/server/machine_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/machine/v1alpha1"
irimeta "github.com/ironcore-dev/ironcore/iri/apis/meta/v1alpha1"
machinepoolletv1alpha1 "github.com/ironcore-dev/ironcore/poollet/machinepoollet/api/v1alpha1"
. "github.com/ironcore-dev/ironcore/utils/testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("ListMachines", func() {
_, srv := SetupTest()
ctx := SetupContext()
_, srv := SetupTest(ctx)
machineClass := SetupMachineClass()

It("should correctly list machines", func(ctx SpecContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ import (
computev1alpha1 "github.com/ironcore-dev/ironcore/api/compute/v1alpha1"
networkingv1alpha1 "github.com/ironcore-dev/ironcore/api/networking/v1alpha1"
iri "github.com/ironcore-dev/ironcore/iri/apis/machine/v1alpha1"
. "github.com/ironcore-dev/ironcore/utils/testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gstruct"
"sigs.k8s.io/controller-runtime/pkg/client"
)

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

It("should correctly attach a network interface", func(ctx SpecContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import (
computev1alpha1 "github.com/ironcore-dev/ironcore/api/compute/v1alpha1"
networkingv1alpha1 "github.com/ironcore-dev/ironcore/api/networking/v1alpha1"
iri "github.com/ironcore-dev/ironcore/iri/apis/machine/v1alpha1"
. "github.com/ironcore-dev/ironcore/utils/testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gstruct"
"sigs.k8s.io/controller-runtime/pkg/client"
)

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

It("should correctly detach a network interface", func(ctx SpecContext) {
Expand Down
4 changes: 3 additions & 1 deletion broker/machinebroker/server/machine_volume_attach_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
computev1alpha1 "github.com/ironcore-dev/ironcore/api/compute/v1alpha1"
storagev1alpha1 "github.com/ironcore-dev/ironcore/api/storage/v1alpha1"
iri "github.com/ironcore-dev/ironcore/iri/apis/machine/v1alpha1"
. "github.com/ironcore-dev/ironcore/utils/testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gstruct"
Expand All @@ -17,7 +18,8 @@ import (
)

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

It("should correctly attach a volume", func(ctx SpecContext) {
Expand Down
4 changes: 3 additions & 1 deletion broker/machinebroker/server/machine_volume_detach_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
computev1alpha1 "github.com/ironcore-dev/ironcore/api/compute/v1alpha1"
storagev1alpha1 "github.com/ironcore-dev/ironcore/api/storage/v1alpha1"
iri "github.com/ironcore-dev/ironcore/iri/apis/machine/v1alpha1"
. "github.com/ironcore-dev/ironcore/utils/testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gstruct"
Expand All @@ -15,7 +16,8 @@ import (
)

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

It("should correctly detach a volume", func(ctx SpecContext) {
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 @@ -43,7 +43,7 @@ var (
)

const (
eventuallyTimeout = 3 * time.Second
eventuallyTimeout = 10 * time.Second
pollingInterval = 50 * time.Millisecond
consistentlyDuration = 1 * time.Second
apiServiceTimeout = 5 * time.Minute
Expand Down Expand Up @@ -109,13 +109,13 @@ 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{}
)

BeforeEach(func(ctx SpecContext) {
BeforeEach(func() {
*ns = corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-ns-",
Expand Down
Loading

0 comments on commit 13d7d0c

Please sign in to comment.