Skip to content

Commit

Permalink
Merge pull request #420 from Sakuralbj/test
Browse files Browse the repository at this point in the history
Add Unit tests
  • Loading branch information
k8s-ci-robot committed Jun 3, 2020
2 parents 783c6fc + 1def440 commit 923e41d
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 0 deletions.
74 changes: 74 additions & 0 deletions pkg/azuredisk/identityserver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
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 azuredisk

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

const (
fakeCSIDriverName = "fake"
vendorVersion = "0.3.0"
)

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

//Check error when driver name is empty
d, _ = NewFakeDriver(t)
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(t)
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(t)
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(t)
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))
}
63 changes: 63 additions & 0 deletions pkg/azuredisk/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
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 azuredisk

import (
"fmt"
"reflect"
"runtime"
"strings"
"testing"

"sigs.k8s.io/yaml"
)

func TestGetVersion(t *testing.T) {
version := GetVersion()

expected := VersionInfo{
DriverName: DriverName,
DriverVersion: "N/A",
GitCommit: "N/A",
BuildDate: "N/A",
GoVersion: runtime.Version(),
Compiler: runtime.Compiler,
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
TopologyKey: "N/A",
}

if !reflect.DeepEqual(version, expected) {
t.Errorf("Unexpected error. \n Expected: %v \n Found: %v", expected, version)
}

}

func TestGetVersionYAML(t *testing.T) {
resp, err := GetVersionYAML()
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

versionInfo := GetVersion()
marshalled, _ := yaml.Marshal(&versionInfo)

expected := strings.TrimSpace(string(marshalled))

if resp != expected {
t.Fatalf("Unexpected error. \n Expected:%v\nFound:%v", expected, resp)
}
}
28 changes: 28 additions & 0 deletions pkg/mounter/safe_mounter_unix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
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 mounter

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestNewSafeMounter(t *testing.T) {
resp, err := NewSafeMounter()
assert.NotNil(t, resp)
assert.Nil(t, err)
}

0 comments on commit 923e41d

Please sign in to comment.