-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
95 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package utils | ||
|
||
// SaveAnnotations adds or updates annotations from a2 from a1 and returns merge | ||
func SaveAnnotations(a1 map[string]string, a2 map[string]string) map[string]string { | ||
if a1 == nil { | ||
a1 = make(map[string]string) | ||
} | ||
if a2 == nil { | ||
a2 = make(map[string]string) | ||
} | ||
for k, v := range a2 { | ||
if a1[k] != v { | ||
a1[k] = v | ||
} | ||
} | ||
return a1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package utils | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var a2 = map[string]string{"k8gb.io/primary-geotag": "eu", "k8gb.io/strategy": "failover"} | ||
var a1 = map[string]string{"field.cattle.io/publicEndpoints": "dummy"} | ||
|
||
func TestAddNewAnnotations(t *testing.T) { | ||
// arrange | ||
// act | ||
repaired := SaveAnnotations(a1, a2) | ||
// assert | ||
assert.Equal(t, 3, len(repaired)) | ||
assert.Equal(t, "eu", repaired["k8gb.io/primary-geotag"]) | ||
assert.Equal(t, "dummy", repaired["field.cattle.io/publicEndpoints"]) | ||
} | ||
|
||
func TestAddExistingAnnotations(t *testing.T) { | ||
// arrange | ||
for k, v := range a2 { | ||
a1[k] = v | ||
} | ||
// act | ||
repaired := SaveAnnotations(a1, a2) | ||
// assert | ||
assert.Equal(t, 3, len(repaired)) | ||
assert.Equal(t, "eu", repaired["k8gb.io/primary-geotag"]) | ||
assert.Equal(t, "dummy", repaired["field.cattle.io/publicEndpoints"]) | ||
assert.Equal(t, "failover", repaired["k8gb.io/strategy"]) | ||
} | ||
|
||
func TestUpdateExistingRecords(t *testing.T) { | ||
// arrange | ||
for k, v := range a2 { | ||
a1[k] = v | ||
} | ||
a1["k8gb.io/primary-geotag"] = "us" | ||
// act | ||
repaired := SaveAnnotations(a1, a2) | ||
// assert | ||
assert.Equal(t, 3, len(repaired)) | ||
assert.Equal(t, "eu", repaired["k8gb.io/primary-geotag"]) | ||
assert.Equal(t, "dummy", repaired["field.cattle.io/publicEndpoints"]) | ||
assert.Equal(t, "failover", repaired["k8gb.io/strategy"]) | ||
} | ||
|
||
func TestEqualAnnotationsWithNilA1(t *testing.T) { | ||
// arrange | ||
// act | ||
repaired := SaveAnnotations(nil, a2) | ||
// assert | ||
assert.True(t, assert.ObjectsAreEqual(a2, repaired)) | ||
} | ||
|
||
func TestEqualAnnotationsWithNilA2(t *testing.T) { | ||
// arrange | ||
// act | ||
repaired := SaveAnnotations(a1, nil) | ||
// assert | ||
assert.True(t, assert.ObjectsAreEqual(a1, repaired)) | ||
} | ||
|
||
func TestEqualAnnotationsWithNilInput(t *testing.T) { | ||
// arrange | ||
// act | ||
repaired := SaveAnnotations(nil, nil) | ||
// assert | ||
assert.Equal(t, 0, len(repaired)) | ||
} |