Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
269 changes: 147 additions & 122 deletions functests/cincinnati_creation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,141 +5,166 @@ import (
"crypto/tls"
"fmt"
"net/http"
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
routev1 "github.com/openshift/api/route/v1"
cincinnativ1beta1 "github.com/openshift/cincinnati-operator/pkg/apis/cincinnati/v1beta1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog"
client "sigs.k8s.io/controller-runtime/pkg/client"
)

var _ = Describe("CustomResource", func() {
RegisterFailHandler(Fail)

func TestCustomResource(t *testing.T) {
k8sClient, err := getK8sClient()
Expect(k8sClient).NotTo(BeNil())
Expect(err).To(BeNil())
if err != nil {
t.Fatal(err)
}

if err := waitForDeployment(k8sClient, operatorName); err != nil {
t.Fatal(err)
}

labelSelector := metav1.LabelSelector{MatchLabels: map[string]string{"name": operatorName}}
listOptions := metav1.ListOptions{
LabelSelector: labels.Set(labelSelector.MatchLabels).String(),
}
if pod, err := k8sClient.CoreV1().Pods(operatorNamespace).List(listOptions); err != nil {
t.Fatal(err)
} else {
if len(pod.Items) < 1 {
t.Fatalf("no pods found in %s matching %s", operatorNamespace, operatorName)
}

if pod.Items[0].Status.Phase != "Running" {
t.Fatalf("unexpected pod %s phase %q (expected Running)", operatorName, pod.Items[0].Status.Phase)
}

if !pod.Items[0].Status.ContainerStatuses[0].Ready {
t.Fatalf("unexpected pod %s container status ready %t (expected true)", operatorName, pod.Items[0].Status.ContainerStatuses[0].Ready)
}
}

if err := waitForService(k8sClient, operatorName+"-metrics"); err != nil {
t.Fatal(err)
}

defer func() {
if err := deleteCR(); err != nil {
t.Log(err)
}
}()

if err := deployCR(); err != nil {
t.Fatal(err)
}

cincinnatiClient, err := getCincinnatiClient()
if err != nil {
t.Fatal(err)
}

result := &cincinnativ1beta1.Cincinnati{}
err = cincinnatiClient.Get().
Resource(resource).
Namespace(operatorNamespace).
Name(customResourceName).
Do().
Into(result)
if err != nil {
t.Fatal(err)
}

if err := waitForDeployment(k8sClient, customResourceName); err != nil {
t.Fatal(err)
}

labelSelector = metav1.LabelSelector{MatchLabels: map[string]string{"app": customResourceName}}
listOptions = metav1.ListOptions{
LabelSelector: labels.Set(labelSelector.MatchLabels).String(),
}
if pod, err := k8sClient.CoreV1().Pods(operatorNamespace).List(listOptions); err != nil {
t.Fatal(err)
} else {
if len(pod.Items) < 1 {
t.Fatalf("no pods found in %s matching %s", operatorNamespace, customResourceName)
}

BeforeEach(func() {
err = waitForDeployment(k8sClient, operatorName)
Expect(err).To(BeNil())
if pod.Items[0].Status.Phase != "Running" {
t.Fatalf("unexpected pod %s phase %q (expected Running)", customResourceName, pod.Items[0].Status.Phase)
}

labelSelector := metav1.LabelSelector{MatchLabels: map[string]string{"name": operatorName}}
listOptions := metav1.ListOptions{
LabelSelector: labels.Set(labelSelector.MatchLabels).String(),
for _, container := range pod.Items[0].Status.InitContainerStatuses {
if !container.Ready {
t.Fatalf("unexpected pod %s init-container %s status ready %t (expected true)", customResourceName, container.Name, container.Ready)
}
}
pod, err := k8sClient.CoreV1().Pods(operatorNamespace).List(listOptions)
Expect(err).To(BeNil())
Expect(string(pod.Items[0].Status.Phase)).Should(Equal("Running"))
Expect(bool(pod.Items[0].Status.ContainerStatuses[0].Ready)).Should(Equal(true))

err = waitForService(k8sClient, operatorName+"-metrics")
Expect(err).To(BeNil())

err = deployCR()
Expect(err).To(BeNil())
})

AfterEach(func() {
err = deleteCR()
Expect(err).To(BeNil())
})

When("Testing custom resource", func() {
It("Should get the custom resource, verify if the deployment is available, pods, services and PodDisruptionBudget are available and route is accessible", func() {
cincinnatiClient, err := getCincinnatiClient()
Expect(cincinnatiClient).NotTo(BeNil())
Expect(err).To(BeNil())

By("Verifying if Cincinnati custom resource is available")
result := &cincinnativ1beta1.Cincinnati{}
err = cincinnatiClient.Get().
Resource(resource).
Namespace(operatorNamespace).
Name(customResourceName).
Do().
Into(result)
Expect(err).To(BeNil())

By("Verifying if deployment " + customResourceName + " is available")
err = waitForDeployment(k8sClient, customResourceName)
Expect(err).To(BeNil())

By("Verifying if pods are running")
labelSelector := metav1.LabelSelector{MatchLabels: map[string]string{"app": customResourceName}}
listOptions := metav1.ListOptions{
LabelSelector: labels.Set(labelSelector.MatchLabels).String(),

for _, container := range pod.Items[0].Status.ContainerStatuses {
if !container.Ready {
t.Fatalf("unexpected pod %s container %s status ready %t (expected true)", customResourceName, container.Name, container.Ready)
}
pod, err := k8sClient.CoreV1().Pods(operatorNamespace).List(listOptions)
Expect(string(pod.Items[0].Status.Phase)).Should(Equal("Running"))
Expect(bool(pod.Items[0].Status.ContainerStatuses[0].Ready)).Should(Equal(true))
Expect(bool(pod.Items[0].Status.ContainerStatuses[1].Ready)).Should(Equal(true))
Expect(bool(pod.Items[0].Status.InitContainerStatuses[0].Ready)).Should(Equal(true))
Expect(err).To(BeNil())

By("Verifying if " + customResourceName + "-graph-builder service is available")
err = waitForService(k8sClient, customResourceName+"-graph-builder")
Expect(err).To(BeNil())

By("Verifying if " + customResourceName + "-policy-engine service is available")
err = waitForService(k8sClient, customResourceName+"-policy-engine")
Expect(err).To(BeNil())

By("Verifying if PodDisruptionBudget is available")
// Checks to see if a given PodDisruptionBudget is available after a specified amount of time.
// If the PodDisruptionBudget is not available after 30 * retries seconds, the condition function returns an error.
err = wait.Poll(retryInterval, timeout, func() (done bool, err error) {
_, err2 := k8sClient.PolicyV1beta1().PodDisruptionBudgets(operatorNamespace).Get(customResourceName, metav1.GetOptions{})
if err2 != nil {
if apierrors.IsNotFound(err2) {
klog.Infof("Waiting for availability of %s PodDisruptionBudget\n", operatorName)
return false, nil
}
return false, err2
}
return true, nil
})
Expect(err).To(BeNil())
klog.Infof("PodDisruptionBudget %s available\n", operatorName)

By("Verifying if route is available")
crClient, err := getCrClient()
Expect(crClient).NotTo(BeNil())
Expect(err).To(BeNil())

route := &routev1.Route{}
err = crClient.Get(context.Background(), client.ObjectKey{
Namespace: operatorNamespace,
Name: routeName,
}, route)
Expect(err).To(BeNil())

By("Verifying if route is accessible")
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
}

if err := waitForService(k8sClient, customResourceName+"-graph-builder"); err != nil {
t.Fatal(err)
}

if err := waitForService(k8sClient, customResourceName+"-policy-engine"); err != nil {
t.Fatal(err)
}

// Checks to see if a given PodDisruptionBudget is available after a specified amount of time.
// If the PodDisruptionBudget is not available after 30 * retries seconds, the condition function returns an error.
if err := wait.Poll(retryInterval, timeout, func() (done bool, err error) {
if _, err := k8sClient.PolicyV1beta1().PodDisruptionBudgets(operatorNamespace).Get(customResourceName, metav1.GetOptions{}); err != nil {
if apierrors.IsNotFound(err) {
t.Logf("Waiting for availability of %s PodDisruptionBudget\n", operatorName)
return false, nil
}
httpClient := &http.Client{Transport: tr}

req, err := http.NewRequest("GET", fmt.Sprintf("https://%s/api/upgrades_info/v1/graph?channel=stable-4.4", route.Spec.Host), nil)
Expect(err).To(BeNil())
req.Header.Set("Accept", "application/json")
err = wait.Poll(retryInterval, timeout, func() (done bool, err error) {
resp, err2 := httpClient.Do(req)
Expect(err2).To(BeNil())
if resp.StatusCode > 200 {
klog.Infof("Waiting for availability of %s route\n", routeName)
return false, nil
}
klog.Infof("Route %s available\n", routeName)
return true, nil
})
Expect(err).To(BeNil())
})

})
})
return false, err
}
return true, nil
}); err != nil {
t.Fatal(err)
}
t.Logf("PodDisruptionBudget %s available", operatorName)

crClient, err := getCrClient()
if err != nil {
t.Fatal(err)
}

route := &routev1.Route{}
if err := crClient.Get(context.Background(), client.ObjectKey{
Namespace: operatorNamespace,
Name: routeName,
}, route); err != nil {
t.Fatal(err)
}

tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
httpClient := &http.Client{Transport: tr}

req, err := http.NewRequest("GET", fmt.Sprintf("https://%s/api/upgrades_info/v1/graph?channel=stable-4.4", route.Spec.Host), nil)
if err != nil {
t.Fatal(err)
}
req.Header.Set("Accept", "application/json")
if err := wait.Poll(retryInterval, timeout, func() (done bool, err error) {
if resp, err := httpClient.Do(req); err != nil {
t.Fatal(err)
} else if resp.StatusCode > 200 {
t.Logf("Waiting for availability of %s route", routeName)
return false, nil
}
t.Logf("Route %s available", routeName)
return true, nil
}); err != nil {
t.Fatal(err)
}
}
51 changes: 0 additions & 51 deletions functests/functests_suite_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ go 1.13

require (
github.com/go-logr/logr v0.1.0
github.com/onsi/ginkgo v1.12.0
github.com/onsi/gomega v1.10.0
github.com/onsi/ginkgo v1.12.0 // indirect
github.com/onsi/gomega v1.10.0 // indirect
github.com/openshift/api v3.9.1-0.20190924102528-32369d4db2ad+incompatible
github.com/openshift/cluster-image-registry-operator v0.0.0-20200415091009-99c06ee64540
github.com/openshift/custom-resource-status v0.0.0-20190822192428-e62f2f3b79f3
Expand Down
18 changes: 1 addition & 17 deletions hack/functest.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
#!/bin/bash

GOBIN="${GOBIN:-$GOPATH/bin}"
GINKGO=$GOBIN/ginkgo

if ! [ -x "$GINKGO" ]; then
# The golang-1.13 image used in OpenShift CI enforces vendoring and go get is disabled.
# Workaround that by unsetting GOFLAGS.
export GOFLAGS=""
echo "Retrieving ginkgo and gomega build dependencies"
go get github.com/onsi/ginkgo/ginkgo
go get github.com/onsi/gomega/...
else
echo "GINKO binary found at $GINKGO"
fi

"$GOBIN"/ginkgo build functests/
# Run functional tests
"$GOBIN"/ginkgo -v --slowSpecThreshold=500 --randomizeAllSpecs --progress functests
go test ./functests/...

This file was deleted.

18 changes: 0 additions & 18 deletions vendor/github.com/hpcloud/tail/.travis.yml

This file was deleted.

Loading