k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/volume/util/volumepathhandler/volume_path_handler_linux_test.go (about)

     1  /*
     2  Copyright 2023 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 volumepathhandler
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  )
    23  
    24  func pathWithSuffix(suffix string) string {
    25  	return fmt.Sprintf("%s%s", "/var/lib/kubelet/plugins/kubernetes.io/csi/volumeDevices/pvc-1d205234-06cd-4fe4-a7ea-0e8f3e2faf5f/dev/e196ebd3-2ab1-4185-bed4-b997ba38d1dc", suffix)
    26  }
    27  
    28  func TestCleanBackingFilePath(t *testing.T) {
    29  	const defaultPath = "/var/lib/kubelet/plugins/kubernetes.io/csi/volumeDevices/pvc-1d205234-06cd-4fe4-a7ea-0e8f3e2faf5f/dev/e196ebd3-2ab1-4185-bed4-b997ba38d1dc"
    30  	testCases := []struct {
    31  		name          string
    32  		input         string
    33  		expectedOuput string
    34  	}{
    35  		{
    36  			name:          "regular path",
    37  			input:         defaultPath,
    38  			expectedOuput: defaultPath,
    39  		},
    40  		{
    41  			name:          "path is suffixed with whitespaces",
    42  			input:         fmt.Sprintf("%s\r\t\n ", defaultPath),
    43  			expectedOuput: defaultPath,
    44  		},
    45  		{
    46  			name:          "path is suffixed with \"(deleted)\"",
    47  			input:         pathWithSuffix("(deleted)"),
    48  			expectedOuput: defaultPath,
    49  		},
    50  		{
    51  			name:          "path is suffixed with \"(deleted)\" and whitespaces",
    52  			input:         pathWithSuffix(" (deleted)\t"),
    53  			expectedOuput: defaultPath,
    54  		},
    55  	}
    56  
    57  	for _, tc := range testCases {
    58  		t.Run(tc.name, func(t *testing.T) {
    59  			output := cleanBackingFilePath(tc.input)
    60  			if output != tc.expectedOuput {
    61  				t.Fatalf("expected %q, got %q", tc.expectedOuput, output)
    62  			}
    63  		})
    64  	}
    65  }