|
6 | 6 | "github.com/pkg/errors" |
7 | 7 | "github.com/stretchr/testify/assert" |
8 | 8 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 9 | + "k8s.io/apimachinery/pkg/types" |
9 | 10 | "k8s.io/apimachinery/pkg/util/sets" |
10 | 11 | gwv1 "sigs.k8s.io/gateway-api/apis/v1" |
11 | 12 | "testing" |
@@ -69,10 +70,12 @@ func Test_listenerAllowsAttachment(t *testing.T) { |
69 | 70 | attachmentHelper := listenerAttachmentHelperImpl{ |
70 | 71 | logger: logr.Discard(), |
71 | 72 | } |
| 73 | + hostnameFromHttpRoute := map[types.NamespacedName][]gwv1.Hostname{} |
| 74 | + hostnameFromGrpcRoute := map[types.NamespacedName][]gwv1.Hostname{} |
72 | 75 | mockReconciler := NewMockRouteReconciler() |
73 | 76 | result, err := attachmentHelper.listenerAllowsAttachment(context.Background(), gw, gwv1.Listener{ |
74 | 77 | Protocol: tc.listenerProtocol, |
75 | | - }, route, mockReconciler) |
| 78 | + }, route, mockReconciler, hostnameFromHttpRoute, hostnameFromGrpcRoute) |
76 | 79 | assert.NoError(t, err) |
77 | 80 | assert.Equal(t, tc.expected, result) |
78 | 81 | }) |
@@ -409,3 +412,199 @@ func Test_kindCheck(t *testing.T) { |
409 | 412 | }) |
410 | 413 | } |
411 | 414 | } |
| 415 | + |
| 416 | +func Test_hostnameCheck(t *testing.T) { |
| 417 | + validHostname := gwv1.Hostname("example.com") |
| 418 | + invalidHostname := gwv1.Hostname("invalid..hostname") |
| 419 | + invalidHostnameTwo := gwv1.Hostname("another..invalid") |
| 420 | + |
| 421 | + tests := []struct { |
| 422 | + name string |
| 423 | + listener gwv1.Listener |
| 424 | + route preLoadRouteDescriptor |
| 425 | + expectedResult bool |
| 426 | + expectedError bool |
| 427 | + }{ |
| 428 | + { |
| 429 | + name: "listener has no hostname - should pass", |
| 430 | + listener: gwv1.Listener{ |
| 431 | + Hostname: nil, |
| 432 | + }, |
| 433 | + route: &mockRoute{ |
| 434 | + hostnames: []gwv1.Hostname{"example.com"}, |
| 435 | + }, |
| 436 | + expectedResult: true, |
| 437 | + expectedError: false, |
| 438 | + }, |
| 439 | + { |
| 440 | + name: "route has no hostnames - should pass", |
| 441 | + listener: gwv1.Listener{ |
| 442 | + Hostname: &validHostname, |
| 443 | + }, |
| 444 | + route: &mockRoute{ |
| 445 | + hostnames: []gwv1.Hostname{}, |
| 446 | + }, |
| 447 | + expectedResult: true, |
| 448 | + expectedError: false, |
| 449 | + }, |
| 450 | + { |
| 451 | + name: "listener hostname invalid - should fail", |
| 452 | + listener: gwv1.Listener{ |
| 453 | + Hostname: &invalidHostname, |
| 454 | + }, |
| 455 | + route: &mockRoute{ |
| 456 | + hostnames: []gwv1.Hostname{"example.com"}, |
| 457 | + }, |
| 458 | + expectedResult: false, |
| 459 | + expectedError: true, |
| 460 | + }, |
| 461 | + { |
| 462 | + name: "compatible hostnames - should pass", |
| 463 | + listener: gwv1.Listener{ |
| 464 | + Hostname: &validHostname, |
| 465 | + }, |
| 466 | + route: &mockRoute{ |
| 467 | + hostnames: []gwv1.Hostname{"example.com"}, |
| 468 | + }, |
| 469 | + expectedResult: true, |
| 470 | + expectedError: false, |
| 471 | + }, |
| 472 | + { |
| 473 | + name: "incompatible hostnames - should fail", |
| 474 | + listener: gwv1.Listener{ |
| 475 | + Hostname: &validHostname, |
| 476 | + }, |
| 477 | + route: &mockRoute{ |
| 478 | + hostnames: []gwv1.Hostname{"example.test.com"}, |
| 479 | + }, |
| 480 | + expectedResult: false, |
| 481 | + expectedError: false, |
| 482 | + }, |
| 483 | + { |
| 484 | + name: "route has invalid hostname but valid one matches - should pass", |
| 485 | + listener: gwv1.Listener{ |
| 486 | + Hostname: &validHostname, |
| 487 | + }, |
| 488 | + route: &mockRoute{ |
| 489 | + hostnames: []gwv1.Hostname{invalidHostname, "example.com"}, |
| 490 | + }, |
| 491 | + expectedResult: true, |
| 492 | + expectedError: false, |
| 493 | + }, |
| 494 | + { |
| 495 | + name: "route has only invalid hostnames - should fail", |
| 496 | + listener: gwv1.Listener{ |
| 497 | + Hostname: &validHostname, |
| 498 | + }, |
| 499 | + route: &mockRoute{ |
| 500 | + hostnames: []gwv1.Hostname{invalidHostname, invalidHostnameTwo}, |
| 501 | + }, |
| 502 | + expectedResult: false, |
| 503 | + expectedError: false, |
| 504 | + }, |
| 505 | + } |
| 506 | + |
| 507 | + for _, tt := range tests { |
| 508 | + t.Run(tt.name, func(t *testing.T) { |
| 509 | + helper := &listenerAttachmentHelperImpl{ |
| 510 | + logger: logr.Discard(), |
| 511 | + } |
| 512 | + |
| 513 | + result, err := helper.hostnameCheck(tt.listener, tt.route) |
| 514 | + |
| 515 | + assert.Equal(t, tt.expectedResult, result) |
| 516 | + if tt.expectedError { |
| 517 | + assert.Error(t, err) |
| 518 | + } else { |
| 519 | + assert.NoError(t, err) |
| 520 | + } |
| 521 | + }) |
| 522 | + } |
| 523 | +} |
| 524 | + |
| 525 | +func Test_crossServingHostnameUniquenessCheck(t *testing.T) { |
| 526 | + hostnames := []gwv1.Hostname{"example.com"} |
| 527 | + namespace := "test-namespace" |
| 528 | + httpRouteName := "http-route-name" |
| 529 | + grpcRouteName := "grpc-route-name" |
| 530 | + tests := []struct { |
| 531 | + name string |
| 532 | + route preLoadRouteDescriptor |
| 533 | + hostnamesFromHttpRoutes map[types.NamespacedName][]gwv1.Hostname |
| 534 | + hostnamesFromGrpcRoutes map[types.NamespacedName][]gwv1.Hostname |
| 535 | + expected bool |
| 536 | + }{ |
| 537 | + { |
| 538 | + name: "GRPC route only - should pass", |
| 539 | + route: &mockRoute{ |
| 540 | + routeKind: GRPCRouteKind, |
| 541 | + hostnames: hostnames, |
| 542 | + namespacedName: types.NamespacedName{Name: grpcRouteName, Namespace: namespace}, |
| 543 | + }, |
| 544 | + hostnamesFromHttpRoutes: map[types.NamespacedName][]gwv1.Hostname{}, |
| 545 | + hostnamesFromGrpcRoutes: map[types.NamespacedName][]gwv1.Hostname{}, |
| 546 | + expected: true, |
| 547 | + }, |
| 548 | + { |
| 549 | + name: "HTTP route only - should pass", |
| 550 | + route: &mockRoute{ |
| 551 | + routeKind: HTTPRouteKind, |
| 552 | + hostnames: hostnames, |
| 553 | + namespacedName: types.NamespacedName{Name: httpRouteName, Namespace: namespace}, |
| 554 | + }, |
| 555 | + hostnamesFromHttpRoutes: map[types.NamespacedName][]gwv1.Hostname{}, |
| 556 | + hostnamesFromGrpcRoutes: map[types.NamespacedName][]gwv1.Hostname{}, |
| 557 | + expected: true, |
| 558 | + }, |
| 559 | + { |
| 560 | + name: "GRPC route with overlapping HTTP route hostname - should fail", |
| 561 | + route: &mockRoute{ |
| 562 | + routeKind: GRPCRouteKind, |
| 563 | + hostnames: hostnames, |
| 564 | + namespacedName: types.NamespacedName{Name: grpcRouteName, Namespace: namespace}, |
| 565 | + }, |
| 566 | + hostnamesFromHttpRoutes: map[types.NamespacedName][]gwv1.Hostname{ |
| 567 | + {Name: httpRouteName, Namespace: namespace}: hostnames, |
| 568 | + }, |
| 569 | + hostnamesFromGrpcRoutes: map[types.NamespacedName][]gwv1.Hostname{}, |
| 570 | + expected: false, |
| 571 | + }, |
| 572 | + { |
| 573 | + name: "HTTP route with overlapping GRPC route hostname - should fail", |
| 574 | + route: &mockRoute{ |
| 575 | + routeKind: HTTPRouteKind, |
| 576 | + hostnames: hostnames, |
| 577 | + namespacedName: types.NamespacedName{Name: httpRouteName, Namespace: namespace}, |
| 578 | + }, |
| 579 | + hostnamesFromHttpRoutes: map[types.NamespacedName][]gwv1.Hostname{}, |
| 580 | + hostnamesFromGrpcRoutes: map[types.NamespacedName][]gwv1.Hostname{ |
| 581 | + {Name: grpcRouteName, Namespace: namespace}: hostnames, |
| 582 | + }, |
| 583 | + expected: false, |
| 584 | + }, |
| 585 | + { |
| 586 | + name: "GRPC route with non-overlapping HTTP route hostname - should pass", |
| 587 | + route: &mockRoute{ |
| 588 | + routeKind: GRPCRouteKind, |
| 589 | + hostnames: []gwv1.Hostname{"grpc.example.com"}, |
| 590 | + namespacedName: types.NamespacedName{Name: grpcRouteName, Namespace: namespace}, |
| 591 | + }, |
| 592 | + hostnamesFromHttpRoutes: map[types.NamespacedName][]gwv1.Hostname{ |
| 593 | + {Name: httpRouteName, Namespace: namespace}: {"http.example.com"}, |
| 594 | + }, |
| 595 | + hostnamesFromGrpcRoutes: map[types.NamespacedName][]gwv1.Hostname{}, |
| 596 | + expected: true, |
| 597 | + }, |
| 598 | + } |
| 599 | + |
| 600 | + for _, tt := range tests { |
| 601 | + t.Run(tt.name, func(t *testing.T) { |
| 602 | + helper := &listenerAttachmentHelperImpl{ |
| 603 | + logger: logr.Discard(), |
| 604 | + } |
| 605 | + |
| 606 | + result, _ := helper.crossServingHostnameUniquenessCheck(tt.route, tt.hostnamesFromHttpRoutes, tt.hostnamesFromGrpcRoutes) |
| 607 | + assert.Equal(t, tt.expected, result) |
| 608 | + }) |
| 609 | + } |
| 610 | +} |
0 commit comments