|
| 1 | +/* |
| 2 | +Copyright 2020 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 kubetest |
| 18 | + |
| 19 | +import ( |
| 20 | + "io/ioutil" |
| 21 | + "os" |
| 22 | + "os/exec" |
| 23 | + "os/user" |
| 24 | + "path" |
| 25 | + "strconv" |
| 26 | + "strings" |
| 27 | + |
| 28 | + "github.com/pkg/errors" |
| 29 | + corev1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 30 | + "sigs.k8s.io/cluster-api/test/framework" |
| 31 | +) |
| 32 | + |
| 33 | +const ( |
| 34 | + standardImage = "gcr.io/k8s-artifacts-prod/conformance" |
| 35 | + ciArtifactImage = "gcr.io/kubernetes-ci-images/conformance" |
| 36 | +) |
| 37 | + |
| 38 | +const ( |
| 39 | + DefaultGinkgoNodes = 1 |
| 40 | + DefaultGinkoSlowSpecThreshold = 120 |
| 41 | +) |
| 42 | + |
| 43 | +type RunInput struct { |
| 44 | + // ClusterProxy is a clusterctl test framework proxy for the workload cluster |
| 45 | + // for which to run kubetest against |
| 46 | + ClusterProxy framework.ClusterProxy |
| 47 | + // NumberOfNodes is the number of cluster nodes that exist for kubetest |
| 48 | + // to be aware of |
| 49 | + NumberOfNodes int |
| 50 | + // UseCIArtifacts will fetch the latest build from the main branch of kubernetes/kubernetes |
| 51 | + UseCIArtifacts bool `json:"useCIArtifacts,omitempty"` |
| 52 | + // ArtifactsDirectory is where conformance suite output will go |
| 53 | + ArtifactsDirectory string `json:"artifactsDirectory,omitempty"` |
| 54 | + // Path to the kubetest e2e config file |
| 55 | + ConfigFilePath string `json:"configFilePath,omitempty"` |
| 56 | + // GinkgoNodes is the number of Ginkgo nodes to use |
| 57 | + GinkgoNodes int `json:"ginkgoNodes,omitempty"` |
| 58 | + // GinkgoSlowSpecThreshold is time in s before spec is marked as slow |
| 59 | + GinkgoSlowSpecThreshold int `json:"ginkgoSlowSpecThreshold,omitempty"` |
| 60 | + // KubernetesVersion is the version of Kubernetes to test |
| 61 | + KubernetesVersion string |
| 62 | + // ConformanceImage is an optional field to specify an exact conformance image |
| 63 | + ConformanceImage string |
| 64 | +} |
| 65 | + |
| 66 | +// Run executes kube-test given an artifact directory, and sets settings |
| 67 | +// required for kubetest to work with Cluster API. JUnit files are |
| 68 | +// also gathered for inclusion in Prow. |
| 69 | +func Run(input RunInput) error { |
| 70 | + if input.GinkgoNodes == 0 { |
| 71 | + input.GinkgoNodes = DefaultGinkgoNodes |
| 72 | + } |
| 73 | + if input.GinkgoSlowSpecThreshold == 0 { |
| 74 | + input.GinkgoSlowSpecThreshold = 120 |
| 75 | + } |
| 76 | + if input.NumberOfNodes == 0 { |
| 77 | + numNodes, err := countClusterNodes(input.ClusterProxy) |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + input.NumberOfNodes = numNodes |
| 82 | + } |
| 83 | + if input.ArtifactsDirectory == "" { |
| 84 | + if dir, ok := os.LookupEnv("ARTIFACTS"); ok { |
| 85 | + input.ArtifactsDirectory = dir |
| 86 | + } |
| 87 | + input.ArtifactsDirectory = "_artifacts" |
| 88 | + } |
| 89 | + reportDir := path.Join(input.ArtifactsDirectory, "kubetest") |
| 90 | + outputDir := path.Join(reportDir, "e2e-output") |
| 91 | + if err := os.MkdirAll(outputDir, 0o750); err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + ginkgoVars := map[string]string{ |
| 95 | + "nodes": strconv.Itoa(input.GinkgoNodes), |
| 96 | + "slowSpecThreshold": strconv.Itoa(input.GinkgoSlowSpecThreshold), |
| 97 | + } |
| 98 | + e2eVars := map[string]string{ |
| 99 | + "kubeconfig": "/tmp/kubeconfig", |
| 100 | + "provider": "skeleton", |
| 101 | + "report-dir": "/output", |
| 102 | + "e2e-output-dir": "/output/e2e-output", |
| 103 | + "dump-logs-on-failure": "false", |
| 104 | + "report-prefix": "kubetest.", |
| 105 | + "num-nodes": strconv.FormatInt(int64(input.NumberOfNodes), 10), |
| 106 | + "viper-config": "/tmp/viper-config.yaml", |
| 107 | + } |
| 108 | + ginkgoArgs := buildArgs(ginkgoVars, "-") |
| 109 | + e2eArgs := buildArgs(e2eVars, "--") |
| 110 | + if input.ConformanceImage == "" { |
| 111 | + input.ConformanceImage = versionToConformanceImage(input.KubernetesVersion, input.UseCIArtifacts) |
| 112 | + } |
| 113 | + kubeConfigVolumeMount := volumeArg(input.ClusterProxy.GetKubeconfigPath(), "/tmp/kubeconfig") |
| 114 | + outputVolumeMount := volumeArg(reportDir, "/output") |
| 115 | + viperVolumeMount := volumeArg(input.ConfigFilePath, "/tmp/viper-config.yaml") |
| 116 | + user, err := user.Current() |
| 117 | + if err != nil { |
| 118 | + return errors.Wrap(err, "unable to determine current user") |
| 119 | + } |
| 120 | + userArg := user.Uid + ":" + user.Gid |
| 121 | + e2eCmd := exec.Command("docker", "run", "--user", userArg, kubeConfigVolumeMount, outputVolumeMount, viperVolumeMount, "-t", input.ConformanceImage) |
| 122 | + e2eCmd.Args = append(e2eCmd.Args, "/usr/local/bin/ginkgo") |
| 123 | + e2eCmd.Args = append(e2eCmd.Args, ginkgoArgs...) |
| 124 | + e2eCmd.Args = append(e2eCmd.Args, "/usr/local/bin/e2e.test") |
| 125 | + e2eCmd.Args = append(e2eCmd.Args, "--") |
| 126 | + e2eCmd.Args = append(e2eCmd.Args, e2eArgs...) |
| 127 | + e2eCmd = framework.CompleteCommand(e2eCmd, "Running e2e test", false) |
| 128 | + if err := e2eCmd.Run(); err != nil { |
| 129 | + return errors.Wrap(err, "Unable to run conformance tests") |
| 130 | + } |
| 131 | + if err := framework.GatherJUnitReports(reportDir, input.ArtifactsDirectory); err != nil { |
| 132 | + return err |
| 133 | + } |
| 134 | + return nil |
| 135 | +} |
| 136 | + |
| 137 | +func countClusterNodes(proxy framework.ClusterProxy) (int, error) { |
| 138 | + nodeList, err := proxy.GetClientSet().CoreV1().Nodes().List(corev1.ListOptions{}) |
| 139 | + if err != nil { |
| 140 | + return 0, errors.Wrap(err, "Unable to count nodes") |
| 141 | + } |
| 142 | + return len(nodeList.Items), nil |
| 143 | +} |
| 144 | + |
| 145 | +func isSELinuxEnforcing() bool { |
| 146 | + dat, err := ioutil.ReadFile("/sys/fs/selinux/enforce") |
| 147 | + if err != nil { |
| 148 | + return false |
| 149 | + } |
| 150 | + return string(dat) == "1" |
| 151 | +} |
| 152 | + |
| 153 | +func volumeArg(src, dest string) string { |
| 154 | + volumeArg := "-v" + src + ":" + dest |
| 155 | + if isSELinuxEnforcing() { |
| 156 | + return volumeArg + ":z" |
| 157 | + } |
| 158 | + return volumeArg |
| 159 | +} |
| 160 | + |
| 161 | +func versionToConformanceImage(kubernetesVersion string, usingCIArtifacts bool) string { |
| 162 | + k8sVersion := strings.ReplaceAll(kubernetesVersion, "+", "_") |
| 163 | + if usingCIArtifacts { |
| 164 | + return ciArtifactImage + ":" + k8sVersion |
| 165 | + } |
| 166 | + return standardImage + ":" + k8sVersion |
| 167 | +} |
| 168 | + |
| 169 | +// buildArgs converts a string map to the format --key=value |
| 170 | +func buildArgs(kv map[string]string, flagMarker string) []string { |
| 171 | + args := make([]string, len(kv)) |
| 172 | + i := 0 |
| 173 | + for k, v := range kv { |
| 174 | + args[i] = flagMarker + k + "=" + v |
| 175 | + i++ |
| 176 | + } |
| 177 | + return args |
| 178 | +} |
0 commit comments