Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre-Emmanuel Jacquier <[email protected]>
  • Loading branch information
pierre-emmanuelJ committed Jan 23, 2024
1 parent 5040fd5 commit d631d10
Show file tree
Hide file tree
Showing 29 changed files with 9,441 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ go.work.sum
kubeconfig

release

bin/
11 changes: 11 additions & 0 deletions driver/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package driver

import "errors"

var (
errLimitLessThanRequiredBytes = errors.New("limit size is less than required size")
errRequiredBytesLessThanMinimun = errors.New("required size is less than the minimun size")
errLimitLessThanMinimum = errors.New("limit size is less than the minimun size")
errRequiredBytesGreaterThanMaximun = errors.New("required size is greater than the maximum size")
errLimitGreaterThanMaximum = errors.New("limit size is greater than the maximum size")
)
11 changes: 5 additions & 6 deletions driver/helpers.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package driver

import (
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -112,23 +111,23 @@ func getNewVolumeSize(capacityRange *csi.CapacityRange) (int64, error) {
}

if requiredSet && limitSet && limitBytes < requiredBytes {
return 0, errors.New("limit size is less than required size")
return 0, errLimitLessThanRequiredBytes
}

if requiredSet && !limitSet && requiredBytes < MinimalVolumeSizeBytes {
return 0, errors.New("required size is less than the minimun size")
return 0, errRequiredBytesLessThanMinimun
}

if limitSet && limitBytes < MinimalVolumeSizeBytes {
return 0, errors.New("limit size is less than the minimun size")
return 0, errLimitLessThanMinimum
}

if requiredSet && requiredBytes > MaximumVolumeSizeBytes {
return 0, errors.New("required size is greater than the maximum size")
return 0, errRequiredBytesGreaterThanMaximun
}

if !requiredSet && limitSet && limitBytes > MaximumVolumeSizeBytes {
return 0, errors.New("limit size is greater than the maximum size")
return 0, errLimitGreaterThanMaximum
}

if requiredSet && limitSet && requiredBytes == limitBytes {
Expand Down
113 changes: 113 additions & 0 deletions driver/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package driver

import (
"testing"

"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/stretchr/testify/require"
)

func TestGetNewVolumeSize(t *testing.T) {
var min int64 = MinimalVolumeSizeBytes
var max int64 = MaximumVolumeSizeBytes
testsBench := []struct {
capRange *csi.CapacityRange
res int64
err error
}{
{
capRange: &csi.CapacityRange{
RequiredBytes: 0,
LimitBytes: 0,
},
res: min,
err: nil,
},
{
capRange: &csi.CapacityRange{
RequiredBytes: min + 10,
LimitBytes: 0,
},
res: min + 10,
err: nil,
},
{
capRange: &csi.CapacityRange{
RequiredBytes: 0,
LimitBytes: min + 10,
},
res: min + 10,
err: nil,
},
{
capRange: &csi.CapacityRange{
RequiredBytes: min - 10,
LimitBytes: 0,
},
res: 0,
err: errRequiredBytesLessThanMinimun,
},
{
capRange: &csi.CapacityRange{
RequiredBytes: 0,
LimitBytes: min - 10,
},
res: 0,
err: errLimitLessThanMinimum,
},
{
capRange: &csi.CapacityRange{
RequiredBytes: min + 10,
LimitBytes: min + 5,
},
res: 0,
err: errLimitLessThanRequiredBytes,
},
{
capRange: &csi.CapacityRange{
RequiredBytes: min + 10,
LimitBytes: min + 5,
},
res: 0,
err: errLimitLessThanRequiredBytes,
},
{
capRange: &csi.CapacityRange{
RequiredBytes: max + 10,
LimitBytes: 0,
},
res: 0,
err: errRequiredBytesGreaterThanMaximun,
},
{
capRange: &csi.CapacityRange{
RequiredBytes: 0,
LimitBytes: max + 10,
},
res: 0,
err: errLimitGreaterThanMaximum,
},
{
capRange: &csi.CapacityRange{
RequiredBytes: min + 10,
LimitBytes: min + 10,
},
res: min + 10,
err: nil,
},
{
capRange: &csi.CapacityRange{
RequiredBytes: min + 10,
LimitBytes: min + 20,
},
res: min + 10,
err: nil,
},
}

for _, test := range testsBench {
res, err := getNewVolumeSize(test.capRange)
require.Equal(t, test.err, err)
require.Equal(t, test.res, res)
}
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/container-storage-interface/spec v1.8.0
github.com/exoscale/egoscale v0.102.4-0.20240117105256-1ace995a320f
github.com/golang/protobuf v1.5.3
github.com/stretchr/testify v1.8.2
golang.org/x/sys v0.15.0
google.golang.org/grpc v1.58.0
google.golang.org/protobuf v1.31.0
Expand Down Expand Up @@ -39,6 +40,7 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/net v0.13.0 // indirect
golang.org/x/oauth2 v0.10.0 // indirect
Expand Down
27 changes: 27 additions & 0 deletions vendor/github.com/pmezard/go-difflib/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d631d10

Please sign in to comment.