|
| 1 | +/* |
| 2 | +Copyright 2020 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 testing |
| 18 | + |
| 19 | +import ( |
| 20 | + "math/rand" |
| 21 | + "testing" |
| 22 | + |
| 23 | + custommetrics "k8s.io/metrics/pkg/apis/custom_metrics" |
| 24 | + custommetricsv1beta1 "k8s.io/metrics/pkg/apis/custom_metrics/v1beta1" |
| 25 | + custommetricsv1beta2 "k8s.io/metrics/pkg/apis/custom_metrics/v1beta2" |
| 26 | + externalmetrics "k8s.io/metrics/pkg/apis/external_metrics" |
| 27 | + externalmetricsv1beta1 "k8s.io/metrics/pkg/apis/external_metrics/v1beta1" |
| 28 | + metrics "k8s.io/metrics/pkg/apis/metrics" |
| 29 | + metricsv1alpha1 "k8s.io/metrics/pkg/apis/metrics/v1alpha1" |
| 30 | + metricsv1beta1 "k8s.io/metrics/pkg/apis/metrics/v1beta1" |
| 31 | + |
| 32 | + "github.com/stretchr/testify/require" |
| 33 | + "k8s.io/apimachinery/pkg/api/apitesting/fuzzer" |
| 34 | + "k8s.io/apimachinery/pkg/api/apitesting/roundtrip" |
| 35 | + genericfuzzer "k8s.io/apimachinery/pkg/apis/meta/fuzzer" |
| 36 | + "k8s.io/apimachinery/pkg/runtime" |
| 37 | + "k8s.io/apimachinery/pkg/runtime/serializer" |
| 38 | +) |
| 39 | + |
| 40 | +var groups = []runtime.SchemeBuilder{ |
| 41 | + custommetrics.SchemeBuilder, |
| 42 | + custommetricsv1beta1.SchemeBuilder, |
| 43 | + custommetricsv1beta2.SchemeBuilder, |
| 44 | + externalmetrics.SchemeBuilder, |
| 45 | + externalmetricsv1beta1.SchemeBuilder, |
| 46 | + metrics.SchemeBuilder, |
| 47 | + metricsv1alpha1.SchemeBuilder, |
| 48 | + metricsv1beta1.SchemeBuilder, |
| 49 | +} |
| 50 | + |
| 51 | +func TestRoundTripTypes(t *testing.T) { |
| 52 | + scheme := runtime.NewScheme() |
| 53 | + codecs := serializer.NewCodecFactory(scheme) |
| 54 | + for _, builder := range groups { |
| 55 | + require.NoError(t, builder.AddToScheme(scheme)) |
| 56 | + } |
| 57 | + seed := rand.Int63() |
| 58 | + // I'm only using the generic fuzzer funcs, but at some point in time we might need to |
| 59 | + // switch to specialized. For now we're happy with the current serialization test. |
| 60 | + fuzzer := fuzzer.FuzzerFor(genericfuzzer.Funcs, rand.NewSource(seed), codecs) |
| 61 | + |
| 62 | + roundtrip.RoundTripExternalTypes(t, scheme, codecs, fuzzer, nil) |
| 63 | + roundtrip.RoundTripTypes(t, scheme, codecs, fuzzer, nil) |
| 64 | +} |
0 commit comments