-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhelper_test.go
146 lines (123 loc) · 4.82 KB
/
helper_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
// Copyright 2022 Camunda Services GmbH
//
// 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 internal
import (
"context"
"strings"
"testing"
"github.com/stretchr/testify/require"
v12 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
dynamicFake "k8s.io/client-go/dynamic/fake"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
)
func CreateFakeClient() K8Client {
scheme := runtime.NewScheme()
groupVersionKind := schema.GroupVersionKind{Group: "cloud.camunda.io", Version: "v1alpha1", Kind: "zeebeclusters"}
scheme.AddKnownTypeWithName(groupVersionKind, &unstructured.Unstructured{})
k8Client := K8Client{Clientset: fake.NewSimpleClientset(),
DynamicClient: dynamicFake.NewSimpleDynamicClient(scheme),
ClientConfig: &testClientConfig{namespace: "testNamespace"}}
return k8Client
}
type testClientConfig struct {
namespace string
namespaceSpecified bool
err error
}
func (c *testClientConfig) Namespace() (string, bool, error) {
return c.namespace, c.namespaceSpecified, c.err
}
func (c *testClientConfig) RawConfig() (api.Config, error) {
panic("implement me")
}
func (c *testClientConfig) ClientConfig() (*rest.Config, error) {
panic("implement me")
}
func (c *testClientConfig) ConfigAccess() clientcmd.ConfigAccess {
panic("implement me")
}
func (c K8Client) CreatePodWithLabels(t *testing.T, selector *metav1.LabelSelector) {
c.CreatePodWithLabelsAndName(t, selector, "testPod")
}
func (c K8Client) CreatePodWithLabelsAndName(t *testing.T, selector *metav1.LabelSelector, podName string) {
_, err := c.Clientset.CoreV1().Pods(c.GetCurrentNamespace()).Create(context.TODO(), &v1.Pod{
ObjectMeta: metav1.ObjectMeta{Labels: selector.MatchLabels, Name: podName},
Spec: v1.PodSpec{},
}, metav1.CreateOptions{})
require.NoError(t, err)
}
func (c K8Client) CreateBrokerPodsWithStatus(t *testing.T, selector *metav1.LabelSelector, podName string, podPhase v1.PodPhase, readyStatus bool) {
_, err := c.Clientset.CoreV1().Pods(c.GetCurrentNamespace()).Create(context.TODO(), &v1.Pod{
ObjectMeta: metav1.ObjectMeta{Labels: selector.MatchLabels, Name: podName},
Spec: v1.PodSpec{},
Status: v1.PodStatus{
Phase: podPhase,
ContainerStatuses: []v1.ContainerStatus{
{
Ready: readyStatus,
},
},
},
}, metav1.CreateOptions{})
require.NoError(t, err)
}
func (c K8Client) CreateDeploymentWithLabelsAndName(t *testing.T, selector *metav1.LabelSelector, name string) {
_, err := c.Clientset.AppsV1().Deployments(c.GetCurrentNamespace()).Create(context.TODO(), &v12.Deployment{
ObjectMeta: metav1.ObjectMeta{Labels: selector.MatchLabels, Name: name},
Spec: v12.DeploymentSpec{},
Status: v12.DeploymentStatus{
UnavailableReplicas: 0,
AvailableReplicas: 1,
},
}, metav1.CreateOptions{})
require.NoError(t, err)
}
func (c *K8Client) createSaaSCRD(t *testing.T) {
zeebeCrd := schema.GroupVersionResource{Group: "cloud.camunda.io", Version: "v1alpha1", Resource: "zeebeclusters"}
obj := &unstructured.Unstructured{}
namespace := c.GetCurrentNamespace()
clusterId := strings.TrimSuffix(namespace, "-zeebe")
obj.SetName(clusterId)
_, err := c.DynamicClient.Resource(zeebeCrd).Create(context.TODO(), obj, metav1.CreateOptions{})
require.NoError(t, err)
saas, err := c.isSaaSEnvironment()
require.NoError(t, err)
c.SaaSEnv = saas
}
func (c K8Client) CreateStatefulSetWithLabelsAndName(t *testing.T, selector *metav1.LabelSelector, name string) {
_, err := c.Clientset.AppsV1().StatefulSets(c.GetCurrentNamespace()).Create(context.TODO(), &v12.StatefulSet{
ObjectMeta: metav1.ObjectMeta{Labels: selector.MatchLabels, Name: name},
Spec: v12.StatefulSetSpec{},
Status: v12.StatefulSetStatus{},
}, metav1.CreateOptions{})
require.NoError(t, err)
}
func (c *K8Client) createSaaSNamespace(t *testing.T) {
namespace := v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: c.GetCurrentNamespace(),
Labels: map[string]string{"pod-security.kubernetes.io/enforce": "true"},
},
}
_, err := c.Clientset.CoreV1().Namespaces().Create(context.TODO(), &namespace, metav1.CreateOptions{})
require.NoError(t, err)
}