local_test.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. // +build linux darwin windows
  2. /*
  3. Copyright 2017 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package local
  15. import (
  16. "fmt"
  17. "os"
  18. "path/filepath"
  19. "reflect"
  20. "runtime"
  21. "testing"
  22. v1 "k8s.io/api/core/v1"
  23. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  24. "k8s.io/apimachinery/pkg/types"
  25. utiltesting "k8s.io/client-go/util/testing"
  26. "k8s.io/kubernetes/pkg/volume"
  27. volumetest "k8s.io/kubernetes/pkg/volume/testing"
  28. "k8s.io/kubernetes/pkg/volume/util/hostutil"
  29. "k8s.io/utils/mount"
  30. )
  31. const (
  32. testPVName = "pvA"
  33. testMountPath = "pods/poduid/volumes/kubernetes.io~local-volume/pvA"
  34. testGlobalPath = "plugins/kubernetes.io~local-volume/volumeDevices/pvA"
  35. testPodPath = "pods/poduid/volumeDevices/kubernetes.io~local-volume"
  36. testBlockFormattingToFSGlobalPath = "plugins/kubernetes.io/local-volume/mounts/pvA"
  37. )
  38. func getPlugin(t *testing.T) (string, volume.VolumePlugin) {
  39. tmpDir, err := utiltesting.MkTmpdir("localVolumeTest")
  40. if err != nil {
  41. t.Fatalf("can't make a temp dir: %v", err)
  42. }
  43. plugMgr := volume.VolumePluginMgr{}
  44. plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, volumetest.NewFakeVolumeHost(t, tmpDir, nil, nil))
  45. plug, err := plugMgr.FindPluginByName(localVolumePluginName)
  46. if err != nil {
  47. os.RemoveAll(tmpDir)
  48. t.Fatalf("Can't find the plugin by name")
  49. }
  50. if plug.GetPluginName() != localVolumePluginName {
  51. t.Errorf("Wrong name: %s", plug.GetPluginName())
  52. }
  53. return tmpDir, plug
  54. }
  55. func getBlockPlugin(t *testing.T) (string, volume.BlockVolumePlugin) {
  56. tmpDir, err := utiltesting.MkTmpdir("localVolumeTest")
  57. if err != nil {
  58. t.Fatalf("can't make a temp dir: %v", err)
  59. }
  60. plugMgr := volume.VolumePluginMgr{}
  61. plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, volumetest.NewFakeVolumeHost(t, tmpDir, nil, nil))
  62. plug, err := plugMgr.FindMapperPluginByName(localVolumePluginName)
  63. if err != nil {
  64. os.RemoveAll(tmpDir)
  65. t.Fatalf("Can't find the plugin by name: %q", localVolumePluginName)
  66. }
  67. if plug.GetPluginName() != localVolumePluginName {
  68. t.Errorf("Wrong name: %s", plug.GetPluginName())
  69. }
  70. return tmpDir, plug
  71. }
  72. func getPersistentPlugin(t *testing.T) (string, volume.PersistentVolumePlugin) {
  73. tmpDir, err := utiltesting.MkTmpdir("localVolumeTest")
  74. if err != nil {
  75. t.Fatalf("can't make a temp dir: %v", err)
  76. }
  77. plugMgr := volume.VolumePluginMgr{}
  78. plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, volumetest.NewFakeVolumeHost(t, tmpDir, nil, nil))
  79. plug, err := plugMgr.FindPersistentPluginByName(localVolumePluginName)
  80. if err != nil {
  81. os.RemoveAll(tmpDir)
  82. t.Fatalf("Can't find the plugin by name")
  83. }
  84. if plug.GetPluginName() != localVolumePluginName {
  85. t.Errorf("Wrong name: %s", plug.GetPluginName())
  86. }
  87. return tmpDir, plug
  88. }
  89. func getDeviceMountablePluginWithBlockPath(t *testing.T, isBlockDevice bool) (string, volume.DeviceMountableVolumePlugin) {
  90. tmpDir, err := utiltesting.MkTmpdir("localVolumeTest")
  91. if err != nil {
  92. t.Fatalf("can't make a temp dir: %v", err)
  93. }
  94. plugMgr := volume.VolumePluginMgr{}
  95. var pathToFSType map[string]hostutil.FileType
  96. if isBlockDevice {
  97. pathToFSType = map[string]hostutil.FileType{
  98. tmpDir: hostutil.FileTypeBlockDev,
  99. }
  100. }
  101. plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, volumetest.NewFakeVolumeHostWithMounterFSType(t, tmpDir, nil, nil, pathToFSType))
  102. plug, err := plugMgr.FindDeviceMountablePluginByName(localVolumePluginName)
  103. if err != nil {
  104. os.RemoveAll(tmpDir)
  105. t.Fatalf("Can't find the plugin by name")
  106. }
  107. if plug.GetPluginName() != localVolumePluginName {
  108. t.Errorf("Wrong name: %s", plug.GetPluginName())
  109. }
  110. return tmpDir, plug
  111. }
  112. func getTestVolume(readOnly bool, path string, isBlock bool, mountOptions []string) *volume.Spec {
  113. pv := &v1.PersistentVolume{
  114. ObjectMeta: metav1.ObjectMeta{
  115. Name: testPVName,
  116. },
  117. Spec: v1.PersistentVolumeSpec{
  118. PersistentVolumeSource: v1.PersistentVolumeSource{
  119. Local: &v1.LocalVolumeSource{
  120. Path: path,
  121. },
  122. },
  123. MountOptions: mountOptions,
  124. },
  125. }
  126. if isBlock {
  127. blockMode := v1.PersistentVolumeBlock
  128. pv.Spec.VolumeMode = &blockMode
  129. }
  130. return volume.NewSpecFromPersistentVolume(pv, readOnly)
  131. }
  132. func TestCanSupport(t *testing.T) {
  133. tmpDir, plug := getPlugin(t)
  134. defer os.RemoveAll(tmpDir)
  135. if !plug.CanSupport(getTestVolume(false, tmpDir, false, nil)) {
  136. t.Errorf("Expected true")
  137. }
  138. }
  139. func TestGetAccessModes(t *testing.T) {
  140. tmpDir, plug := getPersistentPlugin(t)
  141. defer os.RemoveAll(tmpDir)
  142. modes := plug.GetAccessModes()
  143. if !volumetest.ContainsAccessMode(modes, v1.ReadWriteOnce) {
  144. t.Errorf("Expected AccessModeType %q", v1.ReadWriteOnce)
  145. }
  146. if volumetest.ContainsAccessMode(modes, v1.ReadWriteMany) {
  147. t.Errorf("Found AccessModeType %q, expected not", v1.ReadWriteMany)
  148. }
  149. if volumetest.ContainsAccessMode(modes, v1.ReadOnlyMany) {
  150. t.Errorf("Found AccessModeType %q, expected not", v1.ReadOnlyMany)
  151. }
  152. }
  153. func TestGetVolumeName(t *testing.T) {
  154. tmpDir, plug := getPersistentPlugin(t)
  155. defer os.RemoveAll(tmpDir)
  156. volName, err := plug.GetVolumeName(getTestVolume(false, tmpDir, false, nil))
  157. if err != nil {
  158. t.Errorf("Failed to get volume name: %v", err)
  159. }
  160. if volName != testPVName {
  161. t.Errorf("Expected volume name %q, got %q", testPVName, volName)
  162. }
  163. }
  164. func TestInvalidLocalPath(t *testing.T) {
  165. tmpDir, plug := getPlugin(t)
  166. defer os.RemoveAll(tmpDir)
  167. pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: types.UID("poduid")}}
  168. mounter, err := plug.NewMounter(getTestVolume(false, "/no/backsteps/allowed/..", false, nil), pod, volume.VolumeOptions{})
  169. if err != nil {
  170. t.Fatal(err)
  171. }
  172. err = mounter.SetUp(volume.MounterArgs{})
  173. expectedMsg := "invalid path: /no/backsteps/allowed/.. must not contain '..'"
  174. if err.Error() != expectedMsg {
  175. t.Fatalf("expected error `%s` but got `%s`", expectedMsg, err)
  176. }
  177. }
  178. func TestBlockDeviceGlobalPathAndMountDevice(t *testing.T) {
  179. // Block device global mount path testing
  180. tmpBlockDir, plug := getDeviceMountablePluginWithBlockPath(t, true)
  181. defer os.RemoveAll(tmpBlockDir)
  182. dm, err := plug.NewDeviceMounter()
  183. if err != nil {
  184. t.Errorf("Failed to make a new device mounter: %v", err)
  185. }
  186. pvSpec := getTestVolume(false, tmpBlockDir, false, nil)
  187. expectedGlobalPath := filepath.Join(tmpBlockDir, testBlockFormattingToFSGlobalPath)
  188. actualPath, err := dm.GetDeviceMountPath(pvSpec)
  189. if err != nil {
  190. t.Errorf("Failed to get device mount path: %v", err)
  191. }
  192. if expectedGlobalPath != actualPath {
  193. t.Fatalf("Expected device mount global path:%s, got: %s", expectedGlobalPath, actualPath)
  194. }
  195. fmt.Println("expected global path is:", expectedGlobalPath)
  196. err = dm.MountDevice(pvSpec, tmpBlockDir, expectedGlobalPath)
  197. if err != nil {
  198. t.Fatal(err)
  199. }
  200. if _, err := os.Stat(actualPath); err != nil {
  201. if os.IsNotExist(err) {
  202. t.Errorf("DeviceMounter.MountDevice() failed, device mount path not created: %s", actualPath)
  203. } else {
  204. t.Errorf("DeviceMounter.MountDevice() failed: %v", err)
  205. }
  206. }
  207. du, err := plug.NewDeviceUnmounter()
  208. if err != nil {
  209. t.Fatalf("Create device unmounter error: %v", err)
  210. }
  211. err = du.UnmountDevice(actualPath)
  212. if err != nil {
  213. t.Fatalf("Unmount device error: %v", err)
  214. }
  215. }
  216. func TestFSGlobalPathAndMountDevice(t *testing.T) {
  217. // FS global path testing
  218. tmpFSDir, plug := getDeviceMountablePluginWithBlockPath(t, false)
  219. defer os.RemoveAll(tmpFSDir)
  220. dm, err := plug.NewDeviceMounter()
  221. if err != nil {
  222. t.Errorf("Failed to make a new device mounter: %v", err)
  223. }
  224. pvSpec := getTestVolume(false, tmpFSDir, false, nil)
  225. expectedGlobalPath := tmpFSDir
  226. actualPath, err := dm.GetDeviceMountPath(pvSpec)
  227. if err != nil {
  228. t.Errorf("Failed to get device mount path: %v", err)
  229. }
  230. if expectedGlobalPath != actualPath {
  231. t.Fatalf("Expected device mount global path:%s, got: %s", expectedGlobalPath, actualPath)
  232. }
  233. // Actually, we will do nothing if the local path is FS type
  234. err = dm.MountDevice(pvSpec, tmpFSDir, expectedGlobalPath)
  235. if err != nil {
  236. t.Fatal(err)
  237. }
  238. if _, err := os.Stat(expectedGlobalPath); err != nil {
  239. if os.IsNotExist(err) {
  240. t.Errorf("DeviceMounter.MountDevice() failed, device mount path not created: %s", expectedGlobalPath)
  241. } else {
  242. t.Errorf("DeviceMounter.MountDevice() failed: %v", err)
  243. }
  244. }
  245. }
  246. func TestMountUnmount(t *testing.T) {
  247. tmpDir, plug := getPlugin(t)
  248. defer os.RemoveAll(tmpDir)
  249. pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: types.UID("poduid")}}
  250. mounter, err := plug.NewMounter(getTestVolume(false, tmpDir, false, nil), pod, volume.VolumeOptions{})
  251. if err != nil {
  252. t.Errorf("Failed to make a new Mounter: %v", err)
  253. }
  254. if mounter == nil {
  255. t.Fatalf("Got a nil Mounter")
  256. }
  257. volPath := filepath.Join(tmpDir, testMountPath)
  258. path := mounter.GetPath()
  259. if path != volPath {
  260. t.Errorf("Got unexpected path: %s", path)
  261. }
  262. if err := mounter.SetUp(volume.MounterArgs{}); err != nil {
  263. t.Errorf("Expected success, got: %v", err)
  264. }
  265. if runtime.GOOS != "windows" {
  266. // skip this check in windows since the "bind mount" logic is implemented differently in mount_wiondows.go
  267. if _, err := os.Stat(path); err != nil {
  268. if os.IsNotExist(err) {
  269. t.Errorf("SetUp() failed, volume path not created: %s", path)
  270. } else {
  271. t.Errorf("SetUp() failed: %v", err)
  272. }
  273. }
  274. }
  275. unmounter, err := plug.NewUnmounter(testPVName, pod.UID)
  276. if err != nil {
  277. t.Errorf("Failed to make a new Unmounter: %v", err)
  278. }
  279. if unmounter == nil {
  280. t.Fatalf("Got a nil Unmounter")
  281. }
  282. if err := unmounter.TearDown(); err != nil {
  283. t.Errorf("Expected success, got: %v", err)
  284. }
  285. if _, err := os.Stat(path); err == nil {
  286. t.Errorf("TearDown() failed, volume path still exists: %s", path)
  287. } else if !os.IsNotExist(err) {
  288. t.Errorf("TearDown() failed: %v", err)
  289. }
  290. }
  291. // TestMapUnmap tests block map and unmap interfaces.
  292. func TestMapUnmap(t *testing.T) {
  293. tmpDir, plug := getBlockPlugin(t)
  294. defer os.RemoveAll(tmpDir)
  295. pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: types.UID("poduid")}}
  296. volSpec := getTestVolume(false, tmpDir, true /*isBlock*/, nil)
  297. mapper, err := plug.NewBlockVolumeMapper(volSpec, pod, volume.VolumeOptions{})
  298. if err != nil {
  299. t.Errorf("Failed to make a new Mounter: %v", err)
  300. }
  301. if mapper == nil {
  302. t.Fatalf("Got a nil Mounter")
  303. }
  304. expectedGlobalPath := filepath.Join(tmpDir, testGlobalPath)
  305. globalPath, err := mapper.GetGlobalMapPath(volSpec)
  306. if err != nil {
  307. t.Errorf("Failed to get global path: %v", err)
  308. }
  309. if globalPath != expectedGlobalPath {
  310. t.Errorf("Got unexpected path: %s, expected %s", globalPath, expectedGlobalPath)
  311. }
  312. expectedPodPath := filepath.Join(tmpDir, testPodPath)
  313. podPath, volName := mapper.GetPodDeviceMapPath()
  314. if podPath != expectedPodPath {
  315. t.Errorf("Got unexpected pod path: %s, expected %s", podPath, expectedPodPath)
  316. }
  317. if volName != testPVName {
  318. t.Errorf("Got unexpected volNamne: %s, expected %s", volName, testPVName)
  319. }
  320. var devPath string
  321. if customMapper, ok := mapper.(volume.CustomBlockVolumeMapper); ok {
  322. err = customMapper.SetUpDevice()
  323. if err != nil {
  324. t.Errorf("Failed to SetUpDevice, err: %v", err)
  325. }
  326. devPath, err = customMapper.MapPodDevice()
  327. if err != nil {
  328. t.Errorf("Failed to MapPodDevice, err: %v", err)
  329. }
  330. }
  331. if _, err := os.Stat(devPath); err != nil {
  332. if os.IsNotExist(err) {
  333. t.Errorf("SetUpDevice() failed, volume path not created: %s", devPath)
  334. } else {
  335. t.Errorf("SetUpDevice() failed: %v", err)
  336. }
  337. }
  338. unmapper, err := plug.NewBlockVolumeUnmapper(testPVName, pod.UID)
  339. if err != nil {
  340. t.Fatalf("Failed to make a new Unmapper: %v", err)
  341. }
  342. if unmapper == nil {
  343. t.Fatalf("Got a nil Unmapper")
  344. }
  345. if customUnmapper, ok := unmapper.(volume.CustomBlockVolumeUnmapper); ok {
  346. if err := customUnmapper.UnmapPodDevice(); err != nil {
  347. t.Errorf("UnmapPodDevice failed, err: %v", err)
  348. }
  349. if err := customUnmapper.TearDownDevice(globalPath, devPath); err != nil {
  350. t.Errorf("TearDownDevice failed, err: %v", err)
  351. }
  352. }
  353. }
  354. func testFSGroupMount(plug volume.VolumePlugin, pod *v1.Pod, tmpDir string, fsGroup int64) error {
  355. mounter, err := plug.NewMounter(getTestVolume(false, tmpDir, false, nil), pod, volume.VolumeOptions{})
  356. if err != nil {
  357. return err
  358. }
  359. if mounter == nil {
  360. return fmt.Errorf("Got a nil Mounter")
  361. }
  362. volPath := filepath.Join(tmpDir, testMountPath)
  363. path := mounter.GetPath()
  364. if path != volPath {
  365. return fmt.Errorf("Got unexpected path: %s", path)
  366. }
  367. var mounterArgs volume.MounterArgs
  368. mounterArgs.FsGroup = &fsGroup
  369. if err := mounter.SetUp(mounterArgs); err != nil {
  370. return err
  371. }
  372. return nil
  373. }
  374. func TestConstructVolumeSpec(t *testing.T) {
  375. tests := []struct {
  376. name string
  377. mountPoints []mount.MountPoint
  378. expectedPath string
  379. }{
  380. {
  381. name: "filesystem volume with directory source",
  382. mountPoints: []mount.MountPoint{
  383. {
  384. Device: "/mnt/disk/ssd0",
  385. Path: "pods/poduid/volumes/kubernetes.io~local-volume/pvA",
  386. },
  387. },
  388. expectedPath: "",
  389. },
  390. {
  391. name: "filesystem volume with block source",
  392. mountPoints: []mount.MountPoint{
  393. {
  394. Device: "/dev/loop0",
  395. Path: testMountPath,
  396. },
  397. {
  398. Device: "/dev/loop0",
  399. Path: testBlockFormattingToFSGlobalPath,
  400. },
  401. },
  402. expectedPath: "/dev/loop0",
  403. },
  404. }
  405. for _, tt := range tests {
  406. t.Run(tt.name, func(t *testing.T) {
  407. tmpDir, err := utiltesting.MkTmpdir("localVolumeTest")
  408. if err != nil {
  409. t.Fatalf("can't make a temp dir: %v", err)
  410. }
  411. defer os.RemoveAll(tmpDir)
  412. plug := &localVolumePlugin{
  413. host: volumetest.NewFakeVolumeHost(t, tmpDir, nil, nil),
  414. }
  415. mounter := plug.host.GetMounter(plug.GetPluginName())
  416. fakeMountPoints := []mount.MountPoint{}
  417. for _, mp := range tt.mountPoints {
  418. fakeMountPoint := mp
  419. fakeMountPoint.Path = filepath.Join(tmpDir, mp.Path)
  420. fakeMountPoints = append(fakeMountPoints, fakeMountPoint)
  421. }
  422. mounter.(*mount.FakeMounter).MountPoints = fakeMountPoints
  423. volPath := filepath.Join(tmpDir, testMountPath)
  424. spec, err := plug.ConstructVolumeSpec(testPVName, volPath)
  425. if err != nil {
  426. t.Errorf("ConstructVolumeSpec() failed: %v", err)
  427. }
  428. if spec == nil {
  429. t.Fatalf("ConstructVolumeSpec() returned nil")
  430. }
  431. volName := spec.Name()
  432. if volName != testPVName {
  433. t.Errorf("Expected volume name %q, got %q", testPVName, volName)
  434. }
  435. if spec.Volume != nil {
  436. t.Errorf("Volume object returned, expected nil")
  437. }
  438. pv := spec.PersistentVolume
  439. if pv == nil {
  440. t.Fatalf("PersistentVolume object nil")
  441. }
  442. if spec.PersistentVolume.Spec.VolumeMode == nil {
  443. t.Fatalf("Volume mode has not been set.")
  444. }
  445. if *spec.PersistentVolume.Spec.VolumeMode != v1.PersistentVolumeFilesystem {
  446. t.Errorf("Unexpected volume mode %q", *spec.PersistentVolume.Spec.VolumeMode)
  447. }
  448. ls := pv.Spec.PersistentVolumeSource.Local
  449. if ls == nil {
  450. t.Fatalf("LocalVolumeSource object nil")
  451. }
  452. if pv.Spec.PersistentVolumeSource.Local.Path != tt.expectedPath {
  453. t.Fatalf("Unexpected path got %q, expected %q", pv.Spec.PersistentVolumeSource.Local.Path, tt.expectedPath)
  454. }
  455. })
  456. }
  457. }
  458. func TestConstructBlockVolumeSpec(t *testing.T) {
  459. tmpDir, plug := getBlockPlugin(t)
  460. defer os.RemoveAll(tmpDir)
  461. podPath := filepath.Join(tmpDir, testPodPath)
  462. spec, err := plug.ConstructBlockVolumeSpec(types.UID("poduid"), testPVName, podPath)
  463. if err != nil {
  464. t.Errorf("ConstructBlockVolumeSpec() failed: %v", err)
  465. }
  466. if spec == nil {
  467. t.Fatalf("ConstructBlockVolumeSpec() returned nil")
  468. }
  469. volName := spec.Name()
  470. if volName != testPVName {
  471. t.Errorf("Expected volume name %q, got %q", testPVName, volName)
  472. }
  473. if spec.Volume != nil {
  474. t.Errorf("Volume object returned, expected nil")
  475. }
  476. pv := spec.PersistentVolume
  477. if pv == nil {
  478. t.Fatalf("PersistentVolume object nil")
  479. }
  480. if spec.PersistentVolume.Spec.VolumeMode == nil {
  481. t.Fatalf("Volume mode has not been set.")
  482. }
  483. if *spec.PersistentVolume.Spec.VolumeMode != v1.PersistentVolumeBlock {
  484. t.Errorf("Unexpected volume mode %q", *spec.PersistentVolume.Spec.VolumeMode)
  485. }
  486. ls := pv.Spec.PersistentVolumeSource.Local
  487. if ls == nil {
  488. t.Fatalf("LocalVolumeSource object nil")
  489. }
  490. }
  491. func TestMountOptions(t *testing.T) {
  492. tmpDir, plug := getPlugin(t)
  493. defer os.RemoveAll(tmpDir)
  494. pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: types.UID("poduid")}}
  495. mounter, err := plug.NewMounter(getTestVolume(false, tmpDir, false, []string{"test-option"}), pod, volume.VolumeOptions{})
  496. if err != nil {
  497. t.Errorf("Failed to make a new Mounter: %v", err)
  498. }
  499. if mounter == nil {
  500. t.Fatalf("Got a nil Mounter")
  501. }
  502. // Wrap with FakeMounter.
  503. fakeMounter := mount.NewFakeMounter(nil)
  504. mounter.(*localVolumeMounter).mounter = fakeMounter
  505. if err := mounter.SetUp(volume.MounterArgs{}); err != nil {
  506. t.Errorf("Expected success, got: %v", err)
  507. }
  508. mountOptions := fakeMounter.MountPoints[0].Opts
  509. expectedMountOptions := []string{"bind", "test-option"}
  510. if !reflect.DeepEqual(mountOptions, expectedMountOptions) {
  511. t.Errorf("Expected mount options to be %v got %v", expectedMountOptions, mountOptions)
  512. }
  513. }
  514. func TestPersistentClaimReadOnlyFlag(t *testing.T) {
  515. tmpDir, plug := getPlugin(t)
  516. defer os.RemoveAll(tmpDir)
  517. // Read only == true
  518. pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: types.UID("poduid")}}
  519. mounter, err := plug.NewMounter(getTestVolume(true, tmpDir, false, nil), pod, volume.VolumeOptions{})
  520. if err != nil {
  521. t.Errorf("Failed to make a new Mounter: %v", err)
  522. }
  523. if mounter == nil {
  524. t.Fatalf("Got a nil Mounter")
  525. }
  526. if !mounter.GetAttributes().ReadOnly {
  527. t.Errorf("Expected true for mounter.IsReadOnly")
  528. }
  529. // Read only == false
  530. mounter, err = plug.NewMounter(getTestVolume(false, tmpDir, false, nil), pod, volume.VolumeOptions{})
  531. if err != nil {
  532. t.Errorf("Failed to make a new Mounter: %v", err)
  533. }
  534. if mounter == nil {
  535. t.Fatalf("Got a nil Mounter")
  536. }
  537. if mounter.GetAttributes().ReadOnly {
  538. t.Errorf("Expected false for mounter.IsReadOnly")
  539. }
  540. }
  541. func TestUnsupportedPlugins(t *testing.T) {
  542. tmpDir, err := utiltesting.MkTmpdir("localVolumeTest")
  543. if err != nil {
  544. t.Fatalf("can't make a temp dir: %v", err)
  545. }
  546. defer os.RemoveAll(tmpDir)
  547. plugMgr := volume.VolumePluginMgr{}
  548. plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, volumetest.NewFakeVolumeHost(t, tmpDir, nil, nil))
  549. spec := getTestVolume(false, tmpDir, false, nil)
  550. recyclePlug, err := plugMgr.FindRecyclablePluginBySpec(spec)
  551. if err == nil && recyclePlug != nil {
  552. t.Errorf("Recyclable plugin found, expected none")
  553. }
  554. deletePlug, err := plugMgr.FindDeletablePluginByName(localVolumePluginName)
  555. if err == nil && deletePlug != nil {
  556. t.Errorf("Deletable plugin found, expected none")
  557. }
  558. attachPlug, err := plugMgr.FindAttachablePluginByName(localVolumePluginName)
  559. if err == nil && attachPlug != nil {
  560. t.Errorf("Attachable plugin found, expected none")
  561. }
  562. createPlug, err := plugMgr.FindCreatablePluginBySpec(spec)
  563. if err == nil && createPlug != nil {
  564. t.Errorf("Creatable plugin found, expected none")
  565. }
  566. provisionPlug, err := plugMgr.FindProvisionablePluginByName(localVolumePluginName)
  567. if err == nil && provisionPlug != nil {
  568. t.Errorf("Provisionable plugin found, expected none")
  569. }
  570. }
  571. func TestFilterPodMounts(t *testing.T) {
  572. tmpDir, plug := getPlugin(t)
  573. defer os.RemoveAll(tmpDir)
  574. pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: types.UID("poduid")}}
  575. mounter, err := plug.NewMounter(getTestVolume(false, tmpDir, false, nil), pod, volume.VolumeOptions{})
  576. if err != nil {
  577. t.Fatal(err)
  578. }
  579. lvMounter, ok := mounter.(*localVolumeMounter)
  580. if !ok {
  581. t.Fatal("mounter is not localVolumeMounter")
  582. }
  583. host := volumetest.NewFakeVolumeHost(t, tmpDir, nil, nil)
  584. podsDir := host.GetPodsDir()
  585. cases := map[string]struct {
  586. input []string
  587. expected []string
  588. }{
  589. "empty": {
  590. []string{},
  591. []string{},
  592. },
  593. "not-pod-mount": {
  594. []string{"/mnt/outside"},
  595. []string{},
  596. },
  597. "pod-mount": {
  598. []string{filepath.Join(podsDir, "pod-mount")},
  599. []string{filepath.Join(podsDir, "pod-mount")},
  600. },
  601. "not-directory-prefix": {
  602. []string{podsDir + "pod-mount"},
  603. []string{},
  604. },
  605. "mix": {
  606. []string{"/mnt/outside",
  607. filepath.Join(podsDir, "pod-mount"),
  608. "/another/outside",
  609. filepath.Join(podsDir, "pod-mount2")},
  610. []string{filepath.Join(podsDir, "pod-mount"),
  611. filepath.Join(podsDir, "pod-mount2")},
  612. },
  613. }
  614. for name, test := range cases {
  615. output := lvMounter.filterPodMounts(test.input)
  616. if !reflect.DeepEqual(output, test.expected) {
  617. t.Errorf("%v failed: output %+v doesn't equal expected %+v", name, output, test.expected)
  618. }
  619. }
  620. }