diff --git a/Makefile b/Makefile index 24da6f9..570e699 100644 --- a/Makefile +++ b/Makefile @@ -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) ] || { \ diff --git a/internal/autoscaler/planner_test.go b/internal/autoscaler/planner_test.go index 54a4f87..08fb427 100644 --- a/internal/autoscaler/planner_test.go +++ b/internal/autoscaler/planner_test.go @@ -9,11 +9,16 @@ 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}), @@ -21,7 +26,7 @@ func TestBuildPlanUsesExistingNodeCapacityBeforeScaling(t *testing.T) { 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) @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/internal/autoscaler/runner_test.go b/internal/autoscaler/runner_test.go index 2ab1096..4edefdd 100644 --- a/internal/autoscaler/runner_test.go +++ b/internal/autoscaler/runner_test.go @@ -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}), @@ -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}), @@ -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}), @@ -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, diff --git a/internal/gitlab/client_test.go b/internal/gitlab/client_test.go index bc320fa..513cf93 100644 --- a/internal/gitlab/client_test.go +++ b/internal/gitlab/client_test.go @@ -7,6 +7,8 @@ import ( "github.com/containeroo/terrascaler/internal/terraform" ) +const testUsername = "alice" + func TestMergeRequestBranch(t *testing.T) { client := &Client{ cfg: Config{ @@ -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 {