attach_limit_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package util
  14. import (
  15. "crypto/sha1"
  16. "encoding/hex"
  17. "testing"
  18. "k8s.io/api/core/v1"
  19. v1helper "k8s.io/kubernetes/pkg/apis/core/v1/helper"
  20. )
  21. func TestGetCSIAttachLimitKey(t *testing.T) {
  22. // When driverName is less than 39 characters
  23. csiLimitKey := GetCSIAttachLimitKey("com.amazon.ebs")
  24. if csiLimitKey != "attachable-volumes-csi-com.amazon.ebs" {
  25. t.Errorf("Expected com.amazon.ebs got %s", csiLimitKey)
  26. }
  27. // When driver is longer than 39 chars
  28. longDriverName := "com.amazon.kubernetes.eks.ec2.ebs/csi-driver"
  29. csiLimitKeyLonger := GetCSIAttachLimitKey(longDriverName)
  30. if !v1helper.IsAttachableVolumeResourceName(v1.ResourceName(csiLimitKeyLonger)) {
  31. t.Errorf("Expected %s to have attachable prefix", csiLimitKeyLonger)
  32. }
  33. expectedCSIKey := getDriverHash(longDriverName)
  34. if csiLimitKeyLonger != expectedCSIKey {
  35. t.Errorf("Expected limit to be %s got %s", expectedCSIKey, csiLimitKeyLonger)
  36. }
  37. }
  38. func getDriverHash(driverName string) string {
  39. charsFromDriverName := driverName[:23]
  40. hash := sha1.New()
  41. hash.Write([]byte(driverName))
  42. hashed := hex.EncodeToString(hash.Sum(nil))
  43. hashed = hashed[:16]
  44. return CSIAttachLimitPrefix + charsFromDriverName + hashed
  45. }