CustomMetrics in HPA MetricsClient

This commit is contained in:
Marcin Wielgus
2016-01-27 21:14:45 +01:00
parent 471e602bcd
commit a3972947cc
2 changed files with 121 additions and 176 deletions

View File

@@ -61,9 +61,9 @@ type metricPoint struct {
type testCase struct {
replicas int
desiredValue int64
desiredValue float64
desiredError error
targetResource api.ResourceName
targetResource string
targetTimestamp int
reportedMetricsPoints [][]metricPoint
namespace string
@@ -94,7 +94,7 @@ func (tc *testCase) prepareTestClient(t *testing.T) *testclient.Fake {
{
Resources: api.ResourceRequirements{
Requests: api.ResourceList{
tc.targetResource: resource.MustParse("10"),
api.ResourceCPU: resource.MustParse("10"),
},
},
},
@@ -135,17 +135,15 @@ func (tc *testCase) prepareTestClient(t *testing.T) *testclient.Fake {
return fakeClient
}
func (tc *testCase) verifyResults(t *testing.T, val *ResourceConsumption, timestamp time.Time, err error) {
func (tc *testCase) verifyResults(t *testing.T, val *float64, timestamp time.Time, err error) {
assert.Equal(t, tc.desiredError, err)
if tc.desiredError != nil {
return
}
if tc.targetResource == api.ResourceCPU {
assert.Equal(t, tc.desiredValue, val.Quantity.MilliValue())
}
if tc.targetResource == api.ResourceMemory {
assert.Equal(t, tc.desiredValue, val.Quantity.Value())
}
assert.NotNil(t, val)
assert.True(t, tc.desiredValue-0.001 < *val)
assert.True(t, tc.desiredValue+0.001 > *val)
targetTimestamp := fixedTimestamp.Add(time.Duration(tc.targetTimestamp) * time.Minute)
assert.Equal(t, targetTimestamp, timestamp)
}
@@ -153,28 +151,34 @@ func (tc *testCase) verifyResults(t *testing.T, val *ResourceConsumption, timest
func (tc *testCase) runTest(t *testing.T) {
testClient := tc.prepareTestClient(t)
metricsClient := NewHeapsterMetricsClient(testClient, DefaultHeapsterNamespace, DefaultHeapsterScheme, DefaultHeapsterService, DefaultHeapsterPort)
val, _, timestamp, err := metricsClient.GetResourceConsumptionAndRequest(tc.targetResource, tc.namespace, tc.selector)
tc.verifyResults(t, val, timestamp, err)
if tc.targetResource == "cpu-usage" {
val, _, timestamp, err := metricsClient.GetCpuConsumptionAndRequestInMillis(tc.namespace, tc.selector)
fval := float64(val)
tc.verifyResults(t, &fval, timestamp, err)
} else {
val, timestamp, err := metricsClient.GetCustomMetric(tc.targetResource, tc.namespace, tc.selector)
tc.verifyResults(t, val, timestamp, err)
}
}
func TestCPU(t *testing.T) {
tc := testCase{
replicas: 3,
desiredValue: 5000,
targetResource: api.ResourceCPU,
targetResource: "cpu-usage",
targetTimestamp: 1,
reportedMetricsPoints: [][]metricPoint{{{5000, 1}}, {{5000, 1}}, {{5000, 1}}},
}
tc.runTest(t)
}
func TestMemory(t *testing.T) {
func TestQPS(t *testing.T) {
tc := testCase{
replicas: 3,
desiredValue: 5000,
targetResource: api.ResourceMemory,
desiredValue: 13.33333,
targetResource: "qps",
targetTimestamp: 1,
reportedMetricsPoints: [][]metricPoint{{{5000, 1}}, {{5000, 2}}, {{5000, 4}}},
reportedMetricsPoints: [][]metricPoint{{{10, 1}}, {{20, 1}}, {{10, 1}}},
}
tc.runTest(t)
}
@@ -183,18 +187,18 @@ func TestCPUSumEqualZero(t *testing.T) {
tc := testCase{
replicas: 3,
desiredValue: 0,
targetResource: api.ResourceCPU,
targetResource: "cpu-usage",
targetTimestamp: 0,
reportedMetricsPoints: [][]metricPoint{{{0, 0}}, {{0, 0}}, {{0, 0}}},
}
tc.runTest(t)
}
func TestMemorySumEqualZero(t *testing.T) {
func TestQpsSumEqualZero(t *testing.T) {
tc := testCase{
replicas: 3,
desiredValue: 0,
targetResource: api.ResourceMemory,
targetResource: "qps",
targetTimestamp: 0,
reportedMetricsPoints: [][]metricPoint{{{0, 0}}, {{0, 0}}, {{0, 0}}},
}
@@ -205,23 +209,7 @@ func TestCPUMoreMetrics(t *testing.T) {
tc := testCase{
replicas: 5,
desiredValue: 5000,
targetResource: api.ResourceCPU,
targetTimestamp: 10,
reportedMetricsPoints: [][]metricPoint{
{{0, 3}, {0, 6}, {5, 4}, {9000, 10}},
{{5000, 2}, {10, 5}, {66, 1}, {0, 10}},
{{5000, 3}, {80, 5}, {6000, 10}},
{{5000, 3}, {40, 3}, {0, 9}, {200, 2}, {8000, 10}},
{{5000, 2}, {20, 2}, {2000, 10}}},
}
tc.runTest(t)
}
func TestMemoryMoreMetrics(t *testing.T) {
tc := testCase{
replicas: 5,
desiredValue: 5000,
targetResource: api.ResourceMemory,
targetResource: "cpu-usage",
targetTimestamp: 10,
reportedMetricsPoints: [][]metricPoint{
{{0, 3}, {0, 6}, {5, 4}, {9000, 10}},
@@ -237,18 +225,7 @@ func TestCPUResultIsFloat(t *testing.T) {
tc := testCase{
replicas: 6,
desiredValue: 4783,
targetResource: api.ResourceCPU,
targetTimestamp: 4,
reportedMetricsPoints: [][]metricPoint{{{4000, 4}}, {{9500, 4}}, {{3000, 4}}, {{7000, 4}}, {{3200, 4}}, {{2000, 4}}},
}
tc.runTest(t)
}
func TestMemoryResultIsFloat(t *testing.T) {
tc := testCase{
replicas: 6,
desiredValue: 4783,
targetResource: api.ResourceMemory,
targetResource: "cpu-usage",
targetTimestamp: 4,
reportedMetricsPoints: [][]metricPoint{{{4000, 4}}, {{9500, 4}}, {{3000, 4}}, {{7000, 4}}, {{3200, 4}}, {{2000, 4}}},
}
@@ -259,7 +236,7 @@ func TestCPUSamplesWithRandomTimestamps(t *testing.T) {
tc := testCase{
replicas: 3,
desiredValue: 3000,
targetResource: api.ResourceCPU,
targetResource: "cpu-usage",
targetTimestamp: 3,
reportedMetricsPoints: [][]metricPoint{
{{1, 1}, {3000, 5}, {2, 2}},
@@ -269,43 +246,20 @@ func TestCPUSamplesWithRandomTimestamps(t *testing.T) {
tc.runTest(t)
}
func TestMemorySamplesWithRandomTimestamps(t *testing.T) {
tc := testCase{
replicas: 3,
desiredValue: 3000,
targetResource: api.ResourceMemory,
targetTimestamp: 3,
reportedMetricsPoints: [][]metricPoint{
{{1, 1}, {3000, 3}, {2, 2}},
{{2, 2}, {1, 1}, {3000, 3}},
{{3000, 3}, {1, 1}, {2, 2}}},
}
tc.runTest(t)
}
func TestErrorMetricNotDefined(t *testing.T) {
tc := testCase{
replicas: 1,
desiredError: fmt.Errorf("heapster metric not defined for "),
reportedMetricsPoints: [][]metricPoint{{{4000, 4}}},
}
tc.runTest(t)
}
func TestCPUMissingMetrics(t *testing.T) {
tc := testCase{
replicas: 3,
targetResource: api.ResourceCPU,
targetResource: "cpu-usage",
desiredError: fmt.Errorf("metrics obtained for 1/3 of pods"),
reportedMetricsPoints: [][]metricPoint{{{4000, 4}}},
}
tc.runTest(t)
}
func TestMemoryMissingMetrics(t *testing.T) {
func TestQpsMissingMetrics(t *testing.T) {
tc := testCase{
replicas: 3,
targetResource: api.ResourceMemory,
targetResource: "qps",
desiredError: fmt.Errorf("metrics obtained for 1/3 of pods"),
reportedMetricsPoints: [][]metricPoint{{{4000, 4}}},
}
@@ -315,17 +269,17 @@ func TestMemoryMissingMetrics(t *testing.T) {
func TestCPUSuperfluousMetrics(t *testing.T) {
tc := testCase{
replicas: 3,
targetResource: api.ResourceCPU,
targetResource: "cpu-usage",
desiredError: fmt.Errorf("metrics obtained for 6/3 of pods"),
reportedMetricsPoints: [][]metricPoint{{{1000, 1}}, {{2000, 4}}, {{2000, 1}}, {{4000, 5}}, {{2000, 1}}, {{4000, 4}}},
}
tc.runTest(t)
}
func TestMemorySuperfluousMetrics(t *testing.T) {
func TestQpsSuperfluousMetrics(t *testing.T) {
tc := testCase{
replicas: 3,
targetResource: api.ResourceMemory,
targetResource: "qps",
desiredError: fmt.Errorf("metrics obtained for 6/3 of pods"),
reportedMetricsPoints: [][]metricPoint{{{1000, 1}}, {{2000, 4}}, {{2000, 1}}, {{4000, 5}}, {{2000, 1}}, {{4000, 4}}},
}
@@ -335,17 +289,7 @@ func TestMemorySuperfluousMetrics(t *testing.T) {
func TestCPUEmptyMetrics(t *testing.T) {
tc := testCase{
replicas: 3,
targetResource: api.ResourceCPU,
desiredError: fmt.Errorf("metrics obtained for 0/3 of pods"),
reportedMetricsPoints: [][]metricPoint{},
}
tc.runTest(t)
}
func TestMemoryEmptyMetrics(t *testing.T) {
tc := testCase{
replicas: 3,
targetResource: api.ResourceMemory,
targetResource: "cpu-usage",
desiredError: fmt.Errorf("metrics obtained for 0/3 of pods"),
reportedMetricsPoints: [][]metricPoint{},
}
@@ -355,37 +299,17 @@ func TestMemoryEmptyMetrics(t *testing.T) {
func TestCPUZeroReplicas(t *testing.T) {
tc := testCase{
replicas: 0,
targetResource: api.ResourceCPU,
targetResource: "cpu-usage",
desiredError: fmt.Errorf("some pods do not have request for cpu"),
reportedMetricsPoints: [][]metricPoint{},
}
tc.runTest(t)
}
func TestMemoryZeroReplicas(t *testing.T) {
tc := testCase{
replicas: 0,
targetResource: api.ResourceMemory,
desiredError: fmt.Errorf("some pods do not have request for memory"),
reportedMetricsPoints: [][]metricPoint{},
}
tc.runTest(t)
}
func TestCPUEmptyMetricsForOnePod(t *testing.T) {
tc := testCase{
replicas: 3,
targetResource: api.ResourceCPU,
desiredError: fmt.Errorf("metrics obtained for 2/3 of pods"),
reportedMetricsPoints: [][]metricPoint{{}, {{100, 1}}, {{400, 2}, {300, 3}}},
}
tc.runTest(t)
}
func TestMemoryEmptyMetricsForOnePod(t *testing.T) {
tc := testCase{
replicas: 3,
targetResource: api.ResourceMemory,
targetResource: "cpu-usage",
desiredError: fmt.Errorf("metrics obtained for 2/3 of pods"),
reportedMetricsPoints: [][]metricPoint{{}, {{100, 1}}, {{400, 2}, {300, 3}}},
}