Skip to content

Commit

Permalink
Initial tests for GroupController Server and VolumeGroupSnapshot Service
Browse files Browse the repository at this point in the history
These initial tests are very minimal, and only validate the availability
of the VolumeGroupSnapshot Service of the GroupController Service. No
VolumeGroupSnapshots are made (yet).
  • Loading branch information
nixpanic committed Jun 16, 2023
1 parent e49f92b commit 2d4cc5d
Show file tree
Hide file tree
Showing 2 changed files with 345 additions and 0 deletions.
341 changes: 341 additions & 0 deletions pkg/sanity/groupcontroller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,341 @@
/*
Copyright 2023 The Kubernetes 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 sanity

import (
"context"
"fmt"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/container-storage-interface/spec/lib/go/csi"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func isGroupControllerCapabilitySupported(
c csi.GroupControllerClient,
capType csi.GroupControllerServiceCapability_RPC_Type,
) bool {

caps, err := c.GroupControllerGetCapabilities(
context.Background(),
&csi.GroupControllerGetCapabilitiesRequest{})
Expect(err).NotTo(HaveOccurred())
Expect(caps).NotTo(BeNil())
Expect(caps.GetCapabilities()).NotTo(BeNil())

for _, cap := range caps.GetCapabilities() {
Expect(cap.GetRpc()).NotTo(BeNil())
if cap.GetRpc().GetType() == capType {
return true
}
}
return false
}

var _ = DescribeSanity("GroupController Service [GroupController Server]", func(sc *TestContext) {
var r *Resources

BeforeEach(func() {
var providesGroupControllerService bool

i := csi.NewIdentityClient(sc.Conn)
req := &csi.GetPluginCapabilitiesRequest{}
res, err := i.GetPluginCapabilities(context.Background(), req)
Expect(err).NotTo(HaveOccurred())
Expect(res).NotTo(BeNil())

for _, cap := range res.GetCapabilities() {
switch cap.GetType().(type) {
case *csi.PluginCapability_Service_:
switch cap.GetService().GetType() {
case csi.PluginCapability_Service_GROUP_CONTROLLER_SERVICE:
providesGroupControllerService = true
}
}
}

if !providesGroupControllerService {
Skip("GroupControllerService not supported")
}

r = &Resources{
Context: sc,
ControllerClient: csi.NewControllerClient(sc.ControllerConn),
GroupControllerClient: csi.NewGroupControllerClient(sc.ControllerConn),
NodeClient: csi.NewNodeClient(sc.Conn),
}
})

AfterEach(func() {
r.Cleanup()
})

Describe("GroupControllerGetCapabilities", func() {
It("should return appropriate capabilities", func() {
caps, err := r.GroupControllerGetCapabilities(
context.Background(),
&csi.GroupControllerGetCapabilitiesRequest{})

By("checking successful response")
Expect(err).NotTo(HaveOccurred())
Expect(caps).NotTo(BeNil())
Expect(caps.GetCapabilities()).NotTo(BeNil())

for _, cap := range caps.GetCapabilities() {
Expect(cap.GetRpc()).NotTo(BeNil())

switch cap.GetRpc().GetType() {
case csi.GroupControllerServiceCapability_RPC_CREATE_DELETE_GET_VOLUME_GROUP_SNAPSHOT:
default:
Fail(fmt.Sprintf("Unknown capability: %v\n", cap.GetRpc().GetType()))
}
}
})
})
})

var _ = DescribeSanity("GroupController Service [GroupController VolumeGroupSnapshots]", func(sc *TestContext) {
var r *Resources

BeforeEach(func() {
var providesGroupControllerService bool

i := csi.NewIdentityClient(sc.Conn)
req := &csi.GetPluginCapabilitiesRequest{}
res, err := i.GetPluginCapabilities(context.Background(), req)
Expect(err).NotTo(HaveOccurred())
Expect(res).NotTo(BeNil())

for _, cap := range res.GetCapabilities() {
switch cap.GetType().(type) {
case *csi.PluginCapability_Service_:
switch cap.GetService().GetType() {
case csi.PluginCapability_Service_GROUP_CONTROLLER_SERVICE:
providesGroupControllerService = true
}
}
}

if !providesGroupControllerService {
Skip("GroupControllerService not supported")
}

r = &Resources{
Context: sc,
ControllerClient: csi.NewControllerClient(sc.ControllerConn),
GroupControllerClient: csi.NewGroupControllerClient(sc.ControllerConn),
NodeClient: csi.NewNodeClient(sc.Conn),
}

if !isGroupControllerCapabilitySupported(r, csi.GroupControllerServiceCapability_RPC_CREATE_DELETE_GET_VOLUME_GROUP_SNAPSHOT) {
Skip("VolumeGroupSnapshots not supported")
}
})

AfterEach(func() {
r.Cleanup()
})

Describe("CreateVolumeGroupSnapshot", func() {
It("should fail when no name is provided", func() {
_, err := r.CreateVolumeGroupSnapshot(
context.Background(),
&csi.CreateVolumeGroupSnapshotRequest{
Secrets: sc.Secrets.CreateSnapshotSecret,
},
)
Expect(err).To(HaveOccurred())

serverError, ok := status.FromError(err)
Expect(ok).To(BeTrue())
Expect(serverError.Code()).To(Equal(codes.InvalidArgument), "unexpected error: %s", serverError.Message())
})

/* disabled tests, need modifications for VolumeGroupSnapshot
*
It("should fail when requesting to create a volume with already existing name and different capacity", func() {
It("should not fail when creating volume with maximum-length name", func() {
nameBytes := make([]byte, MaxNameLength)
for i := 0; i < MaxNameLength; i++ {
nameBytes[i] = 'a'
}
name := string(nameBytes)
By("creating a volume")
size := TestVolumeSize(sc)
vol := r.MustCreateVolume(
context.Background(),
&csi.CreateVolumeRequest{
Name: name,
VolumeCapabilities: []*csi.VolumeCapability{
TestVolumeCapabilityWithAccessType(sc, csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER),
},
CapacityRange: &csi.CapacityRange{
RequiredBytes: size,
},
Secrets: sc.Secrets.CreateVolumeSecret,
Parameters: sc.Config.TestVolumeParameters,
},
)
Expect(vol.GetVolume().GetCapacityBytes()).To(Or(BeNumerically(">=", size), BeZero()))
})
It("should create volume from an existing source snapshot", func() {
if !isControllerCapabilitySupported(r, csi.ControllerServiceCapability_RPC_CREATE_DELETE_SNAPSHOT) {
Skip("Snapshot not supported")
}
By("creating a snapshot")
vol1Req := MakeCreateVolumeReq(sc, UniqueString("sanity-controller-source-vol"))
snap, _ := r.MustCreateSnapshotFromVolumeRequest(context.Background(), vol1Req, UniqueString("sanity-controller-snap-from-vol"))
By("creating a volume from source snapshot")
vol2Name := UniqueString("sanity-controller-vol-from-snap")
vol2Req := MakeCreateVolumeReq(sc, vol2Name)
vol2Req.VolumeContentSource = &csi.VolumeContentSource{
Type: &csi.VolumeContentSource_Snapshot{
Snapshot: &csi.VolumeContentSource_SnapshotSource{
SnapshotId: snap.GetSnapshot().GetSnapshotId(),
},
},
}
_, err := r.CreateVolume(context.Background(), vol2Req)
Expect(err).NotTo(HaveOccurred())
})
It("should fail when the volume source snapshot is not found", func() {
if !isControllerCapabilitySupported(r, csi.ControllerServiceCapability_RPC_CREATE_DELETE_SNAPSHOT) {
Skip("Snapshot not supported")
}
By("creating a volume from source snapshot")
volName := UniqueString("sanity-controller-vol-from-snap")
volReq := MakeCreateVolumeReq(sc, volName)
volReq.VolumeContentSource = &csi.VolumeContentSource{
Type: &csi.VolumeContentSource_Snapshot{
Snapshot: &csi.VolumeContentSource_SnapshotSource{
SnapshotId: "non-existing-snapshot-id",
},
},
}
_, err := r.CreateVolume(context.Background(), volReq)
Expect(err).To(HaveOccurred())
serverError, ok := status.FromError(err)
Expect(ok).To(BeTrue())
Expect(serverError.Code()).To(Equal(codes.NotFound), "unexpected error: %s", serverError.Message())
})
*/
})

Describe("GetVolumeGroupSnapshot", func() {
It("should fail when no volume id is provided", func() {
_, err := r.GetVolumeGroupSnapshot(
context.Background(),
&csi.GetVolumeGroupSnapshotRequest{
Secrets: sc.Secrets.ListSnapshotsSecret,
},
)
Expect(err).To(HaveOccurred())

serverError, ok := status.FromError(err)
Expect(ok).To(BeTrue())
Expect(serverError.Code()).To(Equal(codes.InvalidArgument), "unexpected error: %s", serverError.Message())
})

It("should fail when an invalid volume id is used", func() {
_, err := r.GetVolumeGroupSnapshot(
context.Background(),
&csi.GetVolumeGroupSnapshotRequest{
GroupSnapshotId: sc.Config.IDGen.GenerateInvalidVolumeID(),
Secrets: sc.Secrets.ListSnapshotsSecret,
},
)
Expect(err).To(HaveOccurred())

serverError, ok := status.FromError(err)
Expect(ok).To(BeTrue())
Expect(serverError.Code()).To(Equal(codes.NotFound), "unexpected error: %s", serverError.Message())
})
})

Describe("DeleteVolumeGroupSnapshot", func() {
It("should fail when no volume id is provided", func() {
_, err := r.DeleteVolumeGroupSnapshot(
context.Background(),
&csi.DeleteVolumeGroupSnapshotRequest{
Secrets: sc.Secrets.DeleteSnapshotSecret,
},
)
Expect(err).To(HaveOccurred())

serverError, ok := status.FromError(err)
Expect(ok).To(BeTrue())
Expect(serverError.Code()).To(Equal(codes.InvalidArgument), "unexpected error: %s", serverError.Message())
})

It("should succeed when an invalid volume id is used", func() {
_, err := r.DeleteVolumeGroupSnapshot(
context.Background(),
&csi.DeleteVolumeGroupSnapshotRequest{
GroupSnapshotId: sc.Config.IDGen.GenerateInvalidVolumeID(),
Secrets: sc.Secrets.DeleteSnapshotSecret,
},
)
Expect(err).NotTo(HaveOccurred())
})

/* disabled for now
It("should return appropriate values (no optional values added)", func() {
// Create Volume First
By("creating a volume")
name := UniqueString("sanity-controller-create-appropriate")
vol := r.MustCreateVolume(
context.Background(),
&csi.CreateVolumeRequest{
Name: name,
VolumeCapabilities: []*csi.VolumeCapability{
TestVolumeCapabilityWithAccessType(sc, csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER),
},
CapacityRange: &csi.CapacityRange{
RequiredBytes: TestVolumeSize(sc),
},
Secrets: sc.Secrets.CreateVolumeSecret,
Parameters: sc.Config.TestVolumeParameters,
},
)
// Delete Volume
By("deleting a volume")
_, err := r.DeleteVolume(
context.Background(),
&csi.DeleteVolumeRequest{
VolumeId: vol.GetVolume().GetVolumeId(),
Secrets: sc.Secrets.DeleteVolumeSecret,
},
)
Expect(err).NotTo(HaveOccurred())
})
*/
})
})
4 changes: 4 additions & 0 deletions pkg/sanity/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ type Resources struct {
// invoked directly if automatic cleanup is not desired and cannot be
// avoided otherwise.
csi.ControllerClient
// GroupControllerClient is meant for struct-internal usage. It should only be
// invoked directly if automatic cleanup is not desired and cannot be
// avoided otherwise.
csi.GroupControllerClient
// NodeClient is meant for struct-internal usage. It should only be invoked
// directly if automatic cleanup is not desired and cannot be avoided
// otherwise.
Expand Down

0 comments on commit 2d4cc5d

Please sign in to comment.