Skip to content

Commit 2a62b9e

Browse files
hakuna-matatahliggitt
authored andcommitted
Test dropped round-trip annotations in HPA conversion
1 parent c9b97dc commit 2a62b9e

File tree

6 files changed

+268
-3
lines changed

6 files changed

+268
-3
lines changed

pkg/apis/autoscaling/v1/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,13 @@ go_test(
3030
embed = [":go_default_library"],
3131
deps = [
3232
"//pkg/api/legacyscheme:go_default_library",
33+
"//pkg/apis/autoscaling:go_default_library",
3334
"//pkg/apis/autoscaling/install:go_default_library",
3435
"//pkg/apis/core/install:go_default_library",
3536
"//staging/src/k8s.io/api/autoscaling/v1:go_default_library",
37+
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
3638
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
39+
"//staging/src/k8s.io/apimachinery/pkg/util/diff:go_default_library",
3740
"//vendor/k8s.io/utils/pointer:go_default_library",
3841
],
3942
)

pkg/apis/autoscaling/v1/defaults_test.go

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ import (
2121
"testing"
2222

2323
autoscalingv1 "k8s.io/api/autoscaling/v1"
24-
24+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2525
"k8s.io/apimachinery/pkg/runtime"
26+
"k8s.io/apimachinery/pkg/util/diff"
2627
"k8s.io/kubernetes/pkg/api/legacyscheme"
28+
"k8s.io/kubernetes/pkg/apis/autoscaling"
2729
_ "k8s.io/kubernetes/pkg/apis/autoscaling/install"
2830
. "k8s.io/kubernetes/pkg/apis/autoscaling/v1"
2931
_ "k8s.io/kubernetes/pkg/apis/core/install"
@@ -67,6 +69,68 @@ func TestSetDefaultHPA(t *testing.T) {
6769
}
6870
}
6971

72+
func TestHorizontalPodAutoscalerAnnotations(t *testing.T) {
73+
tests := []struct {
74+
hpa autoscalingv1.HorizontalPodAutoscaler
75+
test string
76+
}{
77+
{
78+
hpa: autoscalingv1.HorizontalPodAutoscaler{
79+
ObjectMeta: metav1.ObjectMeta{
80+
Annotations: map[string]string{
81+
autoscaling.HorizontalPodAutoscalerConditionsAnnotation: "",
82+
autoscaling.MetricSpecsAnnotation: "",
83+
autoscaling.MetricStatusesAnnotation: "",
84+
},
85+
},
86+
},
87+
test: "test empty value for Annotations",
88+
},
89+
{
90+
hpa: autoscalingv1.HorizontalPodAutoscaler{
91+
ObjectMeta: metav1.ObjectMeta{
92+
Annotations: map[string]string{
93+
autoscaling.HorizontalPodAutoscalerConditionsAnnotation: "abc",
94+
autoscaling.MetricSpecsAnnotation: "abc",
95+
autoscaling.MetricStatusesAnnotation: "abc",
96+
},
97+
},
98+
},
99+
test: "test random value for Annotations",
100+
},
101+
{
102+
hpa: autoscalingv1.HorizontalPodAutoscaler{
103+
ObjectMeta: metav1.ObjectMeta{
104+
Annotations: map[string]string{
105+
autoscaling.HorizontalPodAutoscalerConditionsAnnotation: "[]",
106+
autoscaling.MetricSpecsAnnotation: "[]",
107+
autoscaling.MetricStatusesAnnotation: "[]",
108+
},
109+
},
110+
},
111+
test: "test empty array value for Annotations",
112+
},
113+
}
114+
115+
for _, test := range tests {
116+
hpa := &test.hpa
117+
hpaBeforeMuatate := *hpa.DeepCopy()
118+
obj := roundTrip(t, runtime.Object(hpa))
119+
final_obj, ok := obj.(*autoscalingv1.HorizontalPodAutoscaler)
120+
if !ok {
121+
t.Fatalf("unexpected object: %v", obj)
122+
}
123+
if !reflect.DeepEqual(*hpa, hpaBeforeMuatate) {
124+
t.Errorf("diff: %v", diff.ObjectDiff(*hpa, hpaBeforeMuatate))
125+
t.Errorf("expected: %#v\n actual: %#v", *hpa, hpaBeforeMuatate)
126+
}
127+
128+
if len(final_obj.ObjectMeta.Annotations) != 0 {
129+
t.Fatalf("unexpected annotations: %v", final_obj.ObjectMeta.Annotations)
130+
}
131+
}
132+
}
133+
70134
func roundTrip(t *testing.T, obj runtime.Object) runtime.Object {
71135
data, err := runtime.Encode(legacyscheme.Codecs.LegacyCodec(SchemeGroupVersion), obj)
72136
if err != nil {

pkg/apis/autoscaling/v2beta1/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ go_test(
3939
"//staging/src/k8s.io/api/autoscaling/v2beta1:go_default_library",
4040
"//staging/src/k8s.io/api/core/v1:go_default_library",
4141
"//staging/src/k8s.io/apimachinery/pkg/api/equality:go_default_library",
42+
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
4243
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
44+
"//staging/src/k8s.io/apimachinery/pkg/util/diff:go_default_library",
4345
"//vendor/github.com/stretchr/testify/assert:go_default_library",
4446
"//vendor/k8s.io/utils/pointer:go_default_library",
4547
],

pkg/apis/autoscaling/v2beta1/defaults_test.go

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ import (
2020
"reflect"
2121
"testing"
2222

23+
"k8s.io/apimachinery/pkg/util/diff"
24+
2325
autoscalingv2beta1 "k8s.io/api/autoscaling/v2beta1"
24-
"k8s.io/api/core/v1"
26+
v1 "k8s.io/api/core/v1"
2527
apiequality "k8s.io/apimachinery/pkg/api/equality"
28+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2629
"k8s.io/apimachinery/pkg/runtime"
2730
"k8s.io/kubernetes/pkg/api/legacyscheme"
2831
"k8s.io/kubernetes/pkg/apis/autoscaling"
@@ -105,6 +108,68 @@ func TestSetDefaultHPA(t *testing.T) {
105108
}
106109
}
107110

111+
func TestHorizontalPodAutoscalerAnnotations(t *testing.T) {
112+
tests := []struct {
113+
hpa autoscalingv2beta1.HorizontalPodAutoscaler
114+
test string
115+
}{
116+
{
117+
hpa: autoscalingv2beta1.HorizontalPodAutoscaler{
118+
ObjectMeta: metav1.ObjectMeta{
119+
Annotations: map[string]string{
120+
autoscaling.HorizontalPodAutoscalerConditionsAnnotation: "",
121+
autoscaling.MetricSpecsAnnotation: "",
122+
autoscaling.MetricStatusesAnnotation: "",
123+
},
124+
},
125+
},
126+
test: "test empty value for Annotations",
127+
},
128+
{
129+
hpa: autoscalingv2beta1.HorizontalPodAutoscaler{
130+
ObjectMeta: metav1.ObjectMeta{
131+
Annotations: map[string]string{
132+
autoscaling.HorizontalPodAutoscalerConditionsAnnotation: "abc",
133+
autoscaling.MetricSpecsAnnotation: "abc",
134+
autoscaling.MetricStatusesAnnotation: "abc",
135+
},
136+
},
137+
},
138+
test: "test random value for Annotations",
139+
},
140+
{
141+
hpa: autoscalingv2beta1.HorizontalPodAutoscaler{
142+
ObjectMeta: metav1.ObjectMeta{
143+
Annotations: map[string]string{
144+
autoscaling.HorizontalPodAutoscalerConditionsAnnotation: "[]",
145+
autoscaling.MetricSpecsAnnotation: "[]",
146+
autoscaling.MetricStatusesAnnotation: "[]",
147+
},
148+
},
149+
},
150+
test: "test empty array value for Annotations",
151+
},
152+
}
153+
154+
for _, test := range tests {
155+
hpa := &test.hpa
156+
hpaBeforeMuatate := *hpa.DeepCopy()
157+
obj := roundTrip(t, runtime.Object(hpa))
158+
final_obj, ok := obj.(*autoscalingv2beta1.HorizontalPodAutoscaler)
159+
if !ok {
160+
t.Fatalf("unexpected object: %v", obj)
161+
}
162+
if !reflect.DeepEqual(*hpa, hpaBeforeMuatate) {
163+
t.Errorf("diff: %v", diff.ObjectDiff(*hpa, hpaBeforeMuatate))
164+
t.Errorf("expected: %#v\n actual: %#v", *hpa, hpaBeforeMuatate)
165+
}
166+
167+
if len(final_obj.ObjectMeta.Annotations) != 0 {
168+
t.Fatalf("unexpected annotations: %v", final_obj.ObjectMeta.Annotations)
169+
}
170+
}
171+
}
172+
108173
func roundTrip(t *testing.T, obj runtime.Object) runtime.Object {
109174
data, err := runtime.Encode(legacyscheme.Codecs.LegacyCodec(SchemeGroupVersion), obj)
110175
if err != nil {

pkg/apis/autoscaling/v2beta2/BUILD

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("@io_bazel_rules_go//go:def.bzl", "go_library")
1+
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
22

33
go_library(
44
name = "go_default_library",
@@ -38,3 +38,19 @@ filegroup(
3838
tags = ["automanaged"],
3939
visibility = ["//visibility:public"],
4040
)
41+
42+
go_test(
43+
name = "go_default_test",
44+
srcs = ["defaults_test.go"],
45+
embed = [":go_default_library"],
46+
deps = [
47+
"//pkg/api/legacyscheme:go_default_library",
48+
"//pkg/apis/autoscaling:go_default_library",
49+
"//pkg/apis/autoscaling/install:go_default_library",
50+
"//pkg/apis/core/install:go_default_library",
51+
"//staging/src/k8s.io/api/autoscaling/v2beta2:go_default_library",
52+
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
53+
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
54+
"//staging/src/k8s.io/apimachinery/pkg/util/diff:go_default_library",
55+
],
56+
)
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
Copyright 2017 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 v2beta2_test
18+
19+
import (
20+
"reflect"
21+
"testing"
22+
23+
"k8s.io/apimachinery/pkg/runtime"
24+
"k8s.io/apimachinery/pkg/util/diff"
25+
"k8s.io/kubernetes/pkg/api/legacyscheme"
26+
27+
autoscalingv2 "k8s.io/api/autoscaling/v2beta2"
28+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29+
"k8s.io/kubernetes/pkg/apis/autoscaling"
30+
_ "k8s.io/kubernetes/pkg/apis/autoscaling/install"
31+
. "k8s.io/kubernetes/pkg/apis/autoscaling/v2beta2"
32+
_ "k8s.io/kubernetes/pkg/apis/core/install"
33+
)
34+
35+
func TestHorizontalPodAutoscalerAnnotations(t *testing.T) {
36+
tests := []struct {
37+
hpa autoscalingv2.HorizontalPodAutoscaler
38+
test string
39+
}{
40+
{
41+
hpa: autoscalingv2.HorizontalPodAutoscaler{
42+
ObjectMeta: metav1.ObjectMeta{
43+
Annotations: map[string]string{
44+
autoscaling.HorizontalPodAutoscalerConditionsAnnotation: "",
45+
autoscaling.MetricSpecsAnnotation: "",
46+
autoscaling.MetricStatusesAnnotation: "",
47+
},
48+
},
49+
},
50+
test: "test empty value for Annotations",
51+
},
52+
{
53+
hpa: autoscalingv2.HorizontalPodAutoscaler{
54+
ObjectMeta: metav1.ObjectMeta{
55+
Annotations: map[string]string{
56+
autoscaling.HorizontalPodAutoscalerConditionsAnnotation: "abc",
57+
autoscaling.MetricSpecsAnnotation: "abc",
58+
autoscaling.MetricStatusesAnnotation: "abc",
59+
},
60+
},
61+
},
62+
test: "test random value for Annotations",
63+
},
64+
{
65+
hpa: autoscalingv2.HorizontalPodAutoscaler{
66+
ObjectMeta: metav1.ObjectMeta{
67+
Annotations: map[string]string{
68+
autoscaling.HorizontalPodAutoscalerConditionsAnnotation: "[]",
69+
autoscaling.MetricSpecsAnnotation: "[]",
70+
autoscaling.MetricStatusesAnnotation: "[]",
71+
},
72+
},
73+
},
74+
test: "test empty array value for Annotations",
75+
},
76+
}
77+
78+
for _, test := range tests {
79+
hpa := &test.hpa
80+
hpaBeforeMuatate := *hpa.DeepCopy()
81+
obj := roundTrip(t, runtime.Object(hpa))
82+
final_obj, ok := obj.(*autoscalingv2.HorizontalPodAutoscaler)
83+
if !ok {
84+
t.Fatalf("unexpected object: %v", obj)
85+
}
86+
if !reflect.DeepEqual(*hpa, hpaBeforeMuatate) {
87+
t.Errorf("diff: %v", diff.ObjectDiff(*hpa, hpaBeforeMuatate))
88+
t.Errorf("expected: %#v\n actual: %#v", *hpa, hpaBeforeMuatate)
89+
}
90+
91+
if len(final_obj.ObjectMeta.Annotations) != 0 {
92+
t.Fatalf("unexpected annotations: %v", final_obj.ObjectMeta.Annotations)
93+
}
94+
}
95+
}
96+
97+
func roundTrip(t *testing.T, obj runtime.Object) runtime.Object {
98+
data, err := runtime.Encode(legacyscheme.Codecs.LegacyCodec(SchemeGroupVersion), obj)
99+
if err != nil {
100+
t.Errorf("%v\n %#v", err, obj)
101+
return nil
102+
}
103+
obj2, err := runtime.Decode(legacyscheme.Codecs.UniversalDecoder(), data)
104+
if err != nil {
105+
t.Errorf("%v\nData: %s\nSource: %#v", err, string(data), obj)
106+
return nil
107+
}
108+
obj3 := reflect.New(reflect.TypeOf(obj).Elem()).Interface().(runtime.Object)
109+
err = legacyscheme.Scheme.Convert(obj2, obj3, nil)
110+
if err != nil {
111+
t.Errorf("%v\nSource: %#v", err, obj2)
112+
return nil
113+
}
114+
return obj3
115+
}

0 commit comments

Comments
 (0)