Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add common.go unit test #332

Merged
merged 1 commit into from
Aug 16, 2022
Merged
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
332 changes: 328 additions & 4 deletions pkg/common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@ limitations under the License.
package common

import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"time"

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/rest"
"sigs.k8s.io/sig-storage-local-static-provisioner/pkg/util"
)

func TestSetupClientByKubeConfigEnv(t *testing.T) {
Expand Down Expand Up @@ -84,12 +90,14 @@ func TestLoadProvisionerConfigs(t *testing.T) {
os.RemoveAll(tmpConfigPath)
}()
testcases := []struct {
data map[string]string
expected ProvisionerConfiguration
data map[string]string
expected ProvisionerConfiguration
expectedErr error
}{
{
nil,
ProvisionerConfiguration{},
nil,
},
{
map[string]string{
Expand All @@ -98,6 +106,99 @@ func TestLoadProvisionerConfigs(t *testing.T) {
ProvisionerConfiguration{
UseAlphaAPI: true,
},
nil,
},
{
map[string]string{
"storageClassMap": `local-storage:
hostDir: /mnt/disks
mountDir: /mnt/disks
fsType: ext4
`,
"useAlphaAPI": "true",
"minResyncPeriod": "1h30m",
},
ProvisionerConfiguration{
StorageClassConfig: map[string]MountConfig{
"local-storage": {
HostDir: "/mnt/disks",
MountDir: "/mnt/disks",
BlockCleanerCommand: []string{"/scripts/quick_reset.sh"},
VolumeMode: "Filesystem",
FsType: "ext4",
NamePattern: "*",
},
},
UseAlphaAPI: true,
MinResyncPeriod: metav1.Duration{
Duration: time.Hour + time.Minute*30,
},
},
nil,
},
{
map[string]string{
"storageClassMap": `local-storage:
hostDir: /mnt/disks
mountDir: /mnt/disks
blockCleanerCommand:
- "/scripts/shred.sh"
- "2"
volumeMode: WrongFilesystem
fsType: ext4
namePattern: nvm*,sdb*
`,
"useAlphaAPI": "true",
"minResyncPeriod": "1h30m",
},
ProvisionerConfiguration{
StorageClassConfig: map[string]MountConfig{
"local-storage": {
HostDir: "/mnt/disks",
MountDir: "/mnt/disks",
BlockCleanerCommand: []string{"/scripts/shred.sh", "2"},
VolumeMode: "WrongFilesystem",
FsType: "ext4",
NamePattern: "nvm*,sdb*",
},
},
UseAlphaAPI: true,
MinResyncPeriod: metav1.Duration{
Duration: time.Hour + time.Minute*30,
},
},
fmt.Errorf("unsupported volume mode WrongFilesystem"),
},
{
map[string]string{
"storageClassMap": `local-storage:
hostDir: /mnt/disks
blockCleanerCommand:
- "/scripts/shred.sh"
- "2"
volumeMode: Filesystem
fsType: ext4
namePattern: nvm*,sdb*
`,
"useAlphaAPI": "true",
"minResyncPeriod": "1h30m",
},
ProvisionerConfiguration{
StorageClassConfig: map[string]MountConfig{
"local-storage": {
HostDir: "/mnt/disks",
BlockCleanerCommand: []string{"/scripts/shred.sh", "2"},
VolumeMode: "Filesystem",
FsType: "ext4",
NamePattern: "nvm*,sdb*",
},
},
UseAlphaAPI: true,
MinResyncPeriod: metav1.Duration{
Duration: time.Hour + time.Minute*30,
},
},
fmt.Errorf("Storage Class local-storage is misconfigured, missing HostDir or MountDir parameter"),
},
{
map[string]string{
Expand Down Expand Up @@ -130,6 +231,7 @@ func TestLoadProvisionerConfigs(t *testing.T) {
Duration: time.Hour + time.Minute*30,
},
},
nil,
},
}
for _, v := range testcases {
Expand All @@ -141,11 +243,233 @@ func TestLoadProvisionerConfigs(t *testing.T) {
}
provisionerConfig := ProvisionerConfiguration{}
err = LoadProvisionerConfigs(tmpConfigPath, &provisionerConfig)
if err != nil {
t.Fatalf("LoadProvisionerConfigs error: %v", err)
if !reflect.DeepEqual(err, v.expectedErr) {
t.Errorf("LoadProvisionerConfigs error: expected %v, got %v", v.expectedErr, err)
}
if !reflect.DeepEqual(provisionerConfig, v.expected) {
t.Errorf("Failed to parse config from data %q, expected %+v, got %+v", v.data, v.expected, provisionerConfig)
}
}
}

func TestVolumeConfigToConfigMapData(t *testing.T) {
testcases := []struct {
provisionerConfig *ProvisionerConfiguration
expected map[string]string
expectedErr error
}{
{
provisionerConfig: &ProvisionerConfiguration{},
expected: map[string]string{
"storageClassMap": "null\n",
"useAlphaAPI": "false\n",
},
expectedErr: nil,
},
{
provisionerConfig: &ProvisionerConfiguration{
NodeLabelsForPV: []string{"node1", "node2"},
},
expected: map[string]string{
"storageClassMap": "null\n",
"nodeLabelsForPV": "- node1\n- node2\n",
"useAlphaAPI": "false\n",
},
expectedErr: nil,
},
}
for _, test := range testcases {
mapData, err := VolumeConfigToConfigMapData(test.provisionerConfig)
if !reflect.DeepEqual(mapData, test.expected) || !reflect.DeepEqual(err, test.expectedErr) {
t.Errorf("ProvisionerConfig: %v, expected mapData: %v, got mapData: %v, expected err: %v, got err: %v", test.provisionerConfig, test.expected, mapData, test.expectedErr, err)
}
}
}

func TestCreateLocalPVSpec(t *testing.T) {
volumeModeBlock := v1.PersistentVolumeBlock
ownerReference := &metav1.OwnerReference{}
testcases := []struct {
config *LocalPVConfig
expected *v1.PersistentVolume
}{
{
config: &LocalPVConfig{
VolumeMode: volumeModeBlock,
},
expected: &v1.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
AnnProvisionedBy: "",
},
},
Spec: v1.PersistentVolumeSpec{
Capacity: v1.ResourceList{
v1.ResourceName(v1.ResourceStorage): *resource.NewQuantity(int64(0), resource.BinarySI),
},
PersistentVolumeSource: v1.PersistentVolumeSource{
Local: &v1.LocalVolumeSource{},
},
AccessModes: []v1.PersistentVolumeAccessMode{
v1.ReadWriteOnce,
},
VolumeMode: &volumeModeBlock,
},
},
},
{
config: &LocalPVConfig{
VolumeMode: volumeModeBlock,
UseAlphaAPI: true,
SetPVOwnerRef: true,
OwnerReference: ownerReference,
},
expected: &v1.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
AnnProvisionedBy: "",
AlphaStorageNodeAffinityAnnotation: "",
},
OwnerReferences: []metav1.OwnerReference{*ownerReference},
},
Spec: v1.PersistentVolumeSpec{
Capacity: v1.ResourceList{
v1.ResourceName(v1.ResourceStorage): *resource.NewQuantity(int64(0), resource.BinarySI),
},
PersistentVolumeSource: v1.PersistentVolumeSource{
Local: &v1.LocalVolumeSource{},
},
AccessModes: []v1.PersistentVolumeAccessMode{
v1.ReadWriteOnce,
},
VolumeMode: &volumeModeBlock,
},
},
},
}
for _, test := range testcases {
pv := CreateLocalPVSpec(test.config)
if !reflect.DeepEqual(pv, test.expected) {
t.Errorf("LocalPVConfig: %v, expected PV spec: %v, got PV spec: %v", test.config, test.expected, pv)
}
}
}

func TestGetContainerPath(t *testing.T) {
testcases := []struct {
pv *v1.PersistentVolume
config MountConfig
expected string
expectedErr error
}{
{
pv: &v1.PersistentVolume{
Spec: v1.PersistentVolumeSpec{
PersistentVolumeSource: v1.PersistentVolumeSource{
Local: &v1.LocalVolumeSource{
Path: "/mnt/disks",
},
},
},
},
config: MountConfig{
HostDir: "/mnt/disks",
},
expected: ".",
expectedErr: nil,
},
{
pv: &v1.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{
Name: "wrongpath",
},
Spec: v1.PersistentVolumeSpec{
PersistentVolumeSource: v1.PersistentVolumeSource{
Local: &v1.LocalVolumeSource{
Path: "wrongpath",
},
},
},
},
config: MountConfig{
HostDir: "/mnt/disks",
},
expected: "",
expectedErr: fmt.Errorf("Could not get relative path for pv %q: %v", "wrongpath", errors.New("Rel: can't make wrongpath relative to /mnt/disks")),
},
}
for _, test := range testcases {
path, err := GetContainerPath(test.pv, test.config)
if !reflect.DeepEqual(path, test.expected) || !reflect.DeepEqual(err, test.expectedErr) {
t.Errorf("pv: %v, config: %v, expected path: %v, got path: %v, expected error: %v, got error: %v", test.pv, test.config, test.expected, path, test.expectedErr, err)
}
}
}

func TestGenerateMountName(t *testing.T) {
testcases := []struct {
mountConfig *MountConfig
}{
{
mountConfig: &MountConfig{
HostDir: "/mnt/disks",
MountDir: "/mnt/disks",
},
},
}
for _, test := range testcases {
mountName := GenerateMountName(test.mountConfig)
if !strings.HasPrefix(mountName, "mount-") {
t.Errorf("mountConfig: %v, got mountName: %v", test.mountConfig, mountName)
}
}
}

func TestGetVolumeMode(t *testing.T) {
fakeDirFile := "/file"
fakeDirBlock := "/block"
fakeDirFiles := map[string][]*util.FakeDirEntry{
fakeDirFile: {&util.FakeDirEntry{
Name: "disks",
VolumeType: "file",
Capacity: 64,
}},
fakeDirBlock: {&util.FakeDirEntry{
Name: "disks",
VolumeType: "block",
Capacity: 64,
}},
}
fakeVolUtil := util.NewFakeVolumeUtil(true, fakeDirFiles)
testcases := []struct {
volumeUtil util.VolumeUtil
fullPath string
expected v1.PersistentVolumeMode
expectedErr error
}{
{
volumeUtil: fakeVolUtil,
fullPath: "/file/disks",
expected: v1.PersistentVolumeFilesystem,
expectedErr: nil,
},
{
volumeUtil: fakeVolUtil,
fullPath: "/block/disks",
expected: v1.PersistentVolumeBlock,
expectedErr: nil,
},
{
volumeUtil: fakeVolUtil,
fullPath: "/file/disk",
expected: "",
expectedErr: fmt.Errorf("Directory check for %q failed: %s", "/file/disk", fmt.Errorf("Directory entry %q not found", "/file/disk")),
},
}
for _, test := range testcases {
mode, err := GetVolumeMode(test.volumeUtil, test.fullPath)
if !reflect.DeepEqual(mode, test.expected) || !reflect.DeepEqual(err, test.expectedErr) {
t.Errorf("volumeUtil: %v, fullPath: %v, expected mode: %v, got mode: %v, expected error: %v, got error: %v", test.volumeUtil, test.fullPath, test.expected, mode, test.expectedErr, err)
}
}
}