From 1cb098760ee668ccefa7801a09f949e463a9f8f5 Mon Sep 17 00:00:00 2001 From: Nolan Brubaker Date: Tue, 21 Aug 2018 15:30:30 -0400 Subject: [PATCH] Fix map merging logic Fixes #777 Signed-off-by: Nolan Brubaker --- pkg/restore/merge_service_account.go | 4 +- pkg/util/collections/map_utils.go | 9 ++++- pkg/util/collections/map_utils_test.go | 56 ++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 3 deletions(-) diff --git a/pkg/restore/merge_service_account.go b/pkg/restore/merge_service_account.go index 641586ff0..01b1f870f 100644 --- a/pkg/restore/merge_service_account.go +++ b/pkg/restore/merge_service_account.go @@ -58,9 +58,9 @@ func mergeServiceAccounts(fromCluster, fromBackup *unstructured.Unstructured) (* desired.ImagePullSecrets = mergeLocalObjectReferenceSlices(desired.ImagePullSecrets, backupSA.ImagePullSecrets) - collections.MergeMaps(desired.Labels, backupSA.Labels) + desired.Labels = collections.MergeMaps(desired.Labels, backupSA.Labels) - collections.MergeMaps(desired.Annotations, backupSA.Annotations) + desired.Annotations = collections.MergeMaps(desired.Annotations, backupSA.Annotations) desiredUnstructured, err := runtime.DefaultUnstructuredConverter.ToUnstructured(desired) if err != nil { diff --git a/pkg/util/collections/map_utils.go b/pkg/util/collections/map_utils.go index f0b8fb934..6ddfc8c3c 100644 --- a/pkg/util/collections/map_utils.go +++ b/pkg/util/collections/map_utils.go @@ -125,11 +125,18 @@ func Exists(root map[string]interface{}, path string) bool { // MergeMaps takes two map[string]string and merges missing keys from the second into the first. // If a key already exists, its value is not overwritten. -func MergeMaps(first, second map[string]string) { +func MergeMaps(first, second map[string]string) map[string]string { + // If the first map passed in is empty, just use all of the second map's data + if first == nil { + first = map[string]string{} + } + for k, v := range second { _, ok := first[k] if !ok { first[k] = v } } + + return first } diff --git a/pkg/util/collections/map_utils_test.go b/pkg/util/collections/map_utils_test.go index e717e9438..e2577a96f 100644 --- a/pkg/util/collections/map_utils_test.go +++ b/pkg/util/collections/map_utils_test.go @@ -18,6 +18,8 @@ package collections import ( "testing" + + "github.com/stretchr/testify/assert" ) func TestGetString(t *testing.T) { @@ -44,3 +46,57 @@ func TestGetString(t *testing.T) { } } } + +func TestMergeMaps(t *testing.T) { + var testCases = []struct { + name string + source map[string]string + destination map[string]string + expected map[string]string + }{ + { + name: "nil destination should result in source being copied", + destination: nil, + source: map[string]string{ + "k1": "v1", + }, + expected: map[string]string{ + "k1": "v1", + }, + }, + { + name: "keys missing from destination should be copied from source", + destination: map[string]string{ + "k2": "v2", + }, + source: map[string]string{ + "k1": "v1", + }, + expected: map[string]string{ + "k1": "v1", + "k2": "v2", + }, + }, + { + name: "matching key should not have value copied from source", + destination: map[string]string{ + "k1": "v1", + }, + source: map[string]string{ + "k1": "v2", + }, + expected: map[string]string{ + "k1": "v1", + }, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + + result := MergeMaps(tc.destination, tc.source) + + assert.Equal(t, tc.expected, result) + }) + } +}