iscsi_test.go 16 KB

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