|
| 1 | +package clusterconfig |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/openshift-kni/lifecycle-agent/lca-cli/seedclusterinfo" |
| 8 | + "github.com/openshift-kni/lifecycle-agent/utils" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | +) |
| 11 | + |
| 12 | +func TestSeedReconfigurationDifferentStack(t *testing.T) { |
| 13 | + testcases := []struct { |
| 14 | + name string |
| 15 | + clusterInfo *utils.ClusterInfo |
| 16 | + expectedNodeIP string |
| 17 | + expectedNodeIPs []string |
| 18 | + expectedMachineNetworks []string |
| 19 | + }{ |
| 20 | + { |
| 21 | + name: "Single-stack IPv4", |
| 22 | + clusterInfo: &utils.ClusterInfo{ |
| 23 | + OCPVersion: "4.15.0", |
| 24 | + BaseDomain: "example.com", |
| 25 | + ClusterName: "test-cluster", |
| 26 | + NodeIP: "192.168.1.10", |
| 27 | + NodeIPs: []string{"192.168.1.10"}, |
| 28 | + MachineNetwork: "192.168.1.0/24", |
| 29 | + MachineNetworks: []string{"192.168.1.0/24"}, |
| 30 | + ClusterNetworks: []string{"10.244.0.0/16"}, |
| 31 | + ServiceNetworks: []string{"10.96.0.0/16"}, |
| 32 | + Hostname: "test-node", |
| 33 | + }, |
| 34 | + expectedNodeIP: "192.168.1.10", |
| 35 | + expectedNodeIPs: []string{"192.168.1.10"}, |
| 36 | + expectedMachineNetworks: []string{"192.168.1.0/24"}, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "Single-stack IPv6", |
| 40 | + clusterInfo: &utils.ClusterInfo{ |
| 41 | + OCPVersion: "4.15.0", |
| 42 | + BaseDomain: "example.com", |
| 43 | + ClusterName: "test-cluster", |
| 44 | + NodeIP: "2001:db8::10", |
| 45 | + NodeIPs: []string{"2001:db8::10"}, |
| 46 | + MachineNetwork: "2001:db8::/64", |
| 47 | + MachineNetworks: []string{"2001:db8::/64"}, |
| 48 | + ClusterNetworks: []string{"2001:db8:1::/64"}, |
| 49 | + ServiceNetworks: []string{"2001:db8:2::/64"}, |
| 50 | + Hostname: "test-node", |
| 51 | + }, |
| 52 | + expectedNodeIP: "2001:db8::10", |
| 53 | + expectedNodeIPs: []string{"2001:db8::10"}, |
| 54 | + expectedMachineNetworks: []string{"2001:db8::/64"}, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "Dual-stack IPv4 primary", |
| 58 | + clusterInfo: &utils.ClusterInfo{ |
| 59 | + OCPVersion: "4.15.0", |
| 60 | + BaseDomain: "example.com", |
| 61 | + ClusterName: "test-cluster", |
| 62 | + NodeIP: "192.168.1.10", |
| 63 | + NodeIPs: []string{"192.168.1.10", "2001:db8::10"}, |
| 64 | + MachineNetwork: "192.168.1.0/24", |
| 65 | + MachineNetworks: []string{"192.168.1.0/24", "2001:db8::/64"}, |
| 66 | + ClusterNetworks: []string{"10.244.0.0/16", "2001:db8:1::/64"}, |
| 67 | + ServiceNetworks: []string{"10.96.0.0/16", "2001:db8:2::/64"}, |
| 68 | + Hostname: "test-node", |
| 69 | + }, |
| 70 | + expectedNodeIP: "192.168.1.10", |
| 71 | + expectedNodeIPs: []string{"192.168.1.10", "2001:db8::10"}, |
| 72 | + expectedMachineNetworks: []string{"192.168.1.0/24", "2001:db8::/64"}, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "Dual-stack IPv6 primary", |
| 76 | + clusterInfo: &utils.ClusterInfo{ |
| 77 | + OCPVersion: "4.15.0", |
| 78 | + BaseDomain: "example.com", |
| 79 | + ClusterName: "test-cluster", |
| 80 | + NodeIP: "2001:db8::10", |
| 81 | + NodeIPs: []string{"2001:db8::10", "192.168.1.10"}, |
| 82 | + MachineNetwork: "2001:db8::/64", |
| 83 | + MachineNetworks: []string{"2001:db8::/64", "192.168.1.0/24"}, |
| 84 | + ClusterNetworks: []string{"2001:db8:1::/64", "10.244.0.0/16"}, |
| 85 | + ServiceNetworks: []string{"2001:db8:2::/64", "10.96.0.0/16"}, |
| 86 | + Hostname: "test-node", |
| 87 | + }, |
| 88 | + expectedNodeIP: "2001:db8::10", |
| 89 | + expectedNodeIPs: []string{"2001:db8::10", "192.168.1.10"}, |
| 90 | + expectedMachineNetworks: []string{"2001:db8::/64", "192.168.1.0/24"}, |
| 91 | + }, |
| 92 | + } |
| 93 | + |
| 94 | + for _, tc := range testcases { |
| 95 | + t.Run(tc.name, func(t *testing.T) { |
| 96 | + // Create SeedClusterInfo from ClusterInfo |
| 97 | + seedClusterInfo := seedclusterinfo.NewFromClusterInfo( |
| 98 | + tc.clusterInfo, |
| 99 | + "quay.io/example/seed:latest", |
| 100 | + false, // hasProxy |
| 101 | + false, // hasFIPS |
| 102 | + nil, // additionalTrustBundle |
| 103 | + "", // containerStorageMountpointTarget |
| 104 | + "", // ingressCertificateCN |
| 105 | + ) |
| 106 | + |
| 107 | + // The SeedReconfiguration would be created from this SeedClusterInfo |
| 108 | + // For testing purposes, we verify that the SeedClusterInfo contains the expected data |
| 109 | + assert.Equal(t, tc.expectedNodeIP, seedClusterInfo.NodeIP) |
| 110 | + assert.Equal(t, tc.expectedNodeIPs, seedClusterInfo.NodeIPs) |
| 111 | + assert.Equal(t, tc.expectedMachineNetworks, seedClusterInfo.MachineNetworks) |
| 112 | + |
| 113 | + // Verify that backward compatibility is maintained |
| 114 | + if len(tc.expectedNodeIPs) > 0 { |
| 115 | + assert.Equal(t, tc.expectedNodeIPs[0], seedClusterInfo.NodeIP, "First NodeIP should match legacy NodeIP field") |
| 116 | + } |
| 117 | + if len(tc.expectedMachineNetworks) > 0 { |
| 118 | + // Note: SeedClusterInfo doesn't have a single MachineNetwork field, only MachineNetworks slice |
| 119 | + assert.Contains(t, seedClusterInfo.MachineNetworks, tc.expectedMachineNetworks[0], "First machine network should be in the list") |
| 120 | + } |
| 121 | + }) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +func TestIPStackTypeDetermination(t *testing.T) { |
| 126 | + testcases := []struct { |
| 127 | + name string |
| 128 | + nodeIPs []string |
| 129 | + description string |
| 130 | + stackType string |
| 131 | + }{ |
| 132 | + { |
| 133 | + name: "Single-stack IPv4", |
| 134 | + nodeIPs: []string{"192.168.1.10"}, |
| 135 | + description: "Only IPv4 address", |
| 136 | + stackType: "IPv4", |
| 137 | + }, |
| 138 | + { |
| 139 | + name: "Single-stack IPv6", |
| 140 | + nodeIPs: []string{"2001:db8::10"}, |
| 141 | + description: "Only IPv6 address", |
| 142 | + stackType: "IPv6", |
| 143 | + }, |
| 144 | + { |
| 145 | + name: "Dual-stack IPv4 primary", |
| 146 | + nodeIPs: []string{"192.168.1.10", "2001:db8::10"}, |
| 147 | + description: "IPv4 and IPv6, IPv4 first", |
| 148 | + stackType: "dual-stack", |
| 149 | + }, |
| 150 | + { |
| 151 | + name: "Dual-stack IPv6 primary", |
| 152 | + nodeIPs: []string{"2001:db8::10", "192.168.1.10"}, |
| 153 | + description: "IPv4 and IPv6, IPv6 first", |
| 154 | + stackType: "dual-stack", |
| 155 | + }, |
| 156 | + { |
| 157 | + name: "Multiple IPv4 addresses", |
| 158 | + nodeIPs: []string{"192.168.1.10", "10.0.0.5", "172.16.1.10"}, |
| 159 | + description: "Multiple IPv4 addresses", |
| 160 | + stackType: "IPv4", |
| 161 | + }, |
| 162 | + { |
| 163 | + name: "Multiple IPv6 addresses", |
| 164 | + nodeIPs: []string{"2001:db8::10", "2001:db9::20"}, |
| 165 | + description: "Multiple IPv6 addresses", |
| 166 | + stackType: "IPv6", |
| 167 | + }, |
| 168 | + { |
| 169 | + name: "Complex dual-stack", |
| 170 | + nodeIPs: []string{"192.168.1.10", "2001:db8::10", "10.0.0.5", "2001:db9::20"}, |
| 171 | + description: "Multiple IPv4 and IPv6 addresses", |
| 172 | + stackType: "dual-stack", |
| 173 | + }, |
| 174 | + } |
| 175 | + |
| 176 | + for _, tc := range testcases { |
| 177 | + t.Run(tc.name, func(t *testing.T) { |
| 178 | + // Helper function to determine IP stack type |
| 179 | + stackType := determineIPStackType(tc.nodeIPs) |
| 180 | + assert.Equal(t, tc.stackType, stackType, tc.description) |
| 181 | + }) |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +// Helper function to determine IP stack type for testing |
| 186 | +func determineIPStackType(ips []string) string { |
| 187 | + hasIPv4 := false |
| 188 | + hasIPv6 := false |
| 189 | + |
| 190 | + for _, ip := range ips { |
| 191 | + if strings.Contains(ip, ":") { |
| 192 | + hasIPv6 = true |
| 193 | + } else { |
| 194 | + hasIPv4 = true |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + if hasIPv4 && hasIPv6 { |
| 199 | + return "dual-stack" |
| 200 | + } else if hasIPv6 { |
| 201 | + return "IPv6" |
| 202 | + } else { |
| 203 | + return "IPv4" |
| 204 | + } |
| 205 | +} |
0 commit comments