-
Notifications
You must be signed in to change notification settings - Fork 13
/
volume_test.go
135 lines (125 loc) · 4.05 KB
/
volume_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
// 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"
"math"
"net/http"
"net/http/httptest"
"testing"
types "github.com/dell/goscaleio/types/v1"
)
func Test_GetVolumeStatistics(t *testing.T) {
type checkFn func(*testing.T, error)
check := func(fns ...checkFn) []checkFn { return fns }
hasNoError := func(t *testing.T, err error) {
if err != nil {
t.Fatalf("expected no error")
}
}
hasError := func(t *testing.T, err error) {
if err == nil {
t.Fatalf("expected error")
}
}
tests := map[string]func(t *testing.T) (*httptest.Server, *types.Volume, []checkFn){
"success": func(t *testing.T) (*httptest.Server, *types.Volume, []checkFn) {
VolumeID := "000001111a2222b"
href := fmt.Sprintf("/api/instances/Volume::%s/relationships/Statistics", VolumeID)
vol := types.Volume{
ID: VolumeID,
Links: []*types.Link{
{
Rel: "/api/Volume/relationship/Statistics",
HREF: fmt.Sprintf("/api/instances/Volume::%s/relationships/Statistics", VolumeID),
},
},
}
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))
}
volumeStats := types.VolumeStatistics{}
respData, err := json.Marshal(volumeStats)
if err != nil {
t.Fatal(err)
}
fmt.Fprintln(w, string(respData))
}))
return ts, &vol, check(hasNoError)
},
"error from getJSONWithRetry": func(t *testing.T) (*httptest.Server, *types.Volume, []checkFn) {
VolumeID := "000001111a2222b"
href := fmt.Sprintf("/api/instances/Volume::%s/relationships/Statistics", VolumeID)
vol := types.Volume{
ID: VolumeID,
Links: []*types.Link{
{
Rel: "/api/Volume/relationship/Statistics",
HREF: fmt.Sprintf("/api/instances/Volume::%s/relationships/Statistics", VolumeID),
},
},
}
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, &vol, check(hasError)
},
"error from GetLink": func(t *testing.T) (*httptest.Server, *types.Volume, []checkFn) {
VolumeID := "000001111a2222b"
vol := types.Volume{
ID: VolumeID,
Links: []*types.Link{
{
Rel: "noLink error",
HREF: fmt.Sprintf("/api/instances/Volume::%s/relationships/Statistics", VolumeID),
},
},
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
volumeStats := types.VolumeStatistics{}
respData, err := json.Marshal(volumeStats)
if err != nil {
t.Fatal(err)
}
fmt.Fprintln(w, string(respData))
}))
return ts, &vol, check(hasError)
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
ts, vol, checkFns := tc(t)
defer ts.Close()
client, err := NewClientWithArgs(ts.URL, "", math.MaxInt64, true, false)
if err != nil {
t.Fatal(err)
}
volClient := NewVolume(client)
volClient.Volume = vol
_, err = volClient.GetVolumeStatistics()
for _, checkFn := range checkFns {
checkFn(t, err)
}
})
}
}