-
Notifications
You must be signed in to change notification settings - Fork 129
/
framework.go
299 lines (256 loc) · 11 KB
/
framework.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
package framework
import (
"context"
"fmt"
"testing"
"time"
yaml "gopkg.in/yaml.v2"
appv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
configv1 "github.com/openshift/api/config/v1"
consolev1 "github.com/openshift/api/console/v1"
routev1 "github.com/openshift/api/route/v1"
consoleapi "github.com/openshift/console-operator/pkg/api"
)
var (
// AsyncOperationTimeout is how long we want to wait for asynchronous
// operations to complete. ForeverTestTimeout is not long enough to create
// several replicas and get them available on a slow machine.
// Setting this to 5 minutes:w
AsyncOperationTimeout = 5 * time.Minute
)
type TestFramework struct {
ctx context.Context
}
type TestingResource struct {
kind string
name string
namespace string
}
func getTestingResources() []TestingResource {
return []TestingResource{
{"ConfigMap", consoleapi.OpenShiftConsoleConfigMapName, consoleapi.OpenShiftConsoleNamespace},
{"ConsoleCLIDownloads", consoleapi.OCCLIDownloadsCustomResourceName, ""},
{"ConsoleCLIDownloads", consoleapi.ODOCLIDownloadsCustomResourceName, ""},
{"Deployment", consoleapi.OpenShiftConsoleDeploymentName, consoleapi.OpenShiftConsoleNamespace},
{"Deployment", consoleapi.OpenShiftConsoleDownloadsDeploymentName, consoleapi.OpenShiftConsoleNamespace},
{"Route", consoleapi.OpenShiftConsoleRouteName, consoleapi.OpenShiftConsoleNamespace},
{"Service", consoleapi.OpenShiftConsoleServiceName, consoleapi.OpenShiftConsoleNamespace},
{"PodDisruptionBudget", consoleapi.OpenShiftConsoleName, consoleapi.OpenShiftConsoleNamespace},
{"PodDisruptionBudget", consoleapi.DownloadsResourceName, consoleapi.OpenShiftConsoleNamespace},
}
}
func SetClusterProxyConfig(proxyConfig configv1.ProxySpec, client *ClientSet) error {
_, err := client.Proxy.Proxies().Patch(context.TODO(), consoleapi.ConfigResourceName, types.MergePatchType, []byte(fmt.Sprintf(`{"spec": {"httpProxy": "%s", "httpsProxy": "%s", "noProxy": "%s"}}`, proxyConfig.HTTPProxy, proxyConfig.HTTPSProxy, proxyConfig.NoProxy)), metav1.PatchOptions{})
return err
}
func ResetClusterProxyConfig(client *ClientSet) error {
_, err := client.Proxy.Proxies().Patch(context.TODO(), consoleapi.ConfigResourceName, types.MergePatchType, []byte(`{"spec": {"httpProxy": "", "httpsProxy": "", "noProxy": ""}}`), metav1.PatchOptions{})
return err
}
func DeleteAll(t *testing.T, client *ClientSet) {
resources := getTestingResources()
for _, resource := range resources {
t.Logf("deleting console's %s %s...", resource.name, resource.kind)
if err := DeleteCompletely(
func() (runtime.Object, error) {
return GetResource(client, resource)
},
func(*metav1.DeleteOptions) error {
return deleteResource(client, resource)
},
); err != nil {
t.Fatalf("unable to delete console's %s %s: %s", resource.name, resource.kind, err)
}
}
}
func GetResource(client *ClientSet, resource TestingResource) (runtime.Object, error) {
var res runtime.Object
var err error
switch resource.kind {
case "ConfigMap":
res, err = client.Core.ConfigMaps(resource.namespace).Get(context.TODO(), resource.name, metav1.GetOptions{})
case "Service":
res, err = client.Core.Services(resource.namespace).Get(context.TODO(), resource.name, metav1.GetOptions{})
case "Route":
res, err = client.Routes.Routes(resource.namespace).Get(context.TODO(), resource.name, metav1.GetOptions{})
case "ConsoleCLIDownloads":
res, err = client.ConsoleCliDownloads.Get(context.TODO(), resource.name, metav1.GetOptions{})
case "Deployment":
res, err = client.Apps.Deployments(resource.namespace).Get(context.TODO(), resource.name, metav1.GetOptions{})
case "PodDisruptionBudget":
res, err = client.PodDisruptionBudget.PodDisruptionBudgets(resource.namespace).Get(context.TODO(), resource.name, metav1.GetOptions{})
default:
err = fmt.Errorf("error getting resource: resource %s not identified", resource.kind)
}
return res, err
}
// custom-logo in openshift-console should exist when custom branding is used
func GetCustomLogoConfigMap(client *ClientSet) (*corev1.ConfigMap, error) {
return client.Core.ConfigMaps(consoleapi.OpenShiftConsoleNamespace).Get(context.TODO(), consoleapi.OpenShiftCustomLogoConfigMapName, metav1.GetOptions{})
}
func GetConsoleConfigMap(client *ClientSet) (*corev1.ConfigMap, error) {
return client.Core.ConfigMaps(consoleapi.OpenShiftConsoleNamespace).Get(context.TODO(), consoleapi.OpenShiftConsoleConfigMapName, metav1.GetOptions{})
}
func GetConsoleService(client *ClientSet) (*corev1.Service, error) {
return client.Core.Services(consoleapi.OpenShiftConsoleNamespace).Get(context.TODO(), consoleapi.OpenShiftConsoleServiceName, metav1.GetOptions{})
}
func GetConsoleRoute(client *ClientSet) (*routev1.Route, error) {
return client.Routes.Routes(consoleapi.OpenShiftConsoleNamespace).Get(context.TODO(), consoleapi.OpenShiftConsoleRouteName, metav1.GetOptions{})
}
func GetConsoleDeployment(client *ClientSet) (*appv1.Deployment, error) {
return client.Apps.Deployments(consoleapi.OpenShiftConsoleNamespace).Get(context.TODO(), consoleapi.OpenShiftConsoleDeploymentName, metav1.GetOptions{})
}
func GetDownloadsDeployment(client *ClientSet) (*appv1.Deployment, error) {
return client.Apps.Deployments(consoleapi.OpenShiftConsoleNamespace).Get(context.TODO(), consoleapi.OpenShiftConsoleDownloadsDeploymentName, metav1.GetOptions{})
}
func GetConsoleCLIDownloads(client *ClientSet, consoleCLIDownloadName string) (*consolev1.ConsoleCLIDownload, error) {
return client.ConsoleCliDownloads.Get(context.TODO(), consoleCLIDownloadName, metav1.GetOptions{})
}
func GetConsolePodDisruptionBudget(client *ClientSet, pdbName string) (*policyv1.PodDisruptionBudget, error) {
return client.PodDisruptionBudget.PodDisruptionBudgets(consoleapi.OpenShiftConsoleNamespace).Get(context.TODO(), consoleapi.OpenShiftConsoleName, metav1.GetOptions{})
}
func deleteResource(client *ClientSet, resource TestingResource) error {
var err error
switch resource.kind {
case "ConfigMap":
err = client.Core.ConfigMaps(resource.namespace).Delete(context.TODO(), resource.name, metav1.DeleteOptions{})
case "Service":
err = client.Core.Services(resource.namespace).Delete(context.TODO(), resource.name, metav1.DeleteOptions{})
case "Route":
err = client.Routes.Routes(resource.namespace).Delete(context.TODO(), resource.name, metav1.DeleteOptions{})
case "ConsoleCLIDownloads":
err = client.ConsoleCliDownloads.Delete(context.TODO(), resource.name, metav1.DeleteOptions{})
case "Deployment":
err = client.Apps.Deployments(resource.namespace).Delete(context.TODO(), resource.name, metav1.DeleteOptions{})
case "PodDisruptionBudget":
err = client.PodDisruptionBudget.PodDisruptionBudgets(resource.namespace).Delete(context.TODO(), resource.name, metav1.DeleteOptions{})
default:
err = fmt.Errorf("error deleting resource: resource %s not identified", resource.kind)
}
return err
}
// DeleteCompletely sends a delete request and waits until the resource and
// its dependents are deleted.
func DeleteCompletely(getObject func() (runtime.Object, error), deleteObject func(*metav1.DeleteOptions) error) error {
obj, err := getObject()
if err != nil {
if errors.IsNotFound(err) {
return nil
}
return err
}
accessor, err := meta.Accessor(obj)
uid := accessor.GetUID()
policy := metav1.DeletePropagationForeground
if err := deleteObject(&metav1.DeleteOptions{
Preconditions: &metav1.Preconditions{
UID: &uid,
},
PropagationPolicy: &policy,
}); err != nil {
if errors.IsNotFound(err) {
return nil
}
return err
}
return wait.Poll(1*time.Second, AsyncOperationTimeout, func() (stop bool, err error) {
obj, err = getObject()
if err != nil {
if errors.IsNotFound(err) {
return true, nil
}
return false, err
}
accessor, err := meta.Accessor(obj)
return accessor.GetUID() != uid, nil
})
}
func ConsoleResourcesAvailable(client *ClientSet) error {
resources := getTestingResources()
// We have to test the `console-public` configmap in the TestManaged as well.
resources = append(resources, TestingResource{"ConfigMap", consoleapi.OpenShiftConsolePublicConfigMapName, consoleapi.OpenShiftConfigManagedNamespace})
errChan := make(chan error)
for _, resource := range resources {
go IsResourceAvailable(errChan, client, resource)
}
checkErr := <-errChan
return checkErr
}
// IsResourceAvailable checks if tested resource is available during a 30 second period.
// if the resource does not exist by the end of the period, an error will be returned.
func IsResourceAvailable(errChan chan error, client *ClientSet, resource TestingResource) {
counter := 0
maxCount := 30
err := wait.Poll(1*time.Second, AsyncOperationTimeout, func() (stop bool, err error) {
_, err = GetResource(client, resource)
if err == nil {
return true, nil
}
if counter == maxCount {
if err != nil {
return true, fmt.Errorf("deleted console %s %s was not recreated", resource.kind, resource.name)
}
return true, nil
}
counter++
return false, nil
})
errChan <- err
}
// checks 3 times if the resources are unavailable
// - is fine if fails or 1st or 2nd run, resources could be in the process of being removed
// - is not fine if resources disappear, then reappear
// - it seems to take a bit longer to remove resources, so this wrapper should account for that.
func ConsoleResourcesUnavailable(client *ClientSet) error {
var failed error = nil
// give it 3 tries, then fail
for i := 0; i < 3; i++ {
// testing resources are hard-coded in this func.
err := LoopResources(client, IsResourceUnavailable)
fmt.Printf("validating console resources have been removed... %v\n", err == nil)
failed = err
}
return failed
}
func LoopResources(client *ClientSet, inner func(errChan chan error, client *ClientSet, resource TestingResource)) error {
resources := getTestingResources()
errChan := make(chan error)
for _, resource := range resources {
go inner(errChan, client, resource)
}
checkErr := <-errChan
return checkErr
}
// IsResourceUnavailable checks if tested resource is unavailable during a 15 second period.
// If the resource exists during that time, an error will be returned.
func IsResourceUnavailable(errChan chan error, client *ClientSet, resource TestingResource) {
counter := 0
maxCount := 15
err := wait.Poll(5*time.Second, AsyncOperationTimeout, func() (stop bool, err error) {
obtainedResource, err := GetResource(client, resource)
if err == nil {
yamlBytes, err := yaml.Marshal(obtainedResource)
if err != nil {
fmt.Printf("error marshalling yaml for %s %s %v", resource.kind, resource.name, err)
}
return true, fmt.Errorf("deleted console %s %s was recreated: %#v", resource.kind, resource.name, string(yamlBytes))
}
if !errors.IsNotFound(err) {
return true, err
}
counter++
if counter == maxCount {
return true, nil
}
return false, nil
})
errChan <- err
}