Skip to content

Commit

Permalink
authpolicy integration tests: wait for route to be accepted
Browse files Browse the repository at this point in the history
  • Loading branch information
eguzki committed Nov 27, 2023
1 parent 249b441 commit c79563f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 55 deletions.
30 changes: 4 additions & 26 deletions controllers/authpolicy_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (

authorinoapi "github.com/kuadrant/authorino/api/v1beta2"
api "github.com/kuadrant/kuadrant-operator/api/v1beta2"
"github.com/kuadrant/kuadrant-operator/pkg/common"
)

const (
Expand Down Expand Up @@ -59,12 +58,7 @@ var _ = Describe("AuthPolicy controller", func() {
route := testBuildBasicHttpRoute(testHTTPRouteName, testGatewayName, testNamespace, []string{"*.toystore.com"})
err = k8sClient.Create(context.Background(), route)
Expect(err).ToNot(HaveOccurred())

Eventually(func() bool {
existingRoute := &gatewayapiv1.HTTPRoute{}
err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(route), existingRoute)
return err == nil && common.IsHTTPRouteAccepted(existingRoute)
}, 15*time.Second, 5*time.Second).Should(BeTrue())
Eventually(testRouteIsAccepted(client.ObjectKeyFromObject(route)), time.Minute, 5*time.Second).Should(BeTrue())
})

It("Attaches policy to the Gateway", func() {
Expand Down Expand Up @@ -144,11 +138,7 @@ var _ = Describe("AuthPolicy controller", func() {
route := testBuildBasicHttpRoute(routeName, gatewayName, testNamespace, []string{"*.api.example.com"})
err = k8sClient.Create(context.Background(), route)
Expect(err).ToNot(HaveOccurred())
Eventually(func() bool {
existingRoute := &gatewayapiv1.HTTPRoute{}
err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(route), existingRoute)
return err == nil && common.IsHTTPRouteAccepted(existingRoute)
}, 15*time.Second, 5*time.Second).Should(BeTrue())
Eventually(testRouteIsAccepted(client.ObjectKeyFromObject(route)), time.Minute, 5*time.Second).Should(BeTrue())

policy := &api.AuthPolicy{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -282,6 +272,7 @@ var _ = Describe("AuthPolicy controller", func() {
}
err = k8sClient.Create(context.Background(), otherRoute)
Expect(err).ToNot(HaveOccurred())
Eventually(testRouteIsAccepted(client.ObjectKeyFromObject(otherRoute)), time.Minute, 5*time.Second).Should(BeTrue())

// attach policy to the gatewaay
gwPolicy := &api.AuthPolicy{
Expand Down Expand Up @@ -835,12 +826,7 @@ var _ = Describe("AuthPolicy controller", func() {
route := testBuildMultipleRulesHttpRoute(testHTTPRouteName, testGatewayName, testNamespace, []string{"*.toystore.com", "*.admin.toystore.com"})
err = k8sClient.Create(context.Background(), route)
Expect(err).ToNot(HaveOccurred())

Eventually(func() bool {
existingRoute := &gatewayapiv1.HTTPRoute{}
err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(route), existingRoute)
return err == nil && common.IsHTTPRouteAccepted(existingRoute)
}, 15*time.Second, 5*time.Second).Should(BeTrue())
Eventually(testRouteIsAccepted(client.ObjectKeyFromObject(route)), time.Minute, 5*time.Second).Should(BeTrue())
})

It("Attaches simple policy to the HTTPRoute", func() {
Expand Down Expand Up @@ -1299,14 +1285,6 @@ func testBasicAuthScheme() api.AuthSchemeSpec {
}
}

func testGatewayIsReady(gateway *gatewayapiv1.Gateway) func() bool {
return func() bool {
existingGateway := &gatewayapiv1.Gateway{}
err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(gateway), existingGateway)
return err == nil && meta.IsStatusConditionTrue(existingGateway.Status.Conditions, common.GatewayProgrammedConditionType)
}
}

func testPolicyIsReady(policy *api.AuthPolicy) func() bool {
return func() bool {
existingPolicy := &api.AuthPolicy{}
Expand Down
16 changes: 16 additions & 0 deletions controllers/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,19 @@ func testBuildMultipleRulesHttpRoute(routeName, gwName, ns string, hostnames []s
}
return route
}

func testRouteIsAccepted(routeKey client.ObjectKey) func() bool {
return func() bool {
route := &gatewayapiv1.HTTPRoute{}
err := k8sClient.Get(context.Background(), routeKey, route)
return err == nil && common.IsHTTPRouteAccepted(route)
}
}

func testGatewayIsReady(gateway *gatewayapiv1.Gateway) func() bool {
return func() bool {
existingGateway := &gatewayapiv1.Gateway{}
err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(gateway), existingGateway)
return err == nil && meta.IsStatusConditionTrue(existingGateway.Status.Conditions, common.GatewayProgrammedConditionType)
}
}
32 changes: 3 additions & 29 deletions controllers/ratelimitpolicy_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ var _ = Describe("RateLimitPolicy controller", func() {
httpRoute := testBuildBasicHttpRoute(routeName, gwName, testNamespace, []string{"*.example.com"})
err := k8sClient.Create(context.Background(), httpRoute)
Expect(err).ToNot(HaveOccurred())
Eventually(testRouteIsAcceptedByGateway(client.ObjectKeyFromObject(httpRoute), gateway), time.Minute, 5*time.Second).Should(BeTrue())
Eventually(testRouteIsAccepted(client.ObjectKeyFromObject(httpRoute)), time.Minute, 5*time.Second).Should(BeTrue())

// create ratelimitpolicy
rlp := &kuadrantv1beta2.RateLimitPolicy{
Expand Down Expand Up @@ -239,7 +239,7 @@ var _ = Describe("RateLimitPolicy controller", func() {
}
err := k8sClient.Create(context.Background(), httpRoute)
Expect(err).ToNot(HaveOccurred())
Eventually(testRouteIsAcceptedByGateway(client.ObjectKeyFromObject(httpRoute), gateway), time.Minute, 5*time.Second).Should(BeTrue())
Eventually(testRouteIsAccepted(client.ObjectKeyFromObject(httpRoute)), time.Minute, 5*time.Second).Should(BeTrue())

// create ratelimitpolicy
rlp := &kuadrantv1beta2.RateLimitPolicy{
Expand Down Expand Up @@ -423,7 +423,7 @@ var _ = Describe("RateLimitPolicy controller", func() {
httpRoute := testBuildBasicHttpRoute(routeName, gwName, testNamespace, []string{"*.example.com"})
err := k8sClient.Create(context.Background(), httpRoute)
Expect(err).ToNot(HaveOccurred())
Eventually(testRouteIsAcceptedByGateway(client.ObjectKeyFromObject(httpRoute), gateway), time.Minute, 5*time.Second).Should(BeTrue())
Eventually(testRouteIsAccepted(client.ObjectKeyFromObject(httpRoute)), time.Minute, 5*time.Second).Should(BeTrue())

// create ratelimitpolicy
rlp := &kuadrantv1beta2.RateLimitPolicy{
Expand Down Expand Up @@ -654,29 +654,3 @@ func testWasmPluginIsAvailable(key client.ObjectKey) func() bool {
return true
}
}

func testRouteIsAcceptedByGateway(key client.ObjectKey, gateway *gatewayapiv1.Gateway) func() bool {
return func() bool {
route := &gatewayapiv1.HTTPRoute{}
err := k8sClient.Get(context.Background(), key, route)
if err != nil {
return false
}

routeParentStatus, found := common.Find(route.Status.RouteStatus.Parents, func(p gatewayapiv1.RouteParentStatus) bool {
return *p.ParentRef.Kind == ("Gateway") &&
((p.ParentRef.Namespace == nil && route.GetNamespace() == gateway.Namespace) || string(*p.ParentRef.Namespace) == gateway.Namespace) &&
string(p.ParentRef.Name) == gateway.Name
})

if !found {
return false
}

if !meta.IsStatusConditionTrue(routeParentStatus.Conditions, "Accepted") {
return false
}

return true
}
}

0 comments on commit c79563f

Please sign in to comment.