forked from kubernetes-sigs/kubebuilder
-
Notifications
You must be signed in to change notification settings - Fork 5
/
util.go
145 lines (118 loc) · 4.09 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
/*
Copyright 2018 The Kubernetes Authors.
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 framework
import (
"fmt"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"regexp"
"strings"
"time"
. "github.com/onsi/ginkgo"
"sigs.k8s.io/kubebuilder/test/e2e/framework/ginkgowrapper"
"k8s.io/client-go/tools/clientcmd"
)
// Code originally copied from kubernetes/kubernetes at
// https://github.com/kubernetes/kubernetes/blob/master/test/e2e/framework/util.go
// GetKubectlArgs wraps with default kubectl related args.
func GetKubectlArgs(args []string) []string {
defaultArgs := []string{}
// Reference a --server option so tests can run anywhere.
if TestContext.Host != "" {
defaultArgs = append(defaultArgs, "--"+clientcmd.FlagAPIServer+"="+TestContext.Host)
}
if TestContext.KubeConfig != "" {
defaultArgs = append(defaultArgs, "--"+clientcmd.RecommendedConfigPathFlag+"="+TestContext.KubeConfig)
// Reference the KubeContext
if TestContext.KubeContext != "" {
defaultArgs = append(defaultArgs, "--"+clientcmd.FlagContext+"="+TestContext.KubeContext)
}
} else {
if TestContext.CertDir != "" {
defaultArgs = append(defaultArgs,
fmt.Sprintf("--certificate-authority=%s", filepath.Join(TestContext.CertDir, "ca.crt")),
fmt.Sprintf("--client-certificate=%s", filepath.Join(TestContext.CertDir, "kubecfg.crt")),
fmt.Sprintf("--client-key=%s", filepath.Join(TestContext.CertDir, "kubecfg.key")))
}
}
kubectlArgs := append(defaultArgs, args...)
return kubectlArgs
}
func NowStamp() string {
return time.Now().Format(time.StampMilli)
}
func log(level string, format string, args ...interface{}) {
fmt.Fprintf(GinkgoWriter, NowStamp()+": "+level+": "+format+"\n", args...)
}
func Logf(format string, args ...interface{}) {
log("INFO", format, args...)
}
func Failf(format string, args ...interface{}) {
FailfWithOffset(1, format, args...)
}
// FailfWithOffset calls "Fail" and logs the error at "offset" levels above its caller
// (for example, for call chain f -> g -> FailfWithOffset(1, ...) error would be logged for "f").
func FailfWithOffset(offset int, format string, args ...interface{}) {
msg := fmt.Sprintf(format, args...)
log("INFO", msg)
ginkgowrapper.Fail(NowStamp()+": "+msg, 1+offset)
}
func Skipf(format string, args ...interface{}) {
msg := fmt.Sprintf(format, args...)
log("INFO", msg)
ginkgowrapper.Skip(NowStamp() + ": " + msg)
}
// RandomSuffix provides a random string to append to certain base name.
func RandomSuffix() string {
source := []rune("abcdefghijklmnopqrstuvwxyz")
r := rand.New(rand.NewSource(time.Now().UnixNano()))
res := make([]rune, 4)
for i := range res {
res[i] = source[r.Intn(len(source))]
}
return string(res)
}
// ParseCmdOutput converts given command output string into individual objects
// according to line breakers, and ignores the empty elements in it.
func ParseCmdOutput(output string) []string {
res := []string{}
elements := strings.Split(output, "\n")
for _, element := range elements {
if element != "" {
res = append(res, element)
}
}
return res
}
// ReplaceFileConent tries to replace the source content of given file
// with the target concent, source content can be regex.
func ReplaceFileConent(src, target string, filePath string) error {
// Check if file exist
if _, err := os.Stat(filePath); err != nil {
return err
}
// Read file content
fileContent, err := ioutil.ReadFile(filePath)
if err != nil {
return err
}
// Replace the content
r := regexp.MustCompile(src)
output := r.ReplaceAllString(string(fileContent), target)
if err := ioutil.WriteFile(filePath, []byte(output), os.ModePerm); err != nil {
return err
}
return nil
}