Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

add args unit tests for pmem-ns-init, other minor unit testing improvements #246

Merged
merged 3 commits into from
Apr 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions cmd/pmem-ns-init/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func main() {

glog.Info("Version: ", version)

if err := CheckArgs(*namespacesize, *useforfsdax, *useforsector); err != nil {
fmt.Printf("Arguments check failed: %s", err.Error())
os.Exit(1)
}
ctx, err := ndctl.NewContext()
if err != nil {
fmt.Printf("Failed to initialize pmem context: %s", err.Error())
Expand All @@ -44,26 +48,24 @@ func main() {
initNVdimms(ctx, *namespacesize, *useforfsdax, *useforsector)
}

func initNVdimms(ctx *ndctl.Context, namespacesize int, useforfsdax int, useforsector int) {
glog.Infof("Configured namespacesize; %v GB", namespacesize)
// TODO: rethink is it good idea to reset to sane values or is it cleaner to fail
func CheckArgs(namespacesize int, useforfsdax int, useforsector int) error {
if useforfsdax < 0 || useforfsdax > 100 {
glog.Infof("useforfsdax limit should be 0..100 (seeing %v), resetting to default=100", useforfsdax)
useforfsdax = 100
return fmt.Errorf("useforfsdax value must be 0..100")
}
if useforsector < 0 || useforsector > 100 {
glog.Infof("useforsector limit should be 0..100 (seeing %v), resetting to default=0", useforsector)
useforsector = 0
return fmt.Errorf("useforsector value must be 0..100")
}
if useforfsdax+useforsector > 100 {
glog.Infof("useforfsdax and useforsector combined can not exceed 100, resetting to defaults 100,0")
useforfsdax = 100
useforsector = 0
return fmt.Errorf("useforfsdax and useforsector combined must not exceed 100")
}
if namespacesize < 2 {
glog.Infof("namespacesize has to be at least 2 GB, forcing to 2")
namespacesize = 2
return fmt.Errorf("namespacesize has to be at least 2 GB")
}
return nil
}

func initNVdimms(ctx *ndctl.Context, namespacesize int, useforfsdax int, useforsector int) {
glog.Infof("Configured namespacesize; %v GB", namespacesize)
// we get 1GB smaller than we ask so lets ask for 1GB more
nsSize := uint64(namespacesize+1) * 1024 * 1024 * 1024
for _, bus := range ctx.GetBuses() {
Expand Down
72 changes: 72 additions & 0 deletions cmd/pmem-ns-init/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package main_test

import (
"flag"
"testing"

pmemnsinit "github.com/intel/pmem-csi/cmd/pmem-ns-init"
"k8s.io/klog"

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

func init() {
klog.InitFlags(nil)
flag.Set("logtostderr", "true")
flag.Parse()
}

func TestPmemNSInit(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "NSInit Suite")
}

/*
var _ = BeforeSuite(func() {
var err error
Expect(err).NotTo(HaveOccurred())
})

var _ = AfterSuite(func() {
})*/

var _ = Describe("pmem-ns-init", func() {
Context("Check arguments", func() {
type cases struct {
name string
namespacesize, useforfsdax, useforsector int
}
goodcases := []cases{
{"namespacesize ok", 32, 0, 0},
{"useforfsdax ok", 32, 50, 0},
{"useforsector ok", 32, 0, 50},
{"useforfsdax and useforsector combined 100", 32, 70, 30},
{"useforfsdax and useforsector combined less than 100", 32, 40, 30},
}
badcases := []cases{
{"namespacesize negative", -1, 0, 0},
{"namespacesize too small", 1, 0, 0},
{"useforfsdax negative", 32, -1, 0},
{"useforsector negative", 32, 0, -1},
{"useforfsdax too large", 32, 101, 0},
{"useforsector too large", 32, 0, 101},
{"useforfsdax and useforsector combined too large", 32, 51, 51},
}

for _, c := range goodcases {
c := c
It(c.name, func() {
err := pmemnsinit.CheckArgs(c.namespacesize, c.useforfsdax, c.useforsector)
Expect(err).NotTo(HaveOccurred())
})
}
for _, c := range badcases {
c := c
It(c.name, func() {
err := pmemnsinit.CheckArgs(c.namespacesize, c.useforfsdax, c.useforsector)
Expect(err).To(HaveOccurred())
})
}
})
})
14 changes: 1 addition & 13 deletions pkg/pmem-csi-driver/registryserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"testing"
"time"

"github.com/container-storage-interface/spec/lib/go/csi"
pmemcsidriver "github.com/intel/pmem-csi/pkg/pmem-csi-driver"
pmemgrpc "github.com/intel/pmem-csi/pkg/pmem-grpc"
registry "github.com/intel/pmem-csi/pkg/pmem-registry"
Expand All @@ -27,17 +26,6 @@ func init() {
flag.Parse()
}

type mockNodeController struct {
}

func (m *mockNodeController) ControllerPublishVolume(ctx context.Context, req *csi.ControllerPublishVolumeRequest) (*csi.ControllerPublishVolumeResponse, error) {
return &csi.ControllerPublishVolumeResponse{}, nil
}

func (m *mockNodeController) ControllerUnpublishVolume(ctx context.Context, req *csi.ControllerUnpublishVolumeRequest) (*csi.ControllerUnpublishVolumeResponse, error) {
return &csi.ControllerUnpublishVolumeResponse{}, nil
}

func TestPmemRegistry(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Registry Suite")
Expand Down Expand Up @@ -173,7 +161,7 @@ var _ = Describe("pmem registry", func() {
evilKey = os.ExpandEnv("${TEST_WORK}/evil-ca/pmem-node-controller-key.pem")
)

// This covers different scenarios for connections to the OIM registry.
// This covers different scenarios for connections to the registry.
cases := []struct {
name, ca, cert, key, peerName, errorText string
}{
Expand Down
4 changes: 2 additions & 2 deletions test/test.make
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
TEST_CMD=go test
TEST_ARGS=$(IMPORT_PATH)/pkg/...
TEST_ARGS=$(IMPORT_PATH)/cmd/... $(IMPORT_PATH)/pkg/...

.PHONY: vet
test: vet
Expand Down Expand Up @@ -114,7 +114,7 @@ test_e2e: start

.PHONY: run_tests
test: run_tests
run_tests: pmem-csi-driver _work/pmem-ca/.ca-stamp _work/evil-ca/.ca-stamp
run_tests: all _work/pmem-ca/.ca-stamp _work/evil-ca/.ca-stamp
TEST_WORK=$(abspath _work) \
$(TEST_CMD) $(shell go list $(TEST_ARGS) | sed -e 's;$(IMPORT_PATH);.;')

Expand Down