Skip to content

Commit 5f087d4

Browse files
Merge pull request #199 from wking/setInt32Ptr-both-nil
lib/resourcemerge/core: Fix setInt32Ptr for "both nil" case
2 parents 40ba3b6 + eace91c commit 5f087d4

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

lib/resourcemerge/core.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,9 @@ func setInt32(modified *bool, existing *int32, required int32) {
473473
}
474474

475475
func setInt32Ptr(modified *bool, existing **int32, required *int32) {
476+
if *existing == nil && required == nil {
477+
return
478+
}
476479
if *existing == nil || (required == nil && *existing != nil) {
477480
*modified = true
478481
*existing = required

lib/resourcemerge/core_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package resourcemerge
2+
3+
import (
4+
"testing"
5+
6+
corev1 "k8s.io/api/core/v1"
7+
"k8s.io/apimachinery/pkg/api/equality"
8+
"k8s.io/utils/pointer"
9+
)
10+
11+
func TestEnsurePodSpec(t *testing.T) {
12+
tests := []struct {
13+
name string
14+
existing corev1.PodSpec
15+
input corev1.PodSpec
16+
17+
expectedModified bool
18+
expected corev1.PodSpec
19+
}{{
20+
name: "empty inputs",
21+
existing: corev1.PodSpec{},
22+
input: corev1.PodSpec{},
23+
24+
expectedModified: false,
25+
expected: corev1.PodSpec{},
26+
}}
27+
28+
for _, test := range tests {
29+
t.Run(test.name, func(t *testing.T) {
30+
modified := pointer.BoolPtr(false)
31+
ensurePodSpec(modified, &test.existing, test.input)
32+
if *modified != test.expectedModified {
33+
t.Errorf("mismatch modified got: %v want: %v", *modified, test.expectedModified)
34+
}
35+
36+
if !equality.Semantic.DeepEqual(test.existing, test.expected) {
37+
t.Errorf("mismatch PodSpec got: %v want: %v", test.existing, test.expected)
38+
}
39+
})
40+
}
41+
}

0 commit comments

Comments
 (0)