Skip to content

Commit 02e9c21

Browse files
vincentBaerVincent Baer
authored and
Vincent Baer
committed
Retrieve scsiName with test
1 parent 9c531de commit 02e9c21

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ require (
5858
k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed
5959
)
6060

61+
require github.com/pmezard/go-difflib v1.0.0 // indirect
62+
6163
require (
6264
github.com/PuerkitoBio/purell v1.1.1 // indirect
6365
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
@@ -105,6 +107,7 @@ require (
105107
github.com/prometheus/procfs v0.7.3 // indirect
106108
github.com/spf13/cobra v1.4.0 // indirect
107109
github.com/spf13/pflag v1.0.5 // indirect
110+
github.com/stretchr/testify v1.8.3
108111
go.opentelemetry.io/contrib v0.20.0 // indirect
109112
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.20.0 // indirect
110113
go.opentelemetry.io/otel v0.20.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
400400
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
401401
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
402402
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
403+
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
404+
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
403405
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
404406
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
405407
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=

pkg/driver/node.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"os"
2323
"path/filepath"
24+
"regexp"
2425
"strconv"
2526
"strings"
2627

@@ -654,11 +655,31 @@ func (d *nodeService) findDevicePath(devicePath, volumeID string) (string, error
654655
}
655656

656657
// assumption it is a scsi volume for 3DS env
657-
scsiName := "scsi-0QEMU_QEMU_HARDDISK_sd" + devicePath[len(devicePath)-1:]
658+
scsiName, err := findScsiName(devicePath)
659+
if err != nil {
660+
return "", err
661+
}
662+
658663
klog.V(4).Infof("findDevicePath: check if scsi device for %s is %s and return the device", devicePath, scsiName)
659664
return findScsiVolume(scsiName)
660665
}
661666

667+
func findScsiName(devicePath string) (string, error) {
668+
myreg := regexp.MustCompile(`^/dev/xvd(?P<suffix>[a-z]{1,2})$`)
669+
match := myreg.FindStringSubmatch(devicePath)
670+
result := make(map[string]string)
671+
if myreg.MatchString(devicePath) {
672+
for i, name := range myreg.SubexpNames() {
673+
if i != 0 && name != "" {
674+
result[name] = match[i]
675+
}
676+
}
677+
scsiName := "scsi-0QEMU_QEMU_HARDDISK_sd" + result["suffix"]
678+
return scsiName, nil
679+
}
680+
return "", fmt.Errorf("devicePath %s is not supported", devicePath)
681+
}
682+
662683
func findScsiVolume(findName string) (device string, err error) {
663684
p := filepath.Join("/dev/disk/by-id/", findName)
664685
stat, err := os.Lstat(p)

pkg/driver/node_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package driver
1919
import (
2020
"context"
2121
"errors"
22+
"fmt"
2223
"os"
2324
"reflect"
2425
"testing"
@@ -27,6 +28,7 @@ import (
2728
"github.com/golang/mock/gomock"
2829
"github.com/outscale-dev/osc-bsu-csi-driver/pkg/driver/internal"
2930
"github.com/outscale-dev/osc-bsu-csi-driver/pkg/driver/mocks"
31+
"github.com/stretchr/testify/assert"
3032
"google.golang.org/grpc/codes"
3133
"google.golang.org/grpc/status"
3234
exec "k8s.io/utils/exec"
@@ -1528,6 +1530,44 @@ func TestNodeGetInfo(t *testing.T) {
15281530
}
15291531
}
15301532

1533+
func TestFindScsiName(t *testing.T) {
1534+
findScsiNameCase := []struct {
1535+
name string
1536+
devicePath string
1537+
scsiName string
1538+
expTestFindScsiName error
1539+
}{
1540+
{
1541+
name: "Validate format xvdx",
1542+
devicePath: "/dev/xvda",
1543+
scsiName: "scsi-0QEMU_QEMU_HARDDISK_sda",
1544+
expTestFindScsiName: nil,
1545+
},
1546+
{
1547+
name: "Validate format xvdxy",
1548+
devicePath: "/dev/xvdaa",
1549+
scsiName: "scsi-0QEMU_QEMU_HARDDISK_sdaa",
1550+
expTestFindScsiName: nil,
1551+
},
1552+
{
1553+
name: "Invalide format xvdxyz",
1554+
devicePath: "/dev/xvdaaa",
1555+
scsiName: "scsi-0QEMU_QEMU_HARDDISK_sd",
1556+
expTestFindScsiName: fmt.Errorf("devicePath /dev/xvdaaa is not supported"),
1557+
},
1558+
}
1559+
for _, fsnc := range findScsiNameCase {
1560+
t.Run(fsnc.name, func(t *testing.T) {
1561+
scsiName, err := findScsiName(fsnc.devicePath)
1562+
if err != nil {
1563+
assert.Equal(t, fsnc.expTestFindScsiName.Error(), err.Error())
1564+
} else {
1565+
assert.Equal(t, fsnc.scsiName, scsiName)
1566+
}
1567+
})
1568+
}
1569+
}
1570+
15311571
func expectErr(t *testing.T, actualErr error, expectedCode codes.Code) {
15321572
if actualErr == nil {
15331573
t.Fatalf("Expect error but got no error")

0 commit comments

Comments
 (0)