csi_mounter_test.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. /*
  2. Copyright 2017 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 csi
  14. import (
  15. "fmt"
  16. "math/rand"
  17. "os"
  18. "path"
  19. "path/filepath"
  20. "testing"
  21. "reflect"
  22. api "k8s.io/api/core/v1"
  23. storage "k8s.io/api/storage/v1"
  24. meta "k8s.io/apimachinery/pkg/apis/meta/v1"
  25. "k8s.io/apimachinery/pkg/types"
  26. utilfeature "k8s.io/apiserver/pkg/util/feature"
  27. fakeclient "k8s.io/client-go/kubernetes/fake"
  28. featuregatetesting "k8s.io/component-base/featuregate/testing"
  29. "k8s.io/klog"
  30. "k8s.io/kubernetes/pkg/features"
  31. "k8s.io/kubernetes/pkg/volume"
  32. "k8s.io/kubernetes/pkg/volume/util"
  33. )
  34. var (
  35. testDriver = "test-driver"
  36. testVol = "vol-123"
  37. testns = "test-ns"
  38. testPod = "test-pod"
  39. testPodUID = types.UID("test-pod")
  40. testAccount = "test-service-account"
  41. )
  42. func TestMounterGetPath(t *testing.T) {
  43. plug, tmpDir := newTestPlugin(t, nil)
  44. defer os.RemoveAll(tmpDir)
  45. // TODO (vladimirvivien) specName with slashes will not work
  46. testCases := []struct {
  47. name string
  48. specVolumeName string
  49. path string
  50. }{
  51. {
  52. name: "simple specName",
  53. specVolumeName: "spec-0",
  54. path: filepath.Join(tmpDir, fmt.Sprintf("pods/%s/volumes/kubernetes.io~csi/%s/%s", testPodUID, "spec-0", "/mount")),
  55. },
  56. {
  57. name: "specName with dots",
  58. specVolumeName: "test.spec.1",
  59. path: filepath.Join(tmpDir, fmt.Sprintf("pods/%s/volumes/kubernetes.io~csi/%s/%s", testPodUID, "test.spec.1", "/mount")),
  60. },
  61. }
  62. for _, tc := range testCases {
  63. t.Logf("test case: %s", tc.name)
  64. registerFakePlugin(testDriver, "endpoint", []string{"1.0.0"}, t)
  65. pv := makeTestPV(tc.specVolumeName, 10, testDriver, testVol)
  66. spec := volume.NewSpecFromPersistentVolume(pv, pv.Spec.PersistentVolumeSource.CSI.ReadOnly)
  67. mounter, err := plug.NewMounter(
  68. spec,
  69. &api.Pod{ObjectMeta: meta.ObjectMeta{UID: testPodUID, Namespace: testns}},
  70. volume.VolumeOptions{},
  71. )
  72. if err != nil {
  73. t.Fatalf("Failed to make a new Mounter: %v", err)
  74. }
  75. csiMounter := mounter.(*csiMountMgr)
  76. path := csiMounter.GetPath()
  77. if tc.path != path {
  78. t.Errorf("expecting path %s, got %s", tc.path, path)
  79. }
  80. }
  81. }
  82. func MounterSetUpTests(t *testing.T, podInfoEnabled bool) {
  83. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CSIDriverRegistry, podInfoEnabled)()
  84. tests := []struct {
  85. name string
  86. driver string
  87. volumeContext map[string]string
  88. expectedVolumeContext map[string]string
  89. }{
  90. {
  91. name: "no pod info",
  92. driver: "no-info",
  93. volumeContext: nil,
  94. expectedVolumeContext: nil,
  95. },
  96. {
  97. name: "no CSIDriver -> no pod info",
  98. driver: "unknown-driver",
  99. volumeContext: nil,
  100. expectedVolumeContext: nil,
  101. },
  102. {
  103. name: "CSIDriver with PodInfoRequiredOnMount=nil -> no pod info",
  104. driver: "nil",
  105. volumeContext: nil,
  106. expectedVolumeContext: nil,
  107. },
  108. {
  109. name: "no pod info -> keep existing volumeContext",
  110. driver: "no-info",
  111. volumeContext: map[string]string{"foo": "bar"},
  112. expectedVolumeContext: map[string]string{"foo": "bar"},
  113. },
  114. {
  115. name: "add pod info",
  116. driver: "info",
  117. volumeContext: nil,
  118. expectedVolumeContext: map[string]string{"csi.storage.k8s.io/pod.uid": "test-pod", "csi.storage.k8s.io/serviceAccount.name": "test-service-account", "csi.storage.k8s.io/pod.name": "test-pod", "csi.storage.k8s.io/pod.namespace": "test-ns"},
  119. },
  120. {
  121. name: "add pod info -> keep existing volumeContext",
  122. driver: "info",
  123. volumeContext: map[string]string{"foo": "bar"},
  124. expectedVolumeContext: map[string]string{"foo": "bar", "csi.storage.k8s.io/pod.uid": "test-pod", "csi.storage.k8s.io/serviceAccount.name": "test-service-account", "csi.storage.k8s.io/pod.name": "test-pod", "csi.storage.k8s.io/pod.namespace": "test-ns"},
  125. },
  126. }
  127. noPodMountInfo := false
  128. currentPodInfoMount := true
  129. for _, test := range tests {
  130. t.Run(test.name, func(t *testing.T) {
  131. klog.Infof("Starting test %s", test.name)
  132. fakeClient := fakeclient.NewSimpleClientset(
  133. getTestCSIDriver("no-info", &noPodMountInfo, nil),
  134. getTestCSIDriver("info", &currentPodInfoMount, nil),
  135. getTestCSIDriver("nil", nil, nil),
  136. )
  137. plug, tmpDir := newTestPlugin(t, fakeClient)
  138. defer os.RemoveAll(tmpDir)
  139. registerFakePlugin(test.driver, "endpoint", []string{"1.0.0"}, t)
  140. pv := makeTestPV("test-pv", 10, test.driver, testVol)
  141. pv.Spec.CSI.VolumeAttributes = test.volumeContext
  142. pv.Spec.MountOptions = []string{"foo=bar", "baz=qux"}
  143. pvName := pv.GetName()
  144. mounter, err := plug.NewMounter(
  145. volume.NewSpecFromPersistentVolume(pv, pv.Spec.PersistentVolumeSource.CSI.ReadOnly),
  146. &api.Pod{
  147. ObjectMeta: meta.ObjectMeta{UID: testPodUID, Namespace: testns, Name: testPod},
  148. Spec: api.PodSpec{
  149. ServiceAccountName: testAccount,
  150. },
  151. },
  152. volume.VolumeOptions{},
  153. )
  154. if err != nil {
  155. t.Fatalf("failed to make a new Mounter: %v", err)
  156. }
  157. if mounter == nil {
  158. t.Fatal("failed to create CSI mounter")
  159. }
  160. csiMounter := mounter.(*csiMountMgr)
  161. csiMounter.csiClient = setupClient(t, true)
  162. attachID := getAttachmentName(csiMounter.volumeID, string(csiMounter.driverName), string(plug.host.GetNodeName()))
  163. attachment := &storage.VolumeAttachment{
  164. ObjectMeta: meta.ObjectMeta{
  165. Name: attachID,
  166. },
  167. Spec: storage.VolumeAttachmentSpec{
  168. NodeName: "test-node",
  169. Attacher: CSIPluginName,
  170. Source: storage.VolumeAttachmentSource{
  171. PersistentVolumeName: &pvName,
  172. },
  173. },
  174. Status: storage.VolumeAttachmentStatus{
  175. Attached: false,
  176. AttachError: nil,
  177. DetachError: nil,
  178. },
  179. }
  180. _, err = csiMounter.k8s.StorageV1().VolumeAttachments().Create(attachment)
  181. if err != nil {
  182. t.Fatalf("failed to setup VolumeAttachment: %v", err)
  183. }
  184. // Mounter.SetUp()
  185. var mounterArgs volume.MounterArgs
  186. fsGroup := int64(2000)
  187. mounterArgs.FsGroup = &fsGroup
  188. if err := csiMounter.SetUp(mounterArgs); err != nil {
  189. t.Fatalf("mounter.Setup failed: %v", err)
  190. }
  191. //Test the default value of file system type is not overridden
  192. if len(csiMounter.spec.PersistentVolume.Spec.CSI.FSType) != 0 {
  193. t.Errorf("default value of file system type was overridden by type %s", csiMounter.spec.PersistentVolume.Spec.CSI.FSType)
  194. }
  195. path := csiMounter.GetPath()
  196. if _, err := os.Stat(path); err != nil {
  197. if os.IsNotExist(err) {
  198. t.Errorf("SetUp() failed, volume path not created: %s", path)
  199. } else {
  200. t.Errorf("SetUp() failed: %v", err)
  201. }
  202. }
  203. // ensure call went all the way
  204. pubs := csiMounter.csiClient.(*fakeCsiDriverClient).nodeClient.GetNodePublishedVolumes()
  205. vol, ok := pubs[csiMounter.volumeID]
  206. if !ok {
  207. t.Error("csi server may not have received NodePublishVolume call")
  208. }
  209. if vol.Path != csiMounter.GetPath() {
  210. t.Errorf("csi server expected path %s, got %s", csiMounter.GetPath(), vol.Path)
  211. }
  212. if !reflect.DeepEqual(vol.MountFlags, pv.Spec.MountOptions) {
  213. t.Errorf("csi server expected mount options %v, got %v", pv.Spec.MountOptions, vol.MountFlags)
  214. }
  215. if podInfoEnabled {
  216. if !reflect.DeepEqual(vol.VolumeContext, test.expectedVolumeContext) {
  217. t.Errorf("csi server expected volumeContext %+v, got %+v", test.expectedVolumeContext, vol.VolumeContext)
  218. }
  219. } else {
  220. // CSIPodInfo feature is disabled, we expect no modifications to volumeContext.
  221. if !reflect.DeepEqual(vol.VolumeContext, test.volumeContext) {
  222. t.Errorf("csi server expected volumeContext %+v, got %+v", test.volumeContext, vol.VolumeContext)
  223. }
  224. }
  225. })
  226. }
  227. }
  228. func TestMounterSetUp(t *testing.T) {
  229. t.Run("WithCSIPodInfo", func(t *testing.T) {
  230. MounterSetUpTests(t, true)
  231. })
  232. t.Run("WithoutCSIPodInfo", func(t *testing.T) {
  233. MounterSetUpTests(t, false)
  234. })
  235. }
  236. func TestMounterSetUpSimple(t *testing.T) {
  237. fakeClient := fakeclient.NewSimpleClientset()
  238. plug, tmpDir := newTestPlugin(t, fakeClient)
  239. defer os.RemoveAll(tmpDir)
  240. testCases := []struct {
  241. name string
  242. podUID types.UID
  243. mode driverMode
  244. fsType string
  245. options []string
  246. spec func(string, []string) *volume.Spec
  247. shouldFail bool
  248. }{
  249. {
  250. name: "setup with vol source",
  251. podUID: types.UID(fmt.Sprintf("%08X", rand.Uint64())),
  252. mode: ephemeralDriverMode,
  253. fsType: "ext4",
  254. shouldFail: true,
  255. spec: func(fsType string, options []string) *volume.Spec {
  256. volSrc := makeTestVol("pv1", testDriver)
  257. volSrc.CSI.FSType = &fsType
  258. return volume.NewSpecFromVolume(volSrc)
  259. },
  260. },
  261. {
  262. name: "setup with persistent source",
  263. podUID: types.UID(fmt.Sprintf("%08X", rand.Uint64())),
  264. mode: persistentDriverMode,
  265. fsType: "zfs",
  266. spec: func(fsType string, options []string) *volume.Spec {
  267. pvSrc := makeTestPV("pv1", 20, testDriver, "vol1")
  268. pvSrc.Spec.CSI.FSType = fsType
  269. pvSrc.Spec.MountOptions = options
  270. return volume.NewSpecFromPersistentVolume(pvSrc, false)
  271. },
  272. },
  273. {
  274. name: "setup with persistent source without unspecified fstype and options",
  275. podUID: types.UID(fmt.Sprintf("%08X", rand.Uint64())),
  276. mode: persistentDriverMode,
  277. spec: func(fsType string, options []string) *volume.Spec {
  278. return volume.NewSpecFromPersistentVolume(makeTestPV("pv1", 20, testDriver, "vol2"), false)
  279. },
  280. },
  281. {
  282. name: "setup with missing spec",
  283. shouldFail: true,
  284. spec: func(fsType string, options []string) *volume.Spec { return nil },
  285. },
  286. }
  287. for _, tc := range testCases {
  288. registerFakePlugin(testDriver, "endpoint", []string{"1.0.0"}, t)
  289. t.Run(tc.name, func(t *testing.T) {
  290. mounter, err := plug.NewMounter(
  291. tc.spec(tc.fsType, tc.options),
  292. &api.Pod{ObjectMeta: meta.ObjectMeta{UID: tc.podUID, Namespace: testns}},
  293. volume.VolumeOptions{},
  294. )
  295. if tc.shouldFail && err != nil {
  296. t.Log(err)
  297. return
  298. }
  299. if !tc.shouldFail && err != nil {
  300. t.Fatal("unexpected error:", err)
  301. }
  302. if mounter == nil {
  303. t.Fatal("failed to create CSI mounter")
  304. }
  305. csiMounter := mounter.(*csiMountMgr)
  306. csiMounter.csiClient = setupClient(t, true)
  307. if csiMounter.driverMode != persistentDriverMode {
  308. t.Fatal("unexpected driver mode: ", csiMounter.driverMode)
  309. }
  310. attachID := getAttachmentName(csiMounter.volumeID, string(csiMounter.driverName), string(plug.host.GetNodeName()))
  311. attachment := makeTestAttachment(attachID, "test-node", csiMounter.spec.Name())
  312. _, err = csiMounter.k8s.StorageV1().VolumeAttachments().Create(attachment)
  313. if err != nil {
  314. t.Fatalf("failed to setup VolumeAttachment: %v", err)
  315. }
  316. // Mounter.SetUp()
  317. if err := csiMounter.SetUp(volume.MounterArgs{}); err != nil {
  318. t.Fatalf("mounter.Setup failed: %v", err)
  319. }
  320. // ensure call went all the way
  321. pubs := csiMounter.csiClient.(*fakeCsiDriverClient).nodeClient.GetNodePublishedVolumes()
  322. vol, ok := pubs[csiMounter.volumeID]
  323. if !ok {
  324. t.Error("csi server may not have received NodePublishVolume call")
  325. }
  326. if vol.VolumeHandle != csiMounter.volumeID {
  327. t.Error("volumeHandle not sent to CSI driver properly")
  328. }
  329. devicePath, err := makeDeviceMountPath(plug, csiMounter.spec)
  330. if err != nil {
  331. t.Fatal(err)
  332. }
  333. if vol.DeviceMountPath != devicePath {
  334. t.Errorf("DeviceMountPath not sent properly to CSI driver: %s, %s", vol.DeviceMountPath, devicePath)
  335. }
  336. if !reflect.DeepEqual(vol.MountFlags, csiMounter.spec.PersistentVolume.Spec.MountOptions) {
  337. t.Errorf("unexpected mount flags passed to driver: %+v", vol.MountFlags)
  338. }
  339. if vol.FSType != tc.fsType {
  340. t.Error("unexpected FSType sent to driver:", vol.FSType)
  341. }
  342. if vol.Path != csiMounter.GetPath() {
  343. t.Error("csi server may not have received NodePublishVolume call")
  344. }
  345. })
  346. }
  347. }
  348. func TestMounterSetUpWithInline(t *testing.T) {
  349. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CSIInlineVolume, true)()
  350. fakeClient := fakeclient.NewSimpleClientset()
  351. plug, tmpDir := newTestPlugin(t, fakeClient)
  352. defer os.RemoveAll(tmpDir)
  353. testCases := []struct {
  354. name string
  355. podUID types.UID
  356. mode driverMode
  357. fsType string
  358. options []string
  359. spec func(string, []string) *volume.Spec
  360. shouldFail bool
  361. }{
  362. {
  363. name: "setup with vol source",
  364. podUID: types.UID(fmt.Sprintf("%08X", rand.Uint64())),
  365. mode: ephemeralDriverMode,
  366. fsType: "ext4",
  367. spec: func(fsType string, options []string) *volume.Spec {
  368. volSrc := makeTestVol("pv1", testDriver)
  369. volSrc.CSI.FSType = &fsType
  370. return volume.NewSpecFromVolume(volSrc)
  371. },
  372. },
  373. {
  374. name: "setup with persistent source",
  375. podUID: types.UID(fmt.Sprintf("%08X", rand.Uint64())),
  376. mode: persistentDriverMode,
  377. fsType: "zfs",
  378. spec: func(fsType string, options []string) *volume.Spec {
  379. pvSrc := makeTestPV("pv1", 20, testDriver, "vol1")
  380. pvSrc.Spec.CSI.FSType = fsType
  381. pvSrc.Spec.MountOptions = options
  382. return volume.NewSpecFromPersistentVolume(pvSrc, false)
  383. },
  384. },
  385. {
  386. name: "setup with persistent source without unspecified fstype and options",
  387. podUID: types.UID(fmt.Sprintf("%08X", rand.Uint64())),
  388. mode: persistentDriverMode,
  389. spec: func(fsType string, options []string) *volume.Spec {
  390. return volume.NewSpecFromPersistentVolume(makeTestPV("pv1", 20, testDriver, "vol2"), false)
  391. },
  392. },
  393. {
  394. name: "setup with missing spec",
  395. shouldFail: true,
  396. spec: func(fsType string, options []string) *volume.Spec { return nil },
  397. },
  398. }
  399. for _, tc := range testCases {
  400. registerFakePlugin(testDriver, "endpoint", []string{"1.0.0"}, t)
  401. t.Run(tc.name, func(t *testing.T) {
  402. mounter, err := plug.NewMounter(
  403. tc.spec(tc.fsType, tc.options),
  404. &api.Pod{ObjectMeta: meta.ObjectMeta{UID: tc.podUID, Namespace: testns}},
  405. volume.VolumeOptions{},
  406. )
  407. if tc.shouldFail && err != nil {
  408. t.Log(err)
  409. return
  410. }
  411. if !tc.shouldFail && err != nil {
  412. t.Fatal("unexpected error:", err)
  413. }
  414. if mounter == nil {
  415. t.Fatal("failed to create CSI mounter")
  416. }
  417. csiMounter := mounter.(*csiMountMgr)
  418. csiMounter.csiClient = setupClient(t, true)
  419. if csiMounter.driverMode != tc.mode {
  420. t.Fatal("unexpected driver mode: ", csiMounter.driverMode)
  421. }
  422. if csiMounter.driverMode == ephemeralDriverMode && csiMounter.volumeID != makeVolumeHandle(string(tc.podUID), csiMounter.specVolumeID) {
  423. t.Fatal("unexpected generated volumeHandle:", csiMounter.volumeID)
  424. }
  425. if csiMounter.driverMode == persistentDriverMode {
  426. attachID := getAttachmentName(csiMounter.volumeID, string(csiMounter.driverName), string(plug.host.GetNodeName()))
  427. attachment := makeTestAttachment(attachID, "test-node", csiMounter.spec.Name())
  428. _, err = csiMounter.k8s.StorageV1().VolumeAttachments().Create(attachment)
  429. if err != nil {
  430. t.Fatalf("failed to setup VolumeAttachment: %v", err)
  431. }
  432. }
  433. // Mounter.SetUp()
  434. if err := csiMounter.SetUp(volume.MounterArgs{}); err != nil {
  435. t.Fatalf("mounter.Setup failed: %v", err)
  436. }
  437. // ensure call went all the way
  438. pubs := csiMounter.csiClient.(*fakeCsiDriverClient).nodeClient.GetNodePublishedVolumes()
  439. vol, ok := pubs[csiMounter.volumeID]
  440. if !ok {
  441. t.Error("csi server may not have received NodePublishVolume call")
  442. }
  443. if vol.VolumeHandle != csiMounter.volumeID {
  444. t.Error("volumeHandle not sent to CSI driver properly")
  445. }
  446. // validate stagingTargetPath
  447. if tc.mode == ephemeralDriverMode && vol.DeviceMountPath != "" {
  448. t.Errorf("unexpected devicePathTarget sent to driver: %s", vol.DeviceMountPath)
  449. }
  450. if tc.mode == persistentDriverMode {
  451. devicePath, err := makeDeviceMountPath(plug, csiMounter.spec)
  452. if err != nil {
  453. t.Fatal(err)
  454. }
  455. if vol.DeviceMountPath != devicePath {
  456. t.Errorf("DeviceMountPath not sent properly to CSI driver: %s, %s", vol.DeviceMountPath, devicePath)
  457. }
  458. if !reflect.DeepEqual(vol.MountFlags, csiMounter.spec.PersistentVolume.Spec.MountOptions) {
  459. t.Errorf("unexpected mount flags passed to driver: %+v", vol.MountFlags)
  460. }
  461. }
  462. if vol.FSType != tc.fsType {
  463. t.Error("unexpected FSType sent to driver:", vol.FSType)
  464. }
  465. if vol.Path != csiMounter.GetPath() {
  466. t.Error("csi server may not have received NodePublishVolume call")
  467. }
  468. })
  469. }
  470. }
  471. func TestMounterSetUpWithFSGroup(t *testing.T) {
  472. fakeClient := fakeclient.NewSimpleClientset()
  473. plug, tmpDir := newTestPlugin(t, fakeClient)
  474. defer os.RemoveAll(tmpDir)
  475. testCases := []struct {
  476. name string
  477. accessModes []api.PersistentVolumeAccessMode
  478. readOnly bool
  479. fsType string
  480. setFsGroup bool
  481. fsGroup int64
  482. }{
  483. {
  484. name: "default fstype, with no fsgroup (should not apply fsgroup)",
  485. accessModes: []api.PersistentVolumeAccessMode{
  486. api.ReadWriteOnce,
  487. },
  488. readOnly: false,
  489. fsType: "",
  490. },
  491. {
  492. name: "default fstype with fsgroup (should not apply fsgroup)",
  493. accessModes: []api.PersistentVolumeAccessMode{
  494. api.ReadWriteOnce,
  495. },
  496. readOnly: false,
  497. fsType: "",
  498. setFsGroup: true,
  499. fsGroup: 3000,
  500. },
  501. {
  502. name: "fstype, fsgroup, RWM, ROM provided (should not apply fsgroup)",
  503. accessModes: []api.PersistentVolumeAccessMode{
  504. api.ReadWriteMany,
  505. api.ReadOnlyMany,
  506. },
  507. fsType: "ext4",
  508. setFsGroup: true,
  509. fsGroup: 3000,
  510. },
  511. {
  512. name: "fstype, fsgroup, RWO, but readOnly (should not apply fsgroup)",
  513. accessModes: []api.PersistentVolumeAccessMode{
  514. api.ReadWriteOnce,
  515. },
  516. readOnly: true,
  517. fsType: "ext4",
  518. setFsGroup: true,
  519. fsGroup: 3000,
  520. },
  521. {
  522. name: "fstype, fsgroup, RWO provided (should apply fsgroup)",
  523. accessModes: []api.PersistentVolumeAccessMode{
  524. api.ReadWriteOnce,
  525. },
  526. fsType: "ext4",
  527. setFsGroup: true,
  528. fsGroup: 3000,
  529. },
  530. }
  531. for i, tc := range testCases {
  532. t.Logf("Running test %s", tc.name)
  533. volName := fmt.Sprintf("test-vol-%d", i)
  534. registerFakePlugin(testDriver, "endpoint", []string{"1.0.0"}, t)
  535. pv := makeTestPV("test-pv", 10, testDriver, volName)
  536. pv.Spec.AccessModes = tc.accessModes
  537. pvName := pv.GetName()
  538. spec := volume.NewSpecFromPersistentVolume(pv, tc.readOnly)
  539. if tc.fsType != "" {
  540. spec.PersistentVolume.Spec.CSI.FSType = tc.fsType
  541. }
  542. mounter, err := plug.NewMounter(
  543. spec,
  544. &api.Pod{ObjectMeta: meta.ObjectMeta{UID: testPodUID, Namespace: testns}},
  545. volume.VolumeOptions{},
  546. )
  547. if err != nil {
  548. t.Fatalf("Failed to make a new Mounter: %v", err)
  549. }
  550. if mounter == nil {
  551. t.Fatal("failed to create CSI mounter")
  552. }
  553. csiMounter := mounter.(*csiMountMgr)
  554. csiMounter.csiClient = setupClient(t, true)
  555. attachID := getAttachmentName(csiMounter.volumeID, string(csiMounter.driverName), string(plug.host.GetNodeName()))
  556. attachment := makeTestAttachment(attachID, "test-node", pvName)
  557. _, err = csiMounter.k8s.StorageV1().VolumeAttachments().Create(attachment)
  558. if err != nil {
  559. t.Errorf("failed to setup VolumeAttachment: %v", err)
  560. continue
  561. }
  562. // Mounter.SetUp()
  563. var mounterArgs volume.MounterArgs
  564. var fsGroupPtr *int64
  565. if tc.setFsGroup {
  566. fsGroup := tc.fsGroup
  567. fsGroupPtr = &fsGroup
  568. }
  569. mounterArgs.FsGroup = fsGroupPtr
  570. if err := csiMounter.SetUp(mounterArgs); err != nil {
  571. t.Fatalf("mounter.Setup failed: %v", err)
  572. }
  573. //Test the default value of file system type is not overridden
  574. if len(csiMounter.spec.PersistentVolume.Spec.CSI.FSType) != len(tc.fsType) {
  575. t.Errorf("file system type was overridden by type %s", csiMounter.spec.PersistentVolume.Spec.CSI.FSType)
  576. }
  577. // ensure call went all the way
  578. pubs := csiMounter.csiClient.(*fakeCsiDriverClient).nodeClient.GetNodePublishedVolumes()
  579. if pubs[csiMounter.volumeID].Path != csiMounter.GetPath() {
  580. t.Error("csi server may not have received NodePublishVolume call")
  581. }
  582. }
  583. }
  584. func TestUnmounterTeardown(t *testing.T) {
  585. plug, tmpDir := newTestPlugin(t, nil)
  586. defer os.RemoveAll(tmpDir)
  587. registerFakePlugin(testDriver, "endpoint", []string{"1.0.0"}, t)
  588. pv := makeTestPV("test-pv", 10, testDriver, testVol)
  589. // save the data file prior to unmount
  590. dir := filepath.Join(getTargetPath(testPodUID, pv.ObjectMeta.Name, plug.host), "/mount")
  591. if err := os.MkdirAll(dir, 0755); err != nil && !os.IsNotExist(err) {
  592. t.Errorf("failed to create dir [%s]: %v", dir, err)
  593. }
  594. // do a fake local mount
  595. diskMounter := util.NewSafeFormatAndMountFromHost(plug.GetPluginName(), plug.host)
  596. if err := diskMounter.FormatAndMount("/fake/device", dir, "testfs", nil); err != nil {
  597. t.Errorf("failed to mount dir [%s]: %v", dir, err)
  598. }
  599. if err := saveVolumeData(
  600. path.Dir(dir),
  601. volDataFileName,
  602. map[string]string{
  603. volDataKey.specVolID: pv.ObjectMeta.Name,
  604. volDataKey.driverName: testDriver,
  605. volDataKey.volHandle: testVol,
  606. },
  607. ); err != nil {
  608. t.Fatalf("failed to save volume data: %v", err)
  609. }
  610. unmounter, err := plug.NewUnmounter(pv.ObjectMeta.Name, testPodUID)
  611. if err != nil {
  612. t.Fatalf("failed to make a new Unmounter: %v", err)
  613. }
  614. csiUnmounter := unmounter.(*csiMountMgr)
  615. csiUnmounter.csiClient = setupClient(t, true)
  616. err = csiUnmounter.TearDownAt(dir)
  617. if err != nil {
  618. t.Fatal(err)
  619. }
  620. // ensure csi client call
  621. pubs := csiUnmounter.csiClient.(*fakeCsiDriverClient).nodeClient.GetNodePublishedVolumes()
  622. if _, ok := pubs[csiUnmounter.volumeID]; ok {
  623. t.Error("csi server may not have received NodeUnpublishVolume call")
  624. }
  625. }