Skip to content

Commit

Permalink
Move the test for reatest to suite_test.go (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfclere authored Dec 21, 2023
1 parent 176be0d commit 3a91bb2
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 49 deletions.
63 changes: 59 additions & 4 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ package controllers

import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"time"

Expand All @@ -30,6 +33,7 @@ import (
. "github.com/onsi/gomega"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
Expand All @@ -43,8 +47,10 @@ import (
webserversv1alpha1 "github.com/web-servers/jws-operator/api/v1alpha1"
//+kubebuilder:scaffold:imports

core "k8s.io/api/core/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"k8s.io/kubectl/pkg/util/podutils"
)

// These tests use Ginkgo (BDD-style Go testing framework). Refer to
Expand Down Expand Up @@ -154,6 +160,55 @@ var _ = BeforeSuite(func() {
Expect(err).ToNot(HaveOccurred(), "failed to run manager")
}()

// Test if we have all we need in case of realtest
if noskip {
clientCfg, _ := clientcmd.NewDefaultClientConfigLoadingRules().Load()
namespace := clientCfg.Contexts[clientCfg.CurrentContext].Namespace
sec := &corev1.Secret{}
err = k8sClient.Get(context.Background(), client.ObjectKey{
Namespace: namespace,
Name: "secretfortests",
}, sec)
Expect(err).ToNot(HaveOccurred(), "missing secretfortests")

err = k8sClient.Get(context.Background(), client.ObjectKey{
Namespace: namespace,
Name: "test-tls-secret",
}, sec)
Expect(err).ToNot(HaveOccurred(), "missing test-tls-secret")

podList := &corev1.PodList{}
listOpts := []client.ListOption{client.InNamespace("jws-operator-system")}
err = k8sClient.List(ctx, podList, listOpts...)
Expect(err).ToNot(HaveOccurred())
if int32(len(podList.Items)) != 1 {
err = errors.New("number of jws-operator pod incorrect: " + strconv.Itoa(len(podList.Items)))
}
Expect(err).ToNot(HaveOccurred())
pod := podList.Items[0]
if !podutils.IsPodReady(&pod) {
err = errors.New("the jws-operator pod is NOT ready")
}
Expect(err).ToNot(HaveOccurred())

listOpts = []client.ListOption{client.InNamespace("openshift-operators")}
err = k8sClient.List(ctx, podList, listOpts...)
Expect(err).ToNot(HaveOccurred())
if int32(len(podList.Items)) != 0 {
numop := 0
for _, pod := range podList.Items {
if strings.HasPrefix(pod.Name, "jws-operator-controller-manager-") {
numop++
}
}
if numop != 0 {
err = errors.New("operator pods in openshift-operators namespace: " + strconv.Itoa(numop) + "/" + strconv.Itoa(len(podList.Items)))
}
}
Expect(err).ToNot(HaveOccurred())

}

}, 60)

var _ = AfterSuite(func() {
Expand All @@ -166,10 +221,10 @@ var _ = AfterSuite(func() {
Expect(err).NotTo(HaveOccurred())
})

func SetupTest(ctx context.Context) *core.Namespace {
ns := &core.Namespace{}
func SetupTest(ctx context.Context) *corev1.Namespace {
ns := &corev1.Namespace{}

*ns = core.Namespace{
*ns = corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{Name: "test-env"},
}

Expand Down
45 changes: 0 additions & 45 deletions controllers/webServerApplicationImageBasic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package controllers
import (
"context"
"fmt"
"strconv"
"strings"
"time"

. "github.com/onsi/ginkgo"
Expand Down Expand Up @@ -42,49 +40,6 @@ var _ = Describe("WebServer controller", func() {

if noskip {

sec := &corev1.Secret{}

err := k8sClient.Get(context.Background(), client.ObjectKey{
Namespace: namespace,
Name: "secretfortests",
}, sec)
if err != nil {
thetest.Fatal(err)
}

err = k8sClient.Get(context.Background(), client.ObjectKey{
Namespace: namespace,
Name: "test-tls-secret",
}, sec)
if err != nil {
thetest.Fatal(err)
}
podList := &corev1.PodList{}
listOpts := []client.ListOption{client.InNamespace("jws-operator-system")}
err = k8sClient.List(ctx, podList, listOpts...)
if err != nil {
thetest.Fatal(err)
}
if int32(len(podList.Items)) != 1 {
thetest.Fatal("number of jws-operator pod incorrect: " + strconv.Itoa(len(podList.Items)))
}
listOpts = []client.ListOption{client.InNamespace("openshift-operators")}
err = k8sClient.List(ctx, podList, listOpts...)
if err != nil {
thetest.Fatal(err)
}
if int32(len(podList.Items)) != 0 {
numop := 0
for _, pod := range podList.Items {
if strings.HasPrefix(pod.Name, "jws-operator-controller-manager-") {
numop++
}
}
if numop != 0 {
thetest.Fatal("operator pods in openshift-operators namespace: " + strconv.Itoa(numop) + "/" + strconv.Itoa(len(podList.Items)))
}
}

Expect(webserverstests.WebServerApplicationImageSourcesScriptBasicTest(k8sClient, ctx, thetest, namespace, "sourcesscriptbasictest", "quay.io/web-servers/tomcat10:latest", "https://github.com/web-servers/demo-webapp", "jakartaEE", "quay.io/"+username+"/test", "secretfortests", "quay.io/web-servers/tomcat10-buildah", randemo)).Should(Succeed())
Expect(webserverstests.WebServerApplicationImageBasicTest(k8sClient, ctx, thetest, namespace, "rhregistrybasictest", "registry.redhat.io/jboss-webserver-5/jws56-openjdk11-openshift-rhel8", "/health")).Should(Succeed())
Expect(webserverstests.WebServerApplicationImageBasicTest(k8sClient, ctx, thetest, namespace, "basictest", "quay.io/web-servers/tomcat10:latest", "/health")).Should(Succeed())
Expand Down

0 comments on commit 3a91bb2

Please sign in to comment.