From 44d7510ee05040ce90337215880c7a76b51e7404 Mon Sep 17 00:00:00 2001 From: Fabio Bertinatto Date: Wed, 24 Jul 2019 14:21:36 +0200 Subject: [PATCH] Add unit test for iSCSI refcounter --- pkg/volume/iscsi/iscsi_util_test.go | 82 +++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/pkg/volume/iscsi/iscsi_util_test.go b/pkg/volume/iscsi/iscsi_util_test.go index d4ed4c5f768..c8560297711 100644 --- a/pkg/volume/iscsi/iscsi_util_test.go +++ b/pkg/volume/iscsi/iscsi_util_test.go @@ -19,6 +19,7 @@ package iscsi import ( "errors" "fmt" + "io/ioutil" "os" "path/filepath" "reflect" @@ -329,3 +330,84 @@ func TestClonedIfaceUpdateError(t *testing.T) { } } + +func TestGetVolCount(t *testing.T) { + testCases := []struct { + name string + portal string + iqn string + count int + }{ + { + name: "wrong portal, no volumes", + portal: "192.168.0.2:3260", // incorrect IP address + iqn: "iqn.2003-01.io.k8s:e2e.volume-1", + count: 0, + }, + { + name: "wrong iqn, no volumes", + portal: "127.0.0.1:3260", + iqn: "iqn.2003-01.io.k8s:e2e.volume-3", // incorrect volume + count: 0, + }, + { + name: "single volume", + portal: "192.168.0.1:3260", + iqn: "iqn.2003-01.io.k8s:e2e.volume-1", + count: 1, + }, + { + name: "two volumes", + portal: "127.0.0.1:3260", + iqn: "iqn.2003-01.io.k8s:e2e.volume-1", + count: 2, + }, + } + + // This will create a dir structure like this: + // /tmp/refcounter555814673 + // ├── iface-127.0.0.1:3260:pv1 + // │   └── 127.0.0.1:3260-iqn.2003-01.io.k8s:e2e.volume-1-lun-3 + // └── iface-127.0.0.1:3260:pv2 + // ├── 127.0.0.1:3260-iqn.2003-01.io.k8s:e2e.volume-1-lun-2 + // └── 192.168.0.1:3260-iqn.2003-01.io.k8s:e2e.volume-1-lun-1 + + baseDir, err := createFakePluginDir() + if err != nil { + t.Errorf("error creating fake plugin dir: %v", err) + } + defer os.RemoveAll(baseDir) + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + count, err := getVolCount(baseDir, tc.portal, tc.iqn) + if err != nil { + t.Errorf("expected no error, got %v", err) + } + if count != tc.count { + t.Errorf("expected %d volumes, got %d", tc.count, count) + } + }) + } +} + +func createFakePluginDir() (string, error) { + dir, err := ioutil.TempDir("", "refcounter") + if err != nil { + return "", err + } + + subdirs := []string{ + "iface-127.0.0.1:3260:pv1/127.0.0.1:3260-iqn.2003-01.io.k8s:e2e.volume-1-lun-3", + "iface-127.0.0.1:3260:pv2/127.0.0.1:3260-iqn.2003-01.io.k8s:e2e.volume-1-lun-2", + "iface-127.0.0.1:3260:pv2/192.168.0.1:3260-iqn.2003-01.io.k8s:e2e.volume-1-lun-1", + } + + for _, d := range subdirs { + if err := os.MkdirAll(filepath.Join(dir, d), os.ModePerm); err != nil { + return dir, err + } + } + + return dir, err +}