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