-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Alice Wasko <[email protected]>
- Loading branch information
Alice Wasko
committed
Oct 27, 2023
1 parent
131178b
commit d096ee6
Showing
9 changed files
with
464 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
// Copyright Envoy Gateway Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// The full text of the Apache license is available in the LICENSE file at | ||
// the root of the repo. | ||
|
||
//go:build celvalidation | ||
// +build celvalidation | ||
|
||
package celvalidation | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1" | ||
gwapiv1a2 "sigs.k8s.io/gateway-api/apis/v1alpha2" | ||
) | ||
|
||
func TestBackendTrafficPolicyTarget(t *testing.T) { | ||
ctx := context.Background() | ||
baseBTP := egv1a1.BackendTrafficPolicy{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "btp", | ||
Namespace: metav1.NamespaceDefault, | ||
}, | ||
Spec: egv1a1.BackendTrafficPolicySpec{}, | ||
} | ||
|
||
cases := []struct { | ||
desc string | ||
mutate func(btp *egv1a1.BackendTrafficPolicy) | ||
mutateStatus func(btp *egv1a1.BackendTrafficPolicy) | ||
wantErrors []string | ||
}{ | ||
{ | ||
desc: "valid gateway targetRef", | ||
mutate: func(btp *egv1a1.BackendTrafficPolicy) { | ||
btp.Spec = egv1a1.BackendTrafficPolicySpec{ | ||
TargetRef: gwapiv1a2.PolicyTargetReferenceWithSectionName{ | ||
Group: gwapiv1a2.Group("gateway.networking.k8s.io"), | ||
Kind: gwapiv1a2.Kind("Gateway"), | ||
Name: gwapiv1a2.ObjectName("eg"), | ||
}, | ||
} | ||
}, | ||
wantErrors: []string{}, | ||
}, | ||
{ | ||
desc: "valid httproute targetRef", | ||
mutate: func(btp *egv1a1.BackendTrafficPolicy) { | ||
btp.Spec = egv1a1.BackendTrafficPolicySpec{ | ||
TargetRef: gwapiv1a2.PolicyTargetReferenceWithSectionName{ | ||
Group: gwapiv1a2.Group("gateway.networking.k8s.io"), | ||
Kind: gwapiv1a2.Kind("HTTPRoute"), | ||
Name: gwapiv1a2.ObjectName("httpbin-route"), | ||
}, | ||
} | ||
}, | ||
wantErrors: []string{}, | ||
}, | ||
{ | ||
desc: "no targetRef", | ||
mutate: func(btp *egv1a1.BackendTrafficPolicy) { | ||
btp.Spec = egv1a1.BackendTrafficPolicySpec{} | ||
}, | ||
wantErrors: []string{}, | ||
}, | ||
{ | ||
desc: "targetRef unsupported kind", | ||
mutate: func(btp *egv1a1.BackendTrafficPolicy) { | ||
btp.Spec = egv1a1.BackendTrafficPolicySpec{ | ||
TargetRef: gwapiv1a2.PolicyTargetReferenceWithSectionName{ | ||
Group: gwapiv1a2.Group("gateway.networking.k8s.io"), | ||
Kind: gwapiv1a2.Kind("foo"), | ||
Name: gwapiv1a2.ObjectName("eg"), | ||
}, | ||
} | ||
}, | ||
wantErrors: []string{}, | ||
}, | ||
{ | ||
desc: "targetRef unsupported group", | ||
mutate: func(btp *egv1a1.BackendTrafficPolicy) { | ||
btp.Spec = egv1a1.BackendTrafficPolicySpec{ | ||
TargetRef: gwapiv1a2.PolicyTargetReferenceWithSectionName{ | ||
Group: gwapiv1a2.Group("foo"), | ||
Kind: gwapiv1a2.Kind("Gateway"), | ||
Name: gwapiv1a2.ObjectName("eg"), | ||
}, | ||
} | ||
}, | ||
wantErrors: []string{}, | ||
}, | ||
{ | ||
desc: "targetRef unsupported group and kind", | ||
mutate: func(btp *egv1a1.BackendTrafficPolicy) { | ||
btp.Spec = egv1a1.BackendTrafficPolicySpec{ | ||
TargetRef: gwapiv1a2.PolicyTargetReferenceWithSectionName{ | ||
Group: gwapiv1a2.Group("foo"), | ||
Kind: gwapiv1a2.Kind("bar"), | ||
Name: gwapiv1a2.ObjectName("eg"), | ||
}, | ||
} | ||
}, | ||
wantErrors: []string{}, | ||
}, | ||
{ | ||
desc: "targetRef section name with gateway", | ||
mutate: func(btp *egv1a1.BackendTrafficPolicy) { | ||
btp.Spec = egv1a1.BackendTrafficPolicySpec{ | ||
TargetRef: gwapiv1a2.PolicyTargetReferenceWithSectionName{ | ||
Group: gwapiv1a2.Group("gateway.networking.k8s.io"), | ||
Kind: gwapiv1a2.Kind("Gateway"), | ||
Name: gwapiv1a2.ObjectName("eg"), | ||
SectionName: &gwapiv1a2.SectionName("foo") | ||
}, | ||
} | ||
}, | ||
wantErrors: []string{}, | ||
}, | ||
{ | ||
desc: "targetRef section name with httproute", | ||
mutate: func(btp *egv1a1.BackendTrafficPolicy) { | ||
btp.Spec = egv1a1.BackendTrafficPolicySpec{ | ||
TargetRef: gwapiv1a2.PolicyTargetReferenceWithSectionName{ | ||
Group: gwapiv1a2.Group("gateway.networking.k8s.io"), | ||
Kind: gwapiv1a2.Kind("HTTPRoute"), | ||
Name: gwapiv1a2.ObjectName("eg"), | ||
SectionName: &gwapiv1a2.SectionName("foo") | ||
}, | ||
} | ||
}, | ||
wantErrors: []string{}, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.desc, func(t *testing.T) { | ||
btp := baseBTP.DeepCopy() | ||
btp.Name = fmt.Sprintf("btp-%v", time.Now().UnixNano()) | ||
|
||
if tc.mutate != nil { | ||
tc.mutate(btp) | ||
} | ||
err := c.Create(ctx, btp) | ||
|
||
if tc.mutateStatus != nil { | ||
tc.mutateStatus(btp) | ||
err = c.Status().Update(ctx, btp) | ||
} | ||
|
||
if (len(tc.wantErrors) != 0) != (err != nil) { | ||
t.Fatalf("Unexpected response while creating BackendTrafficPolicy; got err=\n%v\n;want error=%v", err, tc.wantErrors != nil) | ||
} | ||
|
||
var missingErrorStrings []string | ||
for _, wantError := range tc.wantErrors { | ||
if !strings.Contains(strings.ToLower(err.Error()), strings.ToLower(wantError)) { | ||
missingErrorStrings = append(missingErrorStrings, wantError) | ||
} | ||
} | ||
if len(missingErrorStrings) != 0 { | ||
t.Errorf("Unexpected response while creating BackendTrafficPolicy; got err=\n%v\n;missing strings within error=%q", err, missingErrorStrings) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.