-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgoiscsi_mock.go
259 lines (222 loc) · 7.51 KB
/
goiscsi_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
/*
*
* Copyright © 2019-2022 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 goiscsi
import (
"errors"
"fmt"
"strconv"
)
const (
// MockNumberOfInitiators controls the number of initiators found in mock mode
MockNumberOfInitiators = "numberOfInitiators"
// MockNumberOfTargets controls the number of targets found in mock mode
MockNumberOfTargets = "numberOfTargets"
// MockNumberOfSessions controls the number of iSCIS sessions found in mock mode
MockNumberOfSessions = "numberOfSession"
// MockNumberOfNodes controls the number of iSCIS sessions found in mock mode
MockNumberOfNodes = "numberOfNode"
)
// GOISCSIMock is a struct controlling induced errors
var GOISCSIMock struct {
InduceDiscoveryError bool
InduceInitiatorError bool
InduceLoginError bool
InduceLogoutError bool
InduceRescanError bool
InduceGetSessionsError bool
InduceGetNodesError bool
InduceCreateOrUpdateNodeError bool
InduceDeleteNodeError bool
InduceSetCHAPError bool
}
// MockISCSI provides a mock implementation of an iscsi client
type MockISCSI struct {
ISCSIType
}
// NewMockISCSI returns an mock ISCSI client
func NewMockISCSI(opts map[string]string) *MockISCSI {
iscsi := MockISCSI{
ISCSIType: ISCSIType{
mock: true,
options: opts,
},
}
return &iscsi
}
func getOptionAsInt(opts map[string]string, key string) int64 {
v, _ := strconv.ParseInt(opts[key], 10, 64)
return v
}
func (iscsi *MockISCSI) discoverTargets(address string, _ bool) ([]ISCSITarget, error) {
if GOISCSIMock.InduceDiscoveryError {
return []ISCSITarget{}, errors.New("discoverTargets induced error")
}
mockedTargets := make([]ISCSITarget, 0)
count := getOptionAsInt(iscsi.options, MockNumberOfTargets)
if count == 0 {
count = 1
}
for idx := 0; idx < int(count); idx++ {
tgt := fmt.Sprintf("%05d", idx)
mockedTargets = append(mockedTargets,
ISCSITarget{
Portal: address + ":3260",
GroupTag: "0",
Target: "iqn.1992-04.com.mock:600009700bcbb70e32870174000" + tgt,
})
}
// send back a slice of targets
return mockedTargets, nil
}
func (iscsi *MockISCSI) getInitiators(_ string) ([]string, error) {
if GOISCSIMock.InduceInitiatorError {
return []string{}, errors.New("getInitiators induced error")
}
mockedInitiators := make([]string, 0)
count := getOptionAsInt(iscsi.options, MockNumberOfInitiators)
if count == 0 {
count = 1
}
for idx := 0; idx < int(count); idx++ {
init := fmt.Sprintf("%05d", idx)
mockedInitiators = append(mockedInitiators,
"iqn.1993-08.com.mock:01:00000000"+init)
}
return mockedInitiators, nil
}
func (iscsi *MockISCSI) performLogin(_ ISCSITarget) error {
if GOISCSIMock.InduceLoginError {
return errors.New("iSCSI Login induced error")
}
return nil
}
func (iscsi *MockISCSI) performLogout(_ ISCSITarget) error {
if GOISCSIMock.InduceLogoutError {
return errors.New("iSCSI Logout induced error")
}
return nil
}
func (iscsi *MockISCSI) performRescan() error {
if GOISCSIMock.InduceRescanError {
return errors.New("iSCSI Rescan induced error")
}
return nil
}
func (iscsi *MockISCSI) getSessions() ([]ISCSISession, error) {
if GOISCSIMock.InduceGetSessionsError {
return []ISCSISession{}, errors.New("getSessions induced error")
}
var sessions []ISCSISession
count := getOptionAsInt(iscsi.options, MockNumberOfSessions)
if count == 0 {
count = 1
}
for idx := 0; idx < int(count); idx++ {
init := fmt.Sprintf("%05d", idx)
session := ISCSISession{}
session.Target = fmt.Sprintf("iqn.2015-10.com.dell:dellemc-foobar-123-a-7ceb34a%d", idx)
session.Portal = fmt.Sprintf("192.168.1.%d", idx)
session.IfaceInitiatorname = "iqn.1993-08.com.mock:01:00000000" + init
session.IfaceTransport = ISCSITransportNameTCP
session.ISCSIConnectionState = ISCSIConnectionStateINLOGIN
session.ISCSISessionState = ISCSISessionStateLOGGEDIN
session.IfaceIPaddress = "192.168.1.10"
sessions = append(sessions, session)
}
return sessions, nil
}
func (iscsi *MockISCSI) getNodes() ([]ISCSINode, error) {
if GOISCSIMock.InduceGetNodesError {
return []ISCSINode{}, errors.New("getSessions induced error")
}
var nodes []ISCSINode
count := getOptionAsInt(iscsi.options, MockNumberOfNodes)
if count == 0 {
count = 1
}
for idx := 0; idx < int(count); idx++ {
node := ISCSINode{}
node.Target = fmt.Sprintf("iqn.2015-10.com.dell:dellemc-foobar-123-a-7ceb34a%d", idx)
node.Portal = fmt.Sprintf("192.168.1.%d", idx)
node.Fields = make(map[string]string)
node.Fields["node.session.scan"] = "auto"
nodes = append(nodes, node)
}
return nodes, nil
}
func (iscsi *MockISCSI) newNode(_ ISCSITarget, _ map[string]string) error {
if GOISCSIMock.InduceCreateOrUpdateNodeError {
return errors.New("newNode induced error")
}
if GOISCSIMock.InduceSetCHAPError {
return errors.New("set CHAP induced error")
}
return nil
}
func (iscsi *MockISCSI) deleteNode(_ ISCSITarget) error {
if GOISCSIMock.InduceDeleteNodeError {
return errors.New("newNode induced error")
}
return nil
}
// ====================================================================
// Architecture agnostic code for the mock implementation
// DiscoverTargets runs an iSCSI discovery and returns a list of targets.
func (iscsi *MockISCSI) DiscoverTargets(address string, login bool) ([]ISCSITarget, error) {
return iscsi.discoverTargets(address, login)
}
// GetInitiators returns a list of initiators on the local system.
func (iscsi *MockISCSI) GetInitiators(filename string) ([]string, error) {
return iscsi.getInitiators(filename)
}
// PerformLogin will attempt to log into an iSCSI target
func (iscsi *MockISCSI) PerformLogin(target ISCSITarget) error {
return iscsi.performLogin(target)
}
// PerformLogout will attempt to log out of an iSCSI target
func (iscsi *MockISCSI) PerformLogout(target ISCSITarget) error {
return iscsi.performLogout(target)
}
// PerformRescan will will rescan targets known to current sessions
func (iscsi *MockISCSI) PerformRescan() error {
return iscsi.performRescan()
}
// GetSessions will query iSCSI session info
func (iscsi *MockISCSI) GetSessions() ([]ISCSISession, error) {
return iscsi.getSessions()
}
// GetNodes will query iSCSI session info
func (iscsi *MockISCSI) GetNodes() ([]ISCSINode, error) {
return iscsi.getNodes()
}
// CreateOrUpdateNode creates new or update existing iSCSI node in iscsid database
func (iscsi *MockISCSI) CreateOrUpdateNode(target ISCSITarget, options map[string]string) error {
return iscsi.newNode(target, options)
}
// DeleteNode delete iSCSI node from iscsid database
func (iscsi *MockISCSI) DeleteNode(target ISCSITarget) error {
return iscsi.deleteNode(target)
}
// SetCHAPCredentials will set CHAP credentials
func (iscsi *MockISCSI) SetCHAPCredentials(target ISCSITarget, username, password string) error {
options := make(map[string]string)
options["node.session.auth.authmethod"] = "CHAP"
options["node.session.auth.username"] = username
options["node.session.auth.password"] = password
return iscsi.newNode(target, options)
}