Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added more unit test for hpa status #21750

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 25 additions & 1 deletion pkg/controller/podautoscaler/horizontal_test.go
Expand Up @@ -62,6 +62,8 @@ type testCase struct {
desiredReplicas int
// CPU target utilization as a percentage of the requested resources.
CPUTarget int
CPUCurrent int
verifyCPUCurrent bool
reportedLevels []uint64
reportedCPURequests []resource.Quantity
cmTarget *extensions.CustomMetricTargetList
Expand All @@ -71,6 +73,21 @@ type testCase struct {
verifyEvents bool
}

func (tc *testCase) computeCPUCurrent() {
if len(tc.reportedLevels) != len(tc.reportedCPURequests) || len(tc.reportedLevels) == 0 {
return
}
reported := 0
for _, r := range tc.reportedLevels {
reported += int(r)
}
requested := 0
for _, req := range tc.reportedCPURequests {
requested += int(req.MilliValue())
}
tc.CPUCurrent = 100 * reported / requested
}

func (tc *testCase) prepareTestClient(t *testing.T) *fake.Clientset {
namespace := "test-namespace"
hpaName := "test-hpa"
Expand All @@ -80,6 +97,7 @@ func (tc *testCase) prepareTestClient(t *testing.T) *fake.Clientset {
tc.scaleUpdated = false
tc.statusUpdated = false
tc.eventCreated = false
tc.computeCPUCurrent()

fakeClient := &fake.Clientset{}
fakeClient.AddReactor("list", "horizontalpodautoscalers", func(action core.Action) (handled bool, ret runtime.Object, err error) {
Expand Down Expand Up @@ -198,6 +216,10 @@ func (tc *testCase) prepareTestClient(t *testing.T) *fake.Clientset {
assert.Equal(t, hpaName, obj.Name)
assert.Equal(t, tc.desiredReplicas, obj.Status.DesiredReplicas)
tc.statusUpdated = true
if tc.verifyCPUCurrent {
assert.NotNil(t, obj.Status.CurrentCPUUtilizationPercentage)
assert.Equal(t, tc.CPUCurrent, *obj.Status.CurrentCPUUtilizationPercentage)
}
return true, obj, nil
})

Expand Down Expand Up @@ -242,6 +264,7 @@ func TestScaleUp(t *testing.T) {
initialReplicas: 3,
desiredReplicas: 5,
CPUTarget: 30,
verifyCPUCurrent: true,
reportedLevels: []uint64{300, 500, 700},
reportedCPURequests: []resource.Quantity{resource.MustParse("1.0"), resource.MustParse("1.0"), resource.MustParse("1.0")},
}
Expand Down Expand Up @@ -274,6 +297,7 @@ func TestScaleDown(t *testing.T) {
initialReplicas: 5,
desiredReplicas: 3,
CPUTarget: 50,
verifyCPUCurrent: true,
reportedLevels: []uint64{100, 300, 500, 250, 250},
reportedCPURequests: []resource.Quantity{resource.MustParse("1.0"), resource.MustParse("1.0"), resource.MustParse("1.0"), resource.MustParse("1.0"), resource.MustParse("1.0")},
}
Expand Down Expand Up @@ -354,7 +378,7 @@ func TestZeroReplicas(t *testing.T) {
tc.runTest(t)
}

func TestToFewReplicas(t *testing.T) {
func TestTooFewReplicas(t *testing.T) {
tc := testCase{
minReplicas: 3,
maxReplicas: 5,
Expand Down