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