Testing External Metrics Storage

Added a testcase for insert/list/get an external metric

Signed-off-by: Muhammad Muaaz Saleem <muhammad.muaaz.saleem@zalando.de>
This commit is contained in:
Muhammad Muaaz Saleem
2018-11-19 15:57:51 +01:00
parent fb1a08195d
commit 7c8c239d18

View File

@ -10,10 +10,11 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/metrics/pkg/apis/custom_metrics"
"k8s.io/metrics/pkg/apis/external_metrics"
"testing"
)
func TestMetricStore(t *testing.T) {
func TestInternalMetricStorage(t *testing.T) {
var metricStoreTests = []struct {
test string
insert collector.CollectedMetric
@ -138,11 +139,65 @@ func TestMetricStore(t *testing.T) {
metric := metricsStore.GetMetricsByName(tc.byName.name, tc.byName.info)
require.Equal(t, tc.insert.Custom, *metric)
if tc.byLabel.namespace == "default" {
metrics := metricsStore.GetMetricsBySelector(tc.byLabel.namespace, tc.byLabel.selector, tc.byLabel.info)
require.Equal(t, tc.insert.Custom, metrics.Items[0])
metrics := metricsStore.GetMetricsBySelector(tc.byLabel.namespace, tc.byLabel.selector, tc.byLabel.info)
require.Equal(t, tc.insert.Custom, metrics.Items[0])
})
}
}
func TestExternalMetricStorage(t *testing.T) {
var metricStoreTests = []struct {
test string
insert collector.CollectedMetric
list provider.ExternalMetricInfo
get struct {
namespace string
selector labels.Selector
info provider.ExternalMetricInfo
}
}{
{
test: "insert/list/get an external metric",
insert: collector.CollectedMetric{
Type: v2beta1.MetricSourceType("External"),
External: external_metrics.ExternalMetricValue{
MetricName: "metric-per-unit",
Value: *resource.NewQuantity(0, ""),
},
},
list: provider.ExternalMetricInfo{
Metric: "metric-per-unit",
},
get: struct {
namespace string
selector labels.Selector
info provider.ExternalMetricInfo
}{ namespace: "",
selector: labels.Everything(),
info: provider.ExternalMetricInfo{
Metric: "metric-per-unit",
}},
},
}
for _, tc := range metricStoreTests {
t.Run(tc.test, func(t *testing.T) {
metricsStore := NewMetricStore()
// Insert a metric with value
metricsStore.Insert(tc.insert)
// List a metric with value
metricInfos := metricsStore.ListAllExternalMetrics()
require.Equal(t, tc.list, metricInfos[0])
// Get the metric by name
metrics, err := metricsStore.GetExternalMetric(tc.get.namespace, tc.get.selector, tc.get.info)
require.NoError(t, err)
require.Equal(t, tc.insert.External, metrics.Items[0])
}
})
}