...
1
16
17 package util
18
19 import (
20 "crypto/sha1"
21 "encoding/hex"
22 "testing"
23
24 "k8s.io/api/core/v1"
25 v1helper "k8s.io/kubernetes/pkg/apis/core/v1/helper"
26 )
27
28 func TestGetCSIAttachLimitKey(t *testing.T) {
29
30 csiLimitKey := GetCSIAttachLimitKey("com.amazon.ebs")
31 if csiLimitKey != "attachable-volumes-csi-com.amazon.ebs" {
32 t.Errorf("Expected com.amazon.ebs got %s", csiLimitKey)
33 }
34
35
36 longDriverName := "com.amazon.kubernetes.eks.ec2.ebs/csi-driver"
37 csiLimitKeyLonger := GetCSIAttachLimitKey(longDriverName)
38 if !v1helper.IsAttachableVolumeResourceName(v1.ResourceName(csiLimitKeyLonger)) {
39 t.Errorf("Expected %s to have attachable prefix", csiLimitKeyLonger)
40 }
41
42 expectedCSIKey := getDriverHash(longDriverName)
43 if csiLimitKeyLonger != expectedCSIKey {
44 t.Errorf("Expected limit to be %s got %s", expectedCSIKey, csiLimitKeyLonger)
45 }
46 }
47
48 func getDriverHash(driverName string) string {
49 charsFromDriverName := driverName[:23]
50 hash := sha1.New()
51 hash.Write([]byte(driverName))
52 hashed := hex.EncodeToString(hash.Sum(nil))
53 hashed = hashed[:16]
54 return CSIAttachLimitPrefix + charsFromDriverName + hashed
55 }
56
View as plain text