Skip to content

Commit

Permalink
define parameters for initializeClients in unit testing
Browse files Browse the repository at this point in the history
Parameters are not defined for unit tests, variable is added but no
explict check is defined.

Signed-off-by: Jiffin Tony Thottan <[email protected]>
  • Loading branch information
thotz committed Jul 20, 2023
1 parent 1473e55 commit d2a4e0d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 34 deletions.
35 changes: 22 additions & 13 deletions pkg/driver/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,10 @@ func fetchUserCredentials(user rgwadmin.User, endpoint string, region string) ma

func InitializeClients(ctx context.Context, clientset *kubernetes.Clientset, parameters map[string]string) (*s3client.S3Agent, *rgwadmin.API, error) {
klog.V(5).Infof("Initializing clients %v", parameters)
objectStoreUserSecretName := parameters["objectStoreUserSecretName"]
namespace := os.Getenv("POD_NAMESPACE")
if parameters["objectStoreUserSecretNamespace"] != "" {
namespace = parameters["objectStoreUserSecretNamespace"]
}
if objectStoreUserSecretName == "" || namespace == "" {
return nil, nil, status.Error(codes.InvalidArgument, "objectStoreUserSecretName and Namespace is required")

objectStoreUserSecretName, namespace, err := fetchSecretNameAndNamespace(parameters)
if err != nil {
return nil, nil, err
}

objectStoreUserSecret, err := clientset.CoreV1().Secrets(namespace).Get(ctx, objectStoreUserSecretName, metav1.GetOptions{})
Expand Down Expand Up @@ -284,15 +281,27 @@ func InitializeClients(ctx context.Context, clientset *kubernetes.Clientset, par
return s3Client, rgwAdminClient, nil
}

func fetchParameters(parameters map[string][]byte) (string, string, string, string, error) {

accessKey := string(parameters["AccessKey"])
secretKey := string(parameters["SecretKey"])
endPoint := string(parameters["Endpoint"])
func fetchParameters(secretData map[string][]byte) (string, string, string, string, error) {
accessKey := string(secretData["AccessKey"])
secretKey := string(secretData["SecretKey"])
endPoint := string(secretData["Endpoint"])
if endPoint == "" || accessKey == "" || secretKey == "" {
return "", "", "", "", status.Error(codes.InvalidArgument, "endpoint, accessKeyID and secretKey are required")
}
tlsCert := string(parameters["SSLCertSecretName"])
tlsCert := string(secretData["SSLCertSecretName"])

return accessKey, secretKey, endPoint, tlsCert, nil
}

func fetchSecretNameAndNamespace(parameters map[string]string) (string, string, error) {
secretName := parameters["objectStoreUserSecretName"]
namespace := os.Getenv("POD_NAMESPACE")
if parameters["objectStoreUserSecretNamespace"] != "" {
namespace = parameters["objectStoreUserSecretNamespace"]
}
if secretName == "" || namespace == "" {
return "", "", status.Error(codes.InvalidArgument, "objectStoreUserSecretName and Namespace is required")
}

return secretName, namespace, nil
}
59 changes: 38 additions & 21 deletions pkg/driver/provisioner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ const (
"keys": [
{
"user": "test-user",
"access_key": "EOE7FYCNOBZJ5VFV909G",
"secret_key": "qmIqpWm8HxCzmynCrD6U6vKWi4hnDBndOnmxXNsV"
"access_key": "AccessKey",
"secret_key": "SecretKey"
}
],
"swift_keys": [],
Expand Down Expand Up @@ -80,6 +80,12 @@ const (
}`
)

func createParameters() map[string]string {
return map[string]string{
"objectStoreUserSecretName": "test-user-secret",
"objectStoreUserSecretNamespace": "test-namespace",
}
}
func Test_provisionerServer_DriverCreateBucket(t *testing.T) {
type fields struct {
provisioner string
Expand All @@ -91,6 +97,10 @@ func Test_provisionerServer_DriverCreateBucket(t *testing.T) {
}

initializeClients = func(ctx context.Context, clientset *kubernetes.Clientset, parameters map[string]string) (*s3cli.S3Agent, *rgwadmin.API, error) {
_, _, err := fetchSecretNameAndNamespace(parameters)
if err != nil {
t.Fatalf("failed to fetch secret name and namespace: %v", err)
}
s3Client := &s3cli.S3Agent{
Client: mockS3Client{},
}
Expand All @@ -104,11 +114,11 @@ func Test_provisionerServer_DriverCreateBucket(t *testing.T) {
want *cosispec.DriverCreateBucketResponse
wantErr bool
}{
{"Empty Bucket Name", fields{"CreateBucket Empty Bucket Name"}, args{context.Background(), &cosispec.DriverCreateBucketRequest{Name: ""}}, nil, true},
{"Create Bucket success", fields{"CreateBucket Success"}, args{context.Background(), &cosispec.DriverCreateBucketRequest{Name: "test-bucket"}}, &cosispec.DriverCreateBucketResponse{BucketId: "test-bucket"}, false},
{"Create Bucket failure", fields{"CreateBucket Failure"}, args{context.Background(), &cosispec.DriverCreateBucketRequest{Name: "failed-bucket"}}, nil, true},
{"Bucket already Exists", fields{"CreateBucket Already Exists"}, args{context.Background(), &cosispec.DriverCreateBucketRequest{Name: "test-bucket-already-exists"}}, nil, true},
{"Bucket owned same user", fields{"CreateBucket Owned by same user"}, args{context.Background(), &cosispec.DriverCreateBucketRequest{Name: "test-bucket-owned-by-same-user"}}, nil, true},
{"Empty Bucket Name", fields{"CreateBucket Empty Bucket Name"}, args{context.Background(), &cosispec.DriverCreateBucketRequest{Name: "", Parameters: createParameters()}}, nil, true},
{"Create Bucket success", fields{"CreateBucket Success"}, args{context.Background(), &cosispec.DriverCreateBucketRequest{Name: "test-bucket", Parameters: createParameters()}}, &cosispec.DriverCreateBucketResponse{BucketId: "test-bucket"}, false},
{"Create Bucket failure", fields{"CreateBucket Failure"}, args{context.Background(), &cosispec.DriverCreateBucketRequest{Name: "failed-bucket", Parameters: createParameters()}}, nil, true},
{"Bucket already Exists", fields{"CreateBucket Already Exists"}, args{context.Background(), &cosispec.DriverCreateBucketRequest{Name: "test-bucket-already-exists", Parameters: createParameters()}}, nil, true},
{"Bucket owned same user", fields{"CreateBucket Owned by same user"}, args{context.Background(), &cosispec.DriverCreateBucketRequest{Name: "test-bucket-owned-by-same-user", Parameters: createParameters()}}, nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -136,6 +146,11 @@ func Test_provisionerServer_DriverGrantBucketAccess(t *testing.T) {
req *cosispec.DriverGrantBucketAccessRequest
}
initializeClients = func(ctx context.Context, clientset *kubernetes.Clientset, parameters map[string]string) (*s3cli.S3Agent, *rgwadmin.API, error) {
_, _, err := fetchSecretNameAndNamespace(parameters)
if err != nil {
t.Fatalf("failed to fetch secret name and namespace: %v", err)
}

s3Client := &s3cli.S3Agent{
Client: mockS3Client{},
}
Expand Down Expand Up @@ -170,12 +185,12 @@ func Test_provisionerServer_DriverGrantBucketAccess(t *testing.T) {
want *cosispec.DriverGrantBucketAccessResponse
wantErr bool
}{
{"Empty Bucket Name", fields{"GrantBucketAccess Empty Bucket Name"}, args{context.Background(), &cosispec.DriverGrantBucketAccessRequest{BucketId: "", Name: "test-user"}}, nil, true},
{"Empty User Name", fields{"GrantBucketAccess Empty User Name"}, args{context.Background(), &cosispec.DriverGrantBucketAccessRequest{BucketId: "test-bucket", Name: ""}}, nil, true},
{"Grant Bucket Access success", fields{"GrantBucketAccess Success"}, args{context.Background(), &cosispec.DriverGrantBucketAccessRequest{BucketId: "test-bucket", Name: "test-user"}}, &cosispec.DriverGrantBucketAccessResponse{AccountId: "test-user", Credentials: fetchUserCredentials(u, "rgw-my-store:8000", "")}, false},
{"Grant Bucket Access failure", fields{"GrantBucketAccess Failure"}, args{context.Background(), &cosispec.DriverGrantBucketAccessRequest{BucketId: "failed-bucket", Name: "test-user"}}, nil, true},
{"Bucket does not exist", fields{"GrantBucketAccess Does not exist"}, args{context.Background(), &cosispec.DriverGrantBucketAccessRequest{BucketId: "test-bucket-does-not-exist", Name: "test-user"}}, nil, true},
{"User does not exist", fields{"GrantBucketAccess User Does not exist"}, args{context.Background(), &cosispec.DriverGrantBucketAccessRequest{BucketId: "test-bucket", Name: "test-user-does-not-exist"}}, nil, true},
{"Empty Bucket Name", fields{"GrantBucketAccess Empty Bucket Name"}, args{context.Background(), &cosispec.DriverGrantBucketAccessRequest{BucketId: "", Name: "test-user", Parameters: createParameters()}}, nil, true},
{"Empty User Name", fields{"GrantBucketAccess Empty User Name"}, args{context.Background(), &cosispec.DriverGrantBucketAccessRequest{BucketId: "test-bucket", Name: "", Parameters: createParameters()}}, nil, true},
{"Grant Bucket Access success", fields{"GrantBucketAccess Success"}, args{context.Background(), &cosispec.DriverGrantBucketAccessRequest{BucketId: "test-bucket", Name: "test-user", Parameters: createParameters()}}, &cosispec.DriverGrantBucketAccessResponse{AccountId: "test-user", Credentials: fetchUserCredentials(u, "rgw-my-store:8000", "")}, false},
{"Grant Bucket Access failure", fields{"GrantBucketAccess Failure"}, args{context.Background(), &cosispec.DriverGrantBucketAccessRequest{BucketId: "failed-bucket", Name: "test-user", Parameters: createParameters()}}, nil, true},
{"Bucket does not exist", fields{"GrantBucketAccess Does not exist"}, args{context.Background(), &cosispec.DriverGrantBucketAccessRequest{BucketId: "test-bucket-does-not-exist", Name: "test-user", Parameters: createParameters()}}, nil, true},
{"User does not exist", fields{"GrantBucketAccess User Does not exist"}, args{context.Background(), &cosispec.DriverGrantBucketAccessRequest{BucketId: "test-bucket", Name: "test-user-does-not-exist", Parameters: createParameters()}}, nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -205,6 +220,10 @@ func Test_provisionerServer_DriverDeleteBucket(t *testing.T) {
}

initializeClients = func(ctx context.Context, clientset *kubernetes.Clientset, parameters map[string]string) (*s3cli.S3Agent, *rgwadmin.API, error) {
_, _, err := fetchSecretNameAndNamespace(parameters)
if err != nil {
t.Fatalf("failed to fetch secret name and namespace: %v", err)
}
s3Client := &s3cli.S3Agent{
Client: mockS3Client{},
}
Expand Down Expand Up @@ -233,10 +252,7 @@ func Test_provisionerServer_DriverDeleteBucket(t *testing.T) {
},
Spec: v1alpha1.BucketSpec{
DriverName: tt.fields.provisioner,
Parameters: map[string]string{
"ObjectStoreUserSecretName": "test-user-secret",
"ObjectStoreUserSecretNamespace": "test-namespace",
},
Parameters: createParameters(),
},
}
bucketClient := fakebucketclientset.NewSimpleClientset(&b)
Expand Down Expand Up @@ -266,6 +282,10 @@ func Test_provisonerServer_DriverRevokeBucketAccess(t *testing.T) {
}

initializeClients = func(ctx context.Context, clientset *kubernetes.Clientset, parameters map[string]string) (*s3cli.S3Agent, *rgwadmin.API, error) {
_, _, err := fetchSecretNameAndNamespace(parameters)
if err != nil {
t.Fatalf("failed to fetch secret name and namespace: %v", err)
}
s3Client := &s3cli.S3Agent{
Client: mockS3Client{},
}
Expand Down Expand Up @@ -310,10 +330,7 @@ func Test_provisonerServer_DriverRevokeBucketAccess(t *testing.T) {
},
Spec: v1alpha1.BucketSpec{
DriverName: tt.fields.provisioner,
Parameters: map[string]string{
"ObjectStoreUserSecretName": "test-user-secret",
"ObjectStoreUserSecretNamespace": "test-namespace",
},
Parameters: createParameters(),
},
}
bucketClient := fakebucketclientset.NewSimpleClientset(&b)
Expand Down

0 comments on commit d2a4e0d

Please sign in to comment.