-
Notifications
You must be signed in to change notification settings - Fork 13
/
lcm_test.go
149 lines (136 loc) · 4.41 KB
/
lcm_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
// Copyright © 2024 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 (
"math"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_CheckPfmpVersion(t *testing.T) {
type checkFn func(*testing.T, int, error)
check := func(fns ...checkFn) []checkFn { return fns }
hasNoError := func(t *testing.T, _ int, err error) {
if err != nil {
t.Fatalf("expected no error")
}
}
checkVersionEqual := func(t *testing.T, result int, _ error) {
assert.Equal(t, result, 0)
}
checkVersionGreaterThan := func(t *testing.T, result int, _ error) {
assert.Equal(t, result, 1)
}
checkVersionLessThan := func(t *testing.T, result int, _ error) {
assert.Equal(t, result, -1)
}
hasError := func(t *testing.T, _ int, err error) {
if err == nil {
t.Fatalf("expected error")
}
}
tests := map[string]func(t *testing.T) (*httptest.Server, string, []checkFn){
"PFMP version == 4.6": func(t *testing.T) (*httptest.Server, string, []checkFn) {
url := "/Api/V1/corelcm/status"
responseJSON := `{
"lcmStatus": "READY",
"clusterVersion": "4.6.0.0",
"clusterBuild": "1258"
}`
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet && strings.EqualFold(r.URL.Path, url) {
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(responseJSON))
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
return
}
http.NotFound(w, r)
}))
return server, "4.6", check(hasNoError, checkVersionEqual)
},
"PFMP version > 4.6": func(t *testing.T) (*httptest.Server, string, []checkFn) {
url := "/Api/V1/corelcm/status"
responseJSON := `{
"lcmStatus": "READY",
"clusterVersion": "4.7.0.0",
"clusterBuild": "1258"
}`
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet && strings.EqualFold(r.URL.Path, url) {
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(responseJSON))
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
return
}
http.NotFound(w, r)
}))
return server, "4.6.0.0", check(hasNoError, checkVersionGreaterThan)
},
"PFMP version < 4.6": func(t *testing.T) (*httptest.Server, string, []checkFn) {
url := "/Api/V1/corelcm/status"
responseJSON := `{
"lcmStatus": "READY",
"clusterVersion": "4.5.0.0",
"clusterBuild": "1258"
}`
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet && strings.EqualFold(r.URL.Path, url) {
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(responseJSON))
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
return
}
http.NotFound(w, r)
}))
return server, "4.6", check(hasNoError, checkVersionLessThan)
},
"wrong version": func(t *testing.T) (*httptest.Server, string, []checkFn) {
url := "/Api/V1/corelcm/status"
responseJSON := `{
"lcmStatus": "READY",
"clusterVersion": "4.5.0.0",
"clusterBuild": "1258"
}`
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet && strings.EqualFold(r.URL.Path, url) {
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(responseJSON))
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
return
}
http.NotFound(w, r)
}))
return server, "4.a.b.c", check(hasError)
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
server, version, checkFns := tc(t)
defer server.Close()
client, _ := NewClientWithArgs(server.URL, "", math.MaxInt64, true, false)
result, err := CheckPfmpVersion(client, version)
for _, checkFn := range checkFns {
checkFn(t, result, err)
}
})
}
}