Skip to content

Commit

Permalink
refactor: integration test policy factory mutateFn
Browse files Browse the repository at this point in the history
  • Loading branch information
KevFan committed Dec 14, 2023
1 parent ba26ec2 commit 5f3fed7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 41 deletions.
48 changes: 24 additions & 24 deletions controllers/authpolicy_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var _ = Describe("AuthPolicy controller", func() {

AfterEach(DeleteNamespaceCallback(&testNamespace))

apFactory := func(mutateFn func(policy *api.AuthPolicy)) *api.AuthPolicy {
policyFactory := func(mutateFns ...func(policy *api.AuthPolicy)) *api.AuthPolicy {
policy := &api.AuthPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: "toystore",
Expand All @@ -65,7 +65,7 @@ var _ = Describe("AuthPolicy controller", func() {
AuthScheme: testBasicAuthScheme(),
},
}
if mutateFn != nil {
for _, mutateFn := range mutateFns {
mutateFn(policy)
}
return policy
Expand All @@ -83,7 +83,7 @@ var _ = Describe("AuthPolicy controller", func() {
})

It("Attaches policy to the Gateway", func() {
policy := apFactory(func(policy *api.AuthPolicy) {
policy := policyFactory(func(policy *api.AuthPolicy) {
policy.Name = "gw-auth"
policy.Spec.TargetRef.Group = "gateway.networking.k8s.io"
policy.Spec.TargetRef.Kind = "Gateway"
Expand Down Expand Up @@ -152,7 +152,7 @@ var _ = Describe("AuthPolicy controller", func() {
Expect(err).ToNot(HaveOccurred())
Eventually(testRouteIsAccepted(client.ObjectKeyFromObject(route)), time.Minute, 5*time.Second).Should(BeTrue())

policy := apFactory(func(policy *api.AuthPolicy) {
policy := policyFactory(func(policy *api.AuthPolicy) {
policy.Name = "gw-auth"
policy.Spec.TargetRef.Group = "gateway.networking.k8s.io"
policy.Spec.TargetRef.Kind = "Gateway"
Expand All @@ -179,7 +179,7 @@ var _ = Describe("AuthPolicy controller", func() {
})

It("Attaches policy to the HTTPRoute", func() {
policy := apFactory(nil)
policy := policyFactory()

err := k8sClient.Create(context.Background(), policy)
logf.Log.V(1).Info("Creating AuthPolicy", "key", client.ObjectKeyFromObject(policy).String(), "error", err)
Expand Down Expand Up @@ -225,7 +225,7 @@ var _ = Describe("AuthPolicy controller", func() {
})

It("Attaches policy to the Gateway while having other policies attached to some HTTPRoutes", func() {
routePolicy := apFactory(nil)
routePolicy := policyFactory()

err := k8sClient.Create(context.Background(), routePolicy)
logf.Log.V(1).Info("Creating AuthPolicy", "key", client.ObjectKeyFromObject(routePolicy).String(), "error", err)
Expand All @@ -250,7 +250,7 @@ var _ = Describe("AuthPolicy controller", func() {
Eventually(testRouteIsAccepted(client.ObjectKeyFromObject(otherRoute)), time.Minute, 5*time.Second).Should(BeTrue())

// attach policy to the gatewaay
gwPolicy := apFactory(func(policy *api.AuthPolicy) {
gwPolicy := policyFactory(func(policy *api.AuthPolicy) {
policy.Name = "gw-auth"
policy.Spec.TargetRef.Group = "gateway.networking.k8s.io"
policy.Spec.TargetRef.Kind = "Gateway"
Expand Down Expand Up @@ -302,7 +302,7 @@ var _ = Describe("AuthPolicy controller", func() {
})

It("Attaches policy to the Gateway while having other policies attached to all HTTPRoutes", func() {
routePolicy := apFactory(nil)
routePolicy := policyFactory()

err := k8sClient.Create(context.Background(), routePolicy)
logf.Log.V(1).Info("Creating AuthPolicy", "key", client.ObjectKeyFromObject(routePolicy).String(), "error", err)
Expand All @@ -312,7 +312,7 @@ var _ = Describe("AuthPolicy controller", func() {
Eventually(testPolicyIsAccepted(routePolicy), 30*time.Second, 5*time.Second).Should(BeTrue())

// attach policy to the gatewaay
gwPolicy := apFactory(func(policy *api.AuthPolicy) {
gwPolicy := policyFactory(func(policy *api.AuthPolicy) {
policy.Name = "gw-auth"
policy.Spec.TargetRef.Group = "gateway.networking.k8s.io"
policy.Spec.TargetRef.Kind = "Gateway"
Expand Down Expand Up @@ -351,7 +351,7 @@ var _ = Describe("AuthPolicy controller", func() {
})

It("Rejects policy with only unmatching top-level route selectors while trying to configure the gateway", func() {
policy := apFactory(func(policy *api.AuthPolicy) {
policy := policyFactory(func(policy *api.AuthPolicy) {
policy.Spec.RouteSelectors = []api.RouteSelector{
{ // does not select any HTTPRouteRule
Matches: []gatewayapiv1alpha2.HTTPRouteMatch{
Expand Down Expand Up @@ -395,7 +395,7 @@ var _ = Describe("AuthPolicy controller", func() {
})

It("Rejects policy with only unmatching config-level route selectors post-configuring the gateway", func() {
policy := apFactory(nil)
policy := policyFactory()
config := policy.Spec.AuthScheme.Authentication["apiKey"]
config.RouteSelectors = []api.RouteSelector{
{ // does not select any HTTPRouteRule
Expand Down Expand Up @@ -446,7 +446,7 @@ var _ = Describe("AuthPolicy controller", func() {
})

It("Deletes resources when the policy is deleted", func() {
policy := apFactory(nil)
policy := policyFactory()

err := k8sClient.Create(context.Background(), policy)
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -476,7 +476,7 @@ var _ = Describe("AuthPolicy controller", func() {
})

It("Maps to all fields of the AuthConfig", func() {
policy := apFactory(func(policy *api.AuthPolicy) {
policy := policyFactory(func(policy *api.AuthPolicy) {
policy.Spec.NamedPatterns = map[string]authorinoapi.PatternExpressions{
"internal-source": []authorinoapi.PatternExpression{
{
Expand Down Expand Up @@ -720,7 +720,7 @@ var _ = Describe("AuthPolicy controller", func() {
})

It("Attaches simple policy to the HTTPRoute", func() {
policy := apFactory(nil)
policy := policyFactory()

err := k8sClient.Create(context.Background(), policy)
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -791,7 +791,7 @@ var _ = Describe("AuthPolicy controller", func() {
})

It("Attaches policy with top-level route selectors to the HTTPRoute", func() {
policy := apFactory(func(policy *api.AuthPolicy) {
policy := policyFactory(func(policy *api.AuthPolicy) {
policy.Spec.RouteSelectors = []api.RouteSelector{
{ // Selects: POST|DELETE *.admin.toystore.com/admin*
Matches: []gatewayapiv1alpha2.HTTPRouteMatch{
Expand Down Expand Up @@ -895,7 +895,7 @@ var _ = Describe("AuthPolicy controller", func() {
})

It("Attaches policy with config-level route selectors to the HTTPRoute", func() {
policy := apFactory(func(policy *api.AuthPolicy) {
policy := policyFactory(func(policy *api.AuthPolicy) {
config := policy.Spec.AuthScheme.Authentication["apiKey"]
config.RouteSelectors = []api.RouteSelector{
{ // Selects: POST|DELETE *.admin.toystore.com/admin*
Expand Down Expand Up @@ -1009,7 +1009,7 @@ var _ = Describe("AuthPolicy controller", func() {
})

It("Mixes route selectors into other conditions", func() {
policy := apFactory(func(policy *api.AuthPolicy) {
policy := policyFactory(func(policy *api.AuthPolicy) {
config := policy.Spec.AuthScheme.Authentication["apiKey"]
config.RouteSelectors = []api.RouteSelector{
{ // Selects: GET /private*
Expand Down Expand Up @@ -1114,7 +1114,7 @@ var _ = Describe("AuthPolicy controller", func() {
// Accepted reason is already tested generally by the existing tests

It("Target not found reason", func() {
policy := apFactory(nil)
policy := policyFactory()

err := k8sClient.Create(context.Background(), policy)
logf.Log.V(1).Info("Creating AuthPolicy", "key", client.ObjectKeyFromObject(policy).String(), "error", err)
Expand All @@ -1130,12 +1130,12 @@ var _ = Describe("AuthPolicy controller", func() {
Expect(err).ToNot(HaveOccurred())
Eventually(testRouteIsAccepted(client.ObjectKeyFromObject(route)), time.Minute, 5*time.Second).Should(BeTrue())

policy := apFactory(nil)
policy := policyFactory()
err = k8sClient.Create(context.Background(), policy)
logf.Log.V(1).Info("Creating AuthPolicy", "key", client.ObjectKeyFromObject(policy).String(), "error", err)
Expect(err).ToNot(HaveOccurred())

policy2 := apFactory(func(policy *api.AuthPolicy) {
policy2 := policyFactory(func(policy *api.AuthPolicy) {
policy.Name = "conflicting-ap"
})
err = k8sClient.Create(context.Background(), policy2)
Expand All @@ -1150,7 +1150,7 @@ var _ = Describe("AuthPolicy controller", func() {
It("Invalid reason", func() {
const targetRefName, targetRefNamespace = "istio-ingressgateway", "istio-system"

policy := apFactory(func(policy *api.AuthPolicy) {
policy := policyFactory(func(policy *api.AuthPolicy) {
policy.Spec.TargetRef.Kind = "Gateway"
policy.Spec.TargetRef.Name = targetRefName
policy.Spec.TargetRef.Namespace = ptr.To(gatewayapiv1.Namespace(targetRefNamespace))
Expand All @@ -1177,7 +1177,7 @@ var _ = Describe("AuthPolicy CEL Validations", func() {
AfterEach(DeleteNamespaceCallback(&testNamespace))

Context("Spec TargetRef Validations", func() {
policyFactory := func(mutateFn func(policy *api.AuthPolicy)) *api.AuthPolicy {
policyFactory := func(mutateFns ...func(policy *api.AuthPolicy)) *api.AuthPolicy {
policy := &api.AuthPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: "my-policy",
Expand All @@ -1192,15 +1192,15 @@ var _ = Describe("AuthPolicy CEL Validations", func() {
},
}

if mutateFn != nil {
for _, mutateFn := range mutateFns {
mutateFn(policy)
}

return policy
}

It("Valid policy targeting HTTPRoute", func() {
policy := policyFactory(nil)
policy := policyFactory()
err := k8sClient.Create(context.Background(), policy)
Expect(err).To(BeNil())
})
Expand Down
34 changes: 17 additions & 17 deletions controllers/ratelimitpolicy_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ var _ = Describe("RateLimitPolicy controller", func() {
gateway *gatewayapiv1.Gateway
)

rlpFactory := func(mutateFn func(policy *kuadrantv1beta2.RateLimitPolicy)) *kuadrantv1beta2.RateLimitPolicy {
rlp := &kuadrantv1beta2.RateLimitPolicy{
policyFactory := func(mutateFns ...func(policy *kuadrantv1beta2.RateLimitPolicy)) *kuadrantv1beta2.RateLimitPolicy {
policy := &kuadrantv1beta2.RateLimitPolicy{
TypeMeta: metav1.TypeMeta{
Kind: "RateLimitPolicy",
APIVersion: kuadrantv1beta2.GroupVersion.String(),
Expand All @@ -63,11 +63,11 @@ var _ = Describe("RateLimitPolicy controller", func() {
},
},
}

if mutateFn != nil {
mutateFn(rlp)
for _, mutateFn := range mutateFns {
mutateFn(policy)
}
return rlp

return policy
}

beforeEachCallback := func() {
Expand Down Expand Up @@ -120,7 +120,7 @@ var _ = Describe("RateLimitPolicy controller", func() {
Eventually(testRouteIsAccepted(client.ObjectKeyFromObject(httpRoute)), time.Minute, 5*time.Second).Should(BeTrue())

// create ratelimitpolicy
rlp := rlpFactory(nil)
rlp := policyFactory()
err = k8sClient.Create(context.Background(), rlp)
Expect(err).ToNot(HaveOccurred())

Expand Down Expand Up @@ -252,7 +252,7 @@ var _ = Describe("RateLimitPolicy controller", func() {
Eventually(testRouteIsAccepted(client.ObjectKeyFromObject(httpRoute)), time.Minute, 5*time.Second).Should(BeTrue())

// create ratelimitpolicy
rlp := rlpFactory(func(policy *kuadrantv1beta2.RateLimitPolicy) {
rlp := policyFactory(func(policy *kuadrantv1beta2.RateLimitPolicy) {
policy.Spec.Limits = map[string]kuadrantv1beta2.Limit{
"toys": {
Rates: []kuadrantv1beta2.Rate{
Expand Down Expand Up @@ -421,7 +421,7 @@ var _ = Describe("RateLimitPolicy controller", func() {
Eventually(testRouteIsAccepted(client.ObjectKeyFromObject(httpRoute)), time.Minute, 5*time.Second).Should(BeTrue())

// create ratelimitpolicy
rlp := rlpFactory(func(policy *kuadrantv1beta2.RateLimitPolicy) {
rlp := policyFactory(func(policy *kuadrantv1beta2.RateLimitPolicy) {
policy.Spec.TargetRef.Kind = "Gateway"
policy.Spec.TargetRef.Name = gatewayapiv1.ObjectName(gwName)
})
Expand Down Expand Up @@ -517,7 +517,7 @@ var _ = Describe("RateLimitPolicy controller", func() {

It("Creates all the resources for a basic Gateway and RateLimitPolicy when missing a HTTPRoute attached to the Gateway", func() {
// create ratelimitpolicy
rlp := rlpFactory(func(policy *kuadrantv1beta2.RateLimitPolicy) {
rlp := policyFactory(func(policy *kuadrantv1beta2.RateLimitPolicy) {
policy.Spec.TargetRef.Kind = "Gateway"
policy.Spec.TargetRef.Name = gatewayapiv1.ObjectName(gwName)
})
Expand Down Expand Up @@ -594,7 +594,7 @@ var _ = Describe("RateLimitPolicy controller", func() {
// Accepted reason is already tested generally by the existing tests

It("Target not found reason", func() {
rlp := rlpFactory(nil)
rlp := policyFactory()
err := k8sClient.Create(context.Background(), rlp)
Expect(err).ToNot(HaveOccurred())

Expand All @@ -609,11 +609,11 @@ var _ = Describe("RateLimitPolicy controller", func() {
Expect(err).ToNot(HaveOccurred())
Eventually(testRouteIsAccepted(client.ObjectKeyFromObject(httpRoute)), time.Minute, 5*time.Second).Should(BeTrue())

rlp := rlpFactory(nil)
rlp := policyFactory()
err = k8sClient.Create(context.Background(), rlp)
Expect(err).ToNot(HaveOccurred())

rlp2 := rlpFactory(func(policy *kuadrantv1beta2.RateLimitPolicy) {
rlp2 := policyFactory(func(policy *kuadrantv1beta2.RateLimitPolicy) {
policy.Name = "conflicting-rlp"
})
err = k8sClient.Create(context.Background(), rlp2)
Expand All @@ -627,7 +627,7 @@ var _ = Describe("RateLimitPolicy controller", func() {
It("Validation reason", func() {
const targetRefName, targetRefNamespace = "istio-ingressgateway", "istio-system"

rlp := rlpFactory(func(policy *kuadrantv1beta2.RateLimitPolicy) {
rlp := policyFactory(func(policy *kuadrantv1beta2.RateLimitPolicy) {
policy.Spec.TargetRef.Kind = "Gateway"
policy.Spec.TargetRef.Name = targetRefName
policy.Spec.TargetRef.Namespace = ptr.To(gatewayapiv1.Namespace(targetRefNamespace))
Expand All @@ -652,7 +652,7 @@ var _ = Describe("RateLimitPolicy CEL Validations", func() {
AfterEach(DeleteNamespaceCallback(&testNamespace))

Context("Spec TargetRef Validations", func() {
policyFactory := func(mutateFn func(policy *kuadrantv1beta2.RateLimitPolicy)) *kuadrantv1beta2.RateLimitPolicy {
policyFactory := func(mutateFns ...func(policy *kuadrantv1beta2.RateLimitPolicy)) *kuadrantv1beta2.RateLimitPolicy {
policy := &kuadrantv1beta2.RateLimitPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: "my-policy",
Expand All @@ -666,14 +666,14 @@ var _ = Describe("RateLimitPolicy CEL Validations", func() {
},
},
}
if mutateFn != nil {
for _, mutateFn := range mutateFns {
mutateFn(policy)
}

return policy
}
It("Valid policy targeting HTTPRoute", func() {
policy := policyFactory(nil)
policy := policyFactory()
err := k8sClient.Create(context.Background(), policy)
Expect(err).To(BeNil())
})
Expand Down

0 comments on commit 5f3fed7

Please sign in to comment.