|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "regexp" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | + |
| 12 | + . "github.com/onsi/ginkgo/v2" |
| 13 | + . "github.com/onsi/gomega" |
| 14 | + core "k8s.io/api/core/v1" |
| 15 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 16 | + "k8s.io/apimachinery/pkg/types" |
| 17 | + "k8s.io/apimachinery/pkg/util/wait" |
| 18 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 19 | + gatewayv1 "sigs.k8s.io/gateway-api/apis/v1" |
| 20 | + |
| 21 | + "github.com/nginx/nginx-gateway-fabric/tests/framework" |
| 22 | +) |
| 23 | + |
| 24 | +var _ = Describe("AdvancedRouting", Ordered, Label("functional", "routing"), func() { |
| 25 | + var ( |
| 26 | + files = []string{ |
| 27 | + "advanced-routing/cafe.yaml", |
| 28 | + "advanced-routing/gateway.yaml", |
| 29 | + "advanced-routing/grpc-backends.yaml", |
| 30 | + "advanced-routing/routes.yaml", |
| 31 | + } |
| 32 | + |
| 33 | + namespace = "routing" |
| 34 | + ) |
| 35 | + |
| 36 | + BeforeAll(func() { |
| 37 | + ns := &core.Namespace{ |
| 38 | + ObjectMeta: metav1.ObjectMeta{ |
| 39 | + Name: namespace, |
| 40 | + }, |
| 41 | + } |
| 42 | + |
| 43 | + Expect(resourceManager.Apply([]client.Object{ns})).To(Succeed()) |
| 44 | + Expect(resourceManager.ApplyFromFiles(files, namespace)).To(Succeed()) |
| 45 | + Expect(resourceManager.WaitForAppsToBeReady(namespace)).To(Succeed()) |
| 46 | + }) |
| 47 | + |
| 48 | + AfterAll(func() { |
| 49 | + Expect(resourceManager.DeleteFromFiles(files, namespace)).To(Succeed()) |
| 50 | + Expect(resourceManager.DeleteNamespace(namespace)).To(Succeed()) |
| 51 | + }) |
| 52 | + |
| 53 | + When("valid advanced routing settings are configured for Routes", func() { |
| 54 | + var baseURL string |
| 55 | + BeforeAll(func() { |
| 56 | + port := 80 |
| 57 | + if portFwdPort != 0 { |
| 58 | + port = portFwdPort |
| 59 | + } |
| 60 | + |
| 61 | + baseURL = fmt.Sprintf("http://cafe.example.com:%d", port) |
| 62 | + }) |
| 63 | + |
| 64 | + Specify("routes have the Accepted/True/Accepted condition", func() { |
| 65 | + httpRoute := "http-header-and-query-matching" |
| 66 | + grpcRoute := "grpc-header-matching" |
| 67 | + |
| 68 | + nsname := types.NamespacedName{Name: httpRoute, Namespace: namespace} |
| 69 | + err := waitForHTTPRouteToBeAccepted(nsname) |
| 70 | + Expect(err).ToNot(HaveOccurred(), fmt.Sprintf("%s was not accepted", httpRoute)) |
| 71 | + |
| 72 | + nsname = types.NamespacedName{Name: grpcRoute, Namespace: namespace} |
| 73 | + err = waitForGRPCRouteToBeAccepted(nsname) |
| 74 | + Expect(err).ToNot(HaveOccurred(), fmt.Sprintf("%s was not accepted", httpRoute)) |
| 75 | + }) |
| 76 | + |
| 77 | + DescribeTable("verify working traffic for HTTPRoute", |
| 78 | + func(uri string, serverName string, headers map[string]string, queryParams map[string]string) { |
| 79 | + url := baseURL + uri |
| 80 | + Eventually( |
| 81 | + func() error { |
| 82 | + return expectRequestToRespondFromExpectedServer(url, address, serverName, headers, queryParams) |
| 83 | + }). |
| 84 | + WithTimeout(timeoutConfig.ContainerRestartTimeout). |
| 85 | + WithPolling(500 * time.Millisecond). |
| 86 | + Should(Succeed()) |
| 87 | + }, |
| 88 | + Entry("request with no headers or params", "/coffee", "coffee-v1", nil, nil), |
| 89 | + Entry("request with Exact match header", "/coffee", "coffee-v2", map[string]string{"version": "v2"}, nil), |
| 90 | + Entry("request with Exact match query param", "/coffee", "coffee-v2", nil, map[string]string{"TEST": "v2"}), |
| 91 | + Entry( |
| 92 | + "request with RegularExpression match header", |
| 93 | + "/coffee", |
| 94 | + "coffee-v3", |
| 95 | + map[string]string{"headerRegex": "header-regex"}, |
| 96 | + nil, |
| 97 | + ), |
| 98 | + Entry( |
| 99 | + "request with RegularExpression match query param", |
| 100 | + "/coffee", |
| 101 | + "coffee-v3", |
| 102 | + nil, |
| 103 | + map[string]string{"queryRegex": "query-regex"}, |
| 104 | + ), |
| 105 | + Entry( |
| 106 | + "request with non-matching regex header", |
| 107 | + "/coffee", |
| 108 | + "coffee-v1", |
| 109 | + map[string]string{"headerRegex": "headerInvalid"}, |
| 110 | + nil, |
| 111 | + ), |
| 112 | + Entry( |
| 113 | + "request with non-matching regex query param", |
| 114 | + "/coffee", |
| 115 | + "coffee-v1", |
| 116 | + nil, |
| 117 | + map[string]string{"queryRegex": "queryInvalid"}, |
| 118 | + ), |
| 119 | + ) |
| 120 | + }) |
| 121 | +}) |
| 122 | + |
| 123 | +func expectRequestToRespondFromExpectedServer( |
| 124 | + appURL, address, expServerName string, |
| 125 | + headers, queryParams map[string]string, |
| 126 | +) error { |
| 127 | + status, body, err := framework.Get(appURL, address, timeoutConfig.RequestTimeout, headers, queryParams) |
| 128 | + if err != nil { |
| 129 | + return err |
| 130 | + } |
| 131 | + |
| 132 | + if status != http.StatusOK { |
| 133 | + return errors.New("http status was not 200") |
| 134 | + } |
| 135 | + |
| 136 | + actualServerName, err := extractServerName(body) |
| 137 | + if err != nil { |
| 138 | + return err |
| 139 | + } |
| 140 | + |
| 141 | + fmt.Println("server", actualServerName, expServerName) |
| 142 | + if !strings.Contains(actualServerName, expServerName) { |
| 143 | + return errors.New("expected response body to contain correct server name") |
| 144 | + } |
| 145 | + |
| 146 | + return err |
| 147 | +} |
| 148 | + |
| 149 | +func extractServerName(responseBody string) (string, error) { |
| 150 | + re := regexp.MustCompile(`Server name:\s*(\S+)`) |
| 151 | + matches := re.FindStringSubmatch(responseBody) |
| 152 | + if len(matches) < 2 { |
| 153 | + return "", errors.New("server name not found") |
| 154 | + } |
| 155 | + return matches[1], nil |
| 156 | +} |
| 157 | + |
| 158 | +func waitForHTTPRouteToBeAccepted(nsname types.NamespacedName) error { |
| 159 | + ctx, cancel := context.WithTimeout(context.Background(), timeoutConfig.GetStatusTimeout*2) |
| 160 | + defer cancel() |
| 161 | + |
| 162 | + GinkgoWriter.Printf( |
| 163 | + "Waiting for HTTPRoute %q to have the condition Accepted/True/Accepted\n", |
| 164 | + nsname, |
| 165 | + ) |
| 166 | + |
| 167 | + return waitForHTTPRouteToHaveExpectedCondition( |
| 168 | + ctx, |
| 169 | + nsname, |
| 170 | + metav1.ConditionTrue, |
| 171 | + string(gatewayv1.RouteConditionAccepted), |
| 172 | + ) |
| 173 | +} |
| 174 | + |
| 175 | +func waitForGRPCRouteToBeAccepted(nsname types.NamespacedName) error { |
| 176 | + ctx, cancel := context.WithTimeout(context.Background(), timeoutConfig.GetStatusTimeout*2) |
| 177 | + defer cancel() |
| 178 | + |
| 179 | + GinkgoWriter.Printf( |
| 180 | + "Waiting for GRPCRoute %q to have the condition Accepted/True/Accepted\n", |
| 181 | + nsname, |
| 182 | + ) |
| 183 | + |
| 184 | + return waitForGRPCRouteToHaveExpectedCondition( |
| 185 | + ctx, |
| 186 | + nsname, |
| 187 | + metav1.ConditionTrue, |
| 188 | + string(gatewayv1.RouteConditionAccepted), |
| 189 | + ) |
| 190 | +} |
| 191 | + |
| 192 | +func waitForHTTPRouteToHaveExpectedCondition( |
| 193 | + ctx context.Context, |
| 194 | + nsname types.NamespacedName, |
| 195 | + conditionStatus metav1.ConditionStatus, |
| 196 | + reason string, |
| 197 | +) error { |
| 198 | + return wait.PollUntilContextCancel( |
| 199 | + ctx, |
| 200 | + 500*time.Millisecond, |
| 201 | + true, /* poll immediately */ |
| 202 | + func(ctx context.Context) (bool, error) { |
| 203 | + var route gatewayv1.HTTPRoute |
| 204 | + |
| 205 | + if err := k8sClient.Get(ctx, nsname, &route); err != nil { |
| 206 | + return false, err |
| 207 | + } |
| 208 | + |
| 209 | + return verifyConditions(route, nsname, conditionStatus, reason) |
| 210 | + }, |
| 211 | + ) |
| 212 | +} |
| 213 | + |
| 214 | +func waitForGRPCRouteToHaveExpectedCondition( |
| 215 | + ctx context.Context, |
| 216 | + nsname types.NamespacedName, |
| 217 | + conditionStatus metav1.ConditionStatus, |
| 218 | + reason string, |
| 219 | +) error { |
| 220 | + return wait.PollUntilContextCancel( |
| 221 | + ctx, |
| 222 | + 500*time.Millisecond, |
| 223 | + true, /* poll immediately */ |
| 224 | + func(ctx context.Context) (bool, error) { |
| 225 | + var route gatewayv1.GRPCRoute |
| 226 | + |
| 227 | + if err := k8sClient.Get(ctx, nsname, &route); err != nil { |
| 228 | + return false, err |
| 229 | + } |
| 230 | + |
| 231 | + return verifyConditions(route, nsname, conditionStatus, reason) |
| 232 | + }, |
| 233 | + ) |
| 234 | +} |
| 235 | + |
| 236 | +func verifyConditions( |
| 237 | + route interface{}, |
| 238 | + nsname types.NamespacedName, |
| 239 | + conditionStatus metav1.ConditionStatus, |
| 240 | + reason string, |
| 241 | +) (bool, error) { |
| 242 | + var err error |
| 243 | + var parents []gatewayv1.RouteParentStatus |
| 244 | + |
| 245 | + switch r := route.(type) { |
| 246 | + case gatewayv1.HTTPRoute: |
| 247 | + parents = r.Status.Parents |
| 248 | + case gatewayv1.GRPCRoute: |
| 249 | + parents = r.Status.Parents |
| 250 | + default: |
| 251 | + return false, fmt.Errorf("unsupported route type") |
| 252 | + } |
| 253 | + |
| 254 | + if len(parents) == 0 { |
| 255 | + GinkgoWriter.Printf("Route %q does not have a parent status yet\n", nsname) |
| 256 | + |
| 257 | + return false, nil |
| 258 | + } |
| 259 | + |
| 260 | + if len(parents) != 1 { |
| 261 | + return false, fmt.Errorf("route has %d parents, expected 1", len(parents)) |
| 262 | + } |
| 263 | + |
| 264 | + parent := parents[0] |
| 265 | + if parent.Conditions == nil { |
| 266 | + return false, fmt.Errorf("expected parent conditions to not be nil") |
| 267 | + } |
| 268 | + |
| 269 | + cond := parent.Conditions[0] |
| 270 | + if cond.Type != string(gatewayv1.RouteConditionAccepted) { |
| 271 | + return false, fmt.Errorf("expected condition type to be Accepted, got %s", cond.Type) |
| 272 | + } |
| 273 | + |
| 274 | + if cond.Status != conditionStatus { |
| 275 | + return false, fmt.Errorf("expected condition status to be True, got %s", cond.Status) |
| 276 | + } |
| 277 | + |
| 278 | + if cond.Reason != reason { |
| 279 | + return false, fmt.Errorf("expected condition reason to be Accepted, got %s", cond.Reason) |
| 280 | + } |
| 281 | + |
| 282 | + return err == nil, err |
| 283 | +} |
0 commit comments