|
| 1 | +/* |
| 2 | +Copyright 2018 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package framework |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "io/ioutil" |
| 22 | + "math/rand" |
| 23 | + "os" |
| 24 | + "path/filepath" |
| 25 | + "regexp" |
| 26 | + "strings" |
| 27 | + "time" |
| 28 | + |
| 29 | + "github.com/kubernetes-sigs/kubebuilder/test/e2e/framework/ginkgowrapper" |
| 30 | + . "github.com/onsi/ginkgo" |
| 31 | + |
| 32 | + "k8s.io/client-go/tools/clientcmd" |
| 33 | +) |
| 34 | + |
| 35 | +// Code originally copied from kubernetes/kubernetes at |
| 36 | +// https://github.com/kubernetes/kubernetes/blob/master/test/e2e/framework/util.go |
| 37 | + |
| 38 | +// GetKubectlArgs wraps with default kubectl related args. |
| 39 | +func GetKubectlArgs(args []string) []string { |
| 40 | + defaultArgs := []string{} |
| 41 | + |
| 42 | + // Reference a --server option so tests can run anywhere. |
| 43 | + if TestContext.Host != "" { |
| 44 | + defaultArgs = append(defaultArgs, "--"+clientcmd.FlagAPIServer+"="+TestContext.Host) |
| 45 | + } |
| 46 | + if TestContext.KubeConfig != "" { |
| 47 | + defaultArgs = append(defaultArgs, "--"+clientcmd.RecommendedConfigPathFlag+"="+TestContext.KubeConfig) |
| 48 | + |
| 49 | + // Reference the KubeContext |
| 50 | + if TestContext.KubeContext != "" { |
| 51 | + defaultArgs = append(defaultArgs, "--"+clientcmd.FlagContext+"="+TestContext.KubeContext) |
| 52 | + } |
| 53 | + |
| 54 | + } else { |
| 55 | + if TestContext.CertDir != "" { |
| 56 | + defaultArgs = append(defaultArgs, |
| 57 | + fmt.Sprintf("--certificate-authority=%s", filepath.Join(TestContext.CertDir, "ca.crt")), |
| 58 | + fmt.Sprintf("--client-certificate=%s", filepath.Join(TestContext.CertDir, "kubecfg.crt")), |
| 59 | + fmt.Sprintf("--client-key=%s", filepath.Join(TestContext.CertDir, "kubecfg.key"))) |
| 60 | + } |
| 61 | + } |
| 62 | + kubectlArgs := append(defaultArgs, args...) |
| 63 | + |
| 64 | + return kubectlArgs |
| 65 | +} |
| 66 | + |
| 67 | +func NowStamp() string { |
| 68 | + return time.Now().Format(time.StampMilli) |
| 69 | +} |
| 70 | + |
| 71 | +func log(level string, format string, args ...interface{}) { |
| 72 | + fmt.Fprintf(GinkgoWriter, NowStamp()+": "+level+": "+format+"\n", args...) |
| 73 | +} |
| 74 | + |
| 75 | +func Logf(format string, args ...interface{}) { |
| 76 | + log("INFO", format, args...) |
| 77 | +} |
| 78 | + |
| 79 | +func Failf(format string, args ...interface{}) { |
| 80 | + FailfWithOffset(1, format, args...) |
| 81 | +} |
| 82 | + |
| 83 | +// FailfWithOffset calls "Fail" and logs the error at "offset" levels above its caller |
| 84 | +// (for example, for call chain f -> g -> FailfWithOffset(1, ...) error would be logged for "f"). |
| 85 | +func FailfWithOffset(offset int, format string, args ...interface{}) { |
| 86 | + msg := fmt.Sprintf(format, args...) |
| 87 | + log("INFO", msg) |
| 88 | + ginkgowrapper.Fail(NowStamp()+": "+msg, 1+offset) |
| 89 | +} |
| 90 | + |
| 91 | +func Skipf(format string, args ...interface{}) { |
| 92 | + msg := fmt.Sprintf(format, args...) |
| 93 | + log("INFO", msg) |
| 94 | + ginkgowrapper.Skip(NowStamp() + ": " + msg) |
| 95 | +} |
| 96 | + |
| 97 | +// RandomSuffix provides a random string to append to certain base name. |
| 98 | +func RandomSuffix() string { |
| 99 | + source := []rune("abcdefghijklmnopqrstuvwxyz") |
| 100 | + r := rand.New(rand.NewSource(time.Now().UnixNano())) |
| 101 | + res := make([]rune, 4) |
| 102 | + for i := range res { |
| 103 | + res[i] = source[r.Intn(len(source))] |
| 104 | + } |
| 105 | + return string(res) |
| 106 | +} |
| 107 | + |
| 108 | +// ParseCmdOutput converts given command output string into individual objects |
| 109 | +// according to line breakers, and ignores the empty elements in it. |
| 110 | +func ParseCmdOutput(output string) []string { |
| 111 | + res := []string{} |
| 112 | + elements := strings.Split(output, "\n") |
| 113 | + for _, element := range elements { |
| 114 | + if element != "" { |
| 115 | + res = append(res, element) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return res |
| 120 | +} |
| 121 | + |
| 122 | +// ReplaceFileConent tries to replace the source content of given file |
| 123 | +// with the target concent, source content can be regex. |
| 124 | +func ReplaceFileConent(src, target string, filePath string) error { |
| 125 | + // Check if file exist |
| 126 | + if _, err := os.Stat(filePath); err != nil { |
| 127 | + return err |
| 128 | + } |
| 129 | + |
| 130 | + // Read file content |
| 131 | + fileContent, err := ioutil.ReadFile(filePath) |
| 132 | + if err != nil { |
| 133 | + return err |
| 134 | + } |
| 135 | + |
| 136 | + // Replace the content |
| 137 | + r := regexp.MustCompile(src) |
| 138 | + output := r.ReplaceAllString(string(fileContent), target) |
| 139 | + |
| 140 | + if err := ioutil.WriteFile(filePath, []byte(output), os.ModePerm); err != nil { |
| 141 | + return err |
| 142 | + } |
| 143 | + |
| 144 | + return nil |
| 145 | +} |
0 commit comments