|
| 1 | +package model |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/stretchr/testify/assert" |
| 5 | + "sigs.k8s.io/aws-load-balancer-controller/pkg/gateway/routeutils" |
| 6 | + elbv2model "sigs.k8s.io/aws-load-balancer-controller/pkg/model/elbv2" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +// Test IsIPv6Supported |
| 11 | +func Test_IsIPv6Supported(t *testing.T) { |
| 12 | + tests := []struct { |
| 13 | + name string |
| 14 | + ipAddressType elbv2model.IPAddressType |
| 15 | + expected bool |
| 16 | + }{ |
| 17 | + { |
| 18 | + name: "DualStack should support IPv6", |
| 19 | + ipAddressType: elbv2model.IPAddressTypeDualStack, |
| 20 | + expected: true, |
| 21 | + }, |
| 22 | + { |
| 23 | + name: "DualStackWithoutPublicIPV4 should support IPv6", |
| 24 | + ipAddressType: elbv2model.IPAddressTypeDualStackWithoutPublicIPV4, |
| 25 | + expected: true, |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "IPv4 should not support IPv6", |
| 29 | + ipAddressType: elbv2model.IPAddressTypeIPV4, |
| 30 | + expected: false, |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "Empty address type should not support IPv6", |
| 34 | + ipAddressType: "", |
| 35 | + expected: false, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "Unknown address type should not support IPv6", |
| 39 | + ipAddressType: "unknown", |
| 40 | + expected: false, |
| 41 | + }, |
| 42 | + } |
| 43 | + |
| 44 | + for _, tt := range tests { |
| 45 | + t.Run(tt.name, func(t *testing.T) { |
| 46 | + got := isIPv6Supported(tt.ipAddressType) |
| 47 | + assert.Equal(t, tt.expected, got, "isIPv6Supported() = %v, want %v", got, tt.expected) |
| 48 | + }) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// Test SortRoutesByHostnamePrecedence |
| 53 | +func Test_SortRoutesByHostnamePrecedence(t *testing.T) { |
| 54 | + tests := []struct { |
| 55 | + name string |
| 56 | + input []routeutils.RouteDescriptor |
| 57 | + expected []routeutils.RouteDescriptor |
| 58 | + }{ |
| 59 | + { |
| 60 | + name: "empty routes", |
| 61 | + input: []routeutils.RouteDescriptor{}, |
| 62 | + expected: []routeutils.RouteDescriptor{}, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "routes with no hostnames", |
| 66 | + input: []routeutils.RouteDescriptor{ |
| 67 | + &routeutils.MockRoute{Hostnames: []string{}}, |
| 68 | + &routeutils.MockRoute{Hostnames: []string{}}, |
| 69 | + }, |
| 70 | + expected: []routeutils.RouteDescriptor{ |
| 71 | + &routeutils.MockRoute{Hostnames: []string{}}, |
| 72 | + &routeutils.MockRoute{Hostnames: []string{}}, |
| 73 | + }, |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "mix of empty and non-empty hostnames", |
| 77 | + input: []routeutils.RouteDescriptor{ |
| 78 | + &routeutils.MockRoute{Hostnames: []string{}}, |
| 79 | + &routeutils.MockRoute{Hostnames: []string{"example.com"}}, |
| 80 | + &routeutils.MockRoute{Hostnames: []string{"test.com"}}, |
| 81 | + }, |
| 82 | + expected: []routeutils.RouteDescriptor{ |
| 83 | + &routeutils.MockRoute{Hostnames: []string{"example.com"}}, |
| 84 | + &routeutils.MockRoute{Hostnames: []string{"test.com"}}, |
| 85 | + &routeutils.MockRoute{Hostnames: []string{}}, |
| 86 | + }, |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "with and without wildcard hostnames", |
| 90 | + input: []routeutils.RouteDescriptor{ |
| 91 | + &routeutils.MockRoute{Hostnames: []string{"*.example.com"}}, |
| 92 | + &routeutils.MockRoute{Hostnames: []string{"test.example.com"}}, |
| 93 | + }, |
| 94 | + expected: []routeutils.RouteDescriptor{ |
| 95 | + &routeutils.MockRoute{Hostnames: []string{"test.example.com"}}, |
| 96 | + &routeutils.MockRoute{Hostnames: []string{"*.example.com"}}, |
| 97 | + }, |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "complex mixed hostnames", |
| 101 | + input: []routeutils.RouteDescriptor{ |
| 102 | + &routeutils.MockRoute{Hostnames: []string{"*.example.com"}}, |
| 103 | + &routeutils.MockRoute{Hostnames: []string{}}, |
| 104 | + &routeutils.MockRoute{Hostnames: []string{"test.example.com"}}, |
| 105 | + &routeutils.MockRoute{Hostnames: []string{"another.example.com"}}, |
| 106 | + }, |
| 107 | + expected: []routeutils.RouteDescriptor{ |
| 108 | + &routeutils.MockRoute{Hostnames: []string{"another.example.com"}}, |
| 109 | + &routeutils.MockRoute{Hostnames: []string{"test.example.com"}}, |
| 110 | + &routeutils.MockRoute{Hostnames: []string{"*.example.com"}}, |
| 111 | + &routeutils.MockRoute{Hostnames: []string{}}, |
| 112 | + }, |
| 113 | + }, |
| 114 | + } |
| 115 | + |
| 116 | + for _, tt := range tests { |
| 117 | + t.Run(tt.name, func(t *testing.T) { |
| 118 | + // Create a copy of input to avoid modifying the test case data |
| 119 | + actual := make([]routeutils.RouteDescriptor, len(tt.input)) |
| 120 | + copy(actual, tt.input) |
| 121 | + |
| 122 | + // Execute the sort |
| 123 | + sortRoutesByHostnamePrecedence(actual) |
| 124 | + |
| 125 | + // Verify the result |
| 126 | + assert.Equal(t, tt.expected, actual, "sorted routes should match expected order") |
| 127 | + |
| 128 | + // Verify stability of sort |
| 129 | + sortRoutesByHostnamePrecedence(actual) |
| 130 | + assert.Equal(t, tt.expected, actual, "second sort should maintain the same order (stable sort)") |
| 131 | + }) |
| 132 | + } |
| 133 | +} |
0 commit comments