From 1def4403d6352f7006b4a5181ab953b134558ded Mon Sep 17 00:00:00 2001 From: Sakuralbj Date: Wed, 3 Jun 2020 16:36:57 +0800 Subject: [PATCH] Add Unit tests 1.pkg/azuredisk/identity and version. 2.pkg/mounter/safe_mounter_unix. --- pkg/azuredisk/identityserver_test.go | 74 +++++++++++++++++++++++++++ pkg/azuredisk/version_test.go | 63 +++++++++++++++++++++++ pkg/mounter/safe_mounter_unix_test.go | 28 ++++++++++ 3 files changed, 165 insertions(+) create mode 100644 pkg/azuredisk/identityserver_test.go create mode 100644 pkg/azuredisk/version_test.go create mode 100644 pkg/mounter/safe_mounter_unix_test.go diff --git a/pkg/azuredisk/identityserver_test.go b/pkg/azuredisk/identityserver_test.go new file mode 100644 index 0000000000..f4a77d6a19 --- /dev/null +++ b/pkg/azuredisk/identityserver_test.go @@ -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)) +} diff --git a/pkg/azuredisk/version_test.go b/pkg/azuredisk/version_test.go new file mode 100644 index 0000000000..2c7f0e9532 --- /dev/null +++ b/pkg/azuredisk/version_test.go @@ -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) + } +} diff --git a/pkg/mounter/safe_mounter_unix_test.go b/pkg/mounter/safe_mounter_unix_test.go new file mode 100644 index 0000000000..9acf46780d --- /dev/null +++ b/pkg/mounter/safe_mounter_unix_test.go @@ -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) +}