Skip to content

Commit

Permalink
initial commit for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Angelina Nikiforova committed Nov 19, 2019
1 parent c0b9c27 commit a9fcf55
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Makefile
Expand Up @@ -3,9 +3,13 @@ build:
.PHONY: build

test:
go test ./... $(TEST_OPTIONS)
go test $(go list ./... | grep -v /test/) $(TEST_OPTIONS)
.PHONY: test

test-e2e:
go test ./test/integration $(TEST_OPTIONS)
.PHONY: test-e2e

vendor:
go mod tidy
go mod vendor
Expand Down
61 changes: 61 additions & 0 deletions test/integration/bugs_test.go
@@ -0,0 +1,61 @@
package integration

import (
"bytes"
"io"
"testing"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/rest"
)

var (
kubeConfig *rest.Config
kubeClient = KubeClient()
)

// https://bugzilla.redhat.com/show_bug.cgi?id=1750665
func TestDefaultUploadFrequency(t *testing.T) {
// delete any existing overriding secret
err := kubeClient.CoreV1().Secrets("openshift-config").Delete("support", &metav1.DeleteOptions{})

// if the secret is not found, continue, not a problem
if (err != nil) && (err.Error() != "secrets \"support\" not found") {
panic(err.Error())
}

// restart insights-operator (delete pods)
pods, err := kubeClient.CoreV1().Pods("openshift-insights").List(metav1.ListOptions{})
if err != nil {
panic(err.Error())
}

for _, pod := range pods.Items {
kubeClient.CoreV1().Pods("openshift-insights").Delete(pod.Name, &metav1.DeleteOptions{})
}

// check logs for "Gathering cluster info every 2h0m0s"
newPods, err := kubeClient.CoreV1().Pods("openshift-insights").List(metav1.ListOptions{})
if err != nil {
panic(err.Error())
}

for _, newPod := range newPods.Items {
pod, err := kubeClient.CoreV1().Pods("openshift-insights").Get(newPod.Name, metav1.GetOptions{})
if err != nil {
panic(err.Error())
}
req := kubeClient.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, &corev1.PodLogOptions{})
podLogs, err := req.Stream()
defer podLogs.Close()

buf := new(bytes.Buffer)
_, err = io.Copy(buf, podLogs)
if err != nil {
panic(err.Error())
}
log := buf.String()
print(log)
}
}
56 changes: 56 additions & 0 deletions test/integration/main_test.go
@@ -0,0 +1,56 @@
package integration

import (
"fmt"
"os"
"testing"
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)

func KubeClient() (result *kubernetes.Clientset) {
kubeconfig := os.Getenv("KUBECONFIG") // variable is a path to the local kubeconfig

// use the current context in kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
fmt.Printf("%#v", err)
os.Exit(1)
}

kubeClient, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
return kubeClient
}

func TestMain(m *testing.M) {
kubeClient = KubeClient()
// check the operator is up
err := waitForOperator(kubeClient)
if err != nil {
fmt.Println("failed waiting for operator to start")
os.Exit(1)
}
os.Exit(m.Run())
}

func waitForOperator(kubeClient *kubernetes.Clientset) error {
depClient := kubeClient.AppsV1().Deployments("openshift-insights")

err := wait.PollImmediate(1*time.Second, 10*time.Minute, func() (bool, error) {
_, err := depClient.Get("insights-operator", metav1.GetOptions{})
if err != nil {
fmt.Printf("error waiting for operator deployment to exist: %v\n", err)
return false, nil
}
fmt.Println("found operator deployment")
return true, nil
})
return err
}

0 comments on commit a9fcf55

Please sign in to comment.