From e9c7e162661f75dd954f1b0dfa53d8cb9f84ccd8 Mon Sep 17 00:00:00 2001 From: Anurag Mittal Date: Tue, 10 Dec 2024 14:03:29 +0100 Subject: [PATCH] COSI-62: add-tests-for-provisioner-server-init --- pkg/driver/provisioner_server_impl.go | 20 ++++- pkg/driver/provisioner_server_impl_test.go | 93 ++++++++++++++++++++++ 2 files changed, 110 insertions(+), 3 deletions(-) diff --git a/pkg/driver/provisioner_server_impl.go b/pkg/driver/provisioner_server_impl.go index 63d37110..c929323f 100644 --- a/pkg/driver/provisioner_server_impl.go +++ b/pkg/driver/provisioner_server_impl.go @@ -45,26 +45,40 @@ type ProvisionerServer struct { var _ cosiapi.ProvisionerServer = &ProvisionerServer{} // helper methods initialized as variables for testing +var ( + InClusterConfig = rest.InClusterConfig + NewKubernetesClient = func(c *rest.Config) (kubernetes.Interface, error) { + return kubernetes.NewForConfig(c) + } + NewBucketClient = func(c *rest.Config) (bucketclientset.Interface, error) { + return bucketclientset.NewForConfig(c) + } +) var InitializeClient = initializeObjectStorageClient var FetchSecretInformation = fetchObjectStorageProviderSecretInfo var FetchParameters = fetchS3Parameters func InitProvisionerServer(provisioner string) (cosiapi.ProvisionerServer, error) { klog.V(3).InfoS("Initializing ProvisionerServer", "provisioner", provisioner) + if provisioner == "" { + err := errors.New("provisioner name cannot be empty") + klog.ErrorS(err, "Failed to initialize ProvisionerServer: empty provisioner name") + return nil, err + } - kubeConfig, err := rest.InClusterConfig() + kubeConfig, err := InClusterConfig() if err != nil { klog.ErrorS(err, "Failed to get in-cluster config") return nil, err } - clientset, err := kubernetes.NewForConfig(kubeConfig) + clientset, err := NewKubernetesClient(kubeConfig) if err != nil { klog.ErrorS(err, "Failed to create Kubernetes clientset") return nil, err } - bucketClientset, err := bucketclientset.NewForConfig(kubeConfig) + bucketClientset, err := NewBucketClient(kubeConfig) if err != nil { klog.ErrorS(err, "Failed to create BucketClientset") return nil, err diff --git a/pkg/driver/provisioner_server_impl_test.go b/pkg/driver/provisioner_server_impl_test.go index dfb46d39..cb7dd715 100644 --- a/pkg/driver/provisioner_server_impl_test.go +++ b/pkg/driver/provisioner_server_impl_test.go @@ -22,9 +22,102 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes/fake" + "k8s.io/client-go/rest" + bucketclientset "sigs.k8s.io/container-object-storage-interface-api/client/clientset/versioned" + bucketclientfake "sigs.k8s.io/container-object-storage-interface-api/client/clientset/versioned/fake" cosiapi "sigs.k8s.io/container-object-storage-interface-spec" ) +var _ = Describe("ProvisionerServer InitProvisionerServer", func() { + var ( + provisioner string + ) + + BeforeEach(func() { + // Mock InClusterConfig + driver.InClusterConfig = func() (*rest.Config, error) { + return &rest.Config{}, nil + } + + // Mock Kubernetes NewForConfig with an interface-compatible fake client + driver.NewKubernetesClient = func(config *rest.Config) (kubernetes.Interface, error) { + return fake.NewSimpleClientset(), nil + } + + // Mock BucketClientset NewForConfig with an interface-compatible fake client + driver.NewBucketClient = func(config *rest.Config) (bucketclientset.Interface, error) { + return bucketclientfake.NewSimpleClientset(), nil + } + + provisioner = "test-provisioner" + }) + + AfterEach(func() { + // Restore original functions + driver.InClusterConfig = rest.InClusterConfig + driver.NewKubernetesClient = func(c *rest.Config) (kubernetes.Interface, error) { + return kubernetes.NewForConfig(c) + } + driver.NewBucketClient = func(c *rest.Config) (bucketclientset.Interface, error) { + return bucketclientset.NewForConfig(c) + } + }) + + It("should initialize a ProvisionerServer successfully", func() { + server, err := driver.InitProvisionerServer(provisioner) + Expect(err).To(BeNil()) + Expect(server).NotTo(BeNil()) + + ps, ok := server.(*driver.ProvisionerServer) + Expect(ok).To(BeTrue()) + Expect(ps.Provisioner).To(Equal(provisioner)) + Expect(ps.Clientset).NotTo(BeNil()) + Expect(ps.KubeConfig).NotTo(BeNil()) + Expect(ps.BucketClientset).NotTo(BeNil()) + }) + + It("should return error if InClusterConfig fails", func() { + driver.InClusterConfig = func() (*rest.Config, error) { + return nil, errors.New("mock error: failed to get in-cluster config") + } + + server, err := driver.InitProvisionerServer(provisioner) + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(ContainSubstring("mock error: failed to get in-cluster config")) + Expect(server).To(BeNil()) + }) + + It("should return error if Kubernetes client creation fails", func() { + driver.NewKubernetesClient = func(config *rest.Config) (kubernetes.Interface, error) { + return nil, errors.New("mock error: failed to create Kubernetes client") + } + + server, err := driver.InitProvisionerServer(provisioner) + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(ContainSubstring("mock error: failed to create Kubernetes client")) + Expect(server).To(BeNil()) + }) + + It("should return error if BucketClientset creation fails", func() { + driver.NewBucketClient = func(config *rest.Config) (bucketclientset.Interface, error) { + return nil, errors.New("mock error: failed to create BucketClientset") + } + + server, err := driver.InitProvisionerServer(provisioner) + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(ContainSubstring("mock error: failed to create BucketClientset")) + Expect(server).To(BeNil()) + }) + + It("should return error if provisioner name is empty", func() { + provisioner = "" + server, err := driver.InitProvisionerServer(provisioner) + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(ContainSubstring("provisioner name cannot be empty")) + Expect(server).To(BeNil()) + }) +}) + var _ = Describe("ProvisionerServer DriverCreateBucket", Ordered, func() { var ( mockS3 *mock.MockS3Client