Skip to content

Commit afb3cab

Browse files
authored
Merge pull request kosmos-io#720 from qiuwei68/feature_unittests
test:add test case for kubenest util
2 parents 792479f + ba6f80b commit afb3cab

25 files changed

+727
-5789
lines changed

go.mod

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ require (
2626
github.com/spf13/cobra v1.6.0
2727
github.com/spf13/pflag v1.0.5
2828
github.com/spf13/viper v1.12.0
29-
github.com/stretchr/testify v1.8.1
3029
github.com/vishvananda/netlink v1.2.1-beta.2.0.20220630165224-c591ada0fb2b
3130
golang.org/x/sys v0.12.0
3231
golang.org/x/term v0.12.0
@@ -140,7 +139,6 @@ require (
140139
github.com/pelletier/go-toml v1.9.5 // indirect
141140
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
142141
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
143-
github.com/pmezard/go-difflib v1.0.0 // indirect
144142
github.com/projectcalico/go-json v0.0.0-20161128004156-6219dc7339ba // indirect
145143
github.com/projectcalico/go-yaml-wrapper v0.0.0-20191112210931-090425220c54 // indirect
146144
github.com/prometheus/client_golang v1.14.0 // indirect
@@ -151,6 +149,7 @@ require (
151149
github.com/spf13/afero v1.9.2 // indirect
152150
github.com/spf13/jwalterweatherman v1.1.0 // indirect
153151
github.com/stoewer/go-strcase v1.2.0 // indirect
152+
github.com/stretchr/testify v1.9.0 // indirect
154153
github.com/subosito/gotenv v1.3.0 // indirect
155154
github.com/vishvananda/netns v0.0.0-20210104183010-2eb08e3e575f // indirect
156155
github.com/xlab/treeprint v1.1.0 // indirect

go.sum

+3-2
Original file line numberDiff line numberDiff line change
@@ -1387,8 +1387,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
13871387
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
13881388
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
13891389
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
1390-
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
13911390
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
1391+
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
13921392
github.com/stretchr/testify v0.0.0-20180303142811-b89eecf5ca5d/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
13931393
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
13941394
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
@@ -1398,8 +1398,9 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
13981398
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
13991399
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
14001400
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
1401-
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
14021401
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
1402+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
1403+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
14031404
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
14041405
github.com/subosito/gotenv v1.3.0 h1:mjC+YW8QpAdXibNi+vNWgzmgBH4+5l5dCXv8cNysBLI=
14051406
github.com/subosito/gotenv v1.3.0/go.mod h1:YzJjq/33h7nrwdY+iHMhEOEEbW0ovIz0tB6t6PwAXzs=

pkg/kubenest/util/cert/certs_test.go

+273
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
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

Comments
 (0)