From 79f3f6e9b17c8f280a6e63c04a29145df99b67f8 Mon Sep 17 00:00:00 2001 From: HARISH KUNA Date: Mon, 16 Mar 2020 19:07:49 -0700 Subject: [PATCH] Test dropped round-trip annotations in HPA conversion --- pkg/apis/autoscaling/v1/BUILD | 3 + pkg/apis/autoscaling/v1/defaults_test.go | 69 ++++++++++++- pkg/apis/autoscaling/v2beta1/BUILD | 2 + pkg/apis/autoscaling/v2beta1/defaults_test.go | 70 ++++++++++++- pkg/apis/autoscaling/v2beta2/BUILD | 7 ++ pkg/apis/autoscaling/v2beta2/defaults_test.go | 99 ++++++++++++++++++- 6 files changed, 245 insertions(+), 5 deletions(-) diff --git a/pkg/apis/autoscaling/v1/BUILD b/pkg/apis/autoscaling/v1/BUILD index 0360cbfa5f5..e33396d1412 100644 --- a/pkg/apis/autoscaling/v1/BUILD +++ b/pkg/apis/autoscaling/v1/BUILD @@ -30,10 +30,13 @@ go_test( embed = [":go_default_library"], deps = [ "//pkg/api/legacyscheme:go_default_library", + "//pkg/apis/autoscaling:go_default_library", "//pkg/apis/autoscaling/install:go_default_library", "//pkg/apis/core/install:go_default_library", "//staging/src/k8s.io/api/autoscaling/v1:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/util/diff:go_default_library", "//vendor/k8s.io/utils/pointer:go_default_library", ], ) diff --git a/pkg/apis/autoscaling/v1/defaults_test.go b/pkg/apis/autoscaling/v1/defaults_test.go index e2b8cda4290..e2ad0b4fe2d 100644 --- a/pkg/apis/autoscaling/v1/defaults_test.go +++ b/pkg/apis/autoscaling/v1/defaults_test.go @@ -21,9 +21,11 @@ import ( "testing" autoscalingv1 "k8s.io/api/autoscaling/v1" - + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/util/diff" "k8s.io/kubernetes/pkg/api/legacyscheme" + "k8s.io/kubernetes/pkg/apis/autoscaling" _ "k8s.io/kubernetes/pkg/apis/autoscaling/install" . "k8s.io/kubernetes/pkg/apis/autoscaling/v1" _ "k8s.io/kubernetes/pkg/apis/core/install" @@ -67,6 +69,71 @@ func TestSetDefaultHPA(t *testing.T) { } } +func TestHorizontalPodAutoscalerAnnotations(t *testing.T) { + tests := []struct { + hpa autoscalingv1.HorizontalPodAutoscaler + test string + }{ + { + hpa: autoscalingv1.HorizontalPodAutoscaler{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + autoscaling.HorizontalPodAutoscalerConditionsAnnotation: "", + autoscaling.MetricSpecsAnnotation: "", + autoscaling.BehaviorSpecsAnnotation: "", + autoscaling.MetricStatusesAnnotation: "", + }, + }, + }, + test: "test empty value for Annotations", + }, + { + hpa: autoscalingv1.HorizontalPodAutoscaler{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + autoscaling.HorizontalPodAutoscalerConditionsAnnotation: "abc", + autoscaling.MetricSpecsAnnotation: "abc", + autoscaling.BehaviorSpecsAnnotation: "abc", + autoscaling.MetricStatusesAnnotation: "abc", + }, + }, + }, + test: "test random value for Annotations", + }, + { + hpa: autoscalingv1.HorizontalPodAutoscaler{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + autoscaling.HorizontalPodAutoscalerConditionsAnnotation: "[]", + autoscaling.MetricSpecsAnnotation: "[]", + autoscaling.BehaviorSpecsAnnotation: "[]", + autoscaling.MetricStatusesAnnotation: "[]", + }, + }, + }, + test: "test empty array value for Annotations", + }, + } + + for _, test := range tests { + hpa := &test.hpa + hpaBeforeMuatate := *hpa.DeepCopy() + obj := roundTrip(t, runtime.Object(hpa)) + final_obj, ok := obj.(*autoscalingv1.HorizontalPodAutoscaler) + if !ok { + t.Fatalf("unexpected object: %v", obj) + } + if !reflect.DeepEqual(*hpa, hpaBeforeMuatate) { + t.Errorf("diff: %v", diff.ObjectDiff(*hpa, hpaBeforeMuatate)) + t.Errorf("expected: %#v\n actual: %#v", *hpa, hpaBeforeMuatate) + } + + if len(final_obj.ObjectMeta.Annotations) != 0 { + t.Fatalf("unexpected annotations: %v", final_obj.ObjectMeta.Annotations) + } + } +} + func roundTrip(t *testing.T, obj runtime.Object) runtime.Object { data, err := runtime.Encode(legacyscheme.Codecs.LegacyCodec(SchemeGroupVersion), obj) if err != nil { diff --git a/pkg/apis/autoscaling/v2beta1/BUILD b/pkg/apis/autoscaling/v2beta1/BUILD index 5fbc372b458..491d1e9e41b 100644 --- a/pkg/apis/autoscaling/v2beta1/BUILD +++ b/pkg/apis/autoscaling/v2beta1/BUILD @@ -39,7 +39,9 @@ go_test( "//staging/src/k8s.io/api/autoscaling/v2beta1:go_default_library", "//staging/src/k8s.io/api/core/v1:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/api/equality:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/util/diff:go_default_library", "//vendor/github.com/stretchr/testify/assert:go_default_library", "//vendor/k8s.io/utils/pointer:go_default_library", ], diff --git a/pkg/apis/autoscaling/v2beta1/defaults_test.go b/pkg/apis/autoscaling/v2beta1/defaults_test.go index a68e39f1597..71b0d349f91 100644 --- a/pkg/apis/autoscaling/v2beta1/defaults_test.go +++ b/pkg/apis/autoscaling/v2beta1/defaults_test.go @@ -20,9 +20,12 @@ import ( "reflect" "testing" + "k8s.io/apimachinery/pkg/util/diff" + autoscalingv2beta1 "k8s.io/api/autoscaling/v2beta1" - "k8s.io/api/core/v1" + v1 "k8s.io/api/core/v1" apiequality "k8s.io/apimachinery/pkg/api/equality" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/kubernetes/pkg/api/legacyscheme" "k8s.io/kubernetes/pkg/apis/autoscaling" @@ -105,6 +108,71 @@ func TestSetDefaultHPA(t *testing.T) { } } +func TestHorizontalPodAutoscalerAnnotations(t *testing.T) { + tests := []struct { + hpa autoscalingv2beta1.HorizontalPodAutoscaler + test string + }{ + { + hpa: autoscalingv2beta1.HorizontalPodAutoscaler{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + autoscaling.HorizontalPodAutoscalerConditionsAnnotation: "", + autoscaling.MetricSpecsAnnotation: "", + autoscaling.BehaviorSpecsAnnotation: "", + autoscaling.MetricStatusesAnnotation: "", + }, + }, + }, + test: "test empty value for Annotations", + }, + { + hpa: autoscalingv2beta1.HorizontalPodAutoscaler{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + autoscaling.HorizontalPodAutoscalerConditionsAnnotation: "abc", + autoscaling.MetricSpecsAnnotation: "abc", + autoscaling.BehaviorSpecsAnnotation: "abc", + autoscaling.MetricStatusesAnnotation: "abc", + }, + }, + }, + test: "test random value for Annotations", + }, + { + hpa: autoscalingv2beta1.HorizontalPodAutoscaler{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + autoscaling.HorizontalPodAutoscalerConditionsAnnotation: "[]", + autoscaling.MetricSpecsAnnotation: "[]", + autoscaling.BehaviorSpecsAnnotation: "[]", + autoscaling.MetricStatusesAnnotation: "[]", + }, + }, + }, + test: "test empty array value for Annotations", + }, + } + + for _, test := range tests { + hpa := &test.hpa + hpaBeforeMuatate := *hpa.DeepCopy() + obj := roundTrip(t, runtime.Object(hpa)) + final_obj, ok := obj.(*autoscalingv2beta1.HorizontalPodAutoscaler) + if !ok { + t.Fatalf("unexpected object: %v", obj) + } + if !reflect.DeepEqual(*hpa, hpaBeforeMuatate) { + t.Errorf("diff: %v", diff.ObjectDiff(*hpa, hpaBeforeMuatate)) + t.Errorf("expected: %#v\n actual: %#v", *hpa, hpaBeforeMuatate) + } + + if len(final_obj.ObjectMeta.Annotations) != 0 { + t.Fatalf("unexpected annotations: %v", final_obj.ObjectMeta.Annotations) + } + } +} + func roundTrip(t *testing.T, obj runtime.Object) runtime.Object { data, err := runtime.Encode(legacyscheme.Codecs.LegacyCodec(SchemeGroupVersion), obj) if err != nil { diff --git a/pkg/apis/autoscaling/v2beta2/BUILD b/pkg/apis/autoscaling/v2beta2/BUILD index 0dd2134e644..3c6694e297e 100644 --- a/pkg/apis/autoscaling/v2beta2/BUILD +++ b/pkg/apis/autoscaling/v2beta2/BUILD @@ -44,7 +44,14 @@ go_test( srcs = ["defaults_test.go"], embed = [":go_default_library"], deps = [ + "//pkg/api/legacyscheme:go_default_library", + "//pkg/apis/autoscaling:go_default_library", + "//pkg/apis/autoscaling/install:go_default_library", + "//pkg/apis/core/install:go_default_library", "//staging/src/k8s.io/api/autoscaling/v2beta2:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/util/diff:go_default_library", "//vendor/github.com/stretchr/testify/assert:go_default_library", "//vendor/k8s.io/utils/pointer:go_default_library", ], diff --git a/pkg/apis/autoscaling/v2beta2/defaults_test.go b/pkg/apis/autoscaling/v2beta2/defaults_test.go index fcdac48da01..728e2af4ba9 100644 --- a/pkg/apis/autoscaling/v2beta2/defaults_test.go +++ b/pkg/apis/autoscaling/v2beta2/defaults_test.go @@ -17,13 +17,21 @@ limitations under the License. package v2beta2_test import ( + "reflect" "testing" - autoscalingv2 "k8s.io/api/autoscaling/v2beta2" - . "k8s.io/kubernetes/pkg/apis/autoscaling/v2beta2" - utilpointer "k8s.io/utils/pointer" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/util/diff" + "k8s.io/kubernetes/pkg/api/legacyscheme" "github.com/stretchr/testify/assert" + autoscalingv2 "k8s.io/api/autoscaling/v2beta2" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/kubernetes/pkg/apis/autoscaling" + _ "k8s.io/kubernetes/pkg/apis/autoscaling/install" + . "k8s.io/kubernetes/pkg/apis/autoscaling/v2beta2" + _ "k8s.io/kubernetes/pkg/apis/core/install" + utilpointer "k8s.io/utils/pointer" ) func TestGenerateScaleDownRules(t *testing.T) { @@ -229,3 +237,88 @@ func TestGenerateScaleUpRules(t *testing.T) { }) } } + +func TestHorizontalPodAutoscalerAnnotations(t *testing.T) { + tests := []struct { + hpa autoscalingv2.HorizontalPodAutoscaler + test string + }{ + { + hpa: autoscalingv2.HorizontalPodAutoscaler{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + autoscaling.HorizontalPodAutoscalerConditionsAnnotation: "", + autoscaling.MetricSpecsAnnotation: "", + autoscaling.BehaviorSpecsAnnotation: "", + autoscaling.MetricStatusesAnnotation: "", + }, + }, + }, + test: "test empty value for Annotations", + }, + { + hpa: autoscalingv2.HorizontalPodAutoscaler{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + autoscaling.HorizontalPodAutoscalerConditionsAnnotation: "abc", + autoscaling.MetricSpecsAnnotation: "abc", + autoscaling.BehaviorSpecsAnnotation: "abc", + autoscaling.MetricStatusesAnnotation: "abc", + }, + }, + }, + test: "test random value for Annotations", + }, + { + hpa: autoscalingv2.HorizontalPodAutoscaler{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + autoscaling.HorizontalPodAutoscalerConditionsAnnotation: "[]", + autoscaling.MetricSpecsAnnotation: "[]", + autoscaling.BehaviorSpecsAnnotation: "[]", + autoscaling.MetricStatusesAnnotation: "[]", + }, + }, + }, + test: "test empty array value for Annotations", + }, + } + + for _, test := range tests { + hpa := &test.hpa + hpaBeforeMuatate := *hpa.DeepCopy() + obj := roundTrip(t, runtime.Object(hpa)) + final_obj, ok := obj.(*autoscalingv2.HorizontalPodAutoscaler) + if !ok { + t.Fatalf("unexpected object: %v", obj) + } + if !reflect.DeepEqual(*hpa, hpaBeforeMuatate) { + t.Errorf("diff: %v", diff.ObjectDiff(*hpa, hpaBeforeMuatate)) + t.Errorf("expected: %#v\n actual: %#v", *hpa, hpaBeforeMuatate) + } + + if len(final_obj.ObjectMeta.Annotations) != 0 { + t.Fatalf("unexpected annotations: %v", final_obj.ObjectMeta.Annotations) + } + } +} + +func roundTrip(t *testing.T, obj runtime.Object) runtime.Object { + data, err := runtime.Encode(legacyscheme.Codecs.LegacyCodec(SchemeGroupVersion), obj) + if err != nil { + t.Errorf("%v\n %#v", err, obj) + return nil + } + obj2, err := runtime.Decode(legacyscheme.Codecs.UniversalDecoder(), data) + if err != nil { + t.Errorf("%v\nData: %s\nSource: %#v", err, string(data), obj) + return nil + } + obj3 := reflect.New(reflect.TypeOf(obj).Elem()).Interface().(runtime.Object) + err = legacyscheme.Scheme.Convert(obj2, obj3, nil) + if err != nil { + t.Errorf("%v\nSource: %#v", err, obj2) + return nil + } + return obj3 +}