-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgonvme_mock.go
314 lines (264 loc) · 9.46 KB
/
gonvme_mock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
*
* Copyright © 2022-2023 Dell Inc. or its subsidiaries. All Rights Reserved.
*
* 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 gonvme
import (
"errors"
"fmt"
"strconv"
)
const (
// MockNumberOfInitiators controls the number of initiators found in mock mode
MockNumberOfInitiators = "numberOfInitiators"
// MockNumberOfTCPTargets controls the number of NVMeTCP targets found in mock mode
MockNumberOfTCPTargets = "numberOfTCPTargets"
// MockNumberOfFCTargets controls the number of NVMeFC targets found in mock mode
MockNumberOfFCTargets = "numberOfFCTargets"
// MockNumberOfSessions controls the number of NVMe sessions found in mock mode
MockNumberOfSessions = "numberOfSession"
// MockNumberOfNamespaceDevices controls the number of NVMe Namespace Devices found in mock mode
MockNumberOfNamespaceDevices = "numberOfNamespaceDevices"
)
// GONVMEMock is a struct controlling induced errors
var GONVMEMock struct {
InduceDiscoveryError bool
InduceInitiatorError bool
InduceTCPLoginError bool
InduceFCLoginError bool
InduceLogoutError bool
InduceGetSessionsError bool
InducedNVMeDeviceAndNamespaceError bool
InducedNVMeNamespaceIDError bool
InducedNVMeDeviceDataError bool
}
// MockNVMe provides a mock implementation of an NVMe client
type MockNVMe struct {
NVMeType
}
// NewMockNVMe - returns a mock NVMe client
func NewMockNVMe(opts map[string]string) *MockNVMe {
nvme := MockNVMe{
NVMeType: NVMeType{
mock: true,
options: opts,
},
}
return &nvme
}
func getOptionAsInt(opts map[string]string, key string) int64 {
v, _ := strconv.ParseInt(opts[key], 10, 64)
return v
}
func (nvme *MockNVMe) discoverNVMeTCPTargets(address string, _ bool) ([]NVMeTarget, error) {
if GONVMEMock.InduceDiscoveryError {
return []NVMeTarget{}, errors.New("discoverTargets induced error")
}
mockedTargets := make([]NVMeTarget, 0)
count := getOptionAsInt(nvme.options, MockNumberOfTCPTargets)
if count == 0 {
count = 1
}
for idx := 0; idx < int(count); idx++ {
tgt := fmt.Sprintf("%05d", idx)
mockedTargets = append(mockedTargets,
NVMeTarget{
Portal: address,
TargetNqn: "nqn.1988-11.com.dell.mock:e6e2d5b871f1403E169D" + tgt,
TrType: "tcp",
AdrFam: "ipv4",
SubType: "nvme subsystem",
Treq: "not specified",
PortID: "0",
TrsvcID: "none",
SecType: "none",
TargetType: "tcp",
})
}
// send back a slice of targets
return mockedTargets, nil
}
func (nvme *MockNVMe) discoverNVMeFCTargets(address string, _ bool) ([]NVMeTarget, error) {
if GONVMEMock.InduceDiscoveryError {
return []NVMeTarget{}, errors.New("discoverTargets induced error")
}
mockedTargets := make([]NVMeTarget, 0)
count := getOptionAsInt(nvme.options, MockNumberOfFCTargets)
if count == 0 {
count = 1
}
for idx := 0; idx < int(count); idx++ {
tgt := fmt.Sprintf("%05d", idx)
mockedTargets = append(mockedTargets,
NVMeTarget{
Portal: address,
TargetNqn: "nqn.1988-11.com.dell.mock:e6e2d5b871f1403E169D" + tgt,
TrType: "fc",
AdrFam: "fibre-channel",
SubType: "nvme subsystem",
Treq: "not specified",
PortID: "0",
TrsvcID: "none",
SecType: "none",
TargetType: "fc",
HostAdr: "nn-0x58aaa11111111a11:pn-0x58aaa11111111a11",
})
}
// send back a slice of targets
return mockedTargets, nil
}
func (nvme *MockNVMe) getInitiators(_ string) ([]string, error) {
if GONVMEMock.InduceInitiatorError {
return []string{}, errors.New("getInitiators induced error")
}
mockedInitiators := make([]string, 0)
count := getOptionAsInt(nvme.options, MockNumberOfInitiators)
if count == 0 {
count = 1
}
for idx := 0; idx < int(count); idx++ {
init := fmt.Sprintf("%05d", idx)
mockedInitiators = append(mockedInitiators,
"nqn.1988-11.com.dell.mock:01:00000000"+init)
}
return mockedInitiators, nil
}
func (nvme *MockNVMe) nvmeTCPConnect(_ NVMeTarget, _ bool) error {
if GONVMEMock.InduceTCPLoginError {
return errors.New("NVMeTCP Login induced error")
}
return nil
}
func (nvme *MockNVMe) nvmeFCConnect(_ NVMeTarget, _ bool) error {
if GONVMEMock.InduceFCLoginError {
return errors.New("NVMeFC Login induced error")
}
return nil
}
func (nvme *MockNVMe) nvmeDisconnect(_ NVMeTarget) error {
if GONVMEMock.InduceLogoutError {
return errors.New("NVMe Logout induced error")
}
return nil
}
// GetNVMeDeviceData returns the information (nguid and namespace) of an NVME device path
func (nvme *MockNVMe) GetNVMeDeviceData(_ string) (string, string, error) {
if GONVMEMock.InducedNVMeDeviceDataError {
return "", "", errors.New("NVMe Namespace Data Induced Error")
}
nguid := "1a111a1111aa11111aaa1111111111a1"
namespace := "11"
return nguid, namespace, nil
}
// ListNVMeNamespaceID returns the namespace IDs for each NVME device path
func (nvme *MockNVMe) ListNVMeNamespaceID(_ []DevicePathAndNamespace) (map[DevicePathAndNamespace][]string, error) {
if GONVMEMock.InducedNVMeNamespaceIDError {
return map[DevicePathAndNamespace][]string{}, errors.New("listNamespaceID induced error")
}
mockedNamespaceIDs := make(map[DevicePathAndNamespace][]string)
count := getOptionAsInt(nvme.options, MockNumberOfNamespaceDevices)
if count == 0 {
count = 1
}
for idx := 0; idx < int(count); idx++ {
init := fmt.Sprintf("%05d", idx)
var currentPathAndNamespace DevicePathAndNamespace
var namespaceIDs []string
currentPathAndNamespace.DevicePath = "/dev/nvme0n" + init
currentPathAndNamespace.Namespace = init
namespaceIDs = append(namespaceIDs, "0x"+init)
mockedNamespaceIDs[currentPathAndNamespace] = namespaceIDs
}
return mockedNamespaceIDs, nil
}
// ListNVMeDeviceAndNamespace returns the Device Paths and Namespace of each NVMe device and each output content
func (nvme *MockNVMe) ListNVMeDeviceAndNamespace() ([]DevicePathAndNamespace, error) {
if GONVMEMock.InducedNVMeDeviceAndNamespaceError {
return []DevicePathAndNamespace{}, errors.New("listNamespaceDevices induced error")
}
var mockedDeviceAndNamespaces []DevicePathAndNamespace
count := getOptionAsInt(nvme.options, MockNumberOfNamespaceDevices)
if count == 0 {
count = 1
}
for idx := 0; idx < int(count); idx++ {
init := fmt.Sprintf("%05d", idx)
var currentPathAndNamespace DevicePathAndNamespace
currentPathAndNamespace.DevicePath = "/dev/nvme0n" + init
currentPathAndNamespace.Namespace = init
mockedDeviceAndNamespaces = append(mockedDeviceAndNamespaces, currentPathAndNamespace)
}
return mockedDeviceAndNamespaces, nil
}
func (nvme *MockNVMe) getSessions() ([]NVMESession, error) {
if GONVMEMock.InduceGetSessionsError {
return []NVMESession{}, errors.New("getSessions induced error")
}
var sessions []NVMESession
count := getOptionAsInt(nvme.options, MockNumberOfSessions)
if count == 0 {
count = 1
}
for idx := 0; idx < int(count); idx++ {
init := fmt.Sprintf("%0d", idx)
session := NVMESession{}
session.Target = fmt.Sprintf("nqn.1988-11.com.dell.mock:00:e6e2d5b871f1403E169D%d", idx)
session.Portal = fmt.Sprintf("192.168.1.%d", idx)
session.Name = "nvme" + init
session.NVMESessionState = NVMESessionStateLive
session.NVMETransportName = NVMETransportNameTCP
sessions = append(sessions, session)
}
return sessions, nil
}
// ====================================================================
// Architecture agnostic code for the mock implementation
// DiscoverNVMeTCPTargets runs an NVMe discovery and returns a list of targets.
func (nvme *MockNVMe) DiscoverNVMeTCPTargets(address string, login bool) ([]NVMeTarget, error) {
return nvme.discoverNVMeTCPTargets(address, login)
}
// DiscoverNVMeFCTargets runs an NVMe discovery and returns a list of targets.
func (nvme *MockNVMe) DiscoverNVMeFCTargets(address string, login bool) ([]NVMeTarget, error) {
return nvme.discoverNVMeFCTargets(address, login)
}
// GetInitiators returns a list of NVMe initiators on the local system.
func (nvme *MockNVMe) GetInitiators(filename string) ([]string, error) {
return nvme.getInitiators(filename)
}
// NVMeTCPConnect will attempt to log into an NVMe target
func (nvme *MockNVMe) NVMeTCPConnect(target NVMeTarget, duplicateConnect bool) error {
return nvme.nvmeTCPConnect(target, duplicateConnect)
}
// NVMeFCConnect will attempt to log into an NVMe target
func (nvme *MockNVMe) NVMeFCConnect(target NVMeTarget, duplicateConnect bool) error {
return nvme.nvmeFCConnect(target, duplicateConnect)
}
// NVMeDisconnect will attempt to log out of an NVMe target
func (nvme *MockNVMe) NVMeDisconnect(target NVMeTarget) error {
return nvme.nvmeDisconnect(target)
}
// GetSessions Queries NVMe session info
func (nvme *MockNVMe) GetSessions() ([]NVMESession, error) {
return nvme.getSessions()
}
// DeviceRescan rescan the NVMe device
func (nvme *MockNVMe) DeviceRescan(device string) error {
return nvme.deviceRescan(device)
}
func (nvme *MockNVMe) deviceRescan(_ string) error {
if GONVMEMock.InduceGetSessionsError {
return errors.New("deviceRescan induced error")
}
return nil
}