fc_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. Copyright 2015 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 fc
  14. import (
  15. "fmt"
  16. "os"
  17. "runtime"
  18. "strconv"
  19. "strings"
  20. "testing"
  21. "k8s.io/api/core/v1"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/apimachinery/pkg/types"
  24. "k8s.io/client-go/kubernetes/fake"
  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. func TestCanSupport(t *testing.T) {
  31. tmpDir, err := utiltesting.MkTmpdir("fc_test")
  32. if err != nil {
  33. t.Fatalf("error creating temp dir: %v", err)
  34. }
  35. defer os.RemoveAll(tmpDir)
  36. plugMgr := volume.VolumePluginMgr{}
  37. plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, volumetest.NewFakeVolumeHost(tmpDir, nil, nil))
  38. plug, err := plugMgr.FindPluginByName("kubernetes.io/fc")
  39. if err != nil {
  40. t.Errorf("Can't find the plugin by name")
  41. }
  42. if plug.GetPluginName() != "kubernetes.io/fc" {
  43. t.Errorf("Wrong name: %s", plug.GetPluginName())
  44. }
  45. if plug.CanSupport(&volume.Spec{}) {
  46. t.Errorf("Expected false")
  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{FC: &v1.FCVolumeSource{}}}}) {
  52. t.Errorf("Expected true")
  53. }
  54. if plug.CanSupport(&volume.Spec{PersistentVolume: &v1.PersistentVolume{Spec: v1.PersistentVolumeSpec{}}}) {
  55. t.Errorf("Expected false")
  56. }
  57. if plug.CanSupport(&volume.Spec{PersistentVolume: &v1.PersistentVolume{Spec: v1.PersistentVolumeSpec{PersistentVolumeSource: v1.PersistentVolumeSource{}}}}) {
  58. t.Errorf("Expected false")
  59. }
  60. if !plug.CanSupport(&volume.Spec{PersistentVolume: &v1.PersistentVolume{Spec: v1.PersistentVolumeSpec{PersistentVolumeSource: v1.PersistentVolumeSource{FC: &v1.FCVolumeSource{}}}}}) {
  61. t.Errorf("Expected true")
  62. }
  63. }
  64. func TestGetAccessModes(t *testing.T) {
  65. tmpDir, err := utiltesting.MkTmpdir("fc_test")
  66. if err != nil {
  67. t.Fatalf("error creating temp dir: %v", err)
  68. }
  69. defer os.RemoveAll(tmpDir)
  70. plugMgr := volume.VolumePluginMgr{}
  71. plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, volumetest.NewFakeVolumeHost(tmpDir, nil, nil))
  72. plug, err := plugMgr.FindPersistentPluginByName("kubernetes.io/fc")
  73. if err != nil {
  74. t.Errorf("Can't find the plugin by name")
  75. }
  76. if !volumetest.ContainsAccessMode(plug.GetAccessModes(), v1.ReadWriteOnce) || !volumetest.ContainsAccessMode(plug.GetAccessModes(), v1.ReadOnlyMany) {
  77. t.Errorf("Expected two AccessModeTypes: %s and %s", v1.ReadWriteOnce, v1.ReadOnlyMany)
  78. }
  79. }
  80. type fakeDiskManager struct {
  81. tmpDir string
  82. attachCalled bool
  83. detachCalled bool
  84. }
  85. func newFakeDiskManager() *fakeDiskManager {
  86. return &fakeDiskManager{
  87. tmpDir: utiltesting.MkTmpdirOrDie("fc_test"),
  88. }
  89. }
  90. func (fake *fakeDiskManager) Cleanup() {
  91. os.RemoveAll(fake.tmpDir)
  92. }
  93. func (fake *fakeDiskManager) MakeGlobalPDName(disk fcDisk) string {
  94. return fake.tmpDir
  95. }
  96. func (fake *fakeDiskManager) MakeGlobalVDPDName(disk fcDisk) string {
  97. return fake.tmpDir
  98. }
  99. func (fake *fakeDiskManager) AttachDisk(b fcDiskMounter) (string, error) {
  100. globalPath := b.manager.MakeGlobalPDName(*b.fcDisk)
  101. err := os.MkdirAll(globalPath, 0750)
  102. if err != nil {
  103. return "", err
  104. }
  105. // Simulate the global mount so that the fakeMounter returns the
  106. // expected number of mounts for the attached disk.
  107. b.mounter.Mount(globalPath, globalPath, b.fsType, nil)
  108. fake.attachCalled = true
  109. return "", nil
  110. }
  111. func (fake *fakeDiskManager) DetachDisk(c fcDiskUnmounter, mntPath string) error {
  112. globalPath := c.manager.MakeGlobalPDName(*c.fcDisk)
  113. err := os.RemoveAll(globalPath)
  114. if err != nil {
  115. return err
  116. }
  117. fake.detachCalled = true
  118. return nil
  119. }
  120. func (fake *fakeDiskManager) DetachBlockFCDisk(c fcDiskUnmapper, mapPath, devicePath string) error {
  121. err := os.RemoveAll(mapPath)
  122. if err != nil {
  123. return err
  124. }
  125. fake.detachCalled = true
  126. return nil
  127. }
  128. func doTestPlugin(t *testing.T, spec *volume.Spec) {
  129. tmpDir, err := utiltesting.MkTmpdir("fc_test")
  130. if err != nil {
  131. t.Fatalf("error creating temp dir: %v", err)
  132. }
  133. defer os.RemoveAll(tmpDir)
  134. plugMgr := volume.VolumePluginMgr{}
  135. plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, volumetest.NewFakeVolumeHost(tmpDir, nil, nil))
  136. plug, err := plugMgr.FindPluginByName("kubernetes.io/fc")
  137. if err != nil {
  138. t.Errorf("Can't find the plugin by name")
  139. }
  140. fakeManager := newFakeDiskManager()
  141. defer fakeManager.Cleanup()
  142. fakeMounter := &mount.FakeMounter{}
  143. fakeExec := mount.NewFakeExec(nil)
  144. mounter, err := plug.(*fcPlugin).newMounterInternal(spec, types.UID("poduid"), fakeManager, fakeMounter, fakeExec)
  145. if err != nil {
  146. t.Errorf("Failed to make a new Mounter: %v", err)
  147. }
  148. if mounter == nil {
  149. t.Errorf("Got a nil Mounter: %v", err)
  150. }
  151. path := mounter.GetPath()
  152. expectedPath := fmt.Sprintf("%s/pods/poduid/volumes/kubernetes.io~fc/vol1", tmpDir)
  153. if path != expectedPath {
  154. t.Errorf("Unexpected path, expected %q, got: %q", expectedPath, path)
  155. }
  156. if err := mounter.SetUp(volume.MounterArgs{}); err != nil {
  157. t.Errorf("Expected success, got: %v", err)
  158. }
  159. if _, err := os.Stat(path); err != nil {
  160. if os.IsNotExist(err) {
  161. t.Errorf("SetUp() failed, volume path not created: %s", path)
  162. } else {
  163. t.Errorf("SetUp() failed: %v", err)
  164. }
  165. }
  166. fakeManager2 := newFakeDiskManager()
  167. defer fakeManager2.Cleanup()
  168. unmounter, err := plug.(*fcPlugin).newUnmounterInternal("vol1", types.UID("poduid"), fakeManager2, fakeMounter)
  169. if err != nil {
  170. t.Errorf("Failed to make a new Unmounter: %v", err)
  171. }
  172. if unmounter == nil {
  173. t.Errorf("Got a nil Unmounter: %v", err)
  174. }
  175. if err := unmounter.TearDown(); err != nil {
  176. t.Errorf("Expected success, got: %v", err)
  177. }
  178. if _, err := os.Stat(path); err == nil {
  179. t.Errorf("TearDown() failed, volume path still exists: %s", path)
  180. } else if !os.IsNotExist(err) {
  181. t.Errorf("TearDown() failed: %v", err)
  182. }
  183. }
  184. func doTestPluginNilMounter(t *testing.T, spec *volume.Spec) {
  185. tmpDir, err := utiltesting.MkTmpdir("fc_test")
  186. if err != nil {
  187. t.Fatalf("error creating temp dir: %v", err)
  188. }
  189. defer os.RemoveAll(tmpDir)
  190. plugMgr := volume.VolumePluginMgr{}
  191. plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, volumetest.NewFakeVolumeHost(tmpDir, nil, nil))
  192. plug, err := plugMgr.FindPluginByName("kubernetes.io/fc")
  193. if err != nil {
  194. t.Errorf("Can't find the plugin by name")
  195. }
  196. fakeManager := newFakeDiskManager()
  197. defer fakeManager.Cleanup()
  198. fakeMounter := &mount.FakeMounter{}
  199. fakeExec := mount.NewFakeExec(nil)
  200. mounter, err := plug.(*fcPlugin).newMounterInternal(spec, types.UID("poduid"), fakeManager, fakeMounter, fakeExec)
  201. if err == nil {
  202. t.Errorf("Error failed to make a new Mounter is expected: %v", err)
  203. }
  204. if mounter != nil {
  205. t.Errorf("A nil Mounter is expected: %v", err)
  206. }
  207. }
  208. func TestPluginVolume(t *testing.T) {
  209. lun := int32(0)
  210. vol := &v1.Volume{
  211. Name: "vol1",
  212. VolumeSource: v1.VolumeSource{
  213. FC: &v1.FCVolumeSource{
  214. TargetWWNs: []string{"500a0981891b8dc5"},
  215. FSType: "ext4",
  216. Lun: &lun,
  217. },
  218. },
  219. }
  220. doTestPlugin(t, volume.NewSpecFromVolume(vol))
  221. }
  222. func TestPluginPersistentVolume(t *testing.T) {
  223. lun := int32(0)
  224. fs := v1.PersistentVolumeFilesystem
  225. vol := &v1.PersistentVolume{
  226. ObjectMeta: metav1.ObjectMeta{
  227. Name: "vol1",
  228. },
  229. Spec: v1.PersistentVolumeSpec{
  230. PersistentVolumeSource: v1.PersistentVolumeSource{
  231. FC: &v1.FCVolumeSource{
  232. TargetWWNs: []string{"500a0981891b8dc5"},
  233. FSType: "ext4",
  234. Lun: &lun,
  235. },
  236. },
  237. VolumeMode: &fs,
  238. },
  239. }
  240. doTestPlugin(t, volume.NewSpecFromPersistentVolume(vol, false))
  241. }
  242. func TestPluginVolumeWWIDs(t *testing.T) {
  243. vol := &v1.Volume{
  244. Name: "vol1",
  245. VolumeSource: v1.VolumeSource{
  246. FC: &v1.FCVolumeSource{
  247. WWIDs: []string{"3600508b400105e210000900000490000"},
  248. FSType: "ext4",
  249. },
  250. },
  251. }
  252. doTestPlugin(t, volume.NewSpecFromVolume(vol))
  253. }
  254. func TestPluginPersistentVolumeWWIDs(t *testing.T) {
  255. fs := v1.PersistentVolumeFilesystem
  256. vol := &v1.PersistentVolume{
  257. ObjectMeta: metav1.ObjectMeta{
  258. Name: "vol1",
  259. },
  260. Spec: v1.PersistentVolumeSpec{
  261. PersistentVolumeSource: v1.PersistentVolumeSource{
  262. FC: &v1.FCVolumeSource{
  263. WWIDs: []string{"3600508b400105e21 000900000490000"},
  264. FSType: "ext4",
  265. },
  266. },
  267. VolumeMode: &fs,
  268. },
  269. }
  270. doTestPlugin(t, volume.NewSpecFromPersistentVolume(vol, false))
  271. }
  272. func TestPluginVolumeNoDiskInfo(t *testing.T) {
  273. vol := &v1.Volume{
  274. Name: "vol1",
  275. VolumeSource: v1.VolumeSource{
  276. FC: &v1.FCVolumeSource{
  277. FSType: "ext4",
  278. },
  279. },
  280. }
  281. doTestPluginNilMounter(t, volume.NewSpecFromVolume(vol))
  282. }
  283. func TestPluginPersistentVolumeNoDiskInfo(t *testing.T) {
  284. fs := v1.PersistentVolumeFilesystem
  285. vol := &v1.PersistentVolume{
  286. ObjectMeta: metav1.ObjectMeta{
  287. Name: "vol1",
  288. },
  289. Spec: v1.PersistentVolumeSpec{
  290. PersistentVolumeSource: v1.PersistentVolumeSource{
  291. FC: &v1.FCVolumeSource{
  292. FSType: "ext4",
  293. },
  294. },
  295. VolumeMode: &fs,
  296. },
  297. }
  298. doTestPluginNilMounter(t, volume.NewSpecFromPersistentVolume(vol, false))
  299. }
  300. func TestPersistentClaimReadOnlyFlag(t *testing.T) {
  301. tmpDir, err := utiltesting.MkTmpdir("fc_test")
  302. if err != nil {
  303. t.Fatalf("error creating temp dir: %v", err)
  304. }
  305. defer os.RemoveAll(tmpDir)
  306. lun := int32(0)
  307. fs := v1.PersistentVolumeFilesystem
  308. pv := &v1.PersistentVolume{
  309. ObjectMeta: metav1.ObjectMeta{
  310. Name: "pvA",
  311. },
  312. Spec: v1.PersistentVolumeSpec{
  313. PersistentVolumeSource: v1.PersistentVolumeSource{
  314. FC: &v1.FCVolumeSource{
  315. TargetWWNs: []string{"some_wwn"},
  316. FSType: "ext4",
  317. Lun: &lun,
  318. },
  319. },
  320. ClaimRef: &v1.ObjectReference{
  321. Name: "claimA",
  322. },
  323. VolumeMode: &fs,
  324. },
  325. }
  326. claim := &v1.PersistentVolumeClaim{
  327. ObjectMeta: metav1.ObjectMeta{
  328. Name: "claimA",
  329. Namespace: "nsA",
  330. },
  331. Spec: v1.PersistentVolumeClaimSpec{
  332. VolumeName: "pvA",
  333. VolumeMode: &fs,
  334. },
  335. Status: v1.PersistentVolumeClaimStatus{
  336. Phase: v1.ClaimBound,
  337. },
  338. }
  339. client := fake.NewSimpleClientset(pv, claim)
  340. plugMgr := volume.VolumePluginMgr{}
  341. plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, volumetest.NewFakeVolumeHost(tmpDir, client, nil))
  342. plug, _ := plugMgr.FindPluginByName(fcPluginName)
  343. // readOnly bool is supplied by persistent-claim volume source when its mounter creates other volumes
  344. spec := volume.NewSpecFromPersistentVolume(pv, true)
  345. pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: types.UID("poduid")}}
  346. mounter, _ := plug.NewMounter(spec, pod, volume.VolumeOptions{})
  347. if mounter == nil {
  348. t.Fatalf("Got a nil Mounter")
  349. }
  350. if !mounter.GetAttributes().ReadOnly {
  351. t.Errorf("Expected true for mounter.IsReadOnly")
  352. }
  353. }
  354. func Test_getWwnsLun(t *testing.T) {
  355. num := int32(0)
  356. fc := &v1.FCVolumeSource{
  357. TargetWWNs: []string{"500a0981891b8dc5"},
  358. FSType: "ext4",
  359. Lun: &num,
  360. }
  361. wwn, lun, _, err := getWwnsLunWwids(fc)
  362. // if no wwn and lun, exit
  363. if (len(wwn) == 0 && lun != "0") || err != nil {
  364. t.Errorf("no fc disk found")
  365. }
  366. }
  367. func Test_getWwids(t *testing.T) {
  368. fc := &v1.FCVolumeSource{
  369. FSType: "ext4",
  370. WWIDs: []string{"3600508b400105e210000900000490000"},
  371. }
  372. _, _, wwid, err := getWwnsLunWwids(fc)
  373. // if no wwn and lun, exit
  374. if len(wwid) == 0 || err != nil {
  375. t.Errorf("no fc disk found")
  376. }
  377. }
  378. func Test_getWwnsLunWwidsError(t *testing.T) {
  379. fc := &v1.FCVolumeSource{
  380. FSType: "ext4",
  381. }
  382. wwn, lun, wwid, err := getWwnsLunWwids(fc)
  383. // expected no wwn and lun and wwid
  384. if (len(wwn) != 0 && lun != "" && len(wwid) != 0) || err == nil {
  385. t.Errorf("unexpected fc disk found")
  386. }
  387. }
  388. func Test_ConstructVolumeSpec(t *testing.T) {
  389. if runtime.GOOS == "darwin" {
  390. t.Skipf("Test_ConstructVolumeSpec is not supported on GOOS=%s", runtime.GOOS)
  391. }
  392. fm := &mount.FakeMounter{
  393. MountPoints: []mount.MountPoint{
  394. {Device: "/dev/sdb", Path: "/var/lib/kubelet/pods/some-pod/volumes/kubernetes.io~fc/fc-in-pod1"},
  395. {Device: "/dev/sdb", Path: "/var/lib/kubelet/plugins/kubernetes.io/fc/50060e801049cfd1-lun-0"},
  396. {Device: "/dev/sdc", Path: "/var/lib/kubelet/pods/some-pod/volumes/kubernetes.io~fc/fc-in-pod2"},
  397. {Device: "/dev/sdc", Path: "/var/lib/kubelet/plugins/kubernetes.io/fc/volumeDevices/3600508b400105e210000900000490000"},
  398. },
  399. }
  400. mountPaths := []string{
  401. "/var/lib/kubelet/pods/some-pod/volumes/kubernetes.io~fc/fc-in-pod1",
  402. "/var/lib/kubelet/pods/some-pod/volumes/kubernetes.io~fc/fc-in-pod2",
  403. }
  404. for _, path := range mountPaths {
  405. refs, err := fm.GetMountRefs(path)
  406. if err != nil {
  407. t.Errorf("couldn't get mountrefs. err: %v", err)
  408. }
  409. var globalPDPath string
  410. for _, ref := range refs {
  411. if strings.Contains(ref, "kubernetes.io/fc") {
  412. globalPDPath = ref
  413. break
  414. }
  415. }
  416. if len(globalPDPath) == 0 {
  417. t.Errorf("couldn't fetch mountrefs")
  418. }
  419. arr := strings.Split(globalPDPath, "/")
  420. if len(arr) < 1 {
  421. t.Errorf("failed to retrieve volume plugin information from globalPDPath: %v", globalPDPath)
  422. }
  423. volumeInfo := arr[len(arr)-1]
  424. if strings.Contains(volumeInfo, "-lun-") {
  425. wwnLun := strings.Split(volumeInfo, "-lun-")
  426. if len(wwnLun) < 2 {
  427. t.Errorf("failed to retrieve TargetWWN and Lun. volumeInfo is invalid: %v", volumeInfo)
  428. }
  429. lun, _ := strconv.Atoi(wwnLun[1])
  430. lun32 := int32(lun)
  431. if wwnLun[0] != "50060e801049cfd1" || lun32 != 0 {
  432. t.Errorf("failed to retrieve TargetWWN and Lun")
  433. }
  434. } else {
  435. if volumeInfo != "3600508b400105e210000900000490000" {
  436. t.Errorf("failed to retrieve WWIDs")
  437. }
  438. }
  439. }
  440. }
  441. func Test_ConstructVolumeSpecNoRefs(t *testing.T) {
  442. fm := &mount.FakeMounter{
  443. MountPoints: []mount.MountPoint{
  444. {Device: "/dev/sdd", Path: "/var/lib/kubelet/pods/some-pod/volumes/kubernetes.io~fc/fc-in-pod1"},
  445. },
  446. }
  447. mountPaths := []string{
  448. "/var/lib/kubelet/pods/some-pod/volumes/kubernetes.io~fc/fc-in-pod1",
  449. }
  450. for _, path := range mountPaths {
  451. refs, _ := fm.GetMountRefs(path)
  452. var globalPDPath string
  453. for _, ref := range refs {
  454. if strings.Contains(ref, "kubernetes.io/fc") {
  455. globalPDPath = ref
  456. break
  457. }
  458. }
  459. if len(globalPDPath) != 0 {
  460. t.Errorf("invalid globalPDPath")
  461. }
  462. }
  463. }