/
utils_test.go
60 lines (55 loc) · 1.17 KB
/
utils_test.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
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"strings"
"time"
)
func applyManifest(name string, deleteFirst bool) error {
if deleteFirst {
err := deleteManifest(name)
if err != nil {
return nil
}
time.Sleep(2 * time.Second)
}
apply := exec.Command(kubectl(), "apply", "-f", name)
output, err := apply.Output()
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
stdErr := string(exitError.Stderr)
fmt.Printf("Non-zero exit code while applying, stderr: %s", stdErr)
return errors.New(stdErr)
} else {
return err
}
}
fmt.Print(string(output))
return nil
}
func deleteManifest(name string) error {
delete := exec.Command(kubectl(), "delete", "-f", name)
output, err := delete.Output()
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
stdErr := string(exitError.Stderr)
if !strings.Contains(stdErr, "NotFound") {
fmt.Printf("Unexpected error while deleting, stderr: %s", stdErr)
return errors.New(stdErr)
}
} else {
return err
}
}
fmt.Print(string(output))
return nil
}
func kubectl() string {
kubectl := os.Getenv("KUBECTL")
if kubectl == "" {
kubectl = "kubectl"
}
return kubectl
}