-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathutils.go
More file actions
220 lines (188 loc) · 6.29 KB
/
utils.go
File metadata and controls
220 lines (188 loc) · 6.29 KB
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
//
// Copyright (c) 2019-2025 Red Hat, Inc.
// This program and the accompanying materials are made
// available under the terms of the Eclipse Public License 2.0
// which is available at https://www.eclipse.org/legal/epl-2.0/
//
// SPDX-License-Identifier: EPL-2.0
//
// Contributors:
// Red Hat, Inc. - initial API and implementation
//
package test
import (
"context"
"os"
"strings"
"testing"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
securityv1 "github.com/openshift/api/security/v1"
controllerv1alpha1 "github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
routev1 "github.com/openshift/api/route/v1"
"github.com/stretchr/testify/assert"
"k8s.io/utils/pointer"
chev1alpha1 "github.com/che-incubator/kubernetes-image-puller-operator/api/v1alpha1"
chev2 "github.com/eclipse-che/che-operator/api/v2"
"github.com/eclipse-che/che-operator/pkg/common/chetypes"
console "github.com/openshift/api/console/v1"
oauthv1 "github.com/openshift/api/oauth/v1"
templatev1 "github.com/openshift/api/template/v1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
configv1 "github.com/openshift/api/config/v1"
fakeDiscovery "k8s.io/client-go/discovery/fake"
fakeclientset "k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)
type TestExpectedResources struct {
MemoryLimit string
MemoryRequest string
CpuRequest string
CpuLimit string
}
func CompareResources(actualDeployment *appsv1.Deployment, expected TestExpectedResources, t *testing.T) {
container := &actualDeployment.Spec.Template.Spec.Containers[0]
compareQuantity(
"Memory limits",
container.Resources.Limits.Memory(),
expected.MemoryLimit,
t,
)
compareQuantity(
"Memory requests",
container.Resources.Requests.Memory(),
expected.MemoryRequest,
t,
)
compareQuantity(
"CPU limits",
container.Resources.Limits.Cpu(),
expected.CpuLimit,
t,
)
compareQuantity(
"CPU requests",
container.Resources.Requests.Cpu(),
expected.CpuRequest,
t,
)
}
func ValidateSecurityContext(actualDeployment *appsv1.Deployment, t *testing.T) {
assert.Equal(t, corev1.Capability("ALL"), actualDeployment.Spec.Template.Spec.Containers[0].SecurityContext.Capabilities.Drop[0])
assert.Equal(t, pointer.Bool(false), actualDeployment.Spec.Template.Spec.Containers[0].SecurityContext.AllowPrivilegeEscalation)
}
func compareQuantity(resource string, actualQuantity *resource.Quantity, expected string, t *testing.T) {
expectedQuantity := GetResourceQuantity(expected, expected)
if !actualQuantity.Equal(expectedQuantity) {
t.Errorf("%s: expected %s, actual %s", resource, expectedQuantity.String(), actualQuantity.String())
}
}
func ValidateContainData(actualData map[string]string, expectedData map[string]string, t *testing.T) {
for k, v := range expectedData {
actualValue, exists := actualData[k]
if exists {
if actualValue != v {
t.Errorf("Key '%s', actual: '%s', expected: '%s'", k, actualValue, v)
}
} else if v != "" {
t.Errorf("Key '%s' does not exists, expected value: '%s'", k, v)
}
}
}
func FindVolume(volumes []corev1.Volume, name string) corev1.Volume {
for _, volume := range volumes {
if volume.Name == name {
return volume
}
}
return corev1.Volume{}
}
func FindVolumeMount(volumes []corev1.VolumeMount, name string) corev1.VolumeMount {
for _, volumeMount := range volumes {
if volumeMount.Name == name {
return volumeMount
}
}
return corev1.VolumeMount{}
}
func IsObjectExists(client client.Client, key types.NamespacedName, blueprint client.Object) bool {
err := client.Get(context.TODO(), key, blueprint)
if err != nil {
return false
}
return true
}
func GetResourceQuantity(value string, defaultValue string) resource.Quantity {
if value != "" {
return resource.MustParse(value)
}
return resource.MustParse(defaultValue)
}
func EnableTestMode() {
_ = os.Setenv("MOCK_API", "1")
}
func IsTestMode() bool {
testMode := os.Getenv("MOCK_API")
return len(testMode) != 0
}
// Initialize DeployContext for tests
func GetDeployContext(cheCluster *chev2.CheCluster, initObjs []runtime.Object) *chetypes.DeployContext {
if cheCluster == nil {
// use a default checluster
cheCluster = &chev2.CheCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "eclipse-che",
Namespace: "eclipse-che",
},
Status: chev2.CheClusterStatus{
CheURL: "https://che-host",
},
}
}
scheme := scheme.Scheme
chev2.SchemeBuilder.AddToScheme(scheme)
scheme.AddKnownTypes(controllerv1alpha1.SchemeBuilder.GroupVersion, &controllerv1alpha1.DevWorkspaceOperatorConfig{})
scheme.AddKnownTypes(oauthv1.GroupVersion, &oauthv1.OAuthClient{}, &oauthv1.OAuthClientList{})
scheme.AddKnownTypes(configv1.GroupVersion, &configv1.Proxy{}, &configv1.Console{})
scheme.AddKnownTypes(templatev1.GroupVersion, &templatev1.Template{}, &templatev1.TemplateList{})
scheme.AddKnownTypes(routev1.GroupVersion, &routev1.Route{})
scheme.AddKnownTypes(corev1.SchemeGroupVersion, &corev1.Secret{})
scheme.AddKnownTypes(console.GroupVersion, &console.ConsoleLink{})
scheme.AddKnownTypes(chev1alpha1.GroupVersion, &chev1alpha1.KubernetesImagePuller{})
securityv1.Install(scheme)
initObjs = append(initObjs, cheCluster)
cli := fake.NewFakeClientWithScheme(scheme, initObjs...)
clientSet := fakeclientset.NewSimpleClientset()
fakeDiscovery, _ := clientSet.Discovery().(*fakeDiscovery.FakeDiscovery)
return &chetypes.DeployContext{
CheCluster: cheCluster,
ClusterAPI: chetypes.ClusterAPI{
Client: cli,
NonCachingClient: cli,
Scheme: scheme,
DiscoveryClient: fakeDiscovery,
},
Proxy: &chetypes.Proxy{},
CheHost: strings.TrimPrefix(cheCluster.Status.CheURL, "https://"),
}
}
// EnsureReconcile runs the testReconcileFunc until it returns done=true or 10 iterations
func EnsureReconcile(
t *testing.T,
ctx *chetypes.DeployContext,
testReconcileFunc func(ctx *chetypes.DeployContext) (result reconcile.Result, done bool, err error)) {
for i := 0; i < 10; i++ {
_, done, err := testReconcileFunc(ctx)
assert.NoError(t, err)
if done {
return
}
}
assert.Fail(t, "Reconcile did not finish in 10 iterations")
}