github.com/mponton/terratest@v0.44.0/modules/k8s/kubectl.go (about) 1 package k8s 2 3 import ( 4 "io/ioutil" 5 "net/url" 6 "os" 7 8 "github.com/stretchr/testify/require" 9 10 "github.com/mponton/terratest/modules/shell" 11 "github.com/mponton/terratest/modules/testing" 12 ) 13 14 // RunKubectl will call kubectl using the provided options and args, failing the test on error. 15 func RunKubectl(t testing.TestingT, options *KubectlOptions, args ...string) { 16 require.NoError(t, RunKubectlE(t, options, args...)) 17 } 18 19 // RunKubectlE will call kubectl using the provided options and args. 20 func RunKubectlE(t testing.TestingT, options *KubectlOptions, args ...string) error { 21 _, err := RunKubectlAndGetOutputE(t, options, args...) 22 return err 23 } 24 25 // RunKubectlAndGetOutputE will call kubectl using the provided options and args, returning the output of stdout and 26 // stderr. 27 func RunKubectlAndGetOutputE(t testing.TestingT, options *KubectlOptions, args ...string) (string, error) { 28 cmdArgs := []string{} 29 if options.ContextName != "" { 30 cmdArgs = append(cmdArgs, "--context", options.ContextName) 31 } 32 if options.ConfigPath != "" { 33 cmdArgs = append(cmdArgs, "--kubeconfig", options.ConfigPath) 34 } 35 if options.Namespace != "" { 36 cmdArgs = append(cmdArgs, "--namespace", options.Namespace) 37 } 38 cmdArgs = append(cmdArgs, args...) 39 command := shell.Command{ 40 Command: "kubectl", 41 Args: cmdArgs, 42 Env: options.Env, 43 } 44 return shell.RunCommandAndGetOutputE(t, command) 45 } 46 47 // KubectlDelete will take in a file path and delete it from the cluster targeted by KubectlOptions. If there are any 48 // errors, fail the test immediately. 49 func KubectlDelete(t testing.TestingT, options *KubectlOptions, configPath string) { 50 require.NoError(t, KubectlDeleteE(t, options, configPath)) 51 } 52 53 // KubectlDeleteE will take in a file path and delete it from the cluster targeted by KubectlOptions. 54 func KubectlDeleteE(t testing.TestingT, options *KubectlOptions, configPath string) error { 55 return RunKubectlE(t, options, "delete", "-f", configPath) 56 } 57 58 // KubectlDeleteFromKustomize will take in a kustomization directory path and delete it from the cluster targeted by KubectlOptions. If there are any 59 // errors, fail the test immediately. 60 func KubectlDeleteFromKustomize(t testing.TestingT, options *KubectlOptions, configPath string) { 61 require.NoError(t, KubectlDeleteFromKustomizeE(t, options, configPath)) 62 } 63 64 // KubectlDeleteFromKustomizeE will take in a kustomization directory path and delete it from the cluster targeted by KubectlOptions. 65 func KubectlDeleteFromKustomizeE(t testing.TestingT, options *KubectlOptions, configPath string) error { 66 return RunKubectlE(t, options, "delete", "-k", configPath) 67 } 68 69 // KubectlDeleteFromString will take in a kubernetes resource config as a string and delete it on the cluster specified 70 // by the provided kubectl options. 71 func KubectlDeleteFromString(t testing.TestingT, options *KubectlOptions, configData string) { 72 require.NoError(t, KubectlDeleteFromStringE(t, options, configData)) 73 } 74 75 // KubectlDeleteFromStringE will take in a kubernetes resource config as a string and delete it on the cluster specified 76 // by the provided kubectl options. If it fails, this will return the error. 77 func KubectlDeleteFromStringE(t testing.TestingT, options *KubectlOptions, configData string) error { 78 tmpfile, err := StoreConfigToTempFileE(t, configData) 79 if err != nil { 80 return err 81 } 82 defer os.Remove(tmpfile) 83 return KubectlDeleteE(t, options, tmpfile) 84 } 85 86 // KubectlApply will take in a file path and apply it to the cluster targeted by KubectlOptions. If there are any 87 // errors, fail the test immediately. 88 func KubectlApply(t testing.TestingT, options *KubectlOptions, configPath string) { 89 require.NoError(t, KubectlApplyE(t, options, configPath)) 90 } 91 92 // KubectlApplyE will take in a file path and apply it to the cluster targeted by KubectlOptions. 93 func KubectlApplyE(t testing.TestingT, options *KubectlOptions, configPath string) error { 94 return RunKubectlE(t, options, "apply", "-f", configPath) 95 } 96 97 // KubectlApplyFromKustomize will take in a kustomization directory path and apply it to the cluster targeted by KubectlOptions. If there are any 98 // errors, fail the test immediately. 99 func KubectlApplyFromKustomize(t testing.TestingT, options *KubectlOptions, configPath string) { 100 require.NoError(t, KubectlApplyFromKustomizeE(t, options, configPath)) 101 } 102 103 // KubectlApplyFromKustomizeE will take in a kustomization directory path and apply it to the cluster targeted by KubectlOptions. 104 func KubectlApplyFromKustomizeE(t testing.TestingT, options *KubectlOptions, configPath string) error { 105 return RunKubectlE(t, options, "apply", "-k", configPath) 106 } 107 108 // KubectlApplyFromString will take in a kubernetes resource config as a string and apply it on the cluster specified 109 // by the provided kubectl options. 110 func KubectlApplyFromString(t testing.TestingT, options *KubectlOptions, configData string) { 111 require.NoError(t, KubectlApplyFromStringE(t, options, configData)) 112 } 113 114 // KubectlApplyFromStringE will take in a kubernetes resource config as a string and apply it on the cluster specified 115 // by the provided kubectl options. If it fails, this will return the error. 116 func KubectlApplyFromStringE(t testing.TestingT, options *KubectlOptions, configData string) error { 117 tmpfile, err := StoreConfigToTempFileE(t, configData) 118 if err != nil { 119 return err 120 } 121 defer os.Remove(tmpfile) 122 return KubectlApplyE(t, options, tmpfile) 123 } 124 125 // StoreConfigToTempFile will store the provided config data to a temporary file created on the os and return the 126 // filename. 127 func StoreConfigToTempFile(t testing.TestingT, configData string) string { 128 out, err := StoreConfigToTempFileE(t, configData) 129 require.NoError(t, err) 130 return out 131 } 132 133 // StoreConfigToTempFileE will store the provided config data to a temporary file created on the os and return the 134 // filename, or error. 135 func StoreConfigToTempFileE(t testing.TestingT, configData string) (string, error) { 136 escapedTestName := url.PathEscape(t.Name()) 137 tmpfile, err := ioutil.TempFile("", escapedTestName) 138 if err != nil { 139 return "", err 140 } 141 defer tmpfile.Close() 142 143 _, err = tmpfile.WriteString(configData) 144 return tmpfile.Name(), err 145 }