github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/test/e2e/testdata/smoketest/smoketestrun.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 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 smoketest 18 19 import ( 20 "context" 21 "log" 22 "os" 23 "strings" 24 "time" 25 26 . "github.com/onsi/ginkgo/v2" 27 . "github.com/onsi/gomega" 28 "github.com/sirupsen/logrus" 29 apierrors "k8s.io/apimachinery/pkg/api/errors" 30 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 31 "k8s.io/apimachinery/pkg/runtime" 32 "k8s.io/client-go/dynamic" 33 34 extensionsv1alpha1 "github.com/1aal/kubeblocks/apis/extensions/v1alpha1" 35 . "github.com/1aal/kubeblocks/test/e2e" 36 e2eutil "github.com/1aal/kubeblocks/test/e2e/util" 37 "github.com/1aal/kubeblocks/test/testutils" 38 ) 39 40 const ( 41 timeout time.Duration = time.Second * 1500 42 interval time.Duration = time.Second * 10 43 ) 44 45 type Options struct { 46 Dynamic dynamic.Interface 47 } 48 49 func SmokeTest() { 50 BeforeEach(func() { 51 }) 52 53 AfterEach(func() { 54 }) 55 56 Context("KubeBlocks smoke test", func() { 57 It("check addon", func() { 58 cfg, err := e2eutil.GetConfig() 59 if err != nil { 60 logrus.WithError(err).Fatal("could not get config") 61 } 62 dynamic, err := dynamic.NewForConfig(cfg) 63 if err != nil { 64 logrus.WithError(err).Fatal("could not generate dynamic client for config") 65 } 66 objects, err := dynamic.Resource(testutils.AddonGVR()).List(context.TODO(), metav1.ListOptions{ 67 LabelSelector: e2eutil.BuildAddonLabelSelector(), 68 }) 69 if err != nil && !apierrors.IsNotFound(err) { 70 log.Println(err) 71 } 72 if objects == nil || len(objects.Items) == 0 { 73 log.Println("No Addons found") 74 } 75 if len(objects.Items) > 0 { 76 for _, obj := range objects.Items { 77 addon := extensionsv1alpha1.Addon{} 78 if err = runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &addon); err != nil { 79 log.Println(err) 80 } 81 if addon.Status.ObservedGeneration == 0 { 82 log.Printf("Addon %s is not observed yet", addon.Name) 83 } 84 // addon is enabled, then check its status 85 if addon.Spec.InstallSpec.GetEnabled() { 86 if addon.Status.Phase != extensionsv1alpha1.AddonEnabled { 87 log.Printf("Addon %s is not enabled yet", addon.Name) 88 } 89 } 90 } 91 } 92 }) 93 94 It("run test cases", func() { 95 dir, err := os.Getwd() 96 if err != nil { 97 log.Println(err) 98 } 99 log.Println("====================start " + TestType + " e2e test====================") 100 if len(TestType) == 0 { 101 folders, _ := e2eutil.GetFolders(dir + "/testdata/smoketest") 102 for _, folder := range folders { 103 if folder == dir+"/testdata/smoketest" { 104 continue 105 } 106 log.Println("folder: " + folder) 107 getFiles(folder) 108 } 109 } else { 110 getFiles(dir + "/testdata/smoketest/" + TestType) 111 } 112 }) 113 }) 114 } 115 116 func getFiles(folder string) { 117 files, _ := e2eutil.GetFiles(folder) 118 var clusterVersions []string 119 if len(clusterVersions) > 1 { 120 for _, clusterVersion := range clusterVersions { 121 if len(files) > 0 { 122 file := e2eutil.GetClusterCreateYaml(files) 123 e2eutil.ReplaceClusterVersionRef(file, clusterVersion) 124 runTestCases(files) 125 } 126 } 127 } else { 128 runTestCases(files) 129 } 130 } 131 132 func runTestCases(files []string) { 133 var clusterName, nameSpace string 134 var testResult bool 135 shouldSkip := false 136 for _, file := range files { 137 fileName := e2eutil.GetPrefix(file, "/") 138 if len(SkipCase) > 0 { 139 if strings.Contains(SkipCase, ",") { 140 for _, c := range strings.Split(SkipCase, ",") { 141 if strings.Contains(file, c) { 142 e2eResult := NewResult(fileName, false, "") 143 TestResults = append(TestResults, e2eResult) 144 shouldSkip = true 145 break 146 } 147 } 148 if shouldSkip { 149 shouldSkip = false 150 continue 151 } 152 } else { 153 if strings.Contains(file, SkipCase) { 154 e2eResult := NewResult(fileName, false, "") 155 TestResults = append(TestResults, e2eResult) 156 continue 157 } 158 } 159 } 160 if strings.Contains(file, "restore") { 161 backups := "kubectl get backup | awk '{print $1}' | tail -n +2" 162 log.Println(backups) 163 backupList := e2eutil.ExecCommandReadline(backups) 164 log.Println(backupList) 165 Eventually(func(g Gomega) { 166 for _, backup := range backupList { 167 cmd := "kubectl get backup " + backup + " -o=jsonpath='{.status.phase}'" 168 log.Println(cmd) 169 log.Println(e2eutil.ExecCommand(cmd)) 170 g.Expect(e2eutil.ExecCommand(cmd)).Should(Equal("Completed")) 171 } 172 }, timeout, interval).Should(Succeed()) 173 } 174 b := e2eutil.OpsYaml(file, "create") 175 if strings.Contains(file, "00") || strings.Contains(file, "restore") { 176 clusterName, nameSpace = e2eutil.GetName(file) 177 log.Println("clusterName is " + clusterName) 178 } 179 Expect(b).Should(BeTrue()) 180 if !strings.Contains(file, "stop") { 181 testResult = false 182 Eventually(func(g Gomega) { 183 e2eutil.WaitTime(1000000) 184 podStatusResult := e2eutil.CheckPodStatus(clusterName, nameSpace) 185 for _, result := range podStatusResult { 186 g.Expect(result).Should(BeTrue()) 187 } 188 }, timeout, interval).Should(Succeed()) 189 Eventually(func(g Gomega) { 190 clusterStatusResult := e2eutil.CheckClusterStatus(clusterName, nameSpace, "Running") 191 g.Expect(clusterStatusResult).Should(BeTrue()) 192 }, timeout, interval).Should(Succeed()) 193 testResult = true 194 } else { 195 testResult = false 196 Eventually(func(g Gomega) { 197 clusterStatusResult := e2eutil.CheckClusterStatus(clusterName, nameSpace, "Stopped") 198 g.Expect(clusterStatusResult).Should(BeTrue()) 199 }, timeout, interval).Should(Succeed()) 200 testResult = true 201 } 202 log.Println(testResult) 203 if testResult { 204 e2eResult := NewResult(fileName, testResult, "") 205 TestResults = append(TestResults, e2eResult) 206 } else { 207 out := troubleShooting(clusterName) 208 e2eResult := NewResult(fileName, testResult, out) 209 TestResults = append(TestResults, e2eResult) 210 } 211 } 212 } 213 214 func troubleShooting(clusterName string) string { 215 cmd := "kubectl get all -l app.kubernetes.io/instance=" + clusterName 216 allResourceStatus := e2eutil.ExecCommand(cmd) 217 commond := "kubectl describe cluster " + clusterName 218 clusterEvents := e2eutil.ExecCommand(commond) 219 return allResourceStatus + clusterEvents 220 }