Skip to content

Commit

Permalink
test: add unit-test
Browse files Browse the repository at this point in the history
  • Loading branch information
Sakuralbj committed Jul 6, 2020
1 parent ee6737f commit f94d3f7
Show file tree
Hide file tree
Showing 8 changed files with 538 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ before_install:
- GO111MODULE=off go get github.com/mattn/goveralls

script:
- go test -covermode=count -coverprofile=profile.cov ./pkg/...
- sudo -E env "PATH=$PATH" go test -covermode=count -coverprofile=profile.cov ./pkg/...
- $GOPATH/bin/goveralls -coverprofile=profile.cov -service=travis-ci
18 changes: 18 additions & 0 deletions pkg/blobfuse/blobfuse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ import (
"github.com/stretchr/testify/assert"
)

const (
fakeNodeID = "fakeNodeID"
fakeDriverName = "fake"
vendorVersion = "0.3.0"
)

func NewFakeDriver() *Driver {
driver := NewDriver(fakeNodeID)
driver.Name = fakeDriverName
driver.Version = vendorVersion
return driver
}

func TestNewFakeDriver(t *testing.T) {
d := NewDriver(fakeNodeID)
assert.NotNil(t, d)
}

func TestAppendDefaultMountOptions(t *testing.T) {
tests := []struct {
options []string
Expand Down
40 changes: 40 additions & 0 deletions pkg/blobfuse/controllerserver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright 2020 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 blobfuse

import (
"context"
"testing"

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

func TestControllerGetCapabilities(t *testing.T) {
d := NewFakeDriver()
controlCap := []*csi.ControllerServiceCapability{
{
Type: &csi.ControllerServiceCapability_Rpc{},
},
}
d.Cap = controlCap
req := csi.ControllerGetCapabilitiesRequest{}
resp, err := d.ControllerGetCapabilities(context.Background(), &req)
assert.NoError(t, err)
assert.NotNil(t, resp)
assert.Equal(t, resp.Capabilities, controlCap)
}
70 changes: 70 additions & 0 deletions pkg/blobfuse/identityserver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
Copyright 2020 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 blobfuse

import (
"context"
"testing"

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

func TestGetPluginInfo(t *testing.T) {
// Check with correct arguments
d := NewFakeDriver()
req := csi.GetPluginInfoRequest{}
resp, err := d.GetPluginInfo(context.Background(), &req)
assert.NoError(t, err)
assert.Equal(t, resp.Name, fakeDriverName)
assert.Equal(t, resp.GetVendorVersion(), vendorVersion)

//Check error when driver name is empty
d = NewFakeDriver()
d.Name = ""
req = csi.GetPluginInfoRequest{}
resp, err = d.GetPluginInfo(context.Background(), &req)
assert.Error(t, err)
assert.Nil(t, resp)

//Check error when version is empty
d = NewFakeDriver()
d.Version = ""
req = csi.GetPluginInfoRequest{}
resp, err = d.GetPluginInfo(context.Background(), &req)
assert.Error(t, err)
assert.Nil(t, resp)
}

func TestProbe(t *testing.T) {
d := NewFakeDriver()
req := csi.ProbeRequest{}
resp, err := d.Probe(context.Background(), &req)
assert.NoError(t, err)
assert.NotNil(t, resp)
assert.Equal(t, resp.XXX_sizecache, int32(0))
assert.Equal(t, resp.Ready.Value, true)
}

func TestGetPluginCapabilities(t *testing.T) {
d := NewFakeDriver()
req := csi.GetPluginCapabilitiesRequest{}
resp, err := d.GetPluginCapabilities(context.Background(), &req)
assert.NoError(t, err)
assert.NotNil(t, resp)
assert.Equal(t, resp.XXX_sizecache, int32(0))
}
3 changes: 0 additions & 3 deletions pkg/blobfuse/nodeserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ func (d *Driver) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolu
if len(req.GetVolumeId()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume ID missing in request")
}
if len(req.GetTargetPath()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Target path missing in request")
}

source := req.GetStagingTargetPath()
if len(source) == 0 {
Expand Down
Loading

0 comments on commit f94d3f7

Please sign in to comment.