-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
244 lines (215 loc) · 6.64 KB
/
utils.go
File metadata and controls
244 lines (215 loc) · 6.64 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
Copyright 2024.
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 utils
import (
"fmt"
"os"
"os/exec"
"strings"
"github.com/onsi/ginkgo/v2"
)
const (
prometheusOperatorVersion = "v0.68.0"
prometheusOperatorURL = "https://github.com/prometheus-operator/prometheus-operator/" +
"releases/download/%s/bundle.yaml"
certmanagerVersion = "v1.5.3"
certmanagerURLTmpl = "https://github.com/jetstack/cert-manager/releases/download/%s/cert-manager.yaml"
mysqlYaml = "test/e2e/testdata/mysql.yaml"
postgresYaml = "test/e2e/testdata/postgres.yaml"
)
func warnError(err error) {
fmt.Fprintf(ginkgo.GinkgoWriter, "warning: %v\n", err)
}
// InstallRelationalDatabases installs both MySQL and PostgreSQL pods to be used for testing.
func InstallRelationalDatabases() error {
dir, err := GetProjectDir()
if err != nil {
return err
}
errChan := make(chan error, 2)
for _, yaml := range []string{mysqlYaml, postgresYaml} {
cmd := exec.Command("kubectl", "apply", "-f", yaml)
cmd.Dir = dir
fmt.Fprintf(ginkgo.GinkgoWriter, "running: %s in directory: %s\n", strings.Join(cmd.Args, " "), dir)
go func() {
_, err := Run(cmd)
errChan <- err
}()
}
for i := 0; i < 2; i++ {
if err := <-errChan; err != nil {
return err
}
}
return nil
}
// InstallMongoDB installs a MongoDB pod to be used for testing.
func InstallMongoDB() error {
dir, err := GetProjectDir()
if err != nil {
return err
}
cmd := exec.Command("kubectl", "apply", "-f", "test/e2e/testdata/mongodb.yaml")
cmd.Dir = dir
fmt.Fprintf(ginkgo.GinkgoWriter, "running: %s in directory: %s\n", strings.Join(cmd.Args, " "), dir)
_, err = Run(cmd)
return err
}
// UninstallRelationalDatabases uninstalls both MySQL and PostgreSQL pods.
func UninstallRelationalDatabases() {
dir, err := GetProjectDir()
if err != nil {
warnError(err)
}
errChan := make(chan error, 2)
for _, yaml := range []string{mysqlYaml, postgresYaml} {
cmd := exec.Command("kubectl", "delete", "-f", yaml)
cmd.Dir = dir
fmt.Fprintf(ginkgo.GinkgoWriter, "running: %s in directory: %s\n", strings.Join(cmd.Args, " "), dir)
go func() {
_, err := Run(cmd)
errChan <- err
}()
}
for i := 0; i < 2; i++ {
if err := <-errChan; err != nil {
warnError(err)
}
}
}
// UninstallMongoDB uninstalls the MongoDB pod.
func UninstallMongoDB() {
dir, err := GetProjectDir()
if err != nil {
warnError(err)
}
cmd := exec.Command("kubectl", "delete", "-f", "test/e2e/testdata/mongodb.yaml")
cmd.Dir = dir
if _, err := Run(cmd); err != nil {
warnError(err)
}
}
// InstallMySQL installs a MySQL pod to be used for testing.
func InstallMySQL() error {
dir, err := GetProjectDir()
if err != nil {
return err
}
cmd := exec.Command("kubectl", "apply", "-f", mysqlYaml)
cmd.Dir = dir
fmt.Fprintf(ginkgo.GinkgoWriter, "running: %s in directory: %s\n", strings.Join(cmd.Args, " "), dir)
_, err = Run(cmd)
// Note that we don't wait for the pod to be ready here. This is a good test for the controller
// to see if it can handle mysql server not being ready.
return err
}
// UninstallMySQLPod uninstalls the MySQL pod.
func UninstallMySQLPod() {
dir, err := GetProjectDir()
if err != nil {
warnError(err)
}
cmd := exec.Command("kubectl", "delete", "-f", mysqlYaml)
cmd.Dir = dir
if _, err := Run(cmd); err != nil {
warnError(err)
}
}
// InstallPrometheusOperator installs the prometheus Operator to be used to export the enabled metrics.
func InstallPrometheusOperator() error {
url := fmt.Sprintf(prometheusOperatorURL, prometheusOperatorVersion)
cmd := exec.Command("kubectl", "create", "-f", url)
_, err := Run(cmd)
return err
}
// Run executes the provided command within this context
func Run(cmd *exec.Cmd) ([]byte, error) {
dir, _ := GetProjectDir()
cmd.Dir = dir
if err := os.Chdir(cmd.Dir); err != nil {
fmt.Fprintf(ginkgo.GinkgoWriter, "chdir dir: %s\n", err)
}
cmd.Env = append(os.Environ(), "GO111MODULE=on")
command := strings.Join(cmd.Args, " ")
fmt.Fprintf(ginkgo.GinkgoWriter, "running: %s\n", command)
output, err := cmd.CombinedOutput()
if err != nil {
return output, fmt.Errorf("%s failed with error: (%v) %s", command, err, string(output))
}
return output, nil
}
// UninstallPrometheusOperator uninstalls the prometheus
func UninstallPrometheusOperator() {
url := fmt.Sprintf(prometheusOperatorURL, prometheusOperatorVersion)
cmd := exec.Command("kubectl", "delete", "-f", url)
if _, err := Run(cmd); err != nil {
warnError(err)
}
}
// UninstallCertManager uninstalls the cert manager
func UninstallCertManager() {
url := fmt.Sprintf(certmanagerURLTmpl, certmanagerVersion)
cmd := exec.Command("kubectl", "delete", "-f", url)
if _, err := Run(cmd); err != nil {
warnError(err)
}
}
// InstallCertManager installs the cert manager bundle.
func InstallCertManager() error {
url := fmt.Sprintf(certmanagerURLTmpl, certmanagerVersion)
cmd := exec.Command("kubectl", "apply", "-f", url)
if _, err := Run(cmd); err != nil {
return err
}
// Wait for cert-manager-webhook to be ready, which can take time if cert-manager
// was re-installed after uninstalling on a cluster.
cmd = exec.Command("kubectl", "wait", "deployment.apps/cert-manager-webhook",
"--for", "condition=Available",
"--namespace", "cert-manager",
"--timeout", "5m",
)
_, err := Run(cmd)
return err
}
// LoadImageToKindCluster loads a local docker image to the kind cluster
func LoadImageToKindClusterWithName(name string) error {
cluster := "kind"
if v, ok := os.LookupEnv("KIND_CLUSTER"); ok {
cluster = v
}
kindOptions := []string{"load", "docker-image", name, "--name", cluster}
cmd := exec.Command("kind", kindOptions...)
_, err := Run(cmd)
return err
}
// GetNonEmptyLines converts given command output string into individual objects
// according to line breakers, and ignores the empty elements in it.
func GetNonEmptyLines(output string) []string {
var res []string
elements := strings.Split(output, "\n")
for _, element := range elements {
if element != "" {
res = append(res, element)
}
}
return res
}
// GetProjectDir will return the directory where the project is
func GetProjectDir() (string, error) {
wd, err := os.Getwd()
if err != nil {
return wd, err
}
wd = strings.ReplaceAll(wd, "/test/e2e", "")
return wd, nil
}