Test dropped round-trip annotations in HPA conversion

This commit is contained in:
HARISH KUNA
2020-03-16 19:07:49 -07:00
committed by Jordan Liggitt
parent eb69ac30da
commit 79f3f6e9b1
6 changed files with 245 additions and 5 deletions

View File

@@ -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 {