Skip to content

Commit eccf79a

Browse files
committed
nvmeof: add unit test for new functions
added TestSetupListeners and TestSetFromParameters. Signed-off-by: gadi-didi <gadi.didi@ibm.com>
1 parent 91fe557 commit eccf79a

1 file changed

Lines changed: 168 additions & 0 deletions

File tree

internal/nvmeof/volume_test.go

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,171 @@ func TestSetListenersWithDefaults(t *testing.T) {
5050
require.Equal(t, test.out, vol.ListenerInfo)
5151
}
5252
}
53+
54+
func TestSetupListeners(t *testing.T) {
55+
t.Parallel()
56+
tests := []struct {
57+
name string
58+
jsonInput string
59+
expected []ListenerDetails
60+
expectError bool
61+
}{
62+
{
63+
name: "valid listeners JSON",
64+
jsonInput: `[{"hostname": "nvmeof-gw-a", "port": 4420, "address": "10.92.3.12"}]`,
65+
expected: []ListenerDetails{
66+
{Hostname: "nvmeof-gw-a", GatewayAddress: GatewayAddress{Port: 4420, Address: "10.92.3.12"}},
67+
},
68+
expectError: false,
69+
},
70+
{
71+
name: "invalid listeners JSON",
72+
jsonInput: `invalid json`,
73+
expected: nil,
74+
expectError: true,
75+
},
76+
{
77+
name: "missing required fields",
78+
jsonInput: `[{"port": 4420, "address": "10.92.3.12"}]`,
79+
expected: nil,
80+
expectError: true,
81+
},
82+
// An empty listeners array is invalid because at least one listener is required.
83+
// the listener label can be omitted, then network mask label must be provided.
84+
{
85+
name: "empty listeners array",
86+
jsonInput: `[]`,
87+
expected: nil,
88+
expectError: true,
89+
},
90+
{
91+
name: "no listeners entry",
92+
jsonInput: ``,
93+
expected: []ListenerDetails{},
94+
expectError: false,
95+
},
96+
}
97+
98+
for _, test := range tests {
99+
listeners, err := SetupListeners(test.jsonInput)
100+
if test.expectError {
101+
require.Error(t, err)
102+
} else {
103+
require.NoError(t, err)
104+
require.Equal(t, test.expected, listeners)
105+
}
106+
}
107+
}
108+
109+
func TestSetFromParameters(t *testing.T) {
110+
t.Parallel()
111+
tests := []struct {
112+
name string
113+
params map[string]string
114+
expected NVMeoFVolumeData
115+
expectError bool
116+
}{
117+
{
118+
name: "valid parameters with listeners and gateway info",
119+
params: map[string]string{
120+
"subsystemNQN": "nqn.2016-06.io.ceph:subsystem.test-integration",
121+
"nvmeofGatewayAddress": "10.241.1.5",
122+
"nvmeofGatewayPort": "5500",
123+
"listeners": `[{"hostname": "nvmeof-gw-a", "port": 4420, "address": "10.92.3.12"}]`,
124+
},
125+
expected: NVMeoFVolumeData{
126+
SubsystemNQN: "nqn.2016-06.io.ceph:subsystem.test-integration",
127+
GatewayManagementInfo: GatewayConfig{
128+
Address: "10.241.1.5",
129+
Port: 5500,
130+
},
131+
ListenerInfo: []ListenerDetails{
132+
{Hostname: "nvmeof-gw-a", GatewayAddress: GatewayAddress{Port: 4420, Address: "10.92.3.12"}},
133+
},
134+
},
135+
expectError: false,
136+
},
137+
{
138+
name: "valid parameters with DH-CHAP authentication",
139+
params: map[string]string{
140+
"subsystemNQN": "nqn.2016-06.io.ceph:subsystem.test-dhchap",
141+
"nvmeofGatewayAddress": "10.241.1.6",
142+
"nvmeofGatewayPort": "5500",
143+
"dhchapMode": "bidirectional",
144+
"authenticationKMSID": "vault-kms",
145+
"listeners": `[{"hostname": "nvmeof-gw-b", "port": 4420, "address": "10.92.3.13"}]`,
146+
},
147+
expected: NVMeoFVolumeData{
148+
SubsystemNQN: "nqn.2016-06.io.ceph:subsystem.test-dhchap",
149+
GatewayManagementInfo: GatewayConfig{
150+
Address: "10.241.1.6",
151+
Port: 5500,
152+
},
153+
Security: NVMeoFSecurityConfig{
154+
DhchapMode: "bidirectional",
155+
AuthenticationKMSID: "vault-kms",
156+
},
157+
ListenerInfo: []ListenerDetails{
158+
{Hostname: "nvmeof-gw-b", GatewayAddress: GatewayAddress{Port: 4420, Address: "10.92.3.13"}},
159+
},
160+
},
161+
expectError: false,
162+
},
163+
{
164+
name: "valid parameters with default KMS type",
165+
params: map[string]string{
166+
"subsystemNQN": "nqn.2016-06.io.ceph:subsystem.test-dhchap-default-kms",
167+
"nvmeofGatewayAddress": "10.241.1.6",
168+
"nvmeofGatewayPort": "5500",
169+
"dhchapMode": "bidirectional",
170+
"listeners": `[{"hostname": "nvmeof-gw-b", "port": 4420, "address": "10.92.3.13"}]`,
171+
},
172+
expected: NVMeoFVolumeData{
173+
SubsystemNQN: "nqn.2016-06.io.ceph:subsystem.test-dhchap-default-kms",
174+
GatewayManagementInfo: GatewayConfig{
175+
Address: "10.241.1.6",
176+
Port: 5500,
177+
},
178+
Security: NVMeoFSecurityConfig{
179+
DhchapMode: "bidirectional",
180+
AuthenticationKMSID: "metadata",
181+
},
182+
ListenerInfo: []ListenerDetails{
183+
{Hostname: "nvmeof-gw-b", GatewayAddress: GatewayAddress{Port: 4420, Address: "10.92.3.13"}},
184+
},
185+
},
186+
expectError: false,
187+
},
188+
{
189+
name: "invalid gateway port",
190+
params: map[string]string{
191+
"subsystemNQN": "nqn.2016-06.io.ceph:subsystem.test-invalid-port",
192+
"nvmeofGatewayAddress": "10.241.1.7",
193+
"nvmeofGatewayPort": "invalid-port",
194+
},
195+
expected: NVMeoFVolumeData{},
196+
expectError: true,
197+
},
198+
{
199+
name: "invalid listeners JSON",
200+
params: map[string]string{
201+
"subsystemNQN": "nqn.2016-06.io.ceph:subsystem.test-invalid-listeners",
202+
"nvmeofGatewayAddress": "10.241.1.8",
203+
"nvmeofGatewayPort": "5500",
204+
"listeners": `invalid-json`,
205+
},
206+
expected: NVMeoFVolumeData{},
207+
expectError: true,
208+
},
209+
}
210+
for _, test := range tests {
211+
vol := &NVMeoFVolumeData{}
212+
err := vol.SetFromParameters(test.params)
213+
if test.expectError {
214+
require.Error(t, err)
215+
} else {
216+
require.NoError(t, err)
217+
require.Equal(t, test.expected, *vol)
218+
}
219+
}
220+
}

0 commit comments

Comments
 (0)