Skip to content

Commit

Permalink
COSI-62: add-tests-for-provisioner-server-init
Browse files Browse the repository at this point in the history
  • Loading branch information
anurag4DSB committed Dec 10, 2024
1 parent 0a8e922 commit e9c7e16
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 3 deletions.
20 changes: 17 additions & 3 deletions pkg/driver/provisioner_server_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Check warning on line 55 in pkg/driver/provisioner_server_impl.go

View check run for this annotation

Codecov / codecov/patch

pkg/driver/provisioner_server_impl.go#L50-L55

Added lines #L50 - L55 were not covered by tests
)
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
Expand Down
93 changes: 93 additions & 0 deletions pkg/driver/provisioner_server_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e9c7e16

Please sign in to comment.