Skip to content

Commit f4cedfc

Browse files
committed
Add unit test for round-tripping
1 parent 84cc9db commit f4cedfc

File tree

4 files changed

+296
-5
lines changed

4 files changed

+296
-5
lines changed

cluster-autoscaler/cloudprovider/externalgrpc/externalgrpc_cloud_provider_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"google.golang.org/grpc/codes"
2727
"google.golang.org/grpc/status"
2828
"google.golang.org/protobuf/types/known/anypb"
29+
2930
apiv1 "k8s.io/api/core/v1"
3031
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider"
3132
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/externalgrpc/protos"
@@ -309,15 +310,17 @@ func TestCloudProvider_Pricing(t *testing.T) {
309310
// test correct PodPrice call
310311
m.On(
311312
"PricingPodPrice", mock.Anything, mock.MatchedBy(func(req *protos.PricingPodPriceRequest) bool {
312-
return req.Pod.Name == "pod1"
313+
pod := &apiv1.Pod{}
314+
return pod.Unmarshal(req.PodBytes) == nil && pod.Name == "pod1"
313315
}),
314316
).Return(
315317
&protos.PricingPodPriceResponse{Price: 100},
316318
nil,
317319
)
318320
m.On(
319321
"PricingPodPrice", mock.Anything, mock.MatchedBy(func(req *protos.PricingPodPriceRequest) bool {
320-
return req.Pod.Name == "pod2"
322+
pod := &apiv1.Pod{}
323+
return pod.Unmarshal(req.PodBytes) == nil && pod.Name == "pod2"
321324
}),
322325
).Return(
323326
&protos.PricingPodPriceResponse{Price: 200},
@@ -341,7 +344,8 @@ func TestCloudProvider_Pricing(t *testing.T) {
341344
// test grpc error for PodPrice
342345
m.On(
343346
"PricingPodPrice", mock.Anything, mock.MatchedBy(func(req *protos.PricingPodPriceRequest) bool {
344-
return req.Pod.Name == "pod3"
347+
pod := &apiv1.Pod{}
348+
return pod.Unmarshal(req.PodBytes) == nil && pod.Name == "pod3"
345349
}),
346350
).Return(
347351
&protos.PricingPodPriceResponse{},
@@ -357,7 +361,8 @@ func TestCloudProvider_Pricing(t *testing.T) {
357361
// test notImplemented for PodPrice
358362
m.On(
359363
"PricingPodPrice", mock.Anything, mock.MatchedBy(func(req *protos.PricingPodPriceRequest) bool {
360-
return req.Pod.Name == "pod4"
364+
pod := &apiv1.Pod{}
365+
return pod.Unmarshal(req.PodBytes) == nil && pod.Name == "pod4"
361366
}),
362367
).Return(
363368
&protos.PricingPodPriceResponse{},
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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{{
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+
}
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
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+
22+
"github.com/golang/protobuf/proto"
23+
"github.com/google/go-cmp/cmp"
24+
25+
corev1 "k8s.io/api/core/v1"
26+
apiequality "k8s.io/apimachinery/pkg/api/equality"
27+
"k8s.io/apimachinery/pkg/api/resource"
28+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29+
)
30+
31+
func TestRoundTripBestOptionsRequest(t *testing.T) {
32+
pod := &corev1.Pod{
33+
ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "test"},
34+
Spec: corev1.PodSpec{
35+
Containers: []corev1.Container{{
36+
Name: "test",
37+
Image: "test",
38+
Resources: corev1.ResourceRequirements{
39+
Requests: corev1.ResourceList{
40+
"cpu": resource.MustParse("1"),
41+
"memory": resource.MustParse("1Gi"),
42+
},
43+
},
44+
}},
45+
},
46+
}
47+
podBytes, err := pod.Marshal()
48+
if err != nil {
49+
t.Fatal(err)
50+
}
51+
52+
node := &corev1.Node{
53+
ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "test"},
54+
Spec: corev1.NodeSpec{},
55+
Status: corev1.NodeStatus{
56+
Capacity: corev1.ResourceList{
57+
"cpu": resource.MustParse("1"),
58+
"memory": resource.MustParse("1Gi"),
59+
},
60+
},
61+
}
62+
nodeBytes, err := node.Marshal()
63+
if err != nil {
64+
t.Fatal(err)
65+
}
66+
67+
r := &BestOptionsRequest{
68+
Options: []*Option{{
69+
// This field is expected to stop being serializable in 1.35, and to need to be removed from the .proto file
70+
Pod: []*corev1.Pod{pod},
71+
// This field should remain serializable
72+
PodBytes: [][]byte{podBytes},
73+
}},
74+
// This field is expected to stop being serializable in 1.35, and to need to be removed from the .proto file
75+
NodeMap: map[string]*corev1.Node{"node": node},
76+
// This field should remain serializable
77+
NodeBytesMap: map[string][]byte{"node": nodeBytes},
78+
}
79+
data, err := proto.Marshal(r)
80+
if err != nil {
81+
t.Fatal(err)
82+
}
83+
84+
r2 := &BestOptionsRequest{}
85+
if err := proto.Unmarshal(data, r2); err != nil {
86+
t.Fatal(err)
87+
}
88+
89+
pod2 := &corev1.Pod{}
90+
if err := pod2.Unmarshal(r2.Options[0].PodBytes[0]); err != nil {
91+
t.Fatal(err)
92+
}
93+
if !proto.Equal(r, r2) {
94+
t.Fatalf("message did not round-trip: %s", cmp.Diff(r, r2))
95+
}
96+
// The Pod field is expected to be removed in 1.35
97+
if !apiequality.Semantic.DeepEqual(pod, r2.Options[0].Pod[0]) {
98+
t.Fatalf("pod did not round-trip: %s", cmp.Diff(r, r2))
99+
}
100+
// Pod bytes must remain round-trippable
101+
if !apiequality.Semantic.DeepEqual(pod, pod2) {
102+
t.Fatalf("pod bytes did not round-trip: %s", cmp.Diff(r, r2))
103+
}
104+
105+
node2 := &corev1.Node{}
106+
if err := node2.Unmarshal(r2.NodeBytesMap["node"]); err != nil {
107+
t.Fatal(err)
108+
}
109+
if !proto.Equal(r, r2) {
110+
t.Fatalf("message did not round-trip: %s", cmp.Diff(r, r2))
111+
}
112+
// The NodeMap field is expected to be removed in 1.35
113+
if !apiequality.Semantic.DeepEqual(node, r2.NodeMap["node"]) {
114+
t.Fatalf("pod did not round-trip: %s", cmp.Diff(r, r2))
115+
}
116+
// Node bytes must remain round-trippable
117+
if !apiequality.Semantic.DeepEqual(node, node2) {
118+
t.Fatalf("pod bytes did not round-trip: %s", cmp.Diff(r, r2))
119+
}
120+
}
121+
122+
func TestRoundTripBestOptionsResponse(t *testing.T) {
123+
pod := &corev1.Pod{
124+
ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "test"},
125+
Spec: corev1.PodSpec{
126+
Containers: []corev1.Container{{
127+
Name: "test",
128+
Image: "test",
129+
Resources: corev1.ResourceRequirements{
130+
Requests: corev1.ResourceList{
131+
"cpu": resource.MustParse("1"),
132+
"memory": resource.MustParse("1Gi"),
133+
},
134+
},
135+
}},
136+
},
137+
}
138+
podBytes, err := pod.Marshal()
139+
if err != nil {
140+
t.Fatal(err)
141+
}
142+
143+
r := &BestOptionsResponse{
144+
Options: []*Option{{
145+
// This field is expected to stop being serializable in 1.35, and to need to be removed from the .proto file
146+
Pod: []*corev1.Pod{pod},
147+
// This field should remain serializable
148+
PodBytes: [][]byte{podBytes},
149+
}},
150+
}
151+
data, err := proto.Marshal(r)
152+
if err != nil {
153+
t.Fatal(err)
154+
}
155+
156+
r2 := &BestOptionsResponse{}
157+
if err := proto.Unmarshal(data, r2); err != nil {
158+
t.Fatal(err)
159+
}
160+
161+
if !proto.Equal(r, r2) {
162+
t.Fatalf("message did not round-trip: %s", cmp.Diff(r, r2))
163+
}
164+
}

cluster-autoscaler/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ require (
2020
github.com/digitalocean/godo v1.27.0
2121
github.com/gofrs/uuid v4.4.0+incompatible
2222
github.com/golang/mock v1.6.0
23+
github.com/golang/protobuf v1.5.4
2324
github.com/google/go-cmp v0.7.0
2425
github.com/google/go-querystring v1.0.0
2526
github.com/google/uuid v1.6.0
@@ -126,7 +127,6 @@ require (
126127
github.com/golang-jwt/jwt/v4 v4.5.2 // indirect
127128
github.com/golang-jwt/jwt/v5 v5.2.2 // indirect
128129
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
129-
github.com/golang/protobuf v1.5.4 // indirect
130130
github.com/google/btree v1.1.3 // indirect
131131
github.com/google/cadvisor v0.52.1 // indirect
132132
github.com/google/cel-go v0.26.0 // indirect

0 commit comments

Comments
 (0)