Skip to content

Commit c9f5d61

Browse files
committed
add unit-test for K8sNameHash
On-behalf-of: SAP <[email protected]> Signed-off-by: Simon Bein <[email protected]>
1 parent d1159ba commit c9f5d61

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

pkg/controller/utils_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package controller
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"k8s.io/apimachinery/pkg/util/validation"
8+
)
9+
10+
func TestK8sNameHash(t *testing.T) {
11+
tt := []struct {
12+
input []string
13+
expHash string
14+
}{
15+
{
16+
[]string{"test1"},
17+
"wrckybtbh7enmn4vx2nnbpvpkuarsnvm",
18+
},
19+
{
20+
// check that the same string produces the same hash
21+
[]string{"test1"},
22+
"wrckybtbh7enmn4vx2nnbpvpkuarsnvm",
23+
},
24+
{
25+
[]string{"bla"},
26+
"76tha37scj5hjglta4tvn6b4kmxeh3ic",
27+
},
28+
{
29+
[]string{"some other test", "this is a very, very long string"},
30+
"fkkzqgh27xym6tqbswyql3wy4atsf6pt",
31+
},
32+
}
33+
34+
for _, tc := range tt {
35+
t.Run(fmt.Sprint(tc.input), func(t *testing.T) {
36+
res := K8sNameHash(tc.input...)
37+
38+
if res != tc.expHash {
39+
t.Errorf("exp hash %q, got %q", tc.expHash, res)
40+
}
41+
42+
// ensure the result is a valid DNS1123Subdomain
43+
if errs := validation.IsDNS1123Subdomain(res); errs != nil {
44+
t.Errorf("value %q is invalid: %v", res, errs)
45+
}
46+
47+
})
48+
}
49+
50+
}

0 commit comments

Comments
 (0)