forked from kubevirt/containerized-data-importer
/
utils.go
104 lines (88 loc) · 2.52 KB
/
utils.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
package tests
import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
"time"
"github.com/onsi/ginkgo"
k8sv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"kubevirt.io/containerized-data-importer/tests/framework"
)
const (
defaultTimeout = 90 * time.Second
testNamespacePrefix = "cdi-test-"
)
// CDIFailHandler call ginkgo.Fail with printing the additional information
func CDIFailHandler(message string, callerSkip ...int) {
if len(callerSkip) > 0 {
callerSkip[0]++
}
ginkgo.Fail(message, callerSkip...)
}
//RunKubectlCommand ...
func RunKubectlCommand(f *framework.Framework, args ...string) (string, error) {
var errb bytes.Buffer
cmd := CreateKubectlCommand(f, args...)
cmd.Stderr = &errb
stdOutBytes, err := cmd.Output()
if err != nil {
if len(errb.String()) > 0 {
return errb.String(), err
}
}
return string(stdOutBytes), nil
}
// CreateKubectlCommand returns the Cmd to execute kubectl
func CreateKubectlCommand(f *framework.Framework, args ...string) *exec.Cmd {
kubeconfig := f.KubeConfig
path := f.KubectlPath
cmd := exec.Command(path, args...)
kubeconfEnv := fmt.Sprintf("KUBECONFIG=%s", kubeconfig)
cmd.Env = append(os.Environ(), kubeconfEnv)
return cmd
}
//PrintControllerLog ...
func PrintControllerLog(f *framework.Framework) {
PrintPodLog(f, f.ControllerPod.Name, f.CdiInstallNs)
}
//PrintPodLog ...
func PrintPodLog(f *framework.Framework, podName, namespace string) {
log, err := RunKubectlCommand(f, "logs", podName, "-n", namespace)
if err == nil {
fmt.Fprintf(ginkgo.GinkgoWriter, "INFO: Pod log\n%s\n", log)
} else {
fmt.Fprintf(ginkgo.GinkgoWriter, "INFO: Unable to get pod log, %s\n", err.Error())
}
}
//PanicOnError ...
func PanicOnError(err error) {
if err != nil {
panic(err)
}
}
// TODO: maybe move this to framework and add it to an AfterEach. Current framework will delete
// all namespaces that it creates.
//DestroyAllTestNamespaces ...
func DestroyAllTestNamespaces(client *kubernetes.Clientset) {
var namespaces *k8sv1.NamespaceList
var err error
if wait.PollImmediate(2*time.Second, defaultTimeout, func() (bool, error) {
namespaces, err = client.CoreV1().Namespaces().List(metav1.ListOptions{})
if err != nil {
return false, nil
}
return true, nil
}) != nil {
ginkgo.Fail("Unable to list namespaces")
}
for _, namespace := range namespaces.Items {
if strings.HasPrefix(namespace.GetName(), testNamespacePrefix) {
framework.DeleteNS(client, namespace.Name)
}
}
}