| 1 |
/* |
| 2 |
Copyright 2018 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package custom_metrics |
| 18 |
|
| 19 |
import ( |
| 20 |
"testing" |
| 21 |
|
| 22 |
"github.com/stretchr/testify/require" |
| 23 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 24 |
"k8s.io/apimachinery/pkg/runtime" |
| 25 |
"k8s.io/apimachinery/pkg/runtime/schema" |
| 26 |
|
| 27 |
cmint "k8s.io/metrics/pkg/apis/custom_metrics" |
| 28 |
cmv1beta1 "k8s.io/metrics/pkg/apis/custom_metrics/v1beta1" |
| 29 |
cmv1beta2 "k8s.io/metrics/pkg/apis/custom_metrics/v1beta2" |
| 30 |
) |
| 31 |
|
| 32 |
func TestMetricConverter(t *testing.T) { |
| 33 |
testCases := []struct { |
| 34 |
name string |
| 35 |
group schema.GroupVersion |
| 36 |
expected runtime.Object |
| 37 |
}{ |
| 38 |
{ |
| 39 |
name: "Use custom metrics v1beta2", |
| 40 |
group: cmv1beta2.SchemeGroupVersion, |
| 41 |
expected: &cmv1beta2.MetricListOptions{ |
| 42 |
TypeMeta: metav1.TypeMeta{Kind: "MetricListOptions", APIVersion: cmv1beta2.SchemeGroupVersion.String()}, |
| 43 |
MetricLabelSelector: "foo", |
| 44 |
}, |
| 45 |
}, |
| 46 |
{ |
| 47 |
name: "Use custom metrics v1beta1", |
| 48 |
group: cmv1beta1.SchemeGroupVersion, |
| 49 |
expected: &cmv1beta1.MetricListOptions{ |
| 50 |
TypeMeta: metav1.TypeMeta{Kind: "MetricListOptions", APIVersion: cmv1beta1.SchemeGroupVersion.String()}, |
| 51 |
MetricLabelSelector: "foo", |
| 52 |
}, |
| 53 |
}, |
| 54 |
} |
| 55 |
|
| 56 |
for _, test := range testCases { |
| 57 |
t.Run(test.name, func(t *testing.T) { |
| 58 |
metricConverter := NewMetricConverter() |
| 59 |
opts := &cmint.MetricListOptions{MetricLabelSelector: "foo"} |
| 60 |
res, err := metricConverter.ConvertListOptionsToVersion(opts, test.group) |
| 61 |
require.NoError(t, err) |
| 62 |
require.Equal(t, test.expected, res) |
| 63 |
}) |
| 64 |
} |
| 65 |
} |