Skip to content

Commit

Permalink
Add unit test for HashConfigMap
Browse files Browse the repository at this point in the history
Signed-off-by: Balazs Nemeth <[email protected]>
  • Loading branch information
bn222 committed Nov 21, 2023
1 parent 5f0af0e commit 11fe04b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
77 changes: 77 additions & 0 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package utils_test

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

utils "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils"
)

var _ = Describe("HashConfigMap", func() {
It("should hash the ConfigMap correctly", func() {
data := make(map[string]string)
data["key1"] = "value1"
data["key2"] = "value2"
cm := &corev1.ConfigMap{
Data: data,
}

expectedHash := "7cb7a94f45100d7dc8aadffbcd409f25"

actualHash := utils.HashConfigMap(cm)

Expect(actualHash).To(Equal(expectedHash))
})

It("Should not change hash for different resource versions", func() {
data := make(map[string]string)
data["key1"] = "value1"
data["key2"] = "value2"

cm1 := &corev1.ConfigMap{
Data: data,
ObjectMeta: metav1.ObjectMeta{
ResourceVersion: "68790",
},
}

cm2 := &corev1.ConfigMap{
Data: data,
ObjectMeta: metav1.ObjectMeta{
ResourceVersion: "69889",
},
}

hash1 := utils.HashConfigMap(cm1)
hash2 := utils.HashConfigMap(cm2)

Expect(hash1).To(Equal(hash2))
})

It("should not change hash for different key orderings", func() {
data1 := map[string]string{}
data1["key1"] = "value1"
data1["key2"] = "value2"
data2 := map[string]string{}
data2["key1"] = "value1"
data2["key2"] = "value2"
// Collisions in the hashmap _can_ change the order of keys
data2["key2"] = "value2"

cm1 := &corev1.ConfigMap{
Data: data1,
}

cm2 := &corev1.ConfigMap{
Data: data2,
}

hash1 := utils.HashConfigMap(cm1)
hash2 := utils.HashConfigMap(cm2)

Expect(hash1).To(Equal(hash2))
})
})
2 changes: 1 addition & 1 deletion pkg/utils/utils_virtual_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"k8s.io/utils/pointer"
)

func TestUtils(t *testing.T) {
func TestUtilsVirtual(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Utils")
}
Expand Down

0 comments on commit 11fe04b

Please sign in to comment.