|
| 1 | +/* |
| 2 | +Copyright 2025 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 protos |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/golang/protobuf/proto" |
| 24 | + "github.com/google/go-cmp/cmp" |
| 25 | + "google.golang.org/protobuf/types/known/timestamppb" |
| 26 | + |
| 27 | + corev1 "k8s.io/api/core/v1" |
| 28 | + apiequality "k8s.io/apimachinery/pkg/api/equality" |
| 29 | + "k8s.io/apimachinery/pkg/api/resource" |
| 30 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 31 | +) |
| 32 | + |
| 33 | +func TestRoundTripPricingPodPriceRequest(t *testing.T) { |
| 34 | + t1 := time.Unix(1000, 100) |
| 35 | + t1meta := metav1.NewTime(t1) |
| 36 | + pod := &corev1.Pod{ |
| 37 | + ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "test"}, |
| 38 | + Spec: corev1.PodSpec{ |
| 39 | + Containers: []corev1.Container{corev1.Container{ |
| 40 | + Name: "test", |
| 41 | + Image: "test", |
| 42 | + Resources: corev1.ResourceRequirements{ |
| 43 | + Requests: corev1.ResourceList{ |
| 44 | + "cpu": resource.MustParse("1"), |
| 45 | + "memory": resource.MustParse("1Gi"), |
| 46 | + }, |
| 47 | + }, |
| 48 | + }}, |
| 49 | + }, |
| 50 | + } |
| 51 | + podBytes, err := pod.Marshal() |
| 52 | + if err != nil { |
| 53 | + t.Fatal(err) |
| 54 | + } |
| 55 | + |
| 56 | + r := &PricingPodPriceRequest{ |
| 57 | + // These three fields are expected to stop being serializable in 1.35, |
| 58 | + // and to need to be removed from the .proto file |
| 59 | + StartTime: &t1meta, |
| 60 | + EndTime: &t1meta, |
| 61 | + Pod: pod, |
| 62 | + |
| 63 | + // These fields should remain serializable |
| 64 | + StartTimestamp: timestamppb.New(t1), |
| 65 | + EndTimestamp: timestamppb.New(t1), |
| 66 | + PodBytes: podBytes, |
| 67 | + } |
| 68 | + data, err := proto.Marshal(r) |
| 69 | + if err != nil { |
| 70 | + t.Fatal(err) |
| 71 | + } |
| 72 | + |
| 73 | + r2 := &PricingPodPriceRequest{} |
| 74 | + if err := proto.Unmarshal(data, r2); err != nil { |
| 75 | + t.Fatal(err) |
| 76 | + } |
| 77 | + |
| 78 | + pod2 := &corev1.Pod{} |
| 79 | + if err := pod2.Unmarshal(r2.PodBytes); err != nil { |
| 80 | + t.Fatal(err) |
| 81 | + } |
| 82 | + if !proto.Equal(r, r2) { |
| 83 | + t.Fatalf("message did not round-trip: %s", cmp.Diff(r, r2)) |
| 84 | + } |
| 85 | + // The Pod field is expected to be removed in 1.35 |
| 86 | + if !apiequality.Semantic.DeepEqual(pod, r2.Pod) { |
| 87 | + t.Fatalf("pod did not round-trip: %s", cmp.Diff(r, r2)) |
| 88 | + } |
| 89 | + // Pod bytes must remain round-trippable |
| 90 | + if !apiequality.Semantic.DeepEqual(pod, pod2) { |
| 91 | + t.Fatalf("pod bytes did not round-trip: %s", cmp.Diff(r, r2)) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func TestRoundTripPricingNodePriceRequest(t *testing.T) { |
| 96 | + t1 := time.Unix(1000, 100) |
| 97 | + t1meta := metav1.NewTime(t1) |
| 98 | + |
| 99 | + r := &PricingNodePriceRequest{ |
| 100 | + // These three fields are expected to stop being serializable in 1.35, |
| 101 | + // and to need to be removed from the .proto file |
| 102 | + StartTime: &t1meta, |
| 103 | + EndTime: &t1meta, |
| 104 | + |
| 105 | + // These fields should remain serializable |
| 106 | + StartTimestamp: timestamppb.New(t1), |
| 107 | + EndTimestamp: timestamppb.New(t1), |
| 108 | + } |
| 109 | + data, err := proto.Marshal(r) |
| 110 | + if err != nil { |
| 111 | + t.Fatal(err) |
| 112 | + } |
| 113 | + |
| 114 | + r2 := &PricingNodePriceRequest{} |
| 115 | + if err := proto.Unmarshal(data, r2); err != nil { |
| 116 | + t.Fatal(err) |
| 117 | + } |
| 118 | + |
| 119 | + if !proto.Equal(r, r2) { |
| 120 | + t.Fatalf("message did not round-trip: %s", cmp.Diff(r, r2)) |
| 121 | + } |
| 122 | +} |
0 commit comments