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

Add unit test for Auto-scaling Util #2111

Merged
merged 10 commits into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions pkg/autoscaler/autoscaler/calculate/calculate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2020 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package calculate

import (
. "github.com/onsi/gomega"
"testing"
)

func TestCalculate(t *testing.T) {
g := NewGomegaWithT(t)
tests := []struct {
name string
currentReplicas int32
currentValue float64
targetValue float64
expectedReplicas int32
errMsg string
}{
{
name: "under target value",
currentReplicas: 4,
currentValue: 20.0,
targetValue: 30.0,
expectedReplicas: 3,
},
{
name: "equal target value",
currentReplicas: 4,
currentValue: 30.0,
targetValue: 30.0,
expectedReplicas: 4,
},
{
name: "greater than target value",
currentReplicas: 4,
currentValue: 35.0,
targetValue: 30.0,
expectedReplicas: 5,
},
{
name: "target value is zero",
currentReplicas: 4,
currentValue: 35.0,
targetValue: 0,
expectedReplicas: -1,
errMsg: "targetValue in calculate func can't be zero",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r, err := calculate(tt.currentValue, tt.targetValue, tt.currentReplicas)
if len(tt.errMsg) < 1 {
g.Expect(err).Should(BeNil())
} else {
g.Expect(err).ShouldNot(BeNil())
g.Expect(err.Error()).Should(Equal(tt.errMsg))
}
g.Expect(r).Should(Equal(tt.expectedReplicas))
})
}
}
101 changes: 101 additions & 0 deletions pkg/autoscaler/autoscaler/calculate/cpu_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright 2020 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package calculate

import (
"fmt"
"testing"

. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
)

func TestExtractCpuRequestsRatio(t *testing.T) {
g := NewGomegaWithT(t)
tests := []struct {
name string
defineRequest bool
cpuValue string
expectedRadio float64
occurError bool
errMsg string
}{
{
name: "cpu 1",
defineRequest: true,
cpuValue: "1",
occurError: false,
expectedRadio: 1.0,
errMsg: "",
},
{
name: "cpu 1000m",
defineRequest: true,
cpuValue: "1000m",
occurError: false,
expectedRadio: 1.0,
errMsg: "",
},
{
name: "cpu 1500m",
defineRequest: true,
cpuValue: "1500m",
occurError: false,
expectedRadio: 1.5,
errMsg: "",
},
{
name: "no cpu request",
defineRequest: false,
cpuValue: "",
occurError: true,
expectedRadio: 0,
errMsg: fmt.Sprintf("container[%s] cpu requests is empty", "container"),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := newContainer()
if tt.defineRequest {
c.Resources.Requests = map[corev1.ResourceName]resource.Quantity{
corev1.ResourceCPU: resource.MustParse(tt.cpuValue),
}
} else {
c.Resources.Requests = map[corev1.ResourceName]resource.Quantity{}
}
r, err := extractCpuRequestsRatio(c)
if !tt.occurError {
g.Expect(err).Should(BeNil())
} else {
g.Expect(err).ShouldNot(BeNil())
g.Expect(err.Error()).Should(Equal(tt.errMsg))
}
g.Expect(almostEqual(r, tt.expectedRadio)).Should(Equal(true))
})
}
}

func newContainer() *corev1.Container {
return &corev1.Container{
Name: "container",
Image: "fake:fake",
Resources: corev1.ResourceRequirements{
Requests: map[corev1.ResourceName]resource.Quantity{
corev1.ResourceCPU: resource.MustParse("1"),
},
},
}
}
Loading