containerized-data-importer/tests/utils.go
j-griffith 0a84e2fa8c Add lint checks to remaining go src directories
This finishes up the last of the golint implementation, with the
addition of the cmd, tests and tools directories we are now running
golint tests on all of the current go source files in the project.

This change adds all the little fixes (mostly just commenting and
naming) and also enables the new diretories in the lint test that we
gate on.
2018-09-29 08:35:21 -06:00

93 lines
2.2 KiB
Go

package tests
import (
"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 = 30 * 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) {
kubeconfig := f.KubeConfig
path := f.KubectlPath
cmd := exec.Command(path, args...)
kubeconfEnv := fmt.Sprintf("KUBECONFIG=%s", kubeconfig)
cmd.Env = append(os.Environ(), kubeconfEnv)
stdOutBytes, err := cmd.Output()
if err != nil {
return string(stdOutBytes), err
}
return string(stdOutBytes), nil
}
//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: Controller log\n%s\n", log)
} else {
fmt.Fprintf(ginkgo.GinkgoWriter, "INFO: Unable to get controller log")
}
}
//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)
}
}
}