Skip to content

Commit 6f55437

Browse files
committed
kubeadm: increase ut coverage for addons/dns/dns.go
Signed-off-by: xin.li <[email protected]>
1 parent 4619f7e commit 6f55437

File tree

1 file changed

+277
-0
lines changed

1 file changed

+277
-0
lines changed

cmd/kubeadm/app/phases/addons/dns/dns_test.go

Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
v1 "k8s.io/api/core/v1"
2828
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2929
"k8s.io/apimachinery/pkg/runtime"
30+
"k8s.io/apimachinery/pkg/util/intstr"
3031
clientset "k8s.io/client-go/kubernetes"
3132
clientsetfake "k8s.io/client-go/kubernetes/fake"
3233
clientsetscheme "k8s.io/client-go/kubernetes/scheme"
@@ -1138,6 +1139,251 @@ metadata:
11381139
}
11391140
}
11401141

1142+
func TestCreateDNSService(t *testing.T) {
1143+
coreDNSServiceBytes, _ := kubeadmutil.ParseTemplate(CoreDNSService, struct{ DNSIP string }{
1144+
DNSIP: "10.233.0.3",
1145+
})
1146+
type args struct {
1147+
dnsService *v1.Service
1148+
serviceBytes []byte
1149+
}
1150+
tests := []struct {
1151+
name string
1152+
args args
1153+
wantErr bool
1154+
}{
1155+
{
1156+
name: "dnsService and serviceBytes are nil",
1157+
args: args{
1158+
dnsService: nil,
1159+
serviceBytes: nil,
1160+
},
1161+
wantErr: true,
1162+
},
1163+
{
1164+
name: "invalid dns",
1165+
args: args{
1166+
dnsService: nil,
1167+
serviceBytes: coreDNSServiceBytes,
1168+
},
1169+
wantErr: true,
1170+
},
1171+
{
1172+
name: "serviceBytes is not valid",
1173+
args: args{
1174+
dnsService: &v1.Service{
1175+
TypeMeta: metav1.TypeMeta{
1176+
Kind: "Service",
1177+
APIVersion: "v1",
1178+
},
1179+
ObjectMeta: metav1.ObjectMeta{Name: "coredns",
1180+
Labels: map[string]string{"k8s-app": "kube-dns",
1181+
"kubernetes.io/name": "coredns"},
1182+
Namespace: "kube-system",
1183+
},
1184+
Spec: v1.ServiceSpec{
1185+
Ports: []v1.ServicePort{
1186+
{
1187+
Name: "dns",
1188+
Port: 53,
1189+
Protocol: v1.ProtocolUDP,
1190+
TargetPort: intstr.IntOrString{
1191+
Type: 0,
1192+
IntVal: 53,
1193+
},
1194+
},
1195+
{
1196+
Name: "dns-tcp",
1197+
Port: 53,
1198+
Protocol: v1.ProtocolTCP,
1199+
TargetPort: intstr.IntOrString{
1200+
Type: 0,
1201+
IntVal: 53,
1202+
},
1203+
},
1204+
},
1205+
Selector: map[string]string{
1206+
"k8s-app": "kube-dns",
1207+
},
1208+
},
1209+
},
1210+
serviceBytes: []byte{
1211+
'f', 'o', 'o',
1212+
},
1213+
},
1214+
wantErr: true,
1215+
},
1216+
{
1217+
name: "dnsService is valid and serviceBytes is nil",
1218+
args: args{
1219+
dnsService: &v1.Service{
1220+
ObjectMeta: metav1.ObjectMeta{Name: "coredns",
1221+
Labels: map[string]string{"k8s-app": "kube-dns",
1222+
"kubernetes.io/name": "coredns"},
1223+
Namespace: "kube-system",
1224+
},
1225+
Spec: v1.ServiceSpec{
1226+
Ports: []v1.ServicePort{
1227+
{
1228+
Name: "dns",
1229+
Port: 53,
1230+
Protocol: v1.ProtocolUDP,
1231+
TargetPort: intstr.IntOrString{
1232+
Type: 0,
1233+
IntVal: 53,
1234+
},
1235+
},
1236+
{
1237+
Name: "dns-tcp",
1238+
Port: 53,
1239+
Protocol: v1.ProtocolTCP,
1240+
TargetPort: intstr.IntOrString{
1241+
Type: 0,
1242+
IntVal: 53,
1243+
},
1244+
},
1245+
},
1246+
Selector: map[string]string{
1247+
"k8s-app": "kube-dns",
1248+
},
1249+
},
1250+
},
1251+
serviceBytes: nil,
1252+
},
1253+
wantErr: false,
1254+
},
1255+
{
1256+
name: "dnsService and serviceBytes are not nil and valid",
1257+
args: args{
1258+
dnsService: &v1.Service{
1259+
TypeMeta: metav1.TypeMeta{
1260+
Kind: "Service",
1261+
APIVersion: "v1",
1262+
},
1263+
ObjectMeta: metav1.ObjectMeta{Name: "coredns",
1264+
Labels: map[string]string{"k8s-app": "kube-dns",
1265+
"kubernetes.io/name": "coredns"},
1266+
Namespace: "kube-system",
1267+
},
1268+
Spec: v1.ServiceSpec{
1269+
ClusterIP: "10.233.0.3",
1270+
Ports: []v1.ServicePort{
1271+
{
1272+
Name: "dns",
1273+
Port: 53,
1274+
Protocol: v1.ProtocolUDP,
1275+
TargetPort: intstr.IntOrString{
1276+
Type: 0,
1277+
IntVal: 53,
1278+
},
1279+
},
1280+
},
1281+
Selector: map[string]string{
1282+
"k8s-app": "kube-dns",
1283+
},
1284+
},
1285+
},
1286+
serviceBytes: coreDNSServiceBytes,
1287+
},
1288+
wantErr: false,
1289+
},
1290+
{
1291+
name: "the namespace of dnsService is not kube-system",
1292+
args: args{
1293+
dnsService: &v1.Service{
1294+
TypeMeta: metav1.TypeMeta{
1295+
Kind: "Service",
1296+
APIVersion: "v1",
1297+
},
1298+
ObjectMeta: metav1.ObjectMeta{Name: "coredns",
1299+
Labels: map[string]string{"k8s-app": "kube-dns",
1300+
"kubernetes.io/name": "coredns"},
1301+
Namespace: "kube-system-test",
1302+
},
1303+
Spec: v1.ServiceSpec{
1304+
Ports: []v1.ServicePort{
1305+
{
1306+
Name: "dns",
1307+
Port: 53,
1308+
Protocol: v1.ProtocolUDP,
1309+
TargetPort: intstr.IntOrString{
1310+
Type: 0,
1311+
IntVal: 53,
1312+
},
1313+
},
1314+
{
1315+
Name: "dns-tcp",
1316+
Port: 53,
1317+
Protocol: v1.ProtocolTCP,
1318+
TargetPort: intstr.IntOrString{
1319+
Type: 0,
1320+
IntVal: 53,
1321+
},
1322+
},
1323+
},
1324+
Selector: map[string]string{
1325+
"k8s-app": "kube-dns",
1326+
},
1327+
},
1328+
},
1329+
serviceBytes: nil,
1330+
},
1331+
wantErr: true,
1332+
},
1333+
{
1334+
name: "the name of dnsService is not coredns",
1335+
args: args{
1336+
dnsService: &v1.Service{
1337+
TypeMeta: metav1.TypeMeta{
1338+
Kind: "Service",
1339+
APIVersion: "v1",
1340+
},
1341+
ObjectMeta: metav1.ObjectMeta{Name: "coredns-test",
1342+
Labels: map[string]string{"k8s-app": "kube-dns",
1343+
"kubernetes.io/name": "coredns"},
1344+
Namespace: "kube-system",
1345+
},
1346+
Spec: v1.ServiceSpec{
1347+
Ports: []v1.ServicePort{
1348+
{
1349+
Name: "dns",
1350+
Port: 53,
1351+
Protocol: v1.ProtocolUDP,
1352+
TargetPort: intstr.IntOrString{
1353+
Type: 0,
1354+
IntVal: 53,
1355+
},
1356+
},
1357+
{
1358+
Name: "dns-tcp",
1359+
Port: 53,
1360+
Protocol: v1.ProtocolTCP,
1361+
TargetPort: intstr.IntOrString{
1362+
Type: 0,
1363+
IntVal: 53,
1364+
},
1365+
},
1366+
},
1367+
Selector: map[string]string{
1368+
"k8s-app": "kube-dns",
1369+
},
1370+
},
1371+
},
1372+
serviceBytes: nil,
1373+
},
1374+
wantErr: false,
1375+
},
1376+
}
1377+
for _, tt := range tests {
1378+
t.Run(tt.name, func(t *testing.T) {
1379+
client := newMockClientForTest(t, 1, 1)
1380+
if err := createDNSService(tt.args.dnsService, tt.args.serviceBytes, client); (err != nil) != tt.wantErr {
1381+
t.Errorf("createDNSService() error = %v, wantErr %v", err, tt.wantErr)
1382+
}
1383+
})
1384+
}
1385+
}
1386+
11411387
// replicas is replica of each DNS deployment
11421388
// deploymentSize is the number of deployments with `k8s-app=kube-dns` label.
11431389
func newMockClientForTest(t *testing.T, replicas int32, deploymentSize int) *clientsetfake.Clientset {
@@ -1164,5 +1410,36 @@ func newMockClientForTest(t *testing.T, replicas int32, deploymentSize int) *cli
11641410
t.Fatalf("error creating deployment: %v", err)
11651411
}
11661412
}
1413+
_, err := client.CoreV1().Services(metav1.NamespaceSystem).Create(context.TODO(), &v1.Service{
1414+
TypeMeta: metav1.TypeMeta{
1415+
Kind: "Service",
1416+
APIVersion: "v1",
1417+
},
1418+
ObjectMeta: metav1.ObjectMeta{Name: "coredns",
1419+
Labels: map[string]string{"k8s-app": "kube-dns",
1420+
"kubernetes.io/name": "coredns"},
1421+
Namespace: "kube-system",
1422+
},
1423+
Spec: v1.ServiceSpec{
1424+
ClusterIP: "10.233.0.3",
1425+
Ports: []v1.ServicePort{
1426+
{
1427+
Name: "dns",
1428+
Port: 53,
1429+
Protocol: v1.ProtocolUDP,
1430+
TargetPort: intstr.IntOrString{
1431+
Type: 0,
1432+
IntVal: 53,
1433+
},
1434+
},
1435+
},
1436+
Selector: map[string]string{
1437+
"k8s-app": "kube-dns",
1438+
},
1439+
},
1440+
}, metav1.CreateOptions{})
1441+
if err != nil {
1442+
t.Fatalf("error creating service: %v", err)
1443+
}
11671444
return client
11681445
}

0 commit comments

Comments
 (0)