-
Notifications
You must be signed in to change notification settings - Fork 13
/
nfs_export_test.go
333 lines (279 loc) · 9.03 KB
/
nfs_export_test.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
*
* Copyright © 2020-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 goscaleio
import (
"encoding/json"
"fmt"
"math"
"net/http"
"net/http/httptest"
"testing"
types "github.com/dell/goscaleio/types/v1"
"github.com/stretchr/testify/assert"
)
func TestGetNFSExportByIDName(t *testing.T) {
type checkFn func(*testing.T, *types.NFSExport, error)
check := func(fns ...checkFn) []checkFn { return fns }
hasNoError := func(t *testing.T, _ *types.NFSExport, err error) {
if err != nil {
t.Fatalf("expected no error")
}
}
hasError := func(t *testing.T, _ *types.NFSExport, err error) {
if err == nil {
t.Fatalf("expected error")
}
}
checkRespName := func(nfsName string) func(t *testing.T, resp *types.NFSExport, err error) {
return func(t *testing.T, resp *types.NFSExport, _ error) {
assert.Equal(t, nfsName, resp.Name)
}
}
checkRespID := func(nfsID string) func(t *testing.T, resp *types.NFSExport, err error) {
return func(t *testing.T, resp *types.NFSExport, _ error) {
assert.Equal(t, nfsID, resp.ID)
}
}
testsName := map[string]func(t *testing.T) (*httptest.Server, []checkFn){
"success": func(t *testing.T) (*httptest.Server, []checkFn) {
href := "/rest/v1/nfs-exports"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
t.Fatal(fmt.Errorf("wrong method. Expected %s; but got %s", http.MethodGet, r.Method))
}
if r.URL.Path != href {
t.Fatal(fmt.Errorf("wrong path. Expected %s; but got %s", href, r.URL.Path))
}
resp := []types.NFSExport{
{
ID: "64366a19-54e8-1544-f3d7-2a50fb1ccff3",
Name: "nfs-test-1",
},
{
ID: "6436aa58-e6a1-a4e2-de7b-2a50fb1ccff3",
Name: "nfs-test-2",
},
}
respData, err := json.Marshal(resp)
if err != nil {
t.Fatal(err)
}
fmt.Fprintln(w, string(respData))
}))
return ts, check(hasNoError, checkRespName("nfs-test-2"))
},
"not found": func(t *testing.T) (*httptest.Server, []checkFn) {
href := "/rest/v1/nfs-exports"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
t.Fatal(fmt.Errorf("wrong method. Expected %s; but got %s", http.MethodGet, r.Method))
}
if r.URL.Path != href {
t.Fatal(fmt.Errorf("wrong path. Expected %s; but got %s", href, r.URL.Path))
}
resp := []types.NFSExport{
{
ID: "64366a19-54e8-1544-f3d7-2a50fb1ccff3",
Name: "nfs-test-1",
},
{
ID: "6436aa58-e6a1-a4e2-de7b-2a50fb1ccff3",
Name: "nfs-test-2",
},
}
respData, err := json.Marshal(resp)
if err != nil {
t.Fatal(err)
}
fmt.Fprintln(w, string(respData))
}))
return ts, check(hasError)
},
}
testsID := map[string]func(t *testing.T) (*httptest.Server, []checkFn){
"success": func(t *testing.T) (*httptest.Server, []checkFn) {
nfsID := "64242c06-7a78-1773-50f4-2a50fb1ccff3"
href := fmt.Sprintf("/rest/v1/nfs-exports/%s", nfsID)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
t.Fatal(fmt.Errorf("wrong method. Expected %s; but got %s", http.MethodGet, r.Method))
}
if r.URL.Path != href {
t.Fatal(fmt.Errorf("wrong path. Expected %s; but got %s", href, r.URL.Path))
}
resp := types.NFSExport{
ID: "64242c06-7a78-1773-50f4-2a50fb1ccff3",
Name: "nfs-test-1",
FileSystemID: "64242bfb-d188-e87b-c144-2a50fb1ccff3",
}
respData, err := json.Marshal(resp)
if err != nil {
t.Fatal(err)
}
fmt.Fprintln(w, string(respData))
}))
return ts, check(hasNoError, checkRespID("64242c06-7a78-1773-50f4-2a50fb1ccff3"))
},
"not found": func(t *testing.T) (*httptest.Server, []checkFn) {
nfsID := "6433a2b2-6d60-f737-9f3b-2a50fb1ccff3"
href := fmt.Sprintf("/rest/v1/nfs-exports/%s", nfsID)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
t.Fatal(fmt.Errorf("wrong method. Expected %s; but got %s", http.MethodGet, r.Method))
}
if r.URL.Path != href {
t.Fatal(fmt.Errorf("wrong path. Expected %s; but got %s", href, r.URL.Path))
}
http.Error(w, "nas not found", http.StatusNotFound)
}))
return ts, check(hasError)
},
}
testCaseFSNames := map[string]string{
"success": "nfs-test-2",
"not found": "nfs-test-3",
}
testCaseFSIds := map[string]string{
"success": "64242c06-7a78-1773-50f4-2a50fb1ccff3",
"not found": "6433a2b2-6d60-f737-9f3b-2a50fb1ccff3",
}
for name, tc := range testsName {
t.Run(name, func(t *testing.T) {
ts, checkFns := tc(t)
defer ts.Close()
client, err := NewClientWithArgs(ts.URL, "", math.MaxInt64, true, false)
client.configConnect.Version = "4.0"
if err != nil {
t.Fatal(err)
}
s := System{
client: client,
}
resp, err := s.client.GetNFSExportByIDName("", testCaseFSNames[name])
for _, checkFn := range checkFns {
checkFn(t, resp, err)
}
})
}
for id, tc := range testsID {
t.Run(id, func(t *testing.T) {
ts, checkFns := tc(t)
defer ts.Close()
client, err := NewClientWithArgs(ts.URL, "", math.MaxInt64, true, false)
client.configConnect.Version = "4.0"
if err != nil {
t.Fatal(err)
}
s := System{
client: client,
}
resp, err := s.client.GetNFSExportByIDName(testCaseFSIds[id], "")
for _, checkFn := range checkFns {
checkFn(t, resp, err)
}
})
}
}
func TestDeleteNFSExport(t *testing.T) {
id := "new-nas"
// mock a powerflex endpoint
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNoContent)
}))
defer svr.Close()
client, err := NewClientWithArgs(svr.URL, "", math.MaxInt64, true, false)
client.configConnect.Version = "4.0"
if err != nil {
t.Fatal(err)
}
err = client.DeleteNFSExport(id)
assert.Nil(t, err)
}
func TestCreateNFSExport(t *testing.T) {
type checkFn func(*testing.T, *types.NFSExportCreateResponse, error)
check := func(fns ...checkFn) []checkFn { return fns }
hasNoError := func(t *testing.T, _ *types.NFSExportCreateResponse, err error) {
if err != nil {
t.Fatalf("expected no error")
}
}
hasError := func(t *testing.T, _ *types.NFSExportCreateResponse, err error) {
if err == nil {
t.Fatalf("expected error")
}
}
checkResp := func(nfsId string) func(t *testing.T, resp *types.NFSExportCreateResponse, err error) {
return func(t *testing.T, resp *types.NFSExportCreateResponse, _ error) {
assert.Equal(t, nfsId, resp.ID)
}
}
tests := map[string]func(t *testing.T) (*httptest.Server, []checkFn){
"success": func(t *testing.T) (*httptest.Server, []checkFn) {
href := "/rest/v1/nfs-exports"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Fatal(fmt.Errorf("wrong method. Expected %s; but got %s", http.MethodGet, r.Method))
}
if r.URL.Path != href {
t.Fatal(fmt.Errorf("wrong path. Expected %s; but got %s", href, r.URL.Path))
}
resp := types.NFSExportCreateResponse{
ID: "64385158-97a1-bb86-4fd9-2a50fb1ccff3",
}
respData, err := json.Marshal(resp)
if err != nil {
t.Fatal(err)
}
fmt.Fprintln(w, string(respData))
}))
return ts, check(hasNoError, checkResp("64385158-97a1-bb86-4fd9-2a50fb1ccff3"))
},
"bad request": func(t *testing.T) (*httptest.Server, []checkFn) {
href := "/rest/v1/nfs-exports"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Fatal(fmt.Errorf("wrong method. Expected %s; but got %s", http.MethodGet, r.Method))
}
if r.URL.Path != href {
t.Fatal(fmt.Errorf("wrong path. Expected %s; but got %s", href, r.URL.Path))
}
http.Error(w, "bad Request", http.StatusBadRequest)
}))
return ts, check(hasError)
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
ts, checkFns := tc(t)
defer ts.Close()
client, err := NewClientWithArgs(ts.URL, "", math.MaxInt64, true, false)
client.configConnect.Version = "4.0"
if err != nil {
t.Fatal(err)
}
resp, err := client.CreateNFSExport(&types.NFSExportCreate{
Name: "twee-kk",
FileSystemID: "6436aa58-e6a1-a4e2-de7b-2a50fb1ccff3",
Path: "/twee-fs11",
})
for _, checkFn := range checkFns {
checkFn(t, resp, err)
}
})
}
}