iscsi_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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 iscsi
  14. import (
  15. "fmt"
  16. "os"
  17. "strings"
  18. "testing"
  19. "k8s.io/api/core/v1"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/types"
  22. "k8s.io/client-go/kubernetes/fake"
  23. utiltesting "k8s.io/client-go/util/testing"
  24. "k8s.io/kubernetes/pkg/util/mount"
  25. "k8s.io/kubernetes/pkg/volume"
  26. volumetest "k8s.io/kubernetes/pkg/volume/testing"
  27. )
  28. func TestCanSupport(t *testing.T) {
  29. tmpDir, err := utiltesting.MkTmpdir("iscsi_test")
  30. if err != nil {
  31. t.Fatalf("error creating temp dir: %v", err)
  32. }
  33. defer os.RemoveAll(tmpDir)
  34. plugMgr := volume.VolumePluginMgr{}
  35. plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, volumetest.NewFakeVolumeHost(tmpDir, nil, nil))
  36. plug, err := plugMgr.FindPluginByName("kubernetes.io/iscsi")
  37. if err != nil {
  38. t.Errorf("Can't find the plugin by name")
  39. }
  40. if plug.GetPluginName() != "kubernetes.io/iscsi" {
  41. t.Errorf("Wrong name: %s", plug.GetPluginName())
  42. }
  43. if plug.CanSupport(&volume.Spec{Volume: &v1.Volume{VolumeSource: v1.VolumeSource{}}}) {
  44. t.Errorf("Expected false")
  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{ISCSI: &v1.ISCSIVolumeSource{}}}}) {
  50. t.Errorf("Expected true")
  51. }
  52. if plug.CanSupport(&volume.Spec{PersistentVolume: &v1.PersistentVolume{Spec: v1.PersistentVolumeSpec{}}}) {
  53. t.Errorf("Expected false")
  54. }
  55. if plug.CanSupport(&volume.Spec{PersistentVolume: &v1.PersistentVolume{Spec: v1.PersistentVolumeSpec{PersistentVolumeSource: v1.PersistentVolumeSource{}}}}) {
  56. t.Errorf("Expected false")
  57. }
  58. if !plug.CanSupport(&volume.Spec{PersistentVolume: &v1.PersistentVolume{Spec: v1.PersistentVolumeSpec{PersistentVolumeSource: v1.PersistentVolumeSource{ISCSI: &v1.ISCSIPersistentVolumeSource{}}}}}) {
  59. t.Errorf("Expected true")
  60. }
  61. }
  62. func TestGetAccessModes(t *testing.T) {
  63. tmpDir, err := utiltesting.MkTmpdir("iscsi_test")
  64. if err != nil {
  65. t.Fatalf("error creating temp dir: %v", err)
  66. }
  67. defer os.RemoveAll(tmpDir)
  68. plugMgr := volume.VolumePluginMgr{}
  69. plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, volumetest.NewFakeVolumeHost(tmpDir, nil, nil))
  70. plug, err := plugMgr.FindPersistentPluginByName("kubernetes.io/iscsi")
  71. if err != nil {
  72. t.Errorf("Can't find the plugin by name")
  73. }
  74. if !volumetest.ContainsAccessMode(plug.GetAccessModes(), v1.ReadWriteOnce) || !volumetest.ContainsAccessMode(plug.GetAccessModes(), v1.ReadOnlyMany) {
  75. t.Errorf("Expected two AccessModeTypes: %s and %s", v1.ReadWriteOnce, v1.ReadOnlyMany)
  76. }
  77. }
  78. type fakeDiskManager struct {
  79. tmpDir string
  80. attachCalled bool
  81. detachCalled bool
  82. }
  83. func NewFakeDiskManager() *fakeDiskManager {
  84. return &fakeDiskManager{
  85. tmpDir: utiltesting.MkTmpdirOrDie("iscsi_test"),
  86. }
  87. }
  88. func (fake *fakeDiskManager) Cleanup() {
  89. os.RemoveAll(fake.tmpDir)
  90. }
  91. func (fake *fakeDiskManager) MakeGlobalPDName(disk iscsiDisk) string {
  92. return fake.tmpDir
  93. }
  94. func (fake *fakeDiskManager) MakeGlobalVDPDName(disk iscsiDisk) string {
  95. return fake.tmpDir
  96. }
  97. func (fake *fakeDiskManager) AttachDisk(b iscsiDiskMounter) (string, error) {
  98. globalPath := b.manager.MakeGlobalPDName(*b.iscsiDisk)
  99. err := os.MkdirAll(globalPath, 0750)
  100. if err != nil {
  101. return "", err
  102. }
  103. // Simulate the global mount so that the fakeMounter returns the
  104. // expected number of mounts for the attached disk.
  105. b.mounter.Mount(globalPath, globalPath, b.fsType, nil)
  106. return "/dev/sdb", nil
  107. }
  108. func (fake *fakeDiskManager) DetachDisk(c iscsiDiskUnmounter, mntPath string) error {
  109. globalPath := c.manager.MakeGlobalPDName(*c.iscsiDisk)
  110. err := os.RemoveAll(globalPath)
  111. if err != nil {
  112. return err
  113. }
  114. return nil
  115. }
  116. func (fake *fakeDiskManager) DetachBlockISCSIDisk(c iscsiDiskUnmapper, mntPath string) error {
  117. globalPath := c.manager.MakeGlobalVDPDName(*c.iscsiDisk)
  118. err := os.RemoveAll(globalPath)
  119. if err != nil {
  120. return err
  121. }
  122. return nil
  123. }
  124. func doTestPlugin(t *testing.T, spec *volume.Spec) {
  125. tmpDir, err := utiltesting.MkTmpdir("iscsi_test")
  126. if err != nil {
  127. t.Fatalf("error creating temp dir: %v", err)
  128. }
  129. defer os.RemoveAll(tmpDir)
  130. plugMgr := volume.VolumePluginMgr{}
  131. plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, volumetest.NewFakeVolumeHost(tmpDir, nil, nil))
  132. plug, err := plugMgr.FindPluginByName("kubernetes.io/iscsi")
  133. if err != nil {
  134. t.Errorf("Can't find the plugin by name")
  135. }
  136. fakeManager := NewFakeDiskManager()
  137. defer fakeManager.Cleanup()
  138. fakeMounter := &mount.FakeMounter{}
  139. fakeExec := mount.NewFakeExec(nil)
  140. mounter, err := plug.(*iscsiPlugin).newMounterInternal(spec, types.UID("poduid"), fakeManager, fakeMounter, fakeExec, nil)
  141. if err != nil {
  142. t.Errorf("Failed to make a new Mounter: %v", err)
  143. }
  144. if mounter == nil {
  145. t.Error("Got a nil Mounter")
  146. }
  147. path := mounter.GetPath()
  148. expectedPath := fmt.Sprintf("%s/pods/poduid/volumes/kubernetes.io~iscsi/vol1", tmpDir)
  149. if path != expectedPath {
  150. t.Errorf("Unexpected path, expected %q, got: %q", expectedPath, path)
  151. }
  152. if err := mounter.SetUp(volume.MounterArgs{}); err != nil {
  153. t.Errorf("Expected success, got: %v", err)
  154. }
  155. if _, err := os.Stat(path); err != nil {
  156. if os.IsNotExist(err) {
  157. t.Errorf("SetUp() failed, volume path not created: %s", path)
  158. } else {
  159. t.Errorf("SetUp() failed: %v", err)
  160. }
  161. }
  162. fakeManager2 := NewFakeDiskManager()
  163. defer fakeManager2.Cleanup()
  164. unmounter, err := plug.(*iscsiPlugin).newUnmounterInternal("vol1", types.UID("poduid"), fakeManager2, fakeMounter, fakeExec)
  165. if err != nil {
  166. t.Errorf("Failed to make a new Unmounter: %v", err)
  167. }
  168. if unmounter == nil {
  169. t.Error("Got a nil Unmounter")
  170. }
  171. if err := unmounter.TearDown(); err != nil {
  172. t.Errorf("Expected success, got: %v", err)
  173. }
  174. if _, err := os.Stat(path); err == nil {
  175. t.Errorf("TearDown() failed, volume path still exists: %s", path)
  176. } else if !os.IsNotExist(err) {
  177. t.Errorf("TearDown() failed: %v", err)
  178. }
  179. }
  180. func TestPluginVolume(t *testing.T) {
  181. vol := &v1.Volume{
  182. Name: "vol1",
  183. VolumeSource: v1.VolumeSource{
  184. ISCSI: &v1.ISCSIVolumeSource{
  185. TargetPortal: "127.0.0.1:3260",
  186. IQN: "iqn.2014-12.server:storage.target01",
  187. FSType: "ext4",
  188. Lun: 0,
  189. },
  190. },
  191. }
  192. doTestPlugin(t, volume.NewSpecFromVolume(vol))
  193. }
  194. func TestPluginPersistentVolume(t *testing.T) {
  195. vol := &v1.PersistentVolume{
  196. ObjectMeta: metav1.ObjectMeta{
  197. Name: "vol1",
  198. },
  199. Spec: v1.PersistentVolumeSpec{
  200. PersistentVolumeSource: v1.PersistentVolumeSource{
  201. ISCSI: &v1.ISCSIPersistentVolumeSource{
  202. TargetPortal: "127.0.0.1:3260",
  203. IQN: "iqn.2014-12.server:storage.target01",
  204. FSType: "ext4",
  205. Lun: 0,
  206. },
  207. },
  208. },
  209. }
  210. doTestPlugin(t, volume.NewSpecFromPersistentVolume(vol, false))
  211. }
  212. func TestPersistentClaimReadOnlyFlag(t *testing.T) {
  213. tmpDir, err := utiltesting.MkTmpdir("iscsi_test")
  214. if err != nil {
  215. t.Fatalf("error creating temp dir: %v", err)
  216. }
  217. defer os.RemoveAll(tmpDir)
  218. pv := &v1.PersistentVolume{
  219. ObjectMeta: metav1.ObjectMeta{
  220. Name: "pvA",
  221. },
  222. Spec: v1.PersistentVolumeSpec{
  223. PersistentVolumeSource: v1.PersistentVolumeSource{
  224. ISCSI: &v1.ISCSIPersistentVolumeSource{
  225. TargetPortal: "127.0.0.1:3260",
  226. IQN: "iqn.2014-12.server:storage.target01",
  227. FSType: "ext4",
  228. Lun: 0,
  229. },
  230. },
  231. ClaimRef: &v1.ObjectReference{
  232. Name: "claimA",
  233. },
  234. },
  235. }
  236. claim := &v1.PersistentVolumeClaim{
  237. ObjectMeta: metav1.ObjectMeta{
  238. Name: "claimA",
  239. Namespace: "nsA",
  240. },
  241. Spec: v1.PersistentVolumeClaimSpec{
  242. VolumeName: "pvA",
  243. },
  244. Status: v1.PersistentVolumeClaimStatus{
  245. Phase: v1.ClaimBound,
  246. },
  247. }
  248. client := fake.NewSimpleClientset(pv, claim)
  249. plugMgr := volume.VolumePluginMgr{}
  250. plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, volumetest.NewFakeVolumeHost(tmpDir, client, nil))
  251. plug, _ := plugMgr.FindPluginByName(iscsiPluginName)
  252. // readOnly bool is supplied by persistent-claim volume source when its mounter creates other volumes
  253. spec := volume.NewSpecFromPersistentVolume(pv, true)
  254. pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: types.UID("poduid")}}
  255. mounter, _ := plug.NewMounter(spec, pod, volume.VolumeOptions{})
  256. if mounter == nil {
  257. t.Fatalf("Got a nil Mounter")
  258. }
  259. if !mounter.GetAttributes().ReadOnly {
  260. t.Errorf("Expected true for mounter.IsReadOnly")
  261. }
  262. }
  263. func TestPortalMounter(t *testing.T) {
  264. if portal := portalMounter("127.0.0.1"); portal != "127.0.0.1:3260" {
  265. t.Errorf("wrong portal: %s", portal)
  266. }
  267. if portal := portalMounter("127.0.0.1:3260"); portal != "127.0.0.1:3260" {
  268. t.Errorf("wrong portal: %s", portal)
  269. }
  270. }
  271. type testcase struct {
  272. name string
  273. defaultNs string
  274. spec *volume.Spec
  275. // Expected return of the test
  276. expectedName string
  277. expectedNs string
  278. expectedIface string
  279. expectedError error
  280. expectedDiscoveryCHAP bool
  281. expectedSessionCHAP bool
  282. }
  283. func TestGetSecretNameAndNamespaceForPV(t *testing.T) {
  284. tests := []testcase{
  285. {
  286. name: "persistent volume source",
  287. defaultNs: "default",
  288. spec: &volume.Spec{
  289. PersistentVolume: &v1.PersistentVolume{
  290. Spec: v1.PersistentVolumeSpec{
  291. PersistentVolumeSource: v1.PersistentVolumeSource{
  292. ISCSI: &v1.ISCSIPersistentVolumeSource{
  293. TargetPortal: "127.0.0.1:3260",
  294. IQN: "iqn.2014-12.server:storage.target01",
  295. FSType: "ext4",
  296. Lun: 0,
  297. SecretRef: &v1.SecretReference{
  298. Name: "name",
  299. Namespace: "ns",
  300. },
  301. },
  302. },
  303. },
  304. },
  305. },
  306. expectedName: "name",
  307. expectedNs: "ns",
  308. expectedError: nil,
  309. },
  310. {
  311. name: "persistent volume source without namespace",
  312. defaultNs: "default",
  313. spec: &volume.Spec{
  314. PersistentVolume: &v1.PersistentVolume{
  315. Spec: v1.PersistentVolumeSpec{
  316. PersistentVolumeSource: v1.PersistentVolumeSource{
  317. ISCSI: &v1.ISCSIPersistentVolumeSource{
  318. TargetPortal: "127.0.0.1:3260",
  319. IQN: "iqn.2014-12.server:storage.target01",
  320. FSType: "ext4",
  321. Lun: 0,
  322. SecretRef: &v1.SecretReference{
  323. Name: "name",
  324. },
  325. },
  326. },
  327. },
  328. },
  329. },
  330. expectedName: "name",
  331. expectedNs: "default",
  332. expectedError: nil,
  333. },
  334. {
  335. name: "pod volume source",
  336. defaultNs: "default",
  337. spec: &volume.Spec{
  338. Volume: &v1.Volume{
  339. VolumeSource: v1.VolumeSource{
  340. ISCSI: &v1.ISCSIVolumeSource{
  341. TargetPortal: "127.0.0.1:3260",
  342. IQN: "iqn.2014-12.server:storage.target01",
  343. FSType: "ext4",
  344. Lun: 0,
  345. },
  346. },
  347. },
  348. },
  349. expectedName: "",
  350. expectedNs: "",
  351. expectedError: nil,
  352. },
  353. }
  354. for _, testcase := range tests {
  355. resultName, resultNs, err := getISCSISecretNameAndNamespace(testcase.spec, testcase.defaultNs)
  356. if err != testcase.expectedError || resultName != testcase.expectedName || resultNs != testcase.expectedNs {
  357. t.Errorf("%s failed: expected err=%v ns=%q name=%q, got %v/%q/%q", testcase.name, testcase.expectedError, testcase.expectedNs, testcase.expectedName,
  358. err, resultNs, resultName)
  359. }
  360. }
  361. }
  362. func TestGetISCSIInitiatorInfo(t *testing.T) {
  363. tests := []testcase{
  364. {
  365. name: "persistent volume source",
  366. spec: &volume.Spec{
  367. PersistentVolume: &v1.PersistentVolume{
  368. Spec: v1.PersistentVolumeSpec{
  369. PersistentVolumeSource: v1.PersistentVolumeSource{
  370. ISCSI: &v1.ISCSIPersistentVolumeSource{
  371. TargetPortal: "127.0.0.1:3260",
  372. IQN: "iqn.2014-12.server:storage.target01",
  373. FSType: "ext4",
  374. Lun: 0,
  375. ISCSIInterface: "tcp",
  376. },
  377. },
  378. },
  379. },
  380. },
  381. expectedIface: "tcp",
  382. expectedError: nil,
  383. },
  384. {
  385. name: "pod volume source",
  386. spec: &volume.Spec{
  387. Volume: &v1.Volume{
  388. VolumeSource: v1.VolumeSource{
  389. ISCSI: &v1.ISCSIVolumeSource{
  390. TargetPortal: "127.0.0.1:3260",
  391. IQN: "iqn.2014-12.server:storage.target01",
  392. FSType: "ext4",
  393. Lun: 0,
  394. ISCSIInterface: "tcp",
  395. },
  396. },
  397. },
  398. },
  399. expectedIface: "tcp",
  400. expectedError: nil,
  401. },
  402. }
  403. for _, testcase := range tests {
  404. resultIface, _, err := getISCSIInitiatorInfo(testcase.spec)
  405. if err != testcase.expectedError || resultIface != testcase.expectedIface {
  406. t.Errorf("%s failed: expected err=%v iface=%s, got %v/%s", testcase.name, testcase.expectedError, testcase.expectedIface,
  407. err, resultIface)
  408. }
  409. }
  410. }
  411. func TestGetISCSICHAP(t *testing.T) {
  412. tests := []testcase{
  413. {
  414. name: "persistent volume source",
  415. spec: &volume.Spec{
  416. PersistentVolume: &v1.PersistentVolume{
  417. Spec: v1.PersistentVolumeSpec{
  418. PersistentVolumeSource: v1.PersistentVolumeSource{
  419. ISCSI: &v1.ISCSIPersistentVolumeSource{
  420. DiscoveryCHAPAuth: true,
  421. SessionCHAPAuth: true,
  422. },
  423. },
  424. },
  425. },
  426. },
  427. expectedDiscoveryCHAP: true,
  428. expectedSessionCHAP: true,
  429. expectedError: nil,
  430. },
  431. {
  432. name: "pod volume source",
  433. spec: &volume.Spec{
  434. Volume: &v1.Volume{
  435. VolumeSource: v1.VolumeSource{
  436. ISCSI: &v1.ISCSIVolumeSource{
  437. DiscoveryCHAPAuth: true,
  438. SessionCHAPAuth: true,
  439. },
  440. },
  441. },
  442. },
  443. expectedDiscoveryCHAP: true,
  444. expectedSessionCHAP: true,
  445. expectedError: nil,
  446. },
  447. {
  448. name: "no volume",
  449. spec: &volume.Spec{},
  450. expectedDiscoveryCHAP: false,
  451. expectedSessionCHAP: false,
  452. expectedError: fmt.Errorf("Spec does not reference an ISCSI volume type"),
  453. },
  454. }
  455. for _, testcase := range tests {
  456. resultDiscoveryCHAP, err := getISCSIDiscoveryCHAPInfo(testcase.spec)
  457. resultSessionCHAP, err := getISCSISessionCHAPInfo(testcase.spec)
  458. switch testcase.name {
  459. case "no volume":
  460. if err.Error() != testcase.expectedError.Error() || resultDiscoveryCHAP != testcase.expectedDiscoveryCHAP || resultSessionCHAP != testcase.expectedSessionCHAP {
  461. t.Errorf("%s failed: expected err=%v DiscoveryCHAP=%v SessionCHAP=%v, got %v/%v/%v",
  462. testcase.name, testcase.expectedError, testcase.expectedDiscoveryCHAP, testcase.expectedSessionCHAP,
  463. err, resultDiscoveryCHAP, resultSessionCHAP)
  464. }
  465. default:
  466. if err != testcase.expectedError || resultDiscoveryCHAP != testcase.expectedDiscoveryCHAP || resultSessionCHAP != testcase.expectedSessionCHAP {
  467. t.Errorf("%s failed: expected err=%v DiscoveryCHAP=%v SessionCHAP=%v, got %v/%v/%v", testcase.name, testcase.expectedError, testcase.expectedDiscoveryCHAP, testcase.expectedSessionCHAP,
  468. err, resultDiscoveryCHAP, resultSessionCHAP)
  469. }
  470. }
  471. }
  472. }
  473. func TestGetVolumeSpec(t *testing.T) {
  474. path := "plugins/kubernetes.io/iscsi/volumeDevices/iface-default/127.0.0.1:3260-iqn.2014-12.server:storage.target01-lun-0"
  475. spec, _ := getVolumeSpecFromGlobalMapPath("test", path)
  476. portal := spec.PersistentVolume.Spec.PersistentVolumeSource.ISCSI.TargetPortal
  477. if portal != "127.0.0.1:3260" {
  478. t.Errorf("wrong portal: %v", portal)
  479. }
  480. iqn := spec.PersistentVolume.Spec.PersistentVolumeSource.ISCSI.IQN
  481. if iqn != "iqn.2014-12.server:storage.target01" {
  482. t.Errorf("wrong iqn: %v", iqn)
  483. }
  484. lun := spec.PersistentVolume.Spec.PersistentVolumeSource.ISCSI.Lun
  485. if lun != 0 {
  486. t.Errorf("wrong lun: %v", lun)
  487. }
  488. iface := spec.PersistentVolume.Spec.PersistentVolumeSource.ISCSI.ISCSIInterface
  489. if iface != "default" {
  490. t.Errorf("wrong ISCSIInterface: %v", iface)
  491. }
  492. }
  493. func TestGetVolumeSpec_no_lun(t *testing.T) {
  494. path := "plugins/kubernetes.io/iscsi/volumeDevices/iface-default/127.0.0.1:3260-iqn.2014-12.server:storage.target01"
  495. _, err := getVolumeSpecFromGlobalMapPath("test", path)
  496. if !strings.Contains(err.Error(), "malformatted mnt path") {
  497. t.Errorf("should get error: malformatted mnt path")
  498. }
  499. }
  500. func TestGetVolumeSpec_no_iface(t *testing.T) {
  501. path := "plugins/kubernetes.io/iscsi/volumeDevices/default/127.0.0.1:3260-iqn.2014-12.server:storage.target01-lun-0"
  502. _, err := getVolumeSpecFromGlobalMapPath("test", path)
  503. if !strings.Contains(err.Error(), "failed to retrieve iface") {
  504. t.Errorf("should get error: failed to retrieve iface")
  505. }
  506. }