generated from kedacore/github-template
-
Notifications
You must be signed in to change notification settings - Fork 97
/
setup_test.go
265 lines (217 loc) · 8.75 KB
/
setup_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
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
//go:build e2e
// +build e2e
package utils
import (
"fmt"
"os"
"os/exec"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
. "github.com/kedacore/http-add-on/tests/helper"
)
var (
OtlpConfig = `mode: deployment
image:
repository: "otel/opentelemetry-collector-contrib"
config:
exporters:
debug:
verbosity: basic
prometheus:
endpoint: 0.0.0.0:8889
receivers:
jaeger: null
prometheus: null
zipkin: null
service:
pipelines:
traces: null
metrics:
receivers:
- otlp
exporters:
- debug
- prometheus
logs: null
`
OtlpServicePatch = `apiVersion: v1
kind: Service
metadata:
name: opentelemetry-collector
spec:
selector:
app.kubernetes.io/name: opentelemetry-collector
ports:
- protocol: TCP
port: 8889
targetPort: 8889
name: prometheus
type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: opentelemetry-collector
spec:
template:
spec:
containers:
- name: opentelemetry-collector
ports:
- containerPort: 8889
name: prometheus
protocol: TCP
`
gatewayClass = `
apiVersion: gateway.networking.k8s.io/v1
kind: GatewayClass
metadata:
name: eg
spec:
controllerName: gateway.envoyproxy.io/gatewayclass-controller
`
gateway = `
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: eg
namespace: envoy-gateway-system
spec:
gatewayClassName: eg
listeners:
- name: http
protocol: HTTP
port: 80
allowedRoutes:
namespaces:
from: All
`
)
func TestVerifyCommands(t *testing.T) {
commands := []string{"kubectl"}
for _, cmd := range commands {
_, err := exec.LookPath(cmd)
require.NoErrorf(t, err, "%s is required for setup - %s", cmd, err)
}
}
func TestKubernetesConnection(t *testing.T) {
KubeClient = GetKubernetesClient(t)
}
func TestKubernetesVersion(t *testing.T) {
out, err := ExecuteCommand("kubectl version")
require.NoErrorf(t, err, "error getting kubernetes version - %s", err)
t.Logf("kubernetes version: %s", string(out))
}
func TestSetupHelm(t *testing.T) {
_, err := exec.LookPath("helm")
if err == nil {
t.Skip("helm is already installed. skipping setup.")
}
_, err = ExecuteCommand("curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3")
require.NoErrorf(t, err, "cannot download helm installation shell script - %s", err)
_, err = ExecuteCommand("chmod 700 get_helm.sh")
require.NoErrorf(t, err, "cannot change permissions for helm installation script - %s", err)
_, err = ExecuteCommand("./get_helm.sh")
require.NoErrorf(t, err, "cannot download helm - %s", err)
_, err = ExecuteCommand("helm version")
require.NoErrorf(t, err, "cannot get helm version - %s", err)
}
func TestCreateKEDANamespace(t *testing.T) {
KubeClient = GetKubernetesClient(t)
CreateNamespace(t, KubeClient, KEDANamespace)
}
func TestSetupArgoRollouts(t *testing.T) {
KubeClient = GetKubernetesClient(t)
CreateNamespace(t, KubeClient, ArgoRolloutsNamespace)
_, err := ExecuteCommand("helm version")
require.NoErrorf(t, err, "helm is not installed - %s", err)
_, err = ExecuteCommand("helm repo add argo https://argoproj.github.io/argo-helm")
require.NoErrorf(t, err, "cannot add argo helm repo - %s", err)
_, err = ExecuteCommand("helm repo update argo")
require.NoErrorf(t, err, "cannot update argo helm repo - %s", err)
_, err = ExecuteCommand(fmt.Sprintf("helm upgrade --install %s argo/argo-rollouts --namespace %s --wait",
ArgoRolloutsName,
ArgoRolloutsNamespace))
require.NoErrorf(t, err, "cannot install argo-rollouts - %s", err)
}
func TestSetupIngress(t *testing.T) {
KubeClient = GetKubernetesClient(t)
CreateNamespace(t, KubeClient, IngressNamespace)
_, err := ExecuteCommand("helm version")
require.NoErrorf(t, err, "helm is not installed - %s", err)
_, err = ExecuteCommand("helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx")
require.NoErrorf(t, err, "cannot add ingress-nginx helm repo - %s", err)
_, err = ExecuteCommand("helm repo update ingress-nginx")
require.NoErrorf(t, err, "cannot update ingress-nginx helm repo - %s", err)
_, err = ExecuteCommand(fmt.Sprintf("helm upgrade --install %s ingress-nginx/ingress-nginx --set fullnameOverride=%s --set controller.service.type=ClusterIP --namespace %s --wait",
IngressReleaseName, IngressReleaseName, IngressNamespace))
require.NoErrorf(t, err, "cannot install ingress - %s", err)
}
func TestSetupEnvoyGateway(t *testing.T) {
KubeClient = GetKubernetesClient(t)
_, err := ExecuteCommand("helm version")
require.NoErrorf(t, err, "helm is not installed - %s", err)
_, err = ExecuteCommand(fmt.Sprintf("helm install %s oci://docker.io/envoyproxy/gateway-helm --version v1.0.2 -n %s --create-namespace", EnvoyReleaseName, EnvoyNamespace))
require.NoErrorf(t, err, "cannot install envoy gateway - %s", err)
assert.True(t, WaitForDeploymentReplicaReadyCount(t, KubeClient, "envoy-gateway", "envoy-gateway-system", 1, 30, 6))
KubectlApplyWithTemplate(t, nil, "gatewayClass", gatewayClass)
KubectlApplyWithTemplate(t, nil, "gateway", gateway)
}
func TestSetupKEDA(t *testing.T) {
_, err := ExecuteCommand("helm version")
require.NoErrorf(t, err, "helm is not installed - %s", err)
_, err = ExecuteCommand("helm repo add kedacore https://kedacore.github.io/charts")
require.NoErrorf(t, err, "cannot add kedacore helm repo - %s", err)
_, err = ExecuteCommand("helm repo update kedacore")
require.NoErrorf(t, err, "cannot update kedacore helm repo - %s", err)
_, err = ExecuteCommand(fmt.Sprintf("helm upgrade --install keda kedacore/keda --namespace %s --set extraArgs.keda.kube-api-qps=200 --set extraArgs.keda.kube-api-burst=300",
KEDANamespace))
require.NoErrorf(t, err, "cannot install KEDA - %s", err)
KubeClient = GetKubernetesClient(t)
assert.True(t, WaitForDeploymentReplicaReadyCount(t, KubeClient, "keda-operator", KEDANamespace, 1, 30, 6),
"replica count should be 1 after 3 minutes")
assert.True(t, WaitForDeploymentReplicaReadyCount(t, KubeClient, "keda-operator-metrics-apiserver", KEDANamespace, 1, 30, 6),
"replica count should be 1 after 3 minutes")
assert.True(t, WaitForDeploymentReplicaReadyCount(t, KubeClient, "keda-admission-webhooks", KEDANamespace, 1, 30, 6),
"replica count should be 1 after 3 minutes")
}
func TestSetupOpentelemetryComponents(t *testing.T) {
OpentelemetryNamespace := "open-telemetry-system"
otlpTempFileName := "otlp.yml"
otlpServiceTempFileName := "otlpServicePatch.yml"
defer os.Remove(otlpTempFileName)
defer os.Remove(otlpServiceTempFileName)
err := os.WriteFile(otlpTempFileName, []byte(OtlpConfig), 0755)
assert.NoErrorf(t, err, "cannot create otlp config file - %s", err)
err = os.WriteFile(otlpServiceTempFileName, []byte(OtlpServicePatch), 0755)
assert.NoErrorf(t, err, "cannot create otlp service patch file - %s", err)
_, err = ExecuteCommand("helm version")
require.NoErrorf(t, err, "helm is not installed - %s", err)
_, err = ExecuteCommand("helm repo add open-telemetry https://open-telemetry.github.io/opentelemetry-helm-charts")
require.NoErrorf(t, err, "cannot add open-telemetry helm repo - %s", err)
_, err = ExecuteCommand("helm repo update open-telemetry")
require.NoErrorf(t, err, "cannot update open-telemetry helm repo - %s", err)
KubeClient = GetKubernetesClient(t)
CreateNamespace(t, KubeClient, OpentelemetryNamespace)
_, err = ExecuteCommand(fmt.Sprintf("helm upgrade --install opentelemetry-collector open-telemetry/opentelemetry-collector -f %s --namespace %s", otlpTempFileName, OpentelemetryNamespace))
require.NoErrorf(t, err, "cannot install opentelemetry - %s", err)
_, err = ExecuteCommand(fmt.Sprintf("kubectl apply -f %s -n %s", otlpServiceTempFileName, OpentelemetryNamespace))
require.NoErrorf(t, err, "cannot update opentelemetry ports - %s", err)
}
func TestSetupTLSConfiguration(t *testing.T) {
out, err := ExecuteCommandWithDir("make test-certs", "../..")
require.NoErrorf(t, err, "error generating test certs - %s", err)
t.Log(string(out))
t.Log("test certificates successfully generated")
_, err = ExecuteCommand("kubectl -n keda create secret tls keda-tls --cert ../../certs/tls.crt --key ../../certs/tls.key")
require.NoErrorf(t, err, "could not create tls cert secret in keda namespace - %s", err)
_, err = ExecuteCommand("kubectl -n keda create secret tls abc-certs --cert ../../certs/abc.tls.crt --key ../../certs/abc.tls.key")
require.NoErrorf(t, err, "could not create tls cert secret in keda namespace - %s", err)
}
func TestDeployKEDAHttpAddOn(t *testing.T) {
out, err := ExecuteCommandWithDir("make deploy", "../..")
require.NoErrorf(t, err, "error deploying KEDA Http Add-on - %s", err)
t.Log(string(out))
t.Log("KEDA Http Add-on deployed successfully using 'make deploy' command")
}