forked from dell/goscaleio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdc_test.go
119 lines (108 loc) · 3.41 KB
/
sdc_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
// Copyright © 2020 - 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 goscaleio
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
types "github.com/dell/goscaleio/types/v1"
"github.com/stretchr/testify/assert"
)
func Test_FindVolumes(t *testing.T) {
type checkFn func(*testing.T, []*Volume, error)
check := func(fns ...checkFn) []checkFn { return fns }
hasNoError := func(t *testing.T, vols []*Volume, err error) {
if err != nil {
t.Fatalf("expected no error")
}
}
checkLength := func(length int) func(t *testing.T, vols []*Volume, err error) {
return func(t *testing.T, vols []*Volume, err error) {
assert.Equal(t, length, len(vols))
}
}
hasError := func(t *testing.T, vols []*Volume, err error) {
if err == nil {
t.Fatalf("expected error")
}
}
tests := map[string]func(t *testing.T) (*httptest.Server, types.Sdc, []checkFn){
"success": func(t *testing.T) (*httptest.Server, types.Sdc, []checkFn) {
sdcID := "000001111a2222b"
href := fmt.Sprintf("/api/instances/Sdc::%s/relationships/Volume", sdcID)
sdc := types.Sdc{
ID: sdcID,
Links: []*types.Link{
{
Rel: "/api/Sdc/relationship/Volume",
HREF: href,
},
},
}
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))
}
vols := []types.Volume{{}, {}, {}}
respData, err := json.Marshal(vols)
if err != nil {
t.Fatal(err)
}
fmt.Fprintln(w, string(respData))
}))
return ts, sdc, check(hasNoError, checkLength(3))
},
"error from GetVolume": func(t *testing.T) (*httptest.Server, types.Sdc, []checkFn) {
sdcID := "someID"
href := fmt.Sprintf("/api/instances/Sdc::%s/relationships/Volume", sdcID)
sdc := types.Sdc{
ID: sdcID,
Links: []*types.Link{
{
Rel: "/api/Sdc/relationship/Volume",
HREF: href,
},
},
}
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.NotFound(w, r)
}))
return ts, sdc, check(hasError)
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
ts, sdc, checkFns := tc(t)
defer ts.Close()
client, err := NewClientWithArgs(ts.URL, "", true, false)
if err != nil {
t.Fatal(err)
}
sdcClient := NewSdc(client, &sdc)
vols, err := sdcClient.FindVolumes()
for _, checkFn := range checkFns {
checkFn(t, vols, err)
}
})
}
}