@@ -14,10 +14,15 @@ package gofsutil_test
1414
1515import (
1616 "context"
17+ "fmt"
1718 "os"
19+ "path/filepath"
20+ "strings"
1821 "testing"
1922
2023 "github.com/dell/gofsutil"
24+ "github.com/stretchr/testify/assert"
25+ "github.com/stretchr/testify/require"
2126)
2227
2328func TestBindMount (t * testing.T ) {
@@ -81,3 +86,132 @@ func TestGetMounts(t *testing.T) {
8186 t .Logf ("%+v" , m )
8287 }
8388}
89+
90+ func TestGetSysBlockDevicesForVolumeWWN (t * testing.T ) {
91+ tempDir := t .TempDir ()
92+ gofsutil .UseMockSysBlockDir (tempDir )
93+
94+ tests := []struct {
95+ name string
96+ wwn string
97+ nguid string
98+ deviceName string
99+ deviceWwidPath []string
100+ expect []string
101+ errString string
102+ }{
103+ {
104+ name : "iscsi block device" ,
105+ wwn : "example-volume-wwn" ,
106+ deviceName : "sdx" ,
107+ deviceWwidPath : []string {"device" , "wwid" },
108+ expect : []string {"sdx" },
109+ errString : "" ,
110+ },
111+ {
112+ name : "PowerStore nvme block device" ,
113+ wwn : "naa.68ccf098001111a2222b3d4444a1b23c" ,
114+ nguid : "eui.1111a2222b3d44448ccf096800a1b23c" ,
115+ deviceName : "nvme0n1" ,
116+ deviceWwidPath : []string {"wwid" },
117+ expect : []string {"nvme0n1" },
118+ errString : "" ,
119+ },
120+ {
121+ name : "PowerMax nvme block device" ,
122+ wwn : "naa.60000970000120001263533030313434" ,
123+ nguid : "eui.12635330303134340000976000012000" ,
124+ deviceName : "nvme0n2" ,
125+ deviceWwidPath : []string {"wwid" },
126+ expect : []string {"nvme0n2" },
127+ errString : "" ,
128+ },
129+ }
130+
131+ for _ , tt := range tests {
132+ t .Run (tt .name , func (t * testing.T ) {
133+ // Create the necessary directories and files
134+ path := []string {tempDir , tt .deviceName }
135+ path = append (path , tt .deviceWwidPath ... )
136+ deviceWwidFile := filepath .Join (path ... )
137+ err := os .MkdirAll (filepath .Dir (deviceWwidFile ), 0o755 )
138+ require .Nil (t , err )
139+ if strings .HasPrefix (tt .deviceName , "nvme" ) {
140+ err = os .WriteFile (deviceWwidFile , []byte (tt .nguid ), 0o600 )
141+ } else {
142+ err = os .WriteFile (deviceWwidFile , []byte (tt .wwn ), 0o600 )
143+ }
144+ require .Nil (t , err )
145+
146+ // Call the function with the test input
147+ result , err := gofsutil .GetSysBlockDevicesForVolumeWWN (context .Background (), tt .wwn )
148+ assert .Nil (t , err )
149+ assert .Equal (t , tt .expect , result )
150+ })
151+ }
152+ }
153+
154+ func TestGetNVMeController (t * testing.T ) {
155+ tempDir := t .TempDir ()
156+ gofsutil .UseMockSysBlockDir (tempDir )
157+
158+ tests := map [string ]struct {
159+ device string
160+ controller string
161+ path []string
162+ expectedErr error
163+ }{
164+ "device exists and is an NVMe controller" : {
165+ device : "nvme0n1" ,
166+ controller : "nvme0" ,
167+ path : []string {"virtual" , "nvme-fabrics" , "ctl" , "nvme0" , "nvme0n1" },
168+ expectedErr : nil ,
169+ },
170+ "device exists but is not an NVMe controller" : {
171+ device : "nvme1n1" ,
172+ controller : "" ,
173+ path : []string {"virtual" , "nvme-fabrics" , "nvme-subsystem" , "nvme-subsys0" , "nvme1n1" },
174+ expectedErr : nil ,
175+ },
176+ "device exists but NVMe controller not found" : {
177+ device : "nvme2n1" ,
178+ controller : "" ,
179+ path : []string {"virtual" , "nvme-fabrics" , "ctl" , "nvme2n1" },
180+ expectedErr : fmt .Errorf ("controller not found for device nvme2n1" ),
181+ },
182+ "device does not exist" : {
183+ device : "nonexistent" ,
184+ controller : "" ,
185+ expectedErr : fmt .Errorf ("device %s does not exist" , "nonexistent" ),
186+ },
187+ }
188+
189+ for name , test := range tests {
190+ t .Run (name , func (t * testing.T ) {
191+ if name != "device does not exist" {
192+ // Create the necessary directories and files
193+ realPath := []string {tempDir }
194+ realPath = append (realPath , test .path ... )
195+ err := os .MkdirAll (filepath .Join (realPath ... ), 0o755 )
196+ require .NoError (t , err )
197+
198+ sysBlockNVMeDeviceDir := filepath .Join (tempDir , test .device )
199+ err = os .Symlink (filepath .Join (realPath ... ), sysBlockNVMeDeviceDir )
200+ require .NoError (t , err )
201+ }
202+
203+ // Call the function with the test input
204+ controller , err := gofsutil .GetNVMeController (test .device )
205+ if test .expectedErr != nil && err == nil {
206+ t .Errorf ("getNVMeController() did not return error, expected %v" , test .expectedErr )
207+ } else if test .expectedErr == nil && err != nil {
208+ t .Errorf ("getNVMeController() returned error %v, expected no error" , err )
209+ } else if err != nil && err .Error () != test .expectedErr .Error () {
210+ t .Errorf ("getNVMeController() returned error %v, expected %v" , err , test .expectedErr )
211+ }
212+ if controller != test .controller {
213+ t .Errorf ("getNVMeController() = %v, expected %v" , controller , test .controller )
214+ }
215+ })
216+ }
217+ }
0 commit comments