Skip to content

Commit

Permalink
add directives check
Browse files Browse the repository at this point in the history
  • Loading branch information
salonichf5 committed Dec 20, 2024
1 parent 6e267ea commit d33da67
Showing 1 changed file with 42 additions and 37 deletions.
79 changes: 42 additions & 37 deletions tests/suite/upstream_settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ var _ = Describe("UpstreamSettingsPolicy", Ordered, Label("functional", "uspolic
},
}),
Entry("GRPC upstreams", []framework.ExpectedNginxField{
{
Directive: "upstream",
Value: "uspolicy_grpc-backend_8080",
File: "http.conf",
},
{
Directive: "zone",
Value: "uspolicy_grpc-backend_8080 64k",
Expand Down Expand Up @@ -251,8 +256,8 @@ var _ = Describe("UpstreamSettingsPolicy", Ordered, Label("functional", "uspolic

DescribeTable("upstreamSettingsPolicy status is set as expected",
func(name string, status metav1.ConditionStatus, condReason v1alpha2.PolicyConditionReason) {
nsname := types.NamespacedName{Name: name, Namespace: namespace}
Expect(waitForUSPolicyStatus(nsname, gatewayName, status, condReason)).To(Succeed())
uspolicyNsName := types.NamespacedName{Name: name, Namespace: namespace}
Expect(waitForUSPolicyStatus(uspolicyNsName, gatewayName, status, condReason)).To(Succeed())
},
Entry("uspolicy merge-usp-1", "merge-usp-1", metav1.ConditionTrue, v1alpha2.PolicyReasonAccepted),
Entry("uspolicy merge-usp-2", "merge-usp-2", metav1.ConditionTrue, v1alpha2.PolicyReasonAccepted),
Expand Down Expand Up @@ -309,6 +314,11 @@ var _ = Describe("UpstreamSettingsPolicy", Ordered, Label("functional", "uspolic
}
},
Entry("Coffee upstream", []framework.ExpectedNginxField{
{
Directive: "upstream",
Value: "uspolicy_coffee_80",
File: "http.conf",
},
{
Directive: "zone",
Value: "uspolicy_coffee_80 512k",
Expand Down Expand Up @@ -347,44 +357,53 @@ var _ = Describe("UpstreamSettingsPolicy", Ordered, Label("functional", "uspolic
Upstream: "uspolicy_tea_80",
File: "http.conf",
},
{
Directive: "upstream",
Value: "uspolicy_tea_80",
File: "http.conf",
},
}),
)
})
})

When("UpstreamSettingsPolicy targets a Service that does not exists", func() {
When("UpstreamSettingsPolicy targets a Service that does not exist", func() {
Specify("upstreamSettingsPolicy sets no condition", func() {
files := []string{"upstream-settings-policy/invalid-svc-usps.yaml"}

Expect(resourceManager.ApplyFromFiles(files, namespace)).To(Succeed())

nsname := types.NamespacedName{Name: "usps-target-not-found", Namespace: namespace}
Consistently(
func() bool {
return waitForUSPolicyToHaveAncestor(
nsname,
) != nil
}).WithTimeout(timeoutConfig.GetTimeout).
WithPolling(500 * time.Millisecond).
Should(BeTrue())
uspolicyNsName := types.NamespacedName{Name: "usps-target-not-found", Namespace: namespace}
ctx, cancel := context.WithTimeout(context.Background(), timeoutConfig.GetStatusTimeout)
defer cancel()

err := wait.PollUntilContextCancel(
ctx,
1000*time.Millisecond,
true, /* poll immediately */
func(ctx context.Context) (bool, error) {
return usPolicyHasNoAncestors(ctx, uspolicyNsName)
})

Expect(errors.Is(err, context.DeadlineExceeded)).To(BeTrue())

Expect(resourceManager.DeleteFromFiles(files, namespace)).To(Succeed())
})
})

When("UpstreamSettingsPolicy targets a Service that is owned by an invalid Gateway", func() {
Specify("upstreamSettingsPolicy has no condition set", func() {
Specify("upstreamSettingsPolicy is not Accepted with the reason TargetNotFound", func() {
// delete existing gateway
gatewayFileName := "upstream-settings-policy/gateway.yaml"
Expect(resourceManager.DeleteFromFiles([]string{gatewayFileName}, namespace)).To(Succeed())

files := []string{"upstream-settings-policy/invalid-target-usps.yaml"}
Expect(resourceManager.ApplyFromFiles(files, namespace)).To(Succeed())

nsname := types.NamespacedName{Name: "soda-svc-usp", Namespace: namespace}
uspolicyNsName := types.NamespacedName{Name: "soda-svc-usp", Namespace: namespace}
gatewayName = "gateway-not-valid"
Expect(waitForUSPolicyStatus(
nsname,
uspolicyNsName,
gatewayName,
metav1.ConditionFalse,
v1alpha2.PolicyReasonTargetNotFound,
Expand All @@ -395,30 +414,16 @@ var _ = Describe("UpstreamSettingsPolicy", Ordered, Label("functional", "uspolic
})
})

func waitForUSPolicyToHaveAncestor(usPolicyNsName types.NamespacedName) error {
ctx, cancel := context.WithTimeout(context.Background(), timeoutConfig.GetStatusTimeout)
defer cancel()

GinkgoWriter.Printf("Polling for UpstreamSettings Policy %q to not have a condition", usPolicyNsName)
func usPolicyHasNoAncestors(ctx context.Context, usPolicyNsName types.NamespacedName) (bool, error) {
GinkgoWriter.Printf("Checking that UpstreamSettingsPolicy %q has no ancestors in status\n", usPolicyNsName)

return wait.PollUntilContextCancel(
ctx,
timeoutConfig.GetStatusTimeout,
true, /* poll immediately */
func(ctx context.Context) (bool, error) {
var usPolicy ngfAPI.UpstreamSettingsPolicy
var err error
if err = k8sClient.Get(ctx, usPolicyNsName, &usPolicy); err != nil {
return false, err
}

if len(usPolicy.Status.Ancestors) == 0 {
return false, nil
}
var usPolicy ngfAPI.UpstreamSettingsPolicy
if err := k8sClient.Get(ctx, usPolicyNsName, &usPolicy); err != nil {
GinkgoWriter.Printf("Failed to get UpstreamSettingsPolicy %q: %s", usPolicyNsName, err.Error())
return false, err
}

return errors.Is(err, context.DeadlineExceeded), nil
},
)
return len(usPolicy.Status.Ancestors) != 0, nil
}

func waitForUSPolicyStatus(
Expand Down

0 comments on commit d33da67

Please sign in to comment.