local_test.go 19 KB

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