diff --git a/pkg/autoscale/autoscale.go b/pkg/autoscale/autoscale.go index 2ded158..553569f 100644 --- a/pkg/autoscale/autoscale.go +++ b/pkg/autoscale/autoscale.go @@ -1,7 +1,6 @@ package autoscale import ( - "fmt" "strconv" nomad "github.com/hashicorp/nomad/api" @@ -188,8 +187,7 @@ func updateResourceTracker(group string, cpu, mem int, tracker map[string]*scala // updateAutoscaleMeta populates meta with the metrics used to autoscale a job group func updateAutoscaleMeta(group, metricType string, value, threshold int, meta map[string]string) { - key := fmt.Sprintf("group-%s-metric", group) - meta[key+"-type"] = metricType + key := group + "-" + metricType meta[key+"-value"] = strconv.Itoa(value) meta[key+"-threshold"] = strconv.Itoa(threshold) } diff --git a/pkg/autoscale/autoscale_test.go b/pkg/autoscale/autoscale_test.go index ed385f2..0dc64c2 100644 --- a/pkg/autoscale/autoscale_test.go +++ b/pkg/autoscale/autoscale_test.go @@ -6,7 +6,7 @@ import ( "github.com/stretchr/testify/assert" ) -func TestAutoScale_updateResourceTracker(t *testing.T) { +func Test_updateResourceTracker(t *testing.T) { testCases := []struct { group string cpu int @@ -35,3 +35,44 @@ func TestAutoScale_updateResourceTracker(t *testing.T) { assert.Equal(t, tc.expected, tc.tracker) } } + +func Test_updateAutoscaleMeta(t *testing.T) { + testCases := []struct { + inputGroup string + inputMetricType string + inputValue int + inputThreshold int + inputMeta map[string]string + expectedResultMeta map[string]string + name string + }{ + { + inputGroup: "test", + inputMetricType: "cpu", + inputValue: 90, + inputThreshold: 89, + inputMeta: make(map[string]string), + expectedResultMeta: map[string]string{"test-cpu-value": "90", "test-cpu-threshold": "89"}, + name: "empty input meta", + }, + { + inputGroup: "test", + inputMetricType: "memory", + inputValue: 91, + inputThreshold: 89, + inputMeta: map[string]string{"test-cpu-value": "90", "test-cpu-threshold": "89"}, + expectedResultMeta: map[string]string{ + "test-cpu-value": "90", + "test-cpu-threshold": "89", + "test-memory-value": "91", + "test-memory-threshold": "89", + }, + name: "existing input meta", + }, + } + + for _, tc := range testCases { + updateAutoscaleMeta(tc.inputGroup, tc.inputMetricType, tc.inputValue, tc.inputThreshold, tc.inputMeta) + assert.Equal(t, tc.expectedResultMeta, tc.inputMeta, tc.name) + } +}