Skip to content

Commit

Permalink
test(e2e): fixing flakiness for Service and EP metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
prometherion committed Aug 7, 2021
1 parent f15786d commit d698da2
Showing 1 changed file with 58 additions and 10 deletions.
68 changes: 58 additions & 10 deletions e2e/service_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package e2e
import (
"context"

capsulev1beta1 "github.com/clastix/capsule/api/v1beta1"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
Expand All @@ -17,8 +18,7 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/utils/pointer"

capsulev1beta1 "github.com/clastix/capsule/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)

var _ = Describe("adding metadata to Service objects", func() {
Expand Down Expand Up @@ -60,11 +60,48 @@ var _ = Describe("adding metadata to Service objects", func() {
}

JustBeforeEach(func() {
// Mocking EndpointSlice controller Cluster-Role in case it's missing
cr := &rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: "system:controller:endpointslice-controller",
},
}

_, err := controllerutil.CreateOrUpdate(context.Background(), k8sClient, cr, func() error {
cr.Rules = []rbacv1.PolicyRule{
{
Verbs: []string{"get", "list", "watch"},
APIGroups: []string{""},
Resources: []string{"nodes", "pod", "services"},
},
{
Verbs: []string{"update"},
APIGroups: []string{""},
Resources: []string{"services/finalizers"},
},
{
Verbs: []string{"create", "delete", "get", "list", "update"},
APIGroups: []string{"discovery.k8s.io"},
Resources: []string{"endpointslices"},
},
{
Verbs: []string{"create", "patch", "update"},
APIGroups: []string{"", "events.k8s.io"},
Resources: []string{"events"},
},
}

return nil
})

Expect(err).Should(Succeed())

EventuallyCreation(func() error {
tnt.ResourceVersion = ""
return k8sClient.Create(context.TODO(), tnt)
}).Should(Succeed())
})

JustAfterEach(func() {
Expect(k8sClient.Delete(context.TODO(), tnt)).Should(Succeed())
})
Expand Down Expand Up @@ -93,8 +130,11 @@ var _ = Describe("adding metadata to Service objects", func() {
},
},
}
EventuallyCreation(func() error {
return k8sClient.Create(context.TODO(), svc)

EventuallyCreation(func() (err error) {
_, err = ownerClient(tnt.Spec.Owners[0]).CoreV1().Services(ns.GetName()).Create(context.Background(), svc, metav1.CreateOptions{})

return
}).Should(Succeed())

By("checking additional labels", func() {
Expand All @@ -109,6 +149,7 @@ var _ = Describe("adding metadata to Service objects", func() {
return true
}, defaultTimeoutInterval, defaultPollInterval).Should(BeTrue())
})

By("checking additional annotations", func() {
Eventually(func() (ok bool) {
Expect(k8sClient.Get(context.TODO(), types.NamespacedName{Name: svc.GetName(), Namespace: ns.GetName()}, svc)).Should(Succeed())
Expand Down Expand Up @@ -149,9 +190,13 @@ var _ = Describe("adding metadata to Service objects", func() {
},
},
}
EventuallyCreation(func() error {
return k8sClient.Create(context.TODO(), ep)

EventuallyCreation(func() (err error) {
_, err = ownerClient(tnt.Spec.Owners[0]).CoreV1().Endpoints(ns.GetName()).Create(context.Background(), ep, metav1.CreateOptions{})

return
}).Should(Succeed())

By("checking additional labels", func() {
Eventually(func() (ok bool) {
Expect(k8sClient.Get(context.TODO(), types.NamespacedName{Name: ep.GetName(), Namespace: ns.GetName()}, ep)).Should(Succeed())
Expand All @@ -164,6 +209,7 @@ var _ = Describe("adding metadata to Service objects", func() {
return true
}, defaultTimeoutInterval, defaultPollInterval).Should(BeTrue())
})

By("checking additional annotations", func() {
Eventually(func() (ok bool) {
Expect(k8sClient.Get(context.TODO(), types.NamespacedName{Name: ep.GetName(), Namespace: ns.GetName()}, ep)).Should(Succeed())
Expand All @@ -179,8 +225,7 @@ var _ = Describe("adding metadata to Service objects", func() {
})

It("should apply them to EndpointSlice", func() {
maj, min, v := GetKubernetesSemVer()
if maj == 1 && min <= 16 {
if maj, min, v := GetKubernetesSemVer(); maj == 1 && min <= 16 {
Skip("Running test on Kubernetes " + v + ", doesn't provide EndpointSlice resource")
}

Expand All @@ -207,8 +252,10 @@ var _ = Describe("adding metadata to Service objects", func() {
},
}

EventuallyCreation(func() error {
return k8sClient.Create(context.TODO(), eps)
EventuallyCreation(func() (err error) {
_, err = ownerClient(tnt.Spec.Owners[0]).DiscoveryV1beta1().EndpointSlices(ns.GetName()).Create(context.Background(), eps, metav1.CreateOptions{})

return
}).Should(Succeed())

By("checking additional annotations EndpointSlice", func() {
Expand All @@ -223,6 +270,7 @@ var _ = Describe("adding metadata to Service objects", func() {
return true
}, defaultTimeoutInterval, defaultPollInterval).Should(BeTrue())
})

By("checking additional labels on EndpointSlice", func() {
Eventually(func() (ok bool) {
Expect(k8sClient.Get(context.TODO(), types.NamespacedName{Name: eps.GetName(), Namespace: ns.GetName()}, eps)).Should(Succeed())
Expand Down

0 comments on commit d698da2

Please sign in to comment.