github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/pkg/test/live/kind.go (about) 1 // Copyright 2021 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package live 16 17 import ( 18 "bufio" 19 "bytes" 20 "fmt" 21 "os" 22 "os/exec" 23 "strings" 24 "testing" 25 ) 26 27 const ( 28 KindClusterName = "live-e2e-test" 29 K8sVersionEnvName = "K8S_VERSION" 30 ) 31 32 func InstallResourceGroup(t *testing.T) { 33 cmd := exec.Command("kpt", "live", "install-resource-group") 34 if out, err := cmd.CombinedOutput(); err != nil { 35 t.Fatalf("error installing ResourceGroup CRD: %v\n%s", err, out) 36 } 37 } 38 39 func RemoveResourceGroup(t *testing.T) { 40 cmd := exec.Command("kubectl", "delete", "crd", "resourcegroups.kpt.dev") 41 if out, err := cmd.CombinedOutput(); err != nil { 42 t.Fatalf("error removing ResourceGroup CRD: %v\n%s", err, out) 43 } 44 if CheckIfResourceGroupInstalled(t) { 45 t.Fatalf("couldn't remove ResourceGroup CRD") 46 } 47 } 48 49 func CheckIfResourceGroupInstalled(t *testing.T) bool { 50 cmd := exec.Command("kubectl", "get", "resourcegroups.kpt.dev") 51 output, err := cmd.CombinedOutput() 52 if strings.Contains(string(output), "the server doesn't have a resource type") { 53 return false 54 } 55 if err != nil { 56 t.Fatalf("error checking for ResourceGroup CRD: %v", err) 57 } 58 return true 59 } 60 61 func CheckKindClusterAvailable(t *testing.T) bool { 62 cmd := exec.Command("kind", "get", "clusters") 63 output, err := cmd.CombinedOutput() 64 if err != nil { 65 t.Fatalf("failed to check for kind cluster: %v", err) 66 } 67 68 sc := bufio.NewScanner(bytes.NewReader(output)) 69 for sc.Scan() { 70 if strings.TrimSpace(sc.Text()) == KindClusterName { 71 return true 72 } 73 } 74 if err := sc.Err(); err != nil { 75 t.Fatalf("error parsing output from 'kind get cluster': %v", err) 76 } 77 return false 78 } 79 80 func CreateKindCluster(t *testing.T) { 81 args := []string{"create", "cluster", fmt.Sprintf("--name=%s", KindClusterName)} 82 if k8sVersion := os.Getenv(K8sVersionEnvName); k8sVersion != "" { 83 t.Logf("Using version %s", k8sVersion) 84 args = append(args, fmt.Sprintf("--image=kindest/node:v%s", k8sVersion)) 85 } 86 cmd := exec.Command("kind", args...) 87 if out, err := cmd.CombinedOutput(); err != nil { 88 t.Fatalf("failed to create new kind cluster: %v\n%s", err, out) 89 } 90 } 91 92 func RemoveKindCluster(t *testing.T) { 93 cmd := exec.Command("kind", "delete", "cluster", fmt.Sprintf("--name=%s", KindClusterName)) 94 if out, err := cmd.CombinedOutput(); err != nil { 95 t.Fatalf("failed to remove existing cluster: %v\n%s", err, out) 96 } 97 } 98 99 func CreateNamespace(t *testing.T, namespace string) { 100 cmd := exec.Command("kubectl", "create", "ns", namespace) 101 if out, err := cmd.CombinedOutput(); err != nil { 102 t.Fatalf("error creating namespace %s: %v\n%s", namespace, err, out) 103 } 104 } 105 106 func RemoveNamespace(t *testing.T, namespace string) { 107 cmd := exec.Command("kubectl", "delete", "ns", namespace, "--wait=false") 108 if out, err := cmd.CombinedOutput(); err != nil { 109 t.Logf("error deleting namespace %s: %v\n%s", namespace, err, out) 110 } 111 }