-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add testing tool for VolumeGroup
This adds testing tool for VolumeGroup operations. Signed-off-by: ShravaniVangur <[email protected]>
- Loading branch information
1 parent
24832d2
commit 2c7bdb6
Showing
3 changed files
with
295 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,275 @@ | ||
/* | ||
Copyright 2024 The Kubernetes-CSI-Addons Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/csi-addons/kubernetes-csi-addons/internal/proto" | ||
"github.com/csi-addons/kubernetes-csi-addons/internal/sidecar/service" | ||
) | ||
|
||
// CreateVolumeGroup executes the CreateVolumeGroup operation. | ||
type CreateVolumeGroup struct { | ||
grpcClient | ||
volumeGroupName string | ||
parameters map[string]string | ||
secretName string | ||
secretNamespace string | ||
volumeIDs []string | ||
} | ||
|
||
var _ = registerOperation("CreateVolumeGroup", &CreateVolumeGroup{}) | ||
|
||
func (cvg *CreateVolumeGroup) Init(c *command) error { | ||
cvg.volumeGroupName = c.volumeGroupName | ||
if cvg.volumeGroupName == "" { | ||
return fmt.Errorf("volumegroup name is not set") | ||
} | ||
|
||
params := make(map[string]string) | ||
if c.parameters != "" { | ||
pairs := strings.Split(c.parameters, ",") | ||
for _, pair := range pairs { | ||
kv := strings.SplitN(pair, "=", 2) | ||
if len(kv) == 2 { | ||
key := kv[0] | ||
value := kv[1] | ||
params[key] = value | ||
} else { | ||
fmt.Printf("Invalid parameter : %s\n", pair) | ||
} | ||
} | ||
|
||
cvg.parameters = params | ||
} | ||
|
||
secrets := strings.Split(c.secret, "/") | ||
if len(secrets) != 2 { | ||
return errors.New("secret should be specified in the format `namespace/name`") | ||
} | ||
cvg.secretNamespace = secrets[0] | ||
if cvg.secretNamespace == "" { | ||
return errors.New("secret namespace is not set") | ||
} | ||
cvg.secretName = secrets[1] | ||
if cvg.secretName == "" { | ||
return errors.New("secret name is not set") | ||
} | ||
|
||
if c.volumeIDs != "" { | ||
if strings.Contains(c.volumeIDs, ",") { | ||
cvg.volumeIDs = (strings.Split(c.volumeIDs, ",")) | ||
} else { | ||
cvg.volumeIDs = []string{c.volumeIDs} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (cvg *CreateVolumeGroup) Execute() error { | ||
vgs := service.NewVolumeGroupServer(cvg.Client, getKubernetesClient()) | ||
|
||
req := &proto.CreateVolumeGroupRequest{ | ||
Name: cvg.volumeGroupName, | ||
Parameters: cvg.parameters, | ||
SecretName: cvg.secretName, | ||
SecretNamespace: cvg.secretNamespace, | ||
VolumeIds: cvg.volumeIDs, | ||
} | ||
res, err := vgs.CreateVolumeGroup(context.TODO(), req) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("Volume Group created: %+v", res) | ||
return nil | ||
|
||
} | ||
|
||
// ModifyVolumeGroupMembership executes the ModifyVolumeGroupMembership operation. | ||
type ModifyVolumeGroupMembership struct { | ||
grpcClient | ||
volumeGroupID string | ||
volumeIDs []string | ||
secretName string | ||
secretNamespace string | ||
parameters map[string]string | ||
} | ||
|
||
var _ = registerOperation("ModifyVolumeGroupMembership", &ModifyVolumeGroupMembership{}) | ||
|
||
func (mvgm *ModifyVolumeGroupMembership) Init(c *command) error { | ||
mvgm.volumeGroupID = c.volumeGroupID | ||
if mvgm.volumeGroupID == "" { | ||
return fmt.Errorf("volumeGroupID is not set") | ||
} | ||
if c.volumeIDs != "" { | ||
if strings.Contains(c.volumeIDs, ",") { | ||
mvgm.volumeIDs = (strings.Split(c.volumeIDs, ",")) | ||
} else { | ||
mvgm.volumeIDs = []string{c.volumeIDs} | ||
} | ||
|
||
} | ||
secrets := strings.Split(c.secret, "/") | ||
if len(secrets) != 2 { | ||
return errors.New("secret should be specified in the format `namespace/name`") | ||
} | ||
mvgm.secretNamespace = secrets[0] | ||
if mvgm.secretNamespace == "" { | ||
return errors.New("secret namespace is not set") | ||
} | ||
mvgm.secretName = secrets[1] | ||
if mvgm.secretName == "" { | ||
return errors.New("secret name is not set") | ||
} | ||
params := make(map[string]string) | ||
if c.parameters != "" { | ||
pairs := strings.Split(c.parameters, ",") | ||
for _, pair := range pairs { | ||
kv := strings.SplitN(pair, "=", 2) | ||
if len(kv) == 2 { | ||
key := kv[0] | ||
value := kv[1] | ||
params[key] = value | ||
} else { | ||
fmt.Printf("Invalid parameter : %s\n", pair) | ||
} | ||
} | ||
|
||
mvgm.parameters = params | ||
} | ||
return nil | ||
} | ||
|
||
func (mvgm *ModifyVolumeGroupMembership) Execute() error { | ||
vgs := service.NewVolumeGroupServer(mvgm.Client, getKubernetesClient()) | ||
|
||
req := &proto.ModifyVolumeGroupMembershipRequest{ | ||
VolumeGroupId: mvgm.volumeGroupID, | ||
VolumeIds: mvgm.volumeIDs, | ||
SecretName: mvgm.secretName, | ||
SecretNamespace: mvgm.secretNamespace, | ||
Parameters: mvgm.parameters, | ||
} | ||
|
||
res, err := vgs.ModifyVolumeGroupMembership(context.TODO(), req) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("Volume Group Membership modified for- %+v", res) | ||
return nil | ||
} | ||
|
||
// DeleteVolumeGroup executes the DeleteVolumeGroup operation. | ||
type DeleteVolumeGroup struct { | ||
grpcClient | ||
volumeGroupID string | ||
secretName string | ||
secretNamespace string | ||
} | ||
|
||
var _ = registerOperation("DeleteVolumeGroup", &DeleteVolumeGroup{}) | ||
|
||
func (dvg *DeleteVolumeGroup) Init(c *command) error { | ||
dvg.volumeGroupID = c.volumeGroupID | ||
if dvg.volumeGroupID == "" { | ||
return fmt.Errorf("volumeGroupID is not set") | ||
} | ||
secrets := strings.Split(c.secret, "/") | ||
if len(secrets) != 2 { | ||
return errors.New("secret should be specified in the format `namespace/name`") | ||
} | ||
dvg.secretNamespace = secrets[0] | ||
if dvg.secretNamespace == "" { | ||
return errors.New("secret namespace is not set") | ||
} | ||
dvg.secretName = secrets[1] | ||
if dvg.secretName == "" { | ||
return errors.New("secret name is not set") | ||
} | ||
return nil | ||
|
||
} | ||
|
||
func (dvg *DeleteVolumeGroup) Execute() error { | ||
vgs := service.NewVolumeGroupServer(dvg.Client, getKubernetesClient()) | ||
|
||
req := &proto.DeleteVolumeGroupRequest{ | ||
VolumeGroupId: dvg.volumeGroupID, | ||
SecretName: dvg.secretName, | ||
SecretNamespace: dvg.secretNamespace, | ||
} | ||
|
||
_, err := vgs.DeleteVolumeGroup(context.TODO(), req) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("Volume Group Deleted.") | ||
return nil | ||
|
||
} | ||
|
||
// ControllerGetVolumeGroup executes the ControllerGetVolumeGroup operation. | ||
type ControllerGetVolumeGroup struct { | ||
grpcClient | ||
volumeGroupID string | ||
secretName string | ||
secretNamespace string | ||
} | ||
|
||
var _ = registerOperation("ControllerGetVolumeGroup", &ControllerGetVolumeGroup{}) | ||
|
||
func (gvg *ControllerGetVolumeGroup) Init(c *command) error { | ||
gvg.volumeGroupID = c.volumeGroupID | ||
if gvg.volumeGroupID == "" { | ||
return fmt.Errorf("volumeGroupId is not set") | ||
} | ||
secrets := strings.Split(c.secret, "/") | ||
if len(secrets) != 2 { | ||
return errors.New("secret should be specified in the format `namespace/name`") | ||
} | ||
gvg.secretNamespace = secrets[0] | ||
if gvg.secretNamespace == "" { | ||
return errors.New("secret namespace is not set") | ||
} | ||
gvg.secretName = secrets[1] | ||
if gvg.secretName == "" { | ||
return errors.New("secret name is not set") | ||
} | ||
return nil | ||
} | ||
|
||
func (gvg *ControllerGetVolumeGroup) Execute() error { | ||
vgs := service.NewVolumeGroupServer(gvg.Client, getKubernetesClient()) | ||
|
||
req := &proto.ControllerGetVolumeGroupRequest{ | ||
VolumeGroupId: gvg.volumeGroupID, | ||
SecretName: gvg.secretName, | ||
SecretNamespace: gvg.secretNamespace, | ||
} | ||
|
||
res, err := vgs.ControllerGetVolumeGroup(context.TODO(), req) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("Controller Volume Group Info: %+v", res) | ||
return nil | ||
} |