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 ac41707
Showing 1 changed file with 56 additions and 8 deletions.
64 changes: 56 additions & 8 deletions e2e/service_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,48 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

capsulev1beta1 "github.com/clastix/capsule/api/v1beta1"
)

var _ = Describe("adding metadata to Service objects", 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())

tnt := &capsulev1beta1.Tenant{
ObjectMeta: metav1.ObjectMeta{
Name: "service-metadata",
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 ac41707

Please sign in to comment.