forked from RHsyseng/operator-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
platform_versioner_test.go
224 lines (209 loc) · 5.31 KB
/
platform_versioner_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
package platform
import (
"testing"
openapi_v2 "github.com/googleapis/gnostic/openapiv2"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/version"
"k8s.io/client-go/rest"
)
type FakeDiscoverer struct {
info PlatformInfo
serverInfo *version.Info
groupList *v1.APIGroupList
doc *openapi_v2.Document
client rest.Interface
ServerVersionError error
ServerGroupsError error
OpenAPISchemaError error
}
func (d FakeDiscoverer) ServerVersion() (*version.Info, error) {
if d.ServerVersionError != nil {
return nil, d.ServerVersionError
}
return d.serverInfo, nil
}
func (d FakeDiscoverer) ServerGroups() (*v1.APIGroupList, error) {
if d.ServerGroupsError != nil {
return nil, d.ServerGroupsError
}
return d.groupList, nil
}
func (d FakeDiscoverer) OpenAPISchema() (*openapi_v2.Document, error) {
if d.OpenAPISchemaError != nil {
return nil, d.OpenAPISchemaError
}
return d.doc, nil
}
func (d FakeDiscoverer) RESTClient() rest.Interface {
return d.client
}
type FakePlatformVersioner struct {
Info PlatformInfo
Err error
}
func (pv FakePlatformVersioner) GetPlatformInfo(d Discoverer, cfg *rest.Config) (PlatformInfo, error) {
if pv.Err != nil {
return pv.Info, pv.Err
}
return pv.Info, nil
}
func TestK8SBasedPlatformVersioner_GetPlatformInfo(t *testing.T) {
pv := K8SBasedPlatformVersioner{}
fakeErr := errors.New("uh oh")
cases := []struct {
label string
discoverer Discoverer
config *rest.Config
expectedInfo PlatformInfo
expectedErr bool
}{
{
label: "case 1", // trigger error in client.ServerVersion(), only Name present on Info
discoverer: FakeDiscoverer{
ServerVersionError: fakeErr,
},
config: &rest.Config{},
expectedInfo: PlatformInfo{Name: Kubernetes},
expectedErr: true,
},
{
label: "case 2", // trigger error in client.ServerGroups(), K8S major/minor now present
discoverer: FakeDiscoverer{
ServerGroupsError: fakeErr,
serverInfo: &version.Info{
Major: "1",
Minor: "2",
},
},
config: &rest.Config{},
expectedInfo: PlatformInfo{Name: Kubernetes, K8SVersion: "1.2"},
expectedErr: true,
},
{
label: "case 3", // trigger no errors, simulate K8S platform (no OCP route present)
discoverer: FakeDiscoverer{
serverInfo: &version.Info{
Major: "1",
Minor: "2",
},
groupList: &v1.APIGroupList{
TypeMeta: v1.TypeMeta{},
Groups: []v1.APIGroup{},
},
},
config: &rest.Config{},
expectedInfo: PlatformInfo{Name: Kubernetes, K8SVersion: "1.2"},
expectedErr: false,
},
{
label: "case 4", // trigger no errors, simulate OCP route present
discoverer: FakeDiscoverer{
serverInfo: &version.Info{
Major: "1",
Minor: "2",
},
groupList: &v1.APIGroupList{
TypeMeta: v1.TypeMeta{},
Groups: []v1.APIGroup{{Name: "route.openshift.io"}},
},
},
config: &rest.Config{},
expectedInfo: PlatformInfo{Name: OpenShift, K8SVersion: "1.2"},
expectedErr: false,
},
}
for _, c := range cases {
info, err := pv.GetPlatformInfo(c.discoverer, c.config)
assert.Equal(t, c.expectedInfo, info, c.label+": mismatch in returned PlatformInfo")
if c.expectedErr {
assert.Error(t, err, c.label+": expected error, but none occurred")
}
}
}
func TestClientCallVersionComparsion(t *testing.T) {
pv := K8SBasedPlatformVersioner{}
testcases := []struct {
label string
discoverer Discoverer
config *rest.Config
expectedInfo int
expectedErr bool
}{
{
label: "case 1",
discoverer: FakeDiscoverer{
serverInfo: &version.Info{
Major: "1",
Minor: "19",
},
groupList: &v1.APIGroupList{
TypeMeta: v1.TypeMeta{},
Groups: []v1.APIGroup{{Name: "route.openshift.io"}},
},
},
config: &rest.Config{},
expectedInfo: 0,
expectedErr: false,
},
{
label: "case 2",
discoverer: FakeDiscoverer{
serverInfo: &version.Info{
Major: "1",
Minor: "19+",
},
groupList: &v1.APIGroupList{
TypeMeta: v1.TypeMeta{},
Groups: []v1.APIGroup{{Name: "route.openshift.io"}},
},
},
config: &rest.Config{},
expectedInfo: 0,
expectedErr: false,
},
{
label: "case 3",
discoverer: FakeDiscoverer{
serverInfo: &version.Info{
Major: "1",
Minor: "14+",
},
groupList: &v1.APIGroupList{
TypeMeta: v1.TypeMeta{},
Groups: []v1.APIGroup{{Name: "route.openshift.io"}},
},
},
config: &rest.Config{},
expectedInfo: -1,
expectedErr: false,
},
{
label: "case 4",
discoverer: FakeDiscoverer{
serverInfo: &version.Info{
Major: "1",
Minor: "14not",
},
groupList: &v1.APIGroupList{
TypeMeta: v1.TypeMeta{},
Groups: []v1.APIGroup{{Name: "route.openshift.io"}},
},
},
config: &rest.Config{},
expectedInfo: -1,
expectedErr: true,
},
}
versionToTest := "4.6"
for _, tc := range testcases {
res, err := pv.CompareOpenShiftVersion(tc.discoverer, tc.config, versionToTest)
if tc.expectedErr {
assert.Error(t, err, "expected error")
} else {
assert.NoError(t, err, "unexpected error")
}
assert.Equal(t, tc.expectedInfo, res, "The expected and actual versions should be the same.")
}
}