Skip to content

Commit

Permalink
Test dropped round-trip annotations in HPA conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
hakuna-matatah authored and liggitt committed Apr 8, 2020
1 parent d9e69ed commit 13bdbf3
Show file tree
Hide file tree
Showing 6 changed files with 268 additions and 3 deletions.
3 changes: 3 additions & 0 deletions pkg/apis/autoscaling/v1/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)
Expand Down
66 changes: 65 additions & 1 deletion pkg/apis/autoscaling/v1/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -67,6 +69,68 @@ 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.MetricStatusesAnnotation: "",
},
},
},
test: "test empty value for Annotations",
},
{
hpa: autoscalingv1.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
autoscaling.HorizontalPodAutoscalerConditionsAnnotation: "abc",
autoscaling.MetricSpecsAnnotation: "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.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 {
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/autoscaling/v2beta1/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
Expand Down
67 changes: 66 additions & 1 deletion pkg/apis/autoscaling/v2beta1/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -105,6 +108,68 @@ 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.MetricStatusesAnnotation: "",
},
},
},
test: "test empty value for Annotations",
},
{
hpa: autoscalingv2beta1.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
autoscaling.HorizontalPodAutoscalerConditionsAnnotation: "abc",
autoscaling.MetricSpecsAnnotation: "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.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 {
Expand Down
18 changes: 17 additions & 1 deletion pkg/apis/autoscaling/v2beta2/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "go_default_library",
Expand Down Expand Up @@ -38,3 +38,19 @@ filegroup(
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

go_test(
name = "go_default_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",
],
)
115 changes: 115 additions & 0 deletions pkg/apis/autoscaling/v2beta2/defaults_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v2beta2_test

import (
"reflect"
"testing"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/diff"
"k8s.io/kubernetes/pkg/api/legacyscheme"

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"
)

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.MetricStatusesAnnotation: "",
},
},
},
test: "test empty value for Annotations",
},
{
hpa: autoscalingv2.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
autoscaling.HorizontalPodAutoscalerConditionsAnnotation: "abc",
autoscaling.MetricSpecsAnnotation: "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.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
}

0 comments on commit 13bdbf3

Please sign in to comment.