k8s.io/kubernetes@v1.29.3/pkg/volume/vsphere_volume/vsphere_volume_block_test.go (about)

     1  //go:build !providerless
     2  // +build !providerless
     3  
     4  /*
     5  Copyright 2018 The Kubernetes Authors.
     6  
     7  Licensed under the Apache License, Version 2.0 (the "License");
     8  you may not use this file except in compliance with the License.
     9  You may obtain a copy of the License at
    10  
    11      http://www.apache.org/licenses/LICENSE-2.0
    12  
    13  Unless required by applicable law or agreed to in writing, software
    14  distributed under the License is distributed on an "AS IS" BASIS,
    15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  See the License for the specific language governing permissions and
    17  limitations under the License.
    18  */
    19  
    20  package vsphere_volume
    21  
    22  import (
    23  	"os"
    24  	"path/filepath"
    25  	"testing"
    26  
    27  	v1 "k8s.io/api/core/v1"
    28  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    29  	"k8s.io/apimachinery/pkg/types"
    30  	utiltesting "k8s.io/client-go/util/testing"
    31  	"k8s.io/kubernetes/pkg/volume"
    32  	volumetest "k8s.io/kubernetes/pkg/volume/testing"
    33  )
    34  
    35  var (
    36  	testVolumePath = "volPath1"
    37  	testGlobalPath = "plugins/kubernetes.io/vsphere-volume/volumeDevices/volPath1"
    38  	testPodPath    = "pods/poduid/volumeDevices/kubernetes.io~vsphere-volume"
    39  )
    40  
    41  func TestGetVolumeSpecFromGlobalMapPath(t *testing.T) {
    42  	// make our test path for fake GlobalMapPath
    43  	// /tmp symbolized our pluginDir
    44  	// /tmp/testGlobalPathXXXXX/plugins/kubernetes.io/vsphere-volume/volumeDevices/
    45  	tmpVDir, err := utiltesting.MkTmpdir("vsphereBlockVolume")
    46  	if err != nil {
    47  		t.Fatalf("can't make a temp dir: %s", err)
    48  	}
    49  	// deferred clean up
    50  	defer os.RemoveAll(tmpVDir)
    51  
    52  	expectedGlobalPath := filepath.Join(tmpVDir, testGlobalPath)
    53  
    54  	// Bad Path
    55  	badspec, err := getVolumeSpecFromGlobalMapPath("", "")
    56  	if badspec != nil || err == nil {
    57  		t.Errorf("Expected not to get spec from GlobalMapPath but did")
    58  	}
    59  
    60  	// Good Path
    61  	spec, err := getVolumeSpecFromGlobalMapPath("myVolume", expectedGlobalPath)
    62  	if spec == nil || err != nil {
    63  		t.Fatalf("Failed to get spec from GlobalMapPath: %s", err)
    64  	}
    65  	if spec.PersistentVolume.Name != "myVolume" {
    66  		t.Errorf("Invalid PV name from GlobalMapPath spec: %s", spec.PersistentVolume.Name)
    67  	}
    68  	if spec.PersistentVolume.Spec.VsphereVolume.VolumePath != testVolumePath {
    69  		t.Fatalf("Invalid volumePath from GlobalMapPath spec: %s", spec.PersistentVolume.Spec.VsphereVolume.VolumePath)
    70  	}
    71  	block := v1.PersistentVolumeBlock
    72  	specMode := spec.PersistentVolume.Spec.VolumeMode
    73  	if specMode == nil {
    74  		t.Errorf("Invalid volumeMode from GlobalMapPath spec: %v expected: %v", specMode, block)
    75  	}
    76  	if *specMode != block {
    77  		t.Errorf("Invalid volumeMode from GlobalMapPath spec: %v expected: %v", *specMode, block)
    78  	}
    79  }
    80  
    81  func TestGetPodAndPluginMapPaths(t *testing.T) {
    82  	tmpVDir, err := utiltesting.MkTmpdir("vsphereBlockVolume")
    83  	if err != nil {
    84  		t.Fatalf("can't make a temp dir: %s", err)
    85  	}
    86  	// deferred clean up
    87  	defer os.RemoveAll(tmpVDir)
    88  
    89  	expectedGlobalPath := filepath.Join(tmpVDir, testGlobalPath)
    90  	expectedPodPath := filepath.Join(tmpVDir, testPodPath)
    91  
    92  	spec := getTestVolume(true) // block volume
    93  	pluginMgr := volume.VolumePluginMgr{}
    94  	pluginMgr.InitPlugins(ProbeVolumePlugins(), nil, volumetest.NewFakeVolumeHost(t, tmpVDir, nil, nil))
    95  	plugin, err := pluginMgr.FindMapperPluginByName(vsphereVolumePluginName)
    96  	if err != nil {
    97  		os.RemoveAll(tmpVDir)
    98  		t.Fatalf("Can't find the plugin by name: %q", vsphereVolumePluginName)
    99  	}
   100  	if plugin.GetPluginName() != vsphereVolumePluginName {
   101  		t.Fatalf("Wrong name: %s", plugin.GetPluginName())
   102  	}
   103  	pod := &v1.Pod{
   104  		ObjectMeta: metav1.ObjectMeta{
   105  			UID: types.UID("poduid"),
   106  		},
   107  	}
   108  	mapper, err := plugin.NewBlockVolumeMapper(spec, pod, volume.VolumeOptions{})
   109  	if err != nil {
   110  		t.Fatalf("Failed to make a new Mounter: %v", err)
   111  	}
   112  	if mapper == nil {
   113  		t.Fatalf("Got a nil Mounter")
   114  	}
   115  
   116  	// GetGlobalMapPath
   117  	globalMapPath, err := mapper.GetGlobalMapPath(spec)
   118  	if err != nil || len(globalMapPath) == 0 {
   119  		t.Fatalf("Invalid GlobalMapPath from spec: %s", spec.PersistentVolume.Spec.VsphereVolume.VolumePath)
   120  	}
   121  	if globalMapPath != expectedGlobalPath {
   122  		t.Errorf("Failed to get GlobalMapPath: %s %s", globalMapPath, expectedGlobalPath)
   123  	}
   124  
   125  	// GetPodDeviceMapPath
   126  	devicePath, volumeName := mapper.GetPodDeviceMapPath()
   127  	if devicePath != expectedPodPath {
   128  		t.Errorf("Got unexpected pod path: %s, expected %s", devicePath, expectedPodPath)
   129  	}
   130  	if volumeName != testVolumePath {
   131  		t.Errorf("Got unexpected volNamne: %s, expected %s", volumeName, testVolumePath)
   132  	}
   133  }
   134  
   135  func getTestVolume(isBlock bool) *volume.Spec {
   136  	pv := &v1.PersistentVolume{
   137  		ObjectMeta: metav1.ObjectMeta{
   138  			Name: testVolumePath,
   139  		},
   140  		Spec: v1.PersistentVolumeSpec{
   141  			PersistentVolumeSource: v1.PersistentVolumeSource{
   142  				VsphereVolume: &v1.VsphereVirtualDiskVolumeSource{
   143  					VolumePath: testVolumePath,
   144  				},
   145  			},
   146  		},
   147  	}
   148  	if isBlock {
   149  		blockMode := v1.PersistentVolumeBlock
   150  		pv.Spec.VolumeMode = &blockMode
   151  	}
   152  	return volume.NewSpecFromPersistentVolume(pv, true)
   153  }