-
Notifications
You must be signed in to change notification settings - Fork 92
/
util.go
418 lines (353 loc) · 10.5 KB
/
util.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
package utils
import (
"archive/tar"
"bytes"
"compress/gzip"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"github.com/devfile/library/v2/pkg/util"
"github.com/mitchellh/go-homedir"
"github.com/redhat-appstudio/e2e-tests/pkg/constants"
"k8s.io/klog/v2"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)
type Options struct {
ToolchainApiUrl string
KeycloakUrl string
OfflineToken string
}
// check options are valid or not
func CheckOptions(optionsArr []Options) (bool, error) {
if len(optionsArr) == 0 {
return false, nil
}
if len(optionsArr) > 1 {
return true, fmt.Errorf("options array contains more than 1 object")
}
options := optionsArr[0]
if options.ToolchainApiUrl == "" {
return true, fmt.Errorf("ToolchainApiUrl field is empty")
}
if options.KeycloakUrl == "" {
return true, fmt.Errorf("KeycloakUrl field is empty")
}
if options.OfflineToken == "" {
return true, fmt.Errorf("OfflineToken field is empty")
}
return true, nil
}
// CheckIfEnvironmentExists return true/false if the environment variable exists
func CheckIfEnvironmentExists(env string) bool {
_, exist := os.LookupEnv(env)
return exist
}
// Retrieve an environment variable. If will not exists will be used a default value
func GetEnv(key, defaultVal string) string {
if val := os.Getenv(key); val != "" {
return val
}
return defaultVal
}
// Retrieve an environment variable. If it doesn't exist, returns result of a call to `defaultFunc`.
func GetEnvOrFunc(key string, defaultFunc func() (string, error)) (string, error) {
if val := os.Getenv(key); val != "" {
return val, nil
}
return defaultFunc()
}
func GetQuayIOOrganization() string {
return GetEnv(constants.QUAY_E2E_ORGANIZATION_ENV, "redhat-appstudio-qe")
}
func IsPrivateHostname(url string) bool {
// https://www.ibm.com/docs/en/networkmanager/4.2.0?topic=translation-private-address-ranges
privateIPAddressPrefixes := []string{"10.", "172.1", "172.2", "172.3", "192.168"}
addr, err := net.LookupIP(url)
if err != nil {
klog.Infof("Unknown host: %v", err)
return true
}
ip := addr[0]
for _, ipPrefix := range privateIPAddressPrefixes {
if strings.HasPrefix(ip.String(), ipPrefix) {
return true
}
}
return false
}
func GetOpenshiftToken() (token string, err error) {
// Get the token for the current openshift user
tokenBytes, err := exec.Command("oc", "whoami", "--show-token").Output()
if err != nil {
return "", fmt.Errorf("error obtaining oc token %s", err.Error())
}
return strings.TrimSuffix(string(tokenBytes), "\n"), nil
}
func GetGeneratedNamespace(name string) string {
return name + "-" + util.GenerateRandomString(4)
}
func WaitUntilWithInterval(cond wait.ConditionFunc, interval time.Duration, timeout time.Duration) error {
return wait.PollUntilContextTimeout(context.Background(), interval, timeout, true, func(ctx context.Context) (bool, error) { return cond() })
}
func WaitUntil(cond wait.ConditionFunc, timeout time.Duration) error {
return WaitUntilWithInterval(cond, time.Second, timeout)
}
func ExecuteCommandInASpecificDirectory(command string, args []string, directory string) error {
cmd := exec.Command(command, args...) // nolint:gosec
cmd.Dir = directory
stdin, err := cmd.StdinPipe()
if err != nil {
return err
}
defer stdin.Close() // the doc says subProcess.Wait will close it, but I'm not sure, so I kept this line
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err = cmd.Start(); err != nil {
klog.Errorf("an error occurred: %s", err)
return err
}
_, _ = io.WriteString(stdin, "4\n")
if err := cmd.Wait(); err != nil {
return err
}
return err
}
func ToPrettyJSONString(v interface{}) string {
s, _ := json.MarshalIndent(v, "", " ")
return string(s)
}
// GetAdditionalInfo adds information regarding the application name and namespace of the test
func GetAdditionalInfo(applicationName, namespace string) string {
return fmt.Sprintf("(application: %s, namespace: %s)", applicationName, namespace)
}
// contains checks if a string is present in a slice
func Contains(s []string, str string) bool {
for _, v := range s {
if v == str {
return true
}
}
return false
}
func MergeMaps(m1, m2 map[string]string) map[string]string {
resultMap := make(map[string]string)
for k, v := range m1 {
resultMap[k] = v
}
for k, v := range m2 {
resultMap[k] = v
}
return resultMap
}
// CreateDockerConfigFile takes base64 encoded dockerconfig.json and saves it locally (/<home-directory/.docker/config.json)
func CreateDockerConfigFile(base64EncodedString string) error {
var rawRegistryCreds []byte
var homeDir string
var err error
if rawRegistryCreds, err = base64.StdEncoding.DecodeString(base64EncodedString); err != nil {
return fmt.Errorf("unable to decode container registry credentials: %v", err)
}
if homeDir, err = homedir.Dir(); err != nil {
return fmt.Errorf("unable to locate home directory: %v", err)
}
if err = os.MkdirAll(homeDir+"/.docker", 0775); err != nil {
return fmt.Errorf("failed to create '.docker' config directory: %v", err)
}
if err = os.WriteFile(homeDir+"/.docker/config.json", rawRegistryCreds, 0644); err != nil {
return fmt.Errorf("failed to create a docker config file: %v", err)
}
return nil
}
// Return a container logs from a given pod and namespace
func GetContainerLogs(ki kubernetes.Interface, podName, containerName, namespace string) (string, error) {
podLogOpts := corev1.PodLogOptions{
Container: containerName,
}
req := ki.CoreV1().Pods(namespace).GetLogs(podName, &podLogOpts)
podLogs, err := req.Stream(context.Background())
if err != nil {
return "", fmt.Errorf("error in opening the stream: %v", err)
}
defer podLogs.Close()
buf := new(bytes.Buffer)
_, err = io.Copy(buf, podLogs)
if err != nil {
return "", fmt.Errorf("error in copying logs to buf, %v", err)
}
return buf.String(), nil
}
func ExtractGitRepositoryNameFromURL(url string) (name string) {
repoName := url[strings.LastIndex(url, "/")+1:]
return strings.TrimSuffix(repoName, ".git")
}
func GetGithubAppID() (int64, error) {
appIDStr := GetEnv("E2E_PAC_GITHUB_APP_ID", constants.DefaultPaCGitHubAppID)
id, err := strconv.ParseInt(appIDStr, 10, 64)
if err != nil {
return 0, err
}
return id, nil
}
// Build a kubeconfig string from an existing client config
func CreateKubeconfigFileForRestConfig(restConfig rest.Config) ([]byte, error) {
clusters := make(map[string]*clientcmdapi.Cluster)
clusters["default-cluster"] = &clientcmdapi.Cluster{
Server: restConfig.Host,
CertificateAuthorityData: restConfig.CAData,
InsecureSkipTLSVerify: true,
}
contexts := make(map[string]*clientcmdapi.Context)
contexts["default-context"] = &clientcmdapi.Context{
Cluster: "default-cluster",
AuthInfo: "default-user",
}
authinfos := make(map[string]*clientcmdapi.AuthInfo)
authinfos["default-user"] = &clientcmdapi.AuthInfo{
Token: string(restConfig.BearerToken),
}
clientConfig := clientcmdapi.Config{
Kind: "Config",
APIVersion: "v1",
Clusters: clusters,
Contexts: contexts,
CurrentContext: "default-context",
AuthInfos: authinfos,
}
kubeconfiString, err := clientcmd.Write(clientConfig)
if err != nil {
return []byte{}, nil
}
return kubeconfiString, nil
}
func GetFileNamesFromDir(dirPath string) ([]string, error) {
var filesInDir []string
files, err := os.ReadDir(dirPath)
if err != nil {
return nil, fmt.Errorf("error reading directory: %v", err)
}
for _, file := range files {
filesInDir = append(filesInDir, file.Name())
}
return filesInDir, nil
}
func CheckFileExistsInDir(rootDir, filename string) (bool, error) {
absFilePath := filepath.Join(rootDir, filename)
_, err := os.Stat(absFilePath)
if err != nil {
if os.IsNotExist(err) {
return false, nil
} else {
return false, err
}
}
return true, nil
}
func Untar(dst string, tarPath string) error {
tr, err := ReadTarFile(tarPath)
if err != nil {
return err
}
for {
header, err := tr.Next()
switch {
// if no more files are found return
case err == io.EOF:
return nil
// return any other error
case err != nil:
return err
// if the header is nil, just skip it (not sure how this happens)
case header == nil:
continue
}
// the target location where the dir/file should be created
target := filepath.Join(dst, header.Name) // nolint:gosec
// the following switch could also be done using fi.Mode(), not sure if there
// a benefit of using one vs. the other.
// fi := header.FileInfo()
// check the file type
switch header.Typeflag {
// if its a dir and it doesn't exist create it
case tar.TypeDir:
err := CreateDir(target)
if err != nil {
return err
}
// if it's a file create it
case tar.TypeReg:
dirPath := filepath.Dir(target)
if err := CreateDir(dirPath); err != nil {
return fmt.Errorf("error when create parent directories %s: %v", dirPath, err)
}
err = CreateFile(target, header, tr)
if err != nil {
return err
}
}
}
}
func ReadTarFile(tarPath string) (*tar.Reader, error) {
tarFile, err := os.Open(tarPath) // nolint:gosec
if err != nil {
return nil, err
}
tr := tar.NewReader(tarFile)
if strings.HasSuffix(tarPath, ".gz") || strings.HasSuffix(tarPath, ".gzip") {
gz, err := gzip.NewReader(tarFile)
if err != nil {
return nil, err
}
defer gz.Close()
tr = tar.NewReader(gz)
}
return tr, nil
}
func CreateDir(target string) error {
if _, err := os.Stat(target); err != nil {
if err := os.MkdirAll(target, 0755); err != nil { // nolint:gosec
return err
}
}
return nil
}
func CreateFile(target string, header *tar.Header, tr *tar.Reader) error {
f, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode)) // nolint:gosec
if err != nil {
return err
}
// copy over contents
if _, err := io.Copy(f, tr); err != nil { // nolint:gosec
return err
}
// manually close here after each file operation; defering would cause each file close
// to wait until all operations have completed.
return f.Close()
}
func GetRepoName(repoUrl string) string {
return strings.Split(strings.TrimSuffix(repoUrl, ".git"), "/")[4]
}
func FilterSliceUsingPattern(pattern string, lString []string) []string {
var results []string
re := regexp.MustCompile(pattern)
for _, str := range lString {
if re.MatchString(str) {
results = append(results, str)
}
}
return results
}