Skip to content

Commit

Permalink
fix ns backup test (kyma-project#3336)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakkab authored Mar 26, 2019
1 parent 7830ec7 commit 14a0087
Showing 1 changed file with 22 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package backupe2e

import (
"fmt"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
Expand All @@ -15,8 +14,7 @@ import (
const resourceQuotaObjName = "kyma-default"

type namespaceControllerTest struct {
namespaceName string
coreClient *kubernetes.Clientset
coreClient *kubernetes.Clientset
}

func NewNamespaceControllerTest() (namespaceControllerTest, error) {
Expand All @@ -30,59 +28,56 @@ func NewNamespaceControllerTest() (namespaceControllerTest, error) {
}

return namespaceControllerTest{
namespaceName: "test-ns",
coreClient: coreClient,
coreClient: coreClient,
}, nil
}

func (n namespaceControllerTest) CreateResources(_ string) {
err := n.createTestNamespace()
func (n namespaceControllerTest) CreateResources(namespace string) {
err := n.labelTestNamespace(namespace)
So(err, ShouldBeNil)
}

func (n namespaceControllerTest) TestResources(namespace string) {
err := n.waitForResources()
err := n.waitForResourceQuota(namespace)
So(err, ShouldBeNil)
}

func (n namespaceControllerTest) createTestNamespace() error {
func (n namespaceControllerTest) labelTestNamespace(namespaceName string) error {

testNamespace := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: n.namespaceName,
Labels: map[string]string{"env": "true"},
},
namespace, err := n.coreClient.CoreV1().Namespaces().Get(namespaceName, metav1.GetOptions{})
if err != nil {
return err
}

_, err := n.coreClient.CoreV1().Namespaces().Create(testNamespace)
labels := namespace.GetLabels()
labels["env"] = "true"

namespaceCopy := namespace.DeepCopy()
namespaceCopy.SetLabels(labels)

_, err = n.coreClient.CoreV1().Namespaces().Update(namespaceCopy)
return err
}

func (n namespaceControllerTest) waitForResources() error {
func (n namespaceControllerTest) waitForResourceQuota(namespaceName string) error {

timeout := time.After(5 * time.Second)
tick := time.Tick(1 * time.Second)

var messages string

for {
select {
case <-tick:
testNamespace, err := n.coreClient.CoreV1().Namespaces().Get(n.namespaceName, metav1.GetOptions{})
if err != nil {
continue
}

if testNamespace.Status.Phase != corev1.NamespaceActive {
continue
}

_, err = n.coreClient.CoreV1().ResourceQuotas(n.namespaceName).Get(resourceQuotaObjName, metav1.GetOptions{})
_, err := n.coreClient.CoreV1().ResourceQuotas(namespaceName).Get(resourceQuotaObjName, metav1.GetOptions{})
if err != nil {
messages += fmt.Sprintf("%+v\n", err)
continue
}

return nil

case <-timeout:
return fmt.Errorf("unable to fetch namespace %v or reqource quota %v", n.namespaceName, resourceQuotaObjName)}
return fmt.Errorf("unable to fetch resourcequota:\n %v", messages)}
}
}

0 comments on commit 14a0087

Please sign in to comment.