Skip to content

Commit

Permalink
utils coverage at 78.1%
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianAtDell committed Jan 6, 2025
1 parent 58c0519 commit fcc9977
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 72 deletions.
10 changes: 10 additions & 0 deletions mock/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,21 @@ type serviceClient struct {
// New returns a new Service.
func NewServer() MockServer {
s := &service{nodeID: Name}

// add some mock volumes to start with
s.vols = []csi.Volume{
s.newVolume("Mock Volume 1", gib100),
s.newVolume("Mock Volume 2", gib100),
s.newVolume("Mock Volume 3", gib100),
}

// add some mock snapshots to start with, too
s.snaps = []csi.Snapshot{
s.newSnapshot("Mock Snapshot 1", gib100),
s.newSnapshot("Mock Snapshot 2", gib100),
s.newSnapshot("Mock Snapshot 3", gib100),
s.newSnapshot("Mock Snapshot 4", gib100),
}
return s
}

Expand Down
216 changes: 144 additions & 72 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils_test

import (
"context"
"errors"
"fmt"
"math"
Expand All @@ -9,6 +10,7 @@ import (
"testing"

"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/dell/gocsi/mock/service"
"github.com/dell/gocsi/utils"
"google.golang.org/grpc/codes"
grpcstatus "google.golang.org/grpc/status"
Expand Down Expand Up @@ -414,6 +416,13 @@ var _ = Describe("EqualVolumeCapability", func() {
Ω(utils.EqualVolumeCapability(a, b)).Should(BeFalse())
bAT.Mount.MountFlags = nil
Ω(utils.EqualVolumeCapability(a, b)).Should(BeTrue())

// error test: a is non-nil, b is nil
a.AccessMode = &csi.VolumeCapability_AccessMode{
Mode: csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER,
}
b.AccessMode = nil
Ω(utils.EqualVolumeCapability(a, b)).Should(BeFalse())
})
})

Expand Down Expand Up @@ -463,6 +472,34 @@ var _ = Describe("AreVolumeCapabilitiesCompatible", func() {
Block: &csi.VolumeCapability_BlockVolume{},
}
Ω(utils.AreVolumeCapabilitiesCompatible(a, b)).Should(BeTrue())

// make len(a) > len(b) for an error test
a = append(a, &csi.VolumeCapability{
AccessMode: &csi.VolumeCapability_AccessMode{
Mode: csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER,
},
AccessType: &csi.VolumeCapability_Mount{
Mount: &csi.VolumeCapability_MountVolume{
FsType: "ext4",
MountFlags: []string{"rw"},
},
},
})
a = append(a, &csi.VolumeCapability{
AccessMode: &csi.VolumeCapability_AccessMode{
Mode: csi.VolumeCapability_AccessMode_MULTI_NODE_MULTI_WRITER,
},
AccessType: &csi.VolumeCapability_Mount{
Mount: &csi.VolumeCapability_MountVolume{
FsType: "ext4",
MountFlags: []string{"rw"},
},
},
})
out, err := utils.AreVolumeCapabilitiesCompatible(a, b)
Ω(out).Should(BeFalse())
Ω(err).Should(HaveOccurred())

Check failure on line 502 in utils/utils_test.go

View workflow job for this annotation

GitHub Actions / Golang Validation / Lint golang code

File is not properly formatted (gofumpt)
})
})

Expand Down Expand Up @@ -517,86 +554,83 @@ func TestParseSlice(t *testing.T) {
*/
}

/*
var _ = Describe("PageVolumes", func() {
var (
ctx context.Context
client csi.ControllerClient
req csi.ListVolumesRequest
)
func TestPageVolumes(t *testing.T) {
RegisterTestingT(t)

// Create a new CSI controller service
svc := service.NewClient()

BeforeEach(func() {
ctx = context.Background()
client = svc
req = csi.ListVolumesRequest{}
})
Context("Normal List Volumes Call", func() {
It("Should Be Valid", func() {
cvol, cerr := utils.PageVolumes(ctx, client, req)
var (
volumes []csi.Volume
errors []error
)
for {
select {
case v, ok := <-cvol:
if !ok {
break
}
volumes = append(volumes, v)
case e, ok := <-cerr:
if !ok {
break
}
errors = append(errors, e)
}
// Create a mock controller client
// TODO: This is the part that is breaking-- our mock controller client is not functional
//mockClient := svc

Check failure on line 565 in utils/utils_test.go

View workflow job for this annotation

GitHub Actions / Golang Validation / Lint golang code

File is not properly formatted (gofumpt)

// Create a context
ctx := context.Background()

// Create a list volumes request
req := csi.ListVolumesRequest{}

// Call the PageVolumes function
cvol, cerr := utils.PageVolumes(ctx, svc, req)
var err error
vols := []csi.Volume{}
for {
select {
case v, ok := <-cvol:
if !ok {
return
}
vols = append(vols, v)
case e, ok := <-cerr:
if !ok {
return
}
err = e
}
}

Expect(errors).To(BeEmpty())
Expect(volumes).To(HaveLen(3))
})
})
Expect(err).To(BeNil())

Check failure on line 592 in utils/utils_test.go

View workflow job for this annotation

GitHub Actions / Golang Validation / Lint golang code

unreachable: unreachable code (govet)
Expect(vols).To(HaveLen(4))
}

Context("Create Volume Then List", func() {
JustBeforeEach(func() {
client.CreateVolume(ctx, &csi.CreateVolumeRequest{Name: "test"})
})
func TestPageSnapshots(t *testing.T) {
RegisterTestingT(t)

It("Should Be Valid", func() {
cvol, cerr := utils.PageVolumes(ctx, client, req)
var (
volumes []csi.Volume
errors []error
)
for {
select {
case v, ok := <-cvol:
if !ok {
break
}
volumes = append(volumes, v)
case e, ok := <-cerr:
if !ok {
break
}
errors = append(errors, e)
}
// Create a new CSI controller service
svc := service.NewClient()

// Create a mock controller client
// TODO: This is the part that is breaking-- our mock controller client is not functional
//mockClient := svc

Check failure on line 604 in utils/utils_test.go

View workflow job for this annotation

GitHub Actions / Golang Validation / Lint golang code

File is not properly formatted (gofumpt)

// Create a context
ctx := context.Background()

// Create a list volumes request
req := csi.ListSnapshotsRequest{}

// Call the PageSnapshots function
csnap, cerr := utils.PageSnapshots(ctx, svc, req)
snaps := []csi.Snapshot{}
var err error
for {
select {
case v, ok := <-csnap:
if !ok {
return
}
snaps = append(snaps, v)
case e, ok := <-cerr:
if !ok {
return
}
err = e
}
}

Expect(errors).To(BeEmpty())
Expect(volumes).To(HaveLen(4))
})
})
})
*/
Expect(err).To(BeNil())

Check failure on line 631 in utils/utils_test.go

View workflow job for this annotation

GitHub Actions / Golang Validation / Lint golang code

unreachable: unreachable code (govet)
Expect(snaps).To(HaveLen(3))
}

// struct for error injection testing with GRPCStatus
type ErrorStruct struct {
Expand Down Expand Up @@ -665,7 +699,7 @@ func TestParseMapWS(t *testing.T) {
Expect(data["k2"]).To(Equal("v2"))

/*
// This test case SHOULD pass based on the comments at the top of ParseMapWS.
// TODO: This test case SHOULD pass based on the comments at the top of ParseMapWS.
// It is VERY concerning that it doesn't.
// Test case: Two Pair with Quoting & Escaping
data = utils.ParseMapWS(`k1=v1 "k2=v2"`)
Expand All @@ -675,7 +709,7 @@ func TestParseMapWS(t *testing.T) {
*/

/*
// This test case also SHOULD pass. I don't think ParseMapWS handles quotations as advertised.
// TODO: This test case also SHOULD pass. I don't think ParseMapWS handles quotations as advertised.
// Test case: Two Pair with Quoting & Escaping
data = utils.ParseMapWS(`k1=v1 "k2=v2\'s"`)
Expect(data).To(HaveLen(2))
Expand Down Expand Up @@ -802,3 +836,41 @@ func TestGetCSIEndpointListener(t *testing.T) {
Ω(err).Should(HaveOccurred())
Ω(lis).Should(BeNil())
}

func TestIsVolumeCapabilityCompatible(t *testing.T) {
RegisterTestingT(t)

// Test case: Compatible volume capabilities
a := &csi.VolumeCapability{
AccessMode: &csi.VolumeCapability_AccessMode{
Mode: csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER,
},
AccessType: &csi.VolumeCapability_Block{
Block: &csi.VolumeCapability_BlockVolume{},
},
}
b := []*csi.VolumeCapability{
{
AccessMode: &csi.VolumeCapability_AccessMode{
Mode: csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER,
},
AccessType: &csi.VolumeCapability_Block{
Block: &csi.VolumeCapability_BlockVolume{},
},
},
{
AccessMode: &csi.VolumeCapability_AccessMode{
Mode: csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER,
},
AccessType: &csi.VolumeCapability_Mount{
Mount: &csi.VolumeCapability_MountVolume{
FsType: "ext4",
MountFlags: []string{"rw"},
},
},
},
}
out, err := utils.IsVolumeCapabilityCompatible(a, b)
Ω(out).Should(BeTrue())
Ω(err).Should(BeNil())
}

0 comments on commit fcc9977

Please sign in to comment.