Skip to content

Commit 54218a7

Browse files
committed
scheduler: add unit test for pending pods metrics
1 parent 0806ef2 commit 54218a7

File tree

1 file changed

+151
-6
lines changed

1 file changed

+151
-6
lines changed

pkg/scheduler/internal/queue/scheduling_queue_test.go

Lines changed: 151 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ import (
2323
"testing"
2424
"time"
2525

26+
dto "github.com/prometheus/client_model/go"
2627
"k8s.io/api/core/v1"
2728
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2829
"k8s.io/apimachinery/pkg/types"
2930
"k8s.io/apimachinery/pkg/util/clock"
3031
podutil "k8s.io/kubernetes/pkg/api/v1/pod"
32+
"k8s.io/kubernetes/pkg/scheduler/metrics"
3133
"k8s.io/kubernetes/pkg/scheduler/util"
3234
)
3335

@@ -1066,7 +1068,7 @@ func TestPodTimestamp(t *testing.T) {
10661068
tests := []struct {
10671069
name string
10681070
operations []operation
1069-
operants []*podInfo
1071+
operands []*podInfo
10701072
expected []*podInfo
10711073
}{
10721074
{
@@ -1075,7 +1077,7 @@ func TestPodTimestamp(t *testing.T) {
10751077
addPodActiveQ,
10761078
addPodActiveQ,
10771079
},
1078-
operants: []*podInfo{pInfo2, pInfo1},
1080+
operands: []*podInfo{pInfo2, pInfo1},
10791081
expected: []*podInfo{pInfo1, pInfo2},
10801082
},
10811083
{
@@ -1084,7 +1086,7 @@ func TestPodTimestamp(t *testing.T) {
10841086
updatePodActiveQ,
10851087
updatePodActiveQ,
10861088
},
1087-
operants: []*podInfo{pInfo2, pInfo1},
1089+
operands: []*podInfo{pInfo2, pInfo1},
10881090
expected: []*podInfo{pInfo1, pInfo2},
10891091
},
10901092
{
@@ -1094,7 +1096,7 @@ func TestPodTimestamp(t *testing.T) {
10941096
addPodUnschedulableQ,
10951097
moveAllToActiveQ,
10961098
},
1097-
operants: []*podInfo{pInfo2, pInfo1, nil},
1099+
operands: []*podInfo{pInfo2, pInfo1, nil},
10981100
expected: []*podInfo{pInfo1, pInfo2},
10991101
},
11001102
{
@@ -1106,7 +1108,7 @@ func TestPodTimestamp(t *testing.T) {
11061108
flushBackoffQ,
11071109
moveAllToActiveQ,
11081110
},
1109-
operants: []*podInfo{pInfo2, pInfo1, pInfo1, nil, nil},
1111+
operands: []*podInfo{pInfo2, pInfo1, pInfo1, nil, nil},
11101112
expected: []*podInfo{pInfo1, pInfo2},
11111113
},
11121114
}
@@ -1117,7 +1119,7 @@ func TestPodTimestamp(t *testing.T) {
11171119
var podInfoList []*podInfo
11181120

11191121
for i, op := range test.operations {
1120-
op(queue, test.operants[i])
1122+
op(queue, test.operands[i])
11211123
}
11221124

11231125
for i := 0; i < len(test.expected); i++ {
@@ -1135,3 +1137,146 @@ func TestPodTimestamp(t *testing.T) {
11351137
})
11361138
}
11371139
}
1140+
1141+
// TestPendingPodsMetric tests Prometheus metrics related with pending pods
1142+
func TestPendingPodsMetric(t *testing.T) {
1143+
total := 50
1144+
timestamp := time.Now()
1145+
var pInfos = make([]*podInfo, 0, total)
1146+
for i := 1; i <= total; i++ {
1147+
p := &podInfo{
1148+
pod: &v1.Pod{
1149+
ObjectMeta: metav1.ObjectMeta{
1150+
Name: fmt.Sprintf("test-pod-%d", i),
1151+
Namespace: fmt.Sprintf("ns%d", i),
1152+
UID: types.UID(fmt.Sprintf("tp-%d", i)),
1153+
},
1154+
},
1155+
timestamp: timestamp,
1156+
}
1157+
pInfos = append(pInfos, p)
1158+
}
1159+
tests := []struct {
1160+
name string
1161+
operations []operation
1162+
operands [][]*podInfo
1163+
expected []int64
1164+
}{
1165+
{
1166+
name: "add pods to activeQ and unschedulableQ",
1167+
operations: []operation{
1168+
addPodActiveQ,
1169+
addPodUnschedulableQ,
1170+
},
1171+
operands: [][]*podInfo{
1172+
pInfos[:30],
1173+
pInfos[30:],
1174+
},
1175+
expected: []int64{30, 0, 20},
1176+
},
1177+
{
1178+
name: "add pods to all kinds of queues",
1179+
operations: []operation{
1180+
addPodActiveQ,
1181+
backoffPod,
1182+
addPodBackoffQ,
1183+
addPodUnschedulableQ,
1184+
},
1185+
operands: [][]*podInfo{
1186+
pInfos[:15],
1187+
pInfos[15:40],
1188+
pInfos[15:40],
1189+
pInfos[40:],
1190+
},
1191+
expected: []int64{15, 25, 10},
1192+
},
1193+
{
1194+
name: "add pods to unschedulableQ and then move all to activeQ",
1195+
operations: []operation{
1196+
addPodUnschedulableQ,
1197+
moveAllToActiveQ,
1198+
},
1199+
operands: [][]*podInfo{
1200+
pInfos[:total],
1201+
{nil},
1202+
},
1203+
expected: []int64{int64(total), 0, 0},
1204+
},
1205+
{
1206+
name: "make some pods subject to backoff, add pods to unschedulableQ, and then move all to activeQ",
1207+
operations: []operation{
1208+
backoffPod,
1209+
addPodUnschedulableQ,
1210+
moveAllToActiveQ,
1211+
},
1212+
operands: [][]*podInfo{
1213+
pInfos[:20],
1214+
pInfos[:total],
1215+
{nil},
1216+
},
1217+
expected: []int64{int64(total - 20), 20, 0},
1218+
},
1219+
{
1220+
name: "make some pods subject to backoff, add pods to unschedulableQ/activeQ, move all to activeQ, and finally flush backoffQ",
1221+
operations: []operation{
1222+
backoffPod,
1223+
addPodUnschedulableQ,
1224+
addPodActiveQ,
1225+
moveAllToActiveQ,
1226+
flushBackoffQ,
1227+
},
1228+
operands: [][]*podInfo{
1229+
pInfos[:20],
1230+
pInfos[:40],
1231+
pInfos[40:],
1232+
{nil},
1233+
{nil},
1234+
},
1235+
expected: []int64{int64(total), 0, 0},
1236+
},
1237+
}
1238+
1239+
resetMetrics := func() {
1240+
metrics.ActivePods.Set(0)
1241+
metrics.BackoffPods.Set(0)
1242+
metrics.UnschedulablePods.Set(0)
1243+
}
1244+
1245+
for _, test := range tests {
1246+
t.Run(test.name, func(t *testing.T) {
1247+
resetMetrics()
1248+
queue := NewPriorityQueueWithClock(nil, clock.NewFakeClock(timestamp))
1249+
for i, op := range test.operations {
1250+
for _, pInfo := range test.operands[i] {
1251+
op(queue, pInfo)
1252+
}
1253+
}
1254+
1255+
var activeNum, backoffNum, unschedulableNum float64
1256+
metricProto := &dto.Metric{}
1257+
if err := metrics.ActivePods.Write(metricProto); err != nil {
1258+
t.Errorf("error writing ActivePods metric: %v", err)
1259+
}
1260+
activeNum = metricProto.Gauge.GetValue()
1261+
if int64(activeNum) != test.expected[0] {
1262+
t.Errorf("ActivePods: Expected %v, got %v", test.expected[0], activeNum)
1263+
}
1264+
1265+
if err := metrics.BackoffPods.Write(metricProto); err != nil {
1266+
t.Errorf("error writing BackoffPods metric: %v", err)
1267+
}
1268+
backoffNum = metricProto.Gauge.GetValue()
1269+
if int64(backoffNum) != test.expected[1] {
1270+
t.Errorf("BackoffPods: Expected %v, got %v", test.expected[1], backoffNum)
1271+
}
1272+
1273+
if err := metrics.UnschedulablePods.Write(metricProto); err != nil {
1274+
t.Errorf("error writing UnschedulablePods metric: %v", err)
1275+
}
1276+
unschedulableNum = metricProto.Gauge.GetValue()
1277+
if int64(unschedulableNum) != test.expected[2] {
1278+
t.Errorf("UnschedulablePods: Expected %v, got %v", test.expected[2], unschedulableNum)
1279+
}
1280+
})
1281+
}
1282+
}

0 commit comments

Comments
 (0)