Skip to content
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ test: fmt vet ## Run tests.
go test ./... -coverprofile cover.out

GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint
GOLANGCI_LINT_VERSION ?= v2.11.4
GOLANGCI_LINT_VERSION ?= v2.12.2
.PHONY: golangci-lint
golangci-lint: $(LOCALBIN) ## Download golangci-lint locally if necessary.
@[ -f $(GOLANGCI_LINT) ] || { \
Expand Down
37 changes: 21 additions & 16 deletions internal/autoscaler/planner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,24 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
testNodeRoleLabel = "role"
testNodeRoleValue = "worker"
)

func TestBuildPlanUsesExistingNodeCapacityBeforeScaling(t *testing.T) {
now := time.Now()
template := Resources{MilliCPU: 2000, Memory: 8 * 1024 * 1024 * 1024, Pods: 10}
nodes := []corev1.Node{
node("node-1", map[string]string{"role": "worker"}, Resources{MilliCPU: 2000, Memory: 8 * 1024 * 1024 * 1024, Pods: 10}),
node("node-1", map[string]string{testNodeRoleLabel: testNodeRoleValue}, Resources{MilliCPU: 2000, Memory: 8 * 1024 * 1024 * 1024, Pods: 10}),
}
pods := []corev1.Pod{
runningPod("used", "node-1", Resources{MilliCPU: 500, Memory: 1024 * 1024 * 1024, Pods: 1}),
unschedulablePod("pending-a", now, Resources{MilliCPU: 500, Memory: 1024 * 1024 * 1024, Pods: 1}),
unschedulablePod("pending-b", now, Resources{MilliCPU: 500, Memory: 1024 * 1024 * 1024, Pods: 1}),
}

plan := BuildPlan(now, nodes, pods, 1, 0, 10, map[string]string{"role": "worker"}, template, time.Minute)
plan := BuildPlan(now, nodes, pods, 1, 0, 10, map[string]string{testNodeRoleLabel: testNodeRoleValue}, template, time.Minute)

if plan.NewNodes != 0 {
t.Fatalf("NewNodes = %d, want 0", plan.NewNodes)
Expand Down Expand Up @@ -74,16 +79,16 @@ func TestBuildPlanReportsScaleDownPotential(t *testing.T) {
now := time.Now()
template := Resources{MilliCPU: 2000, Memory: 8 * 1024 * 1024 * 1024, Pods: 10}
nodes := []corev1.Node{
node("node-1", map[string]string{"role": "worker"}, template),
node("node-2", map[string]string{"role": "worker"}, template),
node("node-3", map[string]string{"role": "worker"}, template),
node("node-1", map[string]string{testNodeRoleLabel: testNodeRoleValue}, template),
node("node-2", map[string]string{testNodeRoleLabel: testNodeRoleValue}, template),
node("node-3", map[string]string{testNodeRoleLabel: testNodeRoleValue}, template),
}
pods := []corev1.Pod{
runningPod("used-a", "node-1", Resources{MilliCPU: 500, Memory: 1024 * 1024 * 1024, Pods: 1}),
runningPod("used-b", "node-2", Resources{MilliCPU: 500, Memory: 1024 * 1024 * 1024, Pods: 1}),
}

plan := BuildPlan(now, nodes, pods, 3, 1, 10, map[string]string{"role": "worker"}, template, time.Minute)
plan := BuildPlan(now, nodes, pods, 3, 1, 10, map[string]string{testNodeRoleLabel: testNodeRoleValue}, template, time.Minute)

if plan.ScaleDownPotentialNodes != 2 {
t.Fatalf("ScaleDownPotentialNodes = %d, want 2", plan.ScaleDownPotentialNodes)
Expand All @@ -103,15 +108,15 @@ func TestBuildPlanDoesNotReportScaleDownPotentialWithPendingPods(t *testing.T) {
now := time.Now()
template := Resources{MilliCPU: 2000, Memory: 8 * 1024 * 1024 * 1024, Pods: 10}
nodes := []corev1.Node{
node("node-1", map[string]string{"role": "worker"}, template),
node("node-2", map[string]string{"role": "worker"}, template),
node("node-1", map[string]string{testNodeRoleLabel: testNodeRoleValue}, template),
node("node-2", map[string]string{testNodeRoleLabel: testNodeRoleValue}, template),
}
pods := []corev1.Pod{
runningPod("used-a", "node-1", Resources{MilliCPU: 500, Memory: 1024 * 1024 * 1024, Pods: 1}),
unschedulablePod("pending", now, Resources{MilliCPU: 500, Memory: 1024 * 1024 * 1024, Pods: 1}),
}

plan := BuildPlan(now, nodes, pods, 2, 0, 10, map[string]string{"role": "worker"}, template, time.Minute)
plan := BuildPlan(now, nodes, pods, 2, 0, 10, map[string]string{testNodeRoleLabel: testNodeRoleValue}, template, time.Minute)

if plan.ScaleDownPotentialNodes != 0 {
t.Fatalf("ScaleDownPotentialNodes = %d, want 0", plan.ScaleDownPotentialNodes)
Expand All @@ -125,14 +130,14 @@ func TestBuildPlanDoesNotReportScaleDownPotentialWhenRunningPodDoesNotFitTemplat
now := time.Now()
template := Resources{MilliCPU: 1000, Memory: 1024 * 1024 * 1024, Pods: 10}
nodes := []corev1.Node{
node("node-1", map[string]string{"role": "worker"}, Resources{MilliCPU: 4000, Memory: 8 * 1024 * 1024 * 1024, Pods: 10}),
node("node-2", map[string]string{"role": "worker"}, Resources{MilliCPU: 4000, Memory: 8 * 1024 * 1024 * 1024, Pods: 10}),
node("node-1", map[string]string{testNodeRoleLabel: testNodeRoleValue}, Resources{MilliCPU: 4000, Memory: 8 * 1024 * 1024 * 1024, Pods: 10}),
node("node-2", map[string]string{testNodeRoleLabel: testNodeRoleValue}, Resources{MilliCPU: 4000, Memory: 8 * 1024 * 1024 * 1024, Pods: 10}),
}
pods := []corev1.Pod{
runningPod("large", "node-1", Resources{MilliCPU: 2000, Memory: 1024 * 1024 * 1024, Pods: 1}),
}

plan := BuildPlan(now, nodes, pods, 2, 0, 10, map[string]string{"role": "worker"}, template, time.Minute)
plan := BuildPlan(now, nodes, pods, 2, 0, 10, map[string]string{testNodeRoleLabel: testNodeRoleValue}, template, time.Minute)

if plan.ScaleDownPotentialNodes != 0 {
t.Fatalf("ScaleDownPotentialNodes = %d, want 0", plan.ScaleDownPotentialNodes)
Expand All @@ -146,15 +151,15 @@ func TestBuildPlanScaleDownRespectsMinSize(t *testing.T) {
now := time.Now()
template := Resources{MilliCPU: 2000, Memory: 8 * 1024 * 1024 * 1024, Pods: 10}
nodes := []corev1.Node{
node("node-1", map[string]string{"role": "worker"}, template),
node("node-2", map[string]string{"role": "worker"}, template),
node("node-3", map[string]string{"role": "worker"}, template),
node("node-1", map[string]string{testNodeRoleLabel: testNodeRoleValue}, template),
node("node-2", map[string]string{testNodeRoleLabel: testNodeRoleValue}, template),
node("node-3", map[string]string{testNodeRoleLabel: testNodeRoleValue}, template),
}
pods := []corev1.Pod{
runningPod("used-a", "node-1", Resources{MilliCPU: 500, Memory: 1024 * 1024 * 1024, Pods: 1}),
}

plan := BuildPlan(now, nodes, pods, 3, 2, 10, map[string]string{"role": "worker"}, template, time.Minute)
plan := BuildPlan(now, nodes, pods, 3, 2, 10, map[string]string{testNodeRoleLabel: testNodeRoleValue}, template, time.Minute)

if plan.DesiredTarget != 2 {
t.Fatalf("DesiredTarget = %d, want 2", plan.DesiredTarget)
Expand Down
14 changes: 7 additions & 7 deletions internal/autoscaler/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestRunnerScalesTargetWhenPodsDoNotFit(t *testing.T) {
target := &fakeTarget{size: 1}
kube := fake.NewSimpleClientset(
&corev1.NodeList{Items: []corev1.Node{
node("worker-1", map[string]string{"role": "worker"}, Resources{MilliCPU: 1000, Memory: 1024 * 1024 * 1024, Pods: 10}),
node("worker-1", map[string]string{testNodeRoleLabel: testNodeRoleValue}, Resources{MilliCPU: 1000, Memory: 1024 * 1024 * 1024, Pods: 10}),
}},
&corev1.PodList{Items: []corev1.Pod{
runningPod("used", "worker-1", Resources{MilliCPU: 1000, Memory: 128 * 1024 * 1024, Pods: 1}),
Expand Down Expand Up @@ -72,9 +72,9 @@ func TestRunnerScalesDownWhenCapacityRemainsUnneeded(t *testing.T) {
template := Resources{MilliCPU: 1000, Memory: 1024 * 1024 * 1024, Pods: 10}
kube := fake.NewSimpleClientset(
&corev1.NodeList{Items: []corev1.Node{
node("worker-1", map[string]string{"role": "worker"}, template),
node("worker-2", map[string]string{"role": "worker"}, template),
node("worker-3", map[string]string{"role": "worker"}, template),
node("worker-1", map[string]string{testNodeRoleLabel: testNodeRoleValue}, template),
node("worker-2", map[string]string{testNodeRoleLabel: testNodeRoleValue}, template),
node("worker-3", map[string]string{testNodeRoleLabel: testNodeRoleValue}, template),
}},
&corev1.PodList{Items: []corev1.Pod{
runningPod("used", "worker-1", Resources{MilliCPU: 500, Memory: 128 * 1024 * 1024, Pods: 1}),
Expand Down Expand Up @@ -104,8 +104,8 @@ func TestRunnerWaitsForScaleDownUnneededTime(t *testing.T) {
template := Resources{MilliCPU: 1000, Memory: 1024 * 1024 * 1024, Pods: 10}
kube := fake.NewSimpleClientset(
&corev1.NodeList{Items: []corev1.Node{
node("worker-1", map[string]string{"role": "worker"}, template),
node("worker-2", map[string]string{"role": "worker"}, template),
node("worker-1", map[string]string{testNodeRoleLabel: testNodeRoleValue}, template),
node("worker-2", map[string]string{testNodeRoleLabel: testNodeRoleValue}, template),
}},
&corev1.PodList{Items: []corev1.Pod{
runningPod("used", "worker-1", Resources{MilliCPU: 500, Memory: 128 * 1024 * 1024, Pods: 1}),
Expand Down Expand Up @@ -144,7 +144,7 @@ func testConfig() config.Config {
Attribute: "worker_count",
MinSize: 0,
MaxSize: 10,
NodeSelector: map[string]string{"role": "worker"},
NodeSelector: map[string]string{testNodeRoleLabel: testNodeRoleValue},
TemplateCPU: "1",
TemplateMemory: "1Gi",
TemplatePods: 10,
Expand Down
8 changes: 5 additions & 3 deletions internal/gitlab/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/containeroo/terrascaler/internal/terraform"
)

const testUsername = "alice"

func TestMergeRequestBranch(t *testing.T) {
client := &Client{
cfg: Config{
Expand Down Expand Up @@ -61,9 +63,9 @@ func TestNormalizeUsername(t *testing.T) {
in string
want string
}{
"plain": {in: "alice", want: "alice"},
"at": {in: "@alice", want: "alice"},
"spaces": {in: " @alice ", want: "alice"},
"plain": {in: testUsername, want: testUsername},
"at": {in: "@" + testUsername, want: testUsername},
"spaces": {in: " @" + testUsername + " ", want: testUsername},
} {
t.Run(name, func(t *testing.T) {
if got := normalizeUsername(tc.in); got != tc.want {
Expand Down
Loading