|
| 1 | +package cert |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/x509" |
| 5 | + "net" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/kosmos.io/kosmos/pkg/kubenest/constants" |
| 10 | +) |
| 11 | + |
| 12 | +func TestCertConfig_defaultPublicKeyAlgorithm(t *testing.T) { |
| 13 | + // 测试场景 1:PublicKeyAlgorithm 未设置,应该设置为 x509.RSA |
| 14 | + config := &CertConfig{ |
| 15 | + PublicKeyAlgorithm: x509.UnknownPublicKeyAlgorithm, |
| 16 | + } |
| 17 | + config.defaultPublicKeyAlgorithm() |
| 18 | + if config.PublicKeyAlgorithm != x509.RSA { |
| 19 | + t.Errorf("expected PublicKeyAlgorithm to be x509.RSA, got %v", config.PublicKeyAlgorithm) |
| 20 | + } |
| 21 | + |
| 22 | + // 测试场景 2:PublicKeyAlgorithm 已设置,不应更改 |
| 23 | + config = &CertConfig{ |
| 24 | + PublicKeyAlgorithm: x509.ECDSA, |
| 25 | + } |
| 26 | + config.defaultPublicKeyAlgorithm() |
| 27 | + if config.PublicKeyAlgorithm != x509.ECDSA { |
| 28 | + t.Errorf("expected PublicKeyAlgorithm to remain x509.ECDSA, got %v", config.PublicKeyAlgorithm) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func TestCertConfig_defaultNotAfter(t *testing.T) { |
| 33 | + // 测试场景 1:NotAfter 未设置,应该自动设置为当前时间加上常量值 |
| 34 | + config := &CertConfig{ |
| 35 | + NotAfter: nil, |
| 36 | + } |
| 37 | + config.defaultNotAfter() |
| 38 | + expectedNotAfter := time.Now().Add(constants.CertificateValidity) |
| 39 | + if config.NotAfter == nil || config.NotAfter.Sub(expectedNotAfter) > time.Second { |
| 40 | + t.Errorf("expected NotAfter to be %v, got %v", expectedNotAfter, config.NotAfter) |
| 41 | + } |
| 42 | + |
| 43 | + // 测试场景 2:NotAfter 已设置,不应更改 |
| 44 | + expectedTime := time.Now().Add(24 * time.Hour) |
| 45 | + config = &CertConfig{ |
| 46 | + NotAfter: &expectedTime, |
| 47 | + } |
| 48 | + config.defaultNotAfter() |
| 49 | + if config.NotAfter != &expectedTime { |
| 50 | + t.Errorf("expected NotAfter to remain %v, got %v", expectedTime, config.NotAfter) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func TestGetDefaultCertList(t *testing.T) { |
| 55 | + certList := GetDefaultCertList() |
| 56 | + |
| 57 | + // 确认返回的 CertConfig 列表包含预期数量的配置 |
| 58 | + expectedCertCount := 9 |
| 59 | + if len(certList) != expectedCertCount { |
| 60 | + t.Fatalf("expected %d certs, but got %d", expectedCertCount, len(certList)) |
| 61 | + } |
| 62 | + |
| 63 | + // 验证每个 CertConfig 的 Name 是否符合预期 |
| 64 | + expectedNames := []string{ |
| 65 | + constants.CaCertAndKeyName, // CA cert |
| 66 | + constants.VirtualClusterCertAndKeyName, // Admin cert |
| 67 | + constants.ApiserverCertAndKeyName, // Apiserver cert |
| 68 | + constants.FrontProxyCaCertAndKeyName, // Front proxy CA cert |
| 69 | + constants.FrontProxyClientCertAndKeyName, // Front proxy client cert |
| 70 | + constants.EtcdCaCertAndKeyName, // ETCD CA cert |
| 71 | + constants.EtcdServerCertAndKeyName, // ETCD server cert |
| 72 | + constants.EtcdClientCertAndKeyName, // ETCD client cert |
| 73 | + constants.ProxyServerCertAndKeyName, // Proxy server cert |
| 74 | + } |
| 75 | + |
| 76 | + for i, certConfig := range certList { |
| 77 | + if certConfig.Name != expectedNames[i] { |
| 78 | + t.Errorf("expected cert name %s, but got %s", expectedNames[i], certConfig.Name) |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func TestVirtualClusterProxyServer(t *testing.T) { |
| 84 | + certConfig := VirtualClusterProxyServer() |
| 85 | + |
| 86 | + // 验证 certConfig 的各项配置 |
| 87 | + if certConfig.Name != constants.ProxyServerCertAndKeyName { |
| 88 | + t.Errorf("expected Name to be %s, but got %s", constants.ProxyServerCertAndKeyName, certConfig.Name) |
| 89 | + } |
| 90 | + if certConfig.CAName != constants.CaCertAndKeyName { |
| 91 | + t.Errorf("expected CAName to be %s, but got %s", constants.CaCertAndKeyName, certConfig.CAName) |
| 92 | + } |
| 93 | + if certConfig.Config.CommonName != "virtualCluster-proxy-server" { |
| 94 | + t.Errorf("expected CommonName to be virtualCluster-proxy-server, but got %s", certConfig.Config.CommonName) |
| 95 | + } |
| 96 | + expectedUsages := []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth} |
| 97 | + if len(certConfig.Config.Usages) != len(expectedUsages) { |
| 98 | + t.Errorf("expected %d usages, but got %d", len(expectedUsages), len(certConfig.Config.Usages)) |
| 99 | + } |
| 100 | + for i, usage := range certConfig.Config.Usages { |
| 101 | + if usage != expectedUsages[i] { |
| 102 | + t.Errorf("expected usage %v, but got %v", expectedUsages[i], usage) |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func TestVirtualClusterCertEtcdCA(t *testing.T) { |
| 108 | + certConfig := VirtualClusterCertEtcdCA() |
| 109 | + |
| 110 | + // 验证 certConfig 的各项配置 |
| 111 | + if certConfig.Name != constants.EtcdCaCertAndKeyName { |
| 112 | + t.Errorf("expected Name to be %s, but got %s", constants.EtcdCaCertAndKeyName, certConfig.Name) |
| 113 | + } |
| 114 | + if certConfig.Config.CommonName != "virtualcluster-etcd-ca" { |
| 115 | + t.Errorf("expected CommonName to be virtualcluster-etcd-ca, but got %s", certConfig.Config.CommonName) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +// Test VirtualClusterCertEtcdServer |
| 120 | +func TestVirtualClusterCertEtcdServer(t *testing.T) { |
| 121 | + certConfig := VirtualClusterCertEtcdServer() |
| 122 | + |
| 123 | + // 验证 certConfig 的各项配置 |
| 124 | + if certConfig.Name != constants.EtcdServerCertAndKeyName { |
| 125 | + t.Errorf("expected Name to be %s, but got %s", constants.EtcdServerCertAndKeyName, certConfig.Name) |
| 126 | + } |
| 127 | + if certConfig.CAName != constants.EtcdCaCertAndKeyName { |
| 128 | + t.Errorf("expected CAName to be %s, but got %s", constants.EtcdCaCertAndKeyName, certConfig.CAName) |
| 129 | + } |
| 130 | + if certConfig.Config.CommonName != "virtualCluster-etcd-server" { |
| 131 | + t.Errorf("expected CommonName to be virtualCluster-etcd-server, but got %s", certConfig.Config.CommonName) |
| 132 | + } |
| 133 | + expectedUsages := []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth} |
| 134 | + if len(certConfig.Config.Usages) != len(expectedUsages) { |
| 135 | + t.Errorf("expected %d usages, but got %d", len(expectedUsages), len(certConfig.Config.Usages)) |
| 136 | + } |
| 137 | + for i, usage := range certConfig.Config.Usages { |
| 138 | + if usage != expectedUsages[i] { |
| 139 | + t.Errorf("expected usage %v, but got %v", expectedUsages[i], usage) |
| 140 | + } |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +// Test VirtualClusterCertEtcdClient |
| 145 | +func TestVirtualClusterCertEtcdClient(t *testing.T) { |
| 146 | + certConfig := VirtualClusterCertEtcdClient() |
| 147 | + |
| 148 | + // 验证 certConfig 的各项配置 |
| 149 | + if certConfig.Name != constants.EtcdClientCertAndKeyName { |
| 150 | + t.Errorf("expected Name to be %s, but got %s", constants.EtcdClientCertAndKeyName, certConfig.Name) |
| 151 | + } |
| 152 | + if certConfig.CAName != constants.EtcdCaCertAndKeyName { |
| 153 | + t.Errorf("expected CAName to be %s, but got %s", constants.EtcdCaCertAndKeyName, certConfig.CAName) |
| 154 | + } |
| 155 | + if certConfig.Config.CommonName != "virtualCluster-etcd-client" { |
| 156 | + t.Errorf("expected CommonName to be virtualCluster-etcd-client, but got %s", certConfig.Config.CommonName) |
| 157 | + } |
| 158 | + expectedUsages := []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth} |
| 159 | + if len(certConfig.Config.Usages) != len(expectedUsages) { |
| 160 | + t.Errorf("expected %d usages, but got %d", len(expectedUsages), len(certConfig.Config.Usages)) |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +// Test VirtualClusterCertFrontProxyCA |
| 165 | +func TestVirtualClusterCertFrontProxyCA(t *testing.T) { |
| 166 | + certConfig := VirtualClusterCertFrontProxyCA() |
| 167 | + |
| 168 | + // 验证 certConfig 的各项配置 |
| 169 | + if certConfig.Name != constants.FrontProxyCaCertAndKeyName { |
| 170 | + t.Errorf("expected Name to be %s, but got %s", constants.FrontProxyCaCertAndKeyName, certConfig.Name) |
| 171 | + } |
| 172 | + if certConfig.Config.CommonName != "front-proxy-ca" { |
| 173 | + t.Errorf("expected CommonName to be front-proxy-ca, but got %s", certConfig.Config.CommonName) |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +// Test VirtualClusterFrontProxyClient |
| 178 | +func TestVirtualClusterFrontProxyClient(t *testing.T) { |
| 179 | + certConfig := VirtualClusterFrontProxyClient() |
| 180 | + |
| 181 | + // 验证 certConfig 的各项配置 |
| 182 | + if certConfig.Name != constants.FrontProxyClientCertAndKeyName { |
| 183 | + t.Errorf("expected Name to be %s, but got %s", constants.FrontProxyClientCertAndKeyName, certConfig.Name) |
| 184 | + } |
| 185 | + if certConfig.CAName != constants.FrontProxyCaCertAndKeyName { |
| 186 | + t.Errorf("expected CAName to be %s, but got %s", constants.FrontProxyCaCertAndKeyName, certConfig.CAName) |
| 187 | + } |
| 188 | + if certConfig.Config.CommonName != "front-proxy-client" { |
| 189 | + t.Errorf("expected CommonName to be front-proxy-client, but got %s", certConfig.Config.CommonName) |
| 190 | + } |
| 191 | + expectedUsages := []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth} |
| 192 | + if len(certConfig.Config.Usages) != len(expectedUsages) { |
| 193 | + t.Errorf("expected %d usages, but got %d", len(expectedUsages), len(certConfig.Config.Usages)) |
| 194 | + } |
| 195 | + for i, usage := range certConfig.Config.Usages { |
| 196 | + if usage != expectedUsages[i] { |
| 197 | + t.Errorf("expected usage %v, but got %v", expectedUsages[i], usage) |
| 198 | + } |
| 199 | + } |
| 200 | +} |
| 201 | + |
| 202 | +// Test VirtualClusterCertApiserver |
| 203 | +func TestVirtualClusterCertApiserver(t *testing.T) { |
| 204 | + certConfig := VirtualClusterCertApiserver() |
| 205 | + |
| 206 | + // 验证 certConfig 的各项配置 |
| 207 | + if certConfig.Name != constants.ApiserverCertAndKeyName { |
| 208 | + t.Errorf("expected Name to be %s, but got %s", constants.ApiserverCertAndKeyName, certConfig.Name) |
| 209 | + } |
| 210 | + if certConfig.CAName != constants.CaCertAndKeyName { |
| 211 | + t.Errorf("expected CAName to be %s, but got %s", constants.CaCertAndKeyName, certConfig.CAName) |
| 212 | + } |
| 213 | + if certConfig.Config.CommonName != "virtualCluster-apiserver" { |
| 214 | + t.Errorf("expected CommonName to be virtualCluster-apiserver, but got %s", certConfig.Config.CommonName) |
| 215 | + } |
| 216 | + expectedUsages := []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth} |
| 217 | + if len(certConfig.Config.Usages) != len(expectedUsages) { |
| 218 | + t.Errorf("expected %d usages, but got %d", len(expectedUsages), len(certConfig.Config.Usages)) |
| 219 | + } |
| 220 | + for i, usage := range certConfig.Config.Usages { |
| 221 | + if usage != expectedUsages[i] { |
| 222 | + t.Errorf("expected usage %v, but got %v", expectedUsages[i], usage) |
| 223 | + } |
| 224 | + } |
| 225 | +} |
| 226 | + |
| 227 | +// Test etcdServerAltNamesMutator |
| 228 | +func TestEtcdServerAltNamesMutator(t *testing.T) { |
| 229 | + cfg := &AltNamesMutatorConfig{ |
| 230 | + Name: "test", |
| 231 | + Namespace: "default", |
| 232 | + ClusterIPs: []string{ |
| 233 | + "10.96.0.1", |
| 234 | + "10.96.0.2", |
| 235 | + }, |
| 236 | + } |
| 237 | + |
| 238 | + altNames, err := etcdServerAltNamesMutator(cfg) |
| 239 | + if err != nil { |
| 240 | + t.Fatalf("unexpected error: %v", err) |
| 241 | + } |
| 242 | + |
| 243 | + // 验证 DNS 名称 |
| 244 | + expectedDNSNames := []string{ |
| 245 | + "localhost", |
| 246 | + "test.default.svc.cluster.local", |
| 247 | + "*.test.default.svc.cluster.local", |
| 248 | + } |
| 249 | + if len(altNames.DNSNames) != len(expectedDNSNames) { |
| 250 | + t.Fatalf("expected %d DNS names, but got %d", len(expectedDNSNames), len(altNames.DNSNames)) |
| 251 | + } |
| 252 | + for i, dns := range altNames.DNSNames { |
| 253 | + if dns != expectedDNSNames[i] { |
| 254 | + t.Errorf("expected DNS name %s, but got %s", expectedDNSNames[i], dns) |
| 255 | + } |
| 256 | + } |
| 257 | + |
| 258 | + // 验证 IP 地址 |
| 259 | + expectedIPs := []net.IP{ |
| 260 | + net.ParseIP("::1"), |
| 261 | + net.IPv4(127, 0, 0, 1), |
| 262 | + net.ParseIP("10.96.0.1"), |
| 263 | + net.ParseIP("10.96.0.2"), |
| 264 | + } |
| 265 | + if len(altNames.IPs) != len(expectedIPs) { |
| 266 | + t.Fatalf("expected %d IPs, but got %d", len(expectedIPs), len(altNames.IPs)) |
| 267 | + } |
| 268 | + for i, ip := range altNames.IPs { |
| 269 | + if !ip.Equal(expectedIPs[i]) { |
| 270 | + t.Errorf("expected IP %v, but got %v", expectedIPs[i], ip) |
| 271 | + } |
| 272 | + } |
| 273 | +} |
0 commit comments