...

Source file src/k8s.io/kubernetes/pkg/volume/util/attach_limit_test.go

Documentation: k8s.io/kubernetes/pkg/volume/util

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    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  	// When driverName is less than 39 characters
    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  	// When driver is longer than 39 chars
    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