@@ -23,11 +23,11 @@ import (
2323 "log"
2424 "net/http"
2525 "net/url"
26+ "slices"
2627 "strings"
2728 "sync"
2829 "time"
2930
30- "github.com/apache/apisix-ingress-controller/api/v1alpha1"
3131 "github.com/gavv/httpexpect/v2"
3232 "github.com/gruntwork-io/terratest/modules/testing"
3333 "github.com/onsi/gomega"
@@ -36,32 +36,18 @@ import (
3636 "golang.org/x/net/html"
3737 corev1 "k8s.io/api/core/v1"
3838 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
39- "k8s.io/apimachinery/pkg/runtime"
4039 "k8s.io/apimachinery/pkg/types"
41- utilruntime "k8s.io/apimachinery/pkg/util/runtime"
4240 "k8s.io/apimachinery/pkg/util/wait"
4341 "k8s.io/client-go/kubernetes/scheme"
4442 "k8s.io/client-go/tools/remotecommand"
4543 "k8s.io/utils/ptr"
4644 "sigs.k8s.io/controller-runtime/pkg/client"
4745 gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
4846 "sigs.k8s.io/gateway-api/conformance/utils/kubernetes"
49- )
5047
51- var (
52- scheme_ = runtime .NewScheme ()
48+ "github.com/apache/apisix-ingress-controller/api/v1alpha1"
5349)
5450
55- func init () {
56- utilruntime .Must (scheme .AddToScheme (scheme_ ))
57- if err := gatewayv1 .Install (scheme_ ); err != nil {
58- panic (err )
59- }
60- if err := v1alpha1 .AddToScheme (scheme_ ); err != nil {
61- panic (err )
62- }
63- }
64-
6551func (f * Framework ) NewExpectResponse (httpBody any ) * httpexpect.Response {
6652 body , err := json .Marshal (httpBody )
6753 f .GomegaT .Expect (err ).ShouldNot (gomega .HaveOccurred ())
@@ -395,31 +381,32 @@ func CreateTestZipFile(sourceCode, metadata string) ([]byte, error) {
395381 return zipBuffer .Bytes (), nil
396382}
397383
398- func HTTPRoutePolicyMustHaveCondition (t testing.TestingT , client client.Client , timeout time.Duration , refNN , hrpNN types.NamespacedName , condition metav1.Condition ) {
384+ func HTTPRoutePolicyMustHaveCondition (t testing.TestingT , client client.Client , timeout time.Duration , refNN , hrpNN types.NamespacedName ,
385+ condition metav1.Condition ) {
399386 err := EventuallyHTTPRoutePolicyHaveStatus (client , timeout , hrpNN , func (httpRoutePolicy v1alpha1.HTTPRoutePolicy , status v1alpha1.PolicyStatus ) bool {
400- var (
401- ancestors = status .Ancestors
402- conditionFound bool
403- )
404- for _ , ancestor := range ancestors {
387+ for _ , ancestor := range status .Ancestors {
405388 if err := kubernetes .ConditionsHaveLatestObservedGeneration (& httpRoutePolicy , ancestor .Conditions ); err != nil {
406389 log .Printf ("HTTPRoutePolicy %s (parentRef=%v) %v" , hrpNN , parentRefToString (ancestor .AncestorRef ), err )
407390 return false
408391 }
409392
410- if ancestor .AncestorRef .Name == gatewayv1 .ObjectName (refNN .Name ) && (ancestor .AncestorRef .Namespace == nil || string (* ancestor .AncestorRef .Namespace ) == refNN .Namespace ) {
411- if findConditionInList (t , ancestor .Conditions , condition ) {
412- conditionFound = true
393+ if ancestor .AncestorRef .Name == gatewayv1 .ObjectName (refNN .Name ) && (ancestor .AncestorRef .Namespace == nil ||
394+ string (* ancestor .AncestorRef .Namespace ) == refNN .Namespace ) {
395+ if findConditionInList (ancestor .Conditions , condition ) {
396+ return true
397+ } else {
398+ log .Printf ("not found condition %v in list [%v]" , condition , ancestor .Conditions )
413399 }
414400 }
415401 }
416- return conditionFound
402+ return false
417403 })
418404
419405 require .NoError (t , err , "error waiting for HTTPRoutePolicy status to have a Condition matching expectations" )
420406}
421407
422- func EventuallyHTTPRoutePolicyHaveStatus (client client.Client , timeout time.Duration , hrpNN types.NamespacedName , f func (httpRoutePolicy v1alpha1.HTTPRoutePolicy , status v1alpha1.PolicyStatus ) bool ) error {
408+ func EventuallyHTTPRoutePolicyHaveStatus (client client.Client , timeout time.Duration , hrpNN types.NamespacedName ,
409+ f func (httpRoutePolicy v1alpha1.HTTPRoutePolicy , status v1alpha1.PolicyStatus ) bool ) error {
423410 _ = v1alpha1 .AddToScheme (client .Scheme ())
424411 return wait .PollUntilContextTimeout (context .Background (), time .Second , timeout , true , func (ctx context.Context ) (done bool , err error ) {
425412 var httpRoutePolicy v1alpha1.HTTPRoutePolicy
@@ -447,22 +434,10 @@ func parentRefToString(p gatewayv1.ParentReference) string {
447434 return string (p .Name )
448435}
449436
450- func findConditionInList (t testing.TestingT , conditions []metav1.Condition , expected metav1.Condition ) bool {
451- for _ , cond := range conditions {
452- if cond .Type == expected .Type {
453- // an empty Status string means "Match any status".
454- if expected .Status == "" || cond .Status == expected .Status {
455- // an empty Reason string means "Match any reason".
456- if expected .Reason == "" || cond .Reason == expected .Reason {
457- return true
458- }
459- log .Printf ("%s condition Reason set to %s, expected %s" , expected .Type , cond .Reason , expected .Reason )
460- }
461-
462- log .Printf ("%s condition set to Status %s with Reason %v, expected Status %s" , expected .Type , cond .Status , cond .Reason , expected .Status )
463- }
464- }
465-
466- log .Printf ("%s was not in conditions list [%v]" , expected .Type , conditions )
467- return false
437+ func findConditionInList (conditions []metav1.Condition , expected metav1.Condition ) bool {
438+ return slices .ContainsFunc (conditions , func (item metav1.Condition ) bool {
439+ // an empty Status string means "Match any status".
440+ // an empty Reason string means "Match any reason".
441+ return expected .Type == item .Type && (expected .Status == "" || expected .Status == item .Status ) && (expected .Reason == "" || expected .Reason == item .Reason )
442+ })
468443}
0 commit comments