Skip to content

Commit 2d7ffd3

Browse files
authored
Merge pull request #3304 from PiotrLewandowski323/issue-3291
Add unit tests for azure/pointers.go
2 parents a03f5c2 + 7a69b84 commit 2d7ffd3

File tree

5 files changed

+93
-16
lines changed

5 files changed

+93
-16
lines changed

azure/converters/tags.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package converters
1919
import (
2020
"k8s.io/utils/pointer"
2121
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
22+
"sigs.k8s.io/cluster-api-provider-azure/azure"
2223
)
2324

2425
// MapToTags converts a map[string]*string into a infrav1.Tags.
@@ -38,15 +39,5 @@ func MapToTags(src map[string]*string) infrav1.Tags {
3839

3940
// TagsToMap converts infrav1.Tags into a map[string]*string.
4041
func TagsToMap(src infrav1.Tags) map[string]*string {
41-
if src == nil {
42-
return nil
43-
}
44-
45-
tags := make(map[string]*string, len(src))
46-
47-
for k, v := range src {
48-
tags[k] = pointer.String(v)
49-
}
50-
51-
return tags
42+
return azure.StringMapPtr(src)
5243
}

azure/pointers.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,14 @@ func StringSlice(s *[]string) []string {
2727
return nil
2828
}
2929

30-
// StringMapPtr returns a pointer to a given string map, or nil if the map is nil.
31-
func StringMapPtr(m map[string]string) *map[string]*string { //nolint:gocritic
30+
// StringMapPtr converts a map[string]string into a map[string]*string. It returns nil if the map is nil.
31+
func StringMapPtr(m map[string]string) map[string]*string {
32+
if m == nil {
33+
return nil
34+
}
3235
msp := make(map[string]*string, len(m))
3336
for k, v := range m {
3437
msp[k] = pointer.String(v)
3538
}
36-
return &msp
39+
return msp
3740
}

azure/pointers_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
Copyright 2023 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 azure
18+
19+
import (
20+
"testing"
21+
22+
"github.com/google/go-cmp/cmp"
23+
. "github.com/onsi/gomega"
24+
"k8s.io/utils/pointer"
25+
)
26+
27+
func TestStringSlice(t *testing.T) {
28+
cases := []struct {
29+
Name string
30+
Arg *[]string
31+
Expected []string
32+
}{
33+
{
34+
Name: "Should return nil if the pointer is nil",
35+
Arg: nil,
36+
Expected: nil,
37+
},
38+
{
39+
Name: "Should return string slice value for the passed string slice pointer",
40+
Arg: &[]string{"foo", "bar"},
41+
Expected: []string{"foo", "bar"},
42+
},
43+
}
44+
for _, tc := range cases {
45+
t.Run(tc.Name, func(t *testing.T) {
46+
g := NewWithT(t)
47+
actual := StringSlice(tc.Arg)
48+
g.Expect(tc.Expected).To(Equal(actual))
49+
})
50+
}
51+
}
52+
53+
func TestStringMapPtr(t *testing.T) {
54+
cases := []struct {
55+
Name string
56+
Arg map[string]string
57+
Expected map[string]*string
58+
}{
59+
{
60+
Name: "Should return nil if the map is nil",
61+
Arg: nil,
62+
Expected: nil,
63+
},
64+
{
65+
Name: "Should convert to a map[string]*string",
66+
Arg: map[string]string{"foo": "baz", "bar": "qux"},
67+
Expected: map[string]*string{"foo": pointer.String("baz"), "bar": pointer.String("qux")},
68+
},
69+
}
70+
for _, tc := range cases {
71+
t.Run(tc.Name, func(t *testing.T) {
72+
actual := StringMapPtr(tc.Arg)
73+
if !cmp.Equal(tc.Expected, actual) {
74+
t.Errorf("Got difference between expected result and result %v", cmp.Diff(tc.Expected, actual))
75+
}
76+
})
77+
}
78+
}

azure/services/agentpools/spec.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,11 @@ func (s *AgentPoolSpec) Parameters(ctx context.Context, existing interface{}) (p
265265
if s.SKU != "" {
266266
sku = &s.SKU
267267
}
268+
tags := converters.TagsToMap(s.AdditionalTags)
269+
if tags == nil {
270+
// Make sure we send a non-nil, empty map if AdditionalTags are nil as this tells AKS to delete any existing tags.
271+
tags = make(map[string]*string, 0)
272+
}
268273
var vnetSubnetID *string
269274
if s.VnetSubnetID != "" {
270275
vnetSubnetID = &s.VnetSubnetID
@@ -352,7 +357,7 @@ func (s *AgentPoolSpec) Parameters(ctx context.Context, existing interface{}) (p
352357
VnetSubnetID: vnetSubnetID,
353358
EnableNodePublicIP: s.EnableNodePublicIP,
354359
NodePublicIPPrefixID: s.NodePublicIPPrefixID,
355-
Tags: *azure.StringMapPtr(s.AdditionalTags),
360+
Tags: tags,
356361
LinuxOSConfig: linuxOSConfig,
357362
},
358363
}

azure/services/managedclusters/spec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ func (s *ManagedClusterSpec) Parameters(ctx context.Context, existing interface{
347347
Enabled: &item.Enabled,
348348
}
349349
if item.Config != nil {
350-
addonProfile.Config = *azure.StringMapPtr(item.Config)
350+
addonProfile.Config = azure.StringMapPtr(item.Config)
351351
}
352352
managedCluster.AddonProfiles[item.Name] = addonProfile
353353
}

0 commit comments

Comments
 (0)