k8s.io/kubernetes@v1.29.3/pkg/volume/cephfs/cephfs_test.go (about)

     1  /*
     2  Copyright 2015 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 cephfs
    18  
    19  import (
    20  	"os"
    21  	"path/filepath"
    22  	"testing"
    23  
    24  	"k8s.io/mount-utils"
    25  
    26  	v1 "k8s.io/api/core/v1"
    27  	"k8s.io/apimachinery/pkg/types"
    28  	utiltesting "k8s.io/client-go/util/testing"
    29  	"k8s.io/kubernetes/pkg/volume"
    30  	volumetest "k8s.io/kubernetes/pkg/volume/testing"
    31  )
    32  
    33  func TestCanSupport(t *testing.T) {
    34  	tmpDir, err := utiltesting.MkTmpdir("cephTest")
    35  	if err != nil {
    36  		t.Fatalf("can't make a temp dir: %v", err)
    37  	}
    38  	defer os.RemoveAll(tmpDir)
    39  	plugMgr := volume.VolumePluginMgr{}
    40  	plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, volumetest.NewFakeVolumeHost(t, tmpDir, nil, nil))
    41  	plug, err := plugMgr.FindPluginByName("kubernetes.io/cephfs")
    42  	if err != nil {
    43  		t.Fatal("Can't find the plugin by name")
    44  	}
    45  	if plug.GetPluginName() != "kubernetes.io/cephfs" {
    46  		t.Errorf("Wrong name: %s", plug.GetPluginName())
    47  	}
    48  	if plug.CanSupport(&volume.Spec{Volume: &v1.Volume{VolumeSource: v1.VolumeSource{}}}) {
    49  		t.Errorf("Expected false")
    50  	}
    51  	if !plug.CanSupport(&volume.Spec{Volume: &v1.Volume{VolumeSource: v1.VolumeSource{CephFS: &v1.CephFSVolumeSource{}}}}) {
    52  		t.Errorf("Expected true")
    53  	}
    54  }
    55  
    56  func TestPlugin(t *testing.T) {
    57  	tmpDir, err := utiltesting.MkTmpdir("cephTest")
    58  	if err != nil {
    59  		t.Fatalf("can't make a temp dir: %v", err)
    60  	}
    61  	defer os.RemoveAll(tmpDir)
    62  	plugMgr := volume.VolumePluginMgr{}
    63  	plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, volumetest.NewFakeVolumeHost(t, tmpDir, nil, nil))
    64  	plug, err := plugMgr.FindPluginByName("kubernetes.io/cephfs")
    65  	if err != nil {
    66  		t.Errorf("Can't find the plugin by name")
    67  	}
    68  	spec := &v1.Volume{
    69  		Name: "vol1",
    70  		VolumeSource: v1.VolumeSource{
    71  			CephFS: &v1.CephFSVolumeSource{
    72  				Monitors:   []string{"a", "b"},
    73  				User:       "user",
    74  				SecretRef:  nil,
    75  				SecretFile: "/etc/ceph/user.secret",
    76  			},
    77  		},
    78  	}
    79  	mounter, err := plug.(*cephfsPlugin).newMounterInternal(volume.NewSpecFromVolume(spec), types.UID("poduid"), mount.NewFakeMounter(nil), "secrets")
    80  	if err != nil {
    81  		t.Errorf("Failed to make a new Mounter: %v", err)
    82  	}
    83  	if mounter == nil {
    84  		t.Errorf("Got a nil Mounter")
    85  	}
    86  	volumePath := mounter.GetPath()
    87  	volpath := filepath.Join(tmpDir, "pods/poduid/volumes/kubernetes.io~cephfs/vol1")
    88  	if volumePath != volpath {
    89  		t.Errorf("Got unexpected path: %s", volumePath)
    90  	}
    91  	if err := mounter.SetUp(volume.MounterArgs{}); err != nil {
    92  		t.Errorf("Expected success, got: %v", err)
    93  	}
    94  	if _, err := os.Stat(volumePath); err != nil {
    95  		if os.IsNotExist(err) {
    96  			t.Errorf("SetUp() failed, volume path not created: %s", volumePath)
    97  		} else {
    98  			t.Errorf("SetUp() failed: %v", err)
    99  		}
   100  	}
   101  	unmounter, err := plug.(*cephfsPlugin).newUnmounterInternal("vol1", types.UID("poduid"), mount.NewFakeMounter(nil))
   102  	if err != nil {
   103  		t.Errorf("Failed to make a new Unmounter: %v", err)
   104  	}
   105  	if unmounter == nil {
   106  		t.Errorf("Got a nil Unmounter")
   107  	}
   108  	if err := unmounter.TearDown(); err != nil {
   109  		t.Errorf("Expected success, got: %v", err)
   110  	}
   111  	if _, err := os.Stat(volumePath); err == nil {
   112  		t.Errorf("TearDown() failed, volume path still exists: %s", volumePath)
   113  	} else if !os.IsNotExist(err) {
   114  		t.Errorf("TearDown() failed: %v", err)
   115  	}
   116  }
   117  
   118  func TestConstructVolumeSpec(t *testing.T) {
   119  	tmpDir, err := utiltesting.MkTmpdir("cephTest")
   120  	if err != nil {
   121  		t.Fatalf("Can't make a temp dir: %v", err)
   122  	}
   123  	defer os.RemoveAll(tmpDir)
   124  	plugMgr := volume.VolumePluginMgr{}
   125  	plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, volumetest.NewFakeVolumeHost(t, tmpDir, nil, nil))
   126  	plug, err := plugMgr.FindPluginByName("kubernetes.io/cephfs")
   127  	if err != nil {
   128  		t.Errorf("can't find cephfs plugin by name")
   129  	}
   130  
   131  	cephfsVol, err := plug.(*cephfsPlugin).ConstructVolumeSpec("cephfsVolume", "/cephfsVolume/")
   132  	if err != nil {
   133  		t.Errorf("ConstructVolumeSpec() failed: %v", err)
   134  	}
   135  
   136  	if cephfsVol.Spec.Name() != "cephfsVolume" {
   137  		t.Errorf("Get wrong cephfs spec name, got: %s", cephfsVol.Spec.Name())
   138  	}
   139  }
   140  
   141  type testcase struct {
   142  	name      string
   143  	defaultNs string
   144  	spec      *volume.Spec
   145  	// Expected return of the test
   146  	expectedName  string
   147  	expectedNs    string
   148  	expectedError error
   149  }
   150  
   151  func TestGetSecretNameAndNamespaceForPV(t *testing.T) {
   152  	tests := []testcase{
   153  		{
   154  			name:      "persistent volume source",
   155  			defaultNs: "default",
   156  			spec: &volume.Spec{
   157  				PersistentVolume: &v1.PersistentVolume{
   158  					Spec: v1.PersistentVolumeSpec{
   159  						PersistentVolumeSource: v1.PersistentVolumeSource{
   160  							CephFS: &v1.CephFSPersistentVolumeSource{
   161  								Monitors: []string{"a", "b"},
   162  								User:     "user",
   163  								SecretRef: &v1.SecretReference{
   164  									Name:      "name",
   165  									Namespace: "ns",
   166  								},
   167  								SecretFile: "/etc/ceph/user.secret",
   168  							},
   169  						},
   170  					},
   171  				},
   172  			},
   173  			expectedName:  "name",
   174  			expectedNs:    "ns",
   175  			expectedError: nil,
   176  		},
   177  		{
   178  			name:      "persistent volume source without namespace",
   179  			defaultNs: "default",
   180  			spec: &volume.Spec{
   181  				PersistentVolume: &v1.PersistentVolume{
   182  					Spec: v1.PersistentVolumeSpec{
   183  						PersistentVolumeSource: v1.PersistentVolumeSource{
   184  							CephFS: &v1.CephFSPersistentVolumeSource{
   185  								Monitors: []string{"a", "b"},
   186  								User:     "user",
   187  								SecretRef: &v1.SecretReference{
   188  									Name: "name",
   189  								},
   190  								SecretFile: "/etc/ceph/user.secret",
   191  							},
   192  						},
   193  					},
   194  				},
   195  			},
   196  			expectedName:  "name",
   197  			expectedNs:    "default",
   198  			expectedError: nil,
   199  		},
   200  		{
   201  			name:      "pod volume source",
   202  			defaultNs: "default",
   203  			spec: &volume.Spec{
   204  				Volume: &v1.Volume{
   205  					VolumeSource: v1.VolumeSource{
   206  						CephFS: &v1.CephFSVolumeSource{
   207  							Monitors: []string{"a", "b"},
   208  							User:     "user",
   209  							SecretRef: &v1.LocalObjectReference{
   210  								Name: "name",
   211  							},
   212  							SecretFile: "/etc/ceph/user.secret",
   213  						},
   214  					},
   215  				},
   216  			},
   217  			expectedName:  "name",
   218  			expectedNs:    "default",
   219  			expectedError: nil,
   220  		},
   221  	}
   222  	for _, testcase := range tests {
   223  		resultName, resultNs, err := getSecretNameAndNamespace(testcase.spec, testcase.defaultNs)
   224  		if err != testcase.expectedError || resultName != testcase.expectedName || resultNs != testcase.expectedNs {
   225  			t.Errorf("%s failed: expected err=%v ns=%q name=%q, got %v/%q/%q", testcase.name, testcase.expectedError, testcase.expectedNs, testcase.expectedName,
   226  				err, resultNs, resultName)
   227  		}
   228  	}
   229  }
   230  
   231  func TestGetAccessModes(t *testing.T) {
   232  	tmpDir, err := utiltesting.MkTmpdir("cephfs_test")
   233  	if err != nil {
   234  		t.Fatalf("error creating temp dir: %v", err)
   235  	}
   236  	defer os.RemoveAll(tmpDir)
   237  
   238  	plugMgr := volume.VolumePluginMgr{}
   239  	plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, volumetest.NewFakeVolumeHost(t, tmpDir, nil, nil))
   240  
   241  	plug, err := plugMgr.FindPersistentPluginByName("kubernetes.io/cephfs")
   242  	if err != nil {
   243  		t.Fatal("Can't find the plugin by name")
   244  	}
   245  	modes := plug.GetAccessModes()
   246  	for _, v := range modes {
   247  		if !volumetest.ContainsAccessMode(modes, v) {
   248  			t.Errorf("Expected AccessModeTypes: %s", v)
   249  		}
   250  	}
   251  }