sio_volume_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /*
  2. Copyright 2017 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 scaleio
  14. import (
  15. "fmt"
  16. "os"
  17. "path/filepath"
  18. "strings"
  19. "testing"
  20. "k8s.io/klog"
  21. api "k8s.io/api/core/v1"
  22. meta "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/apimachinery/pkg/runtime"
  24. "k8s.io/apimachinery/pkg/types"
  25. fakeclient "k8s.io/client-go/kubernetes/fake"
  26. utiltesting "k8s.io/client-go/util/testing"
  27. "k8s.io/kubernetes/pkg/volume"
  28. volumetest "k8s.io/kubernetes/pkg/volume/testing"
  29. )
  30. var (
  31. testSioSystem = "sio"
  32. testSioPD = "default"
  33. testSioVol = "vol-0001"
  34. testns = "default"
  35. testSecret = "sio-secret"
  36. testSioVolName = fmt.Sprintf("%s%s%s", testns, "-", testSioVol)
  37. podUID = types.UID("sio-pod")
  38. )
  39. func newPluginMgr(t *testing.T, apiObject runtime.Object) (*volume.VolumePluginMgr, string) {
  40. tmpDir, err := utiltesting.MkTmpdir("scaleio-test")
  41. if err != nil {
  42. t.Fatalf("can't make a temp dir: %v", err)
  43. }
  44. fakeClient := fakeclient.NewSimpleClientset(apiObject)
  45. host := volumetest.NewFakeVolumeHostWithNodeLabels(
  46. tmpDir,
  47. fakeClient,
  48. nil,
  49. map[string]string{sdcGUIDLabelName: "abc-123"},
  50. )
  51. plugMgr := &volume.VolumePluginMgr{}
  52. plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, host)
  53. return plugMgr, tmpDir
  54. }
  55. func makeScaleIOSecret(name, namespace string) *api.Secret {
  56. return &api.Secret{
  57. ObjectMeta: meta.ObjectMeta{
  58. Name: name,
  59. Namespace: namespace,
  60. UID: "1234567890",
  61. },
  62. Type: api.SecretType("kubernetes.io/scaleio"),
  63. Data: map[string][]byte{
  64. "username": []byte("username"),
  65. "password": []byte("password"),
  66. },
  67. }
  68. }
  69. func TestVolumeCanSupport(t *testing.T) {
  70. plugMgr, tmpDir := newPluginMgr(t, makeScaleIOSecret(testSecret, testns))
  71. defer os.RemoveAll(tmpDir)
  72. plug, err := plugMgr.FindPluginByName(sioPluginName)
  73. if err != nil {
  74. t.Errorf("Can't find the plugin %s by name", sioPluginName)
  75. }
  76. if plug.GetPluginName() != "kubernetes.io/scaleio" {
  77. t.Errorf("Wrong name: %s", plug.GetPluginName())
  78. }
  79. if !plug.CanSupport(
  80. &volume.Spec{
  81. Volume: &api.Volume{
  82. VolumeSource: api.VolumeSource{
  83. ScaleIO: &api.ScaleIOVolumeSource{},
  84. },
  85. },
  86. },
  87. ) {
  88. t.Errorf("Expected true for CanSupport LibStorage VolumeSource")
  89. }
  90. if !plug.CanSupport(
  91. &volume.Spec{
  92. PersistentVolume: &api.PersistentVolume{
  93. Spec: api.PersistentVolumeSpec{
  94. PersistentVolumeSource: api.PersistentVolumeSource{
  95. ScaleIO: &api.ScaleIOPersistentVolumeSource{},
  96. },
  97. },
  98. },
  99. },
  100. ) {
  101. t.Errorf("Expected true for CanSupport LibStorage PersistentVolumeSource")
  102. }
  103. }
  104. func TestVolumeGetAccessModes(t *testing.T) {
  105. plugMgr, tmpDir := newPluginMgr(t, makeScaleIOSecret(testSecret, testns))
  106. defer os.RemoveAll(tmpDir)
  107. plug, err := plugMgr.FindPersistentPluginByName(sioPluginName)
  108. if err != nil {
  109. t.Errorf("Can't find the plugin %v", sioPluginName)
  110. }
  111. if !containsMode(plug.GetAccessModes(), api.ReadWriteOnce) {
  112. t.Errorf("Expected two AccessModeTypes: %s or %s", api.ReadWriteOnce, api.ReadOnlyMany)
  113. }
  114. }
  115. func containsMode(modes []api.PersistentVolumeAccessMode, mode api.PersistentVolumeAccessMode) bool {
  116. for _, m := range modes {
  117. if m == mode {
  118. return true
  119. }
  120. }
  121. return false
  122. }
  123. func TestVolumeMounterUnmounter(t *testing.T) {
  124. plugMgr, tmpDir := newPluginMgr(t, makeScaleIOSecret(testSecret, testns))
  125. defer os.RemoveAll(tmpDir)
  126. plug, err := plugMgr.FindPluginByName(sioPluginName)
  127. if err != nil {
  128. t.Errorf("Can't find the plugin %v", sioPluginName)
  129. }
  130. sioPlug, ok := plug.(*sioPlugin)
  131. if !ok {
  132. t.Errorf("Cannot assert plugin to be type sioPlugin")
  133. }
  134. vol := &api.Volume{
  135. Name: testSioVolName,
  136. VolumeSource: api.VolumeSource{
  137. ScaleIO: &api.ScaleIOVolumeSource{
  138. Gateway: "http://test.scaleio:1111",
  139. System: testSioSystem,
  140. ProtectionDomain: testSioPD,
  141. StoragePool: "default",
  142. VolumeName: testSioVol,
  143. FSType: "ext4",
  144. SecretRef: &api.LocalObjectReference{Name: testSecret},
  145. ReadOnly: false,
  146. },
  147. },
  148. }
  149. sioMounter, err := sioPlug.NewMounter(
  150. volume.NewSpecFromVolume(vol),
  151. &api.Pod{ObjectMeta: meta.ObjectMeta{UID: podUID, Namespace: testns}},
  152. volume.VolumeOptions{},
  153. )
  154. if err != nil {
  155. t.Fatalf("Failed to make a new Mounter: %v", err)
  156. }
  157. if sioMounter == nil {
  158. t.Fatal("Got a nil Mounter")
  159. }
  160. sio := newFakeSio()
  161. sioVol := sioMounter.(*sioVolume)
  162. if err := sioVol.setSioMgr(); err != nil {
  163. t.Fatalf("failed to create sio mgr: %v", err)
  164. }
  165. sioVol.sioMgr.client = sio
  166. sioVol.sioMgr.CreateVolume(testSioVol, 8) //create vol ahead of time
  167. volPath := filepath.Join(tmpDir, fmt.Sprintf("pods/%s/volumes/kubernetes.io~scaleio/%s", podUID, testSioVolName))
  168. path := sioMounter.GetPath()
  169. if path != volPath {
  170. t.Errorf("Got unexpected path: %s", path)
  171. }
  172. if err := sioMounter.SetUp(volume.MounterArgs{}); err != nil {
  173. t.Errorf("Expected success, got: %v", err)
  174. }
  175. if _, err := os.Stat(path); err != nil {
  176. if os.IsNotExist(err) {
  177. t.Errorf("SetUp() failed, volume path not created: %s", path)
  178. } else {
  179. t.Errorf("SetUp() failed: %v", err)
  180. }
  181. }
  182. if sio.isMultiMap {
  183. t.Errorf("SetUp() - expecting multiple volume disabled by default")
  184. }
  185. // did we read sdcGUID label
  186. if _, ok := sioVol.sioMgr.configData[confKey.sdcGUID]; !ok {
  187. t.Errorf("Expected to find node label scaleio.sdcGUID, but did not find it")
  188. }
  189. // rebuild spec
  190. builtSpec, err := sioPlug.ConstructVolumeSpec(volume.NewSpecFromVolume(vol).Name(), path)
  191. if err != nil {
  192. t.Errorf("ConstructVolumeSpec failed %v", err)
  193. }
  194. if builtSpec.Name() != vol.Name {
  195. t.Errorf("Unexpected spec name %s", builtSpec.Name())
  196. }
  197. // unmount
  198. sioUnmounter, err := sioPlug.NewUnmounter(volume.NewSpecFromVolume(vol).Name(), podUID)
  199. if err != nil {
  200. t.Fatalf("Failed to make a new Unmounter: %v", err)
  201. }
  202. if sioUnmounter == nil {
  203. t.Fatal("Got a nil Unmounter")
  204. }
  205. sioVol = sioUnmounter.(*sioVolume)
  206. if err := sioVol.resetSioMgr(); err != nil {
  207. t.Fatalf("failed to reset sio mgr: %v", err)
  208. }
  209. sioVol.sioMgr.client = sio
  210. if err := sioUnmounter.TearDown(); err != nil {
  211. t.Errorf("Expected success, got: %v", err)
  212. }
  213. // is mount point gone ?
  214. if _, err := os.Stat(path); err == nil {
  215. t.Errorf("TearDown() failed, volume path still exists: %s", path)
  216. } else if !os.IsNotExist(err) {
  217. t.Errorf("TearDown() failed: %v", err)
  218. }
  219. // are we still mapped
  220. if sio.volume.MappedSdcInfo != nil {
  221. t.Errorf("expected SdcMappedInfo to be nil, volume may still be mapped")
  222. }
  223. }
  224. func TestVolumeProvisioner(t *testing.T) {
  225. plugMgr, tmpDir := newPluginMgr(t, makeScaleIOSecret(testSecret, testns))
  226. defer os.RemoveAll(tmpDir)
  227. plug, err := plugMgr.FindPluginByName(sioPluginName)
  228. if err != nil {
  229. t.Fatalf("Can't find the plugin %v", sioPluginName)
  230. }
  231. sioPlug, ok := plug.(*sioPlugin)
  232. if !ok {
  233. t.Fatal("Cannot assert plugin to be type sioPlugin")
  234. }
  235. options := volume.VolumeOptions{
  236. ClusterName: "testcluster",
  237. PVC: volumetest.CreateTestPVC("100Mi", []api.PersistentVolumeAccessMode{api.ReadWriteOnce}),
  238. PersistentVolumeReclaimPolicy: api.PersistentVolumeReclaimDelete,
  239. }
  240. options.PVC.Name = "testpvc"
  241. options.PVC.Namespace = testns
  242. options.PVC.Spec.AccessModes = []api.PersistentVolumeAccessMode{
  243. api.ReadOnlyMany,
  244. }
  245. options.Parameters = map[string]string{
  246. confKey.gateway: "http://test.scaleio:11111",
  247. confKey.system: "sio",
  248. confKey.protectionDomain: testSioPD,
  249. confKey.storagePool: "default",
  250. confKey.secretName: testSecret,
  251. }
  252. provisioner, err := sioPlug.NewProvisioner(options)
  253. if err != nil {
  254. t.Fatalf("failed to create new provisioner: %v", err)
  255. }
  256. if provisioner == nil {
  257. t.Fatal("got a nil provisioner")
  258. }
  259. sio := newFakeSio()
  260. sioVol := provisioner.(*sioVolume)
  261. if err := sioVol.setSioMgrFromConfig(); err != nil {
  262. t.Fatalf("failed to create scaleio mgr from config: %v", err)
  263. }
  264. sioVol.sioMgr.client = sio
  265. spec, err := provisioner.Provision(nil, nil)
  266. if err != nil {
  267. t.Fatalf("call to Provision() failed: %v", err)
  268. }
  269. if spec.Namespace != testns {
  270. t.Fatalf("unexpected namespace %v", spec.Namespace)
  271. }
  272. if spec.Spec.ScaleIO.SecretRef == nil {
  273. t.Fatalf("unexpected nil value for spec.SecretRef")
  274. }
  275. if spec.Spec.ScaleIO.SecretRef.Name != testSecret ||
  276. spec.Spec.ScaleIO.SecretRef.Namespace != testns {
  277. t.Fatalf("spec.SecretRef is not being set properly")
  278. }
  279. spec.Spec.ClaimRef = &api.ObjectReference{Namespace: testns}
  280. // validate provision
  281. actualSpecName := spec.Name
  282. actualVolName := spec.Spec.PersistentVolumeSource.ScaleIO.VolumeName
  283. if !strings.HasPrefix(actualSpecName, "k8svol-") {
  284. t.Errorf("expecting volume name to start with k8svol-, got %s", actualSpecName)
  285. }
  286. vol, err := sio.FindVolume(actualVolName)
  287. if err != nil {
  288. t.Fatalf("failed getting volume %v: %v", actualVolName, err)
  289. }
  290. if vol.Name != actualVolName {
  291. t.Errorf("expected volume name to be %s, got %s", actualVolName, vol.Name)
  292. }
  293. if vol.SizeInKb != 8*1024*1024 {
  294. klog.V(4).Info(log("unexpected volume size"))
  295. }
  296. // mount dynamic vol
  297. sioMounter, err := sioPlug.NewMounter(
  298. volume.NewSpecFromPersistentVolume(spec, false),
  299. &api.Pod{ObjectMeta: meta.ObjectMeta{UID: podUID, Namespace: testns}},
  300. volume.VolumeOptions{},
  301. )
  302. if err != nil {
  303. t.Fatalf("Failed to make a new Mounter: %v", err)
  304. }
  305. sioVol = sioMounter.(*sioVolume)
  306. if err := sioVol.setSioMgr(); err != nil {
  307. t.Fatalf("failed to create sio mgr: %v", err)
  308. }
  309. sioVol.sioMgr.client = sio
  310. if err := sioMounter.SetUp(volume.MounterArgs{}); err != nil {
  311. t.Fatalf("Expected success, got: %v", err)
  312. }
  313. // did we read sdcGUID label
  314. if _, ok := sioVol.sioMgr.configData[confKey.sdcGUID]; !ok {
  315. t.Errorf("Expected to find node label scaleio.sdcGUID, but did not find it")
  316. }
  317. // isMultiMap applied
  318. if !sio.isMultiMap {
  319. t.Errorf("SetUp() expecting attached volume with multi-mapping")
  320. }
  321. // teardown dynamic vol
  322. sioUnmounter, err := sioPlug.NewUnmounter(spec.Name, podUID)
  323. if err != nil {
  324. t.Fatalf("Failed to make a new Unmounter: %v", err)
  325. }
  326. sioVol = sioUnmounter.(*sioVolume)
  327. if err := sioVol.resetSioMgr(); err != nil {
  328. t.Fatalf("failed to reset sio mgr: %v", err)
  329. }
  330. sioVol.sioMgr.client = sio
  331. if err := sioUnmounter.TearDown(); err != nil {
  332. t.Errorf("Expected success, got: %v", err)
  333. }
  334. // test deleter
  335. deleter, err := sioPlug.NewDeleter(volume.NewSpecFromPersistentVolume(spec, false))
  336. if err != nil {
  337. t.Fatalf("failed to create a deleter %v", err)
  338. }
  339. sioVol = deleter.(*sioVolume)
  340. if err := sioVol.setSioMgrFromSpec(); err != nil {
  341. t.Fatalf("failed to set sio mgr: %v", err)
  342. }
  343. sioVol.sioMgr.client = sio
  344. if err := deleter.Delete(); err != nil {
  345. t.Fatalf("failed while deleting vol: %v", err)
  346. }
  347. path := deleter.GetPath()
  348. if _, err := os.Stat(path); err == nil {
  349. t.Errorf("TearDown() failed, volume path still exists: %s", path)
  350. } else if !os.IsNotExist(err) {
  351. t.Errorf("Deleter did not delete path %v: %v", path, err)
  352. }
  353. }
  354. func TestVolumeProvisionerWithIncompleteConfig(t *testing.T) {
  355. plugMgr, tmpDir := newPluginMgr(t, makeScaleIOSecret(testSecret, testns))
  356. defer os.RemoveAll(tmpDir)
  357. plug, err := plugMgr.FindPluginByName(sioPluginName)
  358. if err != nil {
  359. t.Fatalf("Can't find the plugin %v", sioPluginName)
  360. }
  361. sioPlug, ok := plug.(*sioPlugin)
  362. if !ok {
  363. t.Fatal("Cannot assert plugin to be type sioPlugin")
  364. }
  365. options := volume.VolumeOptions{
  366. ClusterName: "testcluster",
  367. PVName: "pvc-sio-dynamic-vol",
  368. PVC: volumetest.CreateTestPVC("100Mi", []api.PersistentVolumeAccessMode{api.ReadWriteOnce}),
  369. PersistentVolumeReclaimPolicy: api.PersistentVolumeReclaimDelete,
  370. }
  371. options.PVC.Namespace = testns
  372. options.PVC.Spec.AccessModes = []api.PersistentVolumeAccessMode{
  373. api.ReadWriteOnce,
  374. }
  375. // incomplete options, test should fail
  376. _, err = sioPlug.NewProvisioner(options)
  377. if err == nil {
  378. t.Fatal("expected failure due to incomplete options")
  379. }
  380. }
  381. func TestVolumeProvisionerWithZeroCapacity(t *testing.T) {
  382. plugMgr, tmpDir := newPluginMgr(t, makeScaleIOSecret(testSecret, testns))
  383. defer os.RemoveAll(tmpDir)
  384. plug, err := plugMgr.FindPluginByName(sioPluginName)
  385. if err != nil {
  386. t.Fatalf("Can't find the plugin %v", sioPluginName)
  387. }
  388. sioPlug, ok := plug.(*sioPlugin)
  389. if !ok {
  390. t.Fatal("Cannot assert plugin to be type sioPlugin")
  391. }
  392. options := volume.VolumeOptions{
  393. ClusterName: "testcluster",
  394. PVName: "pvc-sio-dynamic-vol",
  395. PVC: volumetest.CreateTestPVC("0Mi", []api.PersistentVolumeAccessMode{api.ReadWriteOnce}),
  396. PersistentVolumeReclaimPolicy: api.PersistentVolumeReclaimDelete,
  397. }
  398. options.PVC.Namespace = testns
  399. options.PVC.Spec.AccessModes = []api.PersistentVolumeAccessMode{
  400. api.ReadWriteOnce,
  401. }
  402. options.Parameters = map[string]string{
  403. confKey.gateway: "http://test.scaleio:11111",
  404. confKey.system: "sio",
  405. confKey.protectionDomain: testSioPD,
  406. confKey.storagePool: "default",
  407. confKey.secretName: "sio-secret",
  408. }
  409. provisioner, _ := sioPlug.NewProvisioner(options)
  410. sio := newFakeSio()
  411. sioVol := provisioner.(*sioVolume)
  412. if err := sioVol.setSioMgrFromConfig(); err != nil {
  413. t.Fatalf("failed to create scaleio mgr from config: %v", err)
  414. }
  415. sioVol.sioMgr.client = sio
  416. _, err = provisioner.Provision(nil, nil)
  417. if err == nil {
  418. t.Fatalf("call to Provision() should fail with invalid capacity")
  419. }
  420. }
  421. func TestVolumeProvisionerWithSecretNamespace(t *testing.T) {
  422. plugMgr, tmpDir := newPluginMgr(t, makeScaleIOSecret("sio-sec", "sio-ns"))
  423. defer os.RemoveAll(tmpDir)
  424. plug, err := plugMgr.FindPluginByName(sioPluginName)
  425. if err != nil {
  426. t.Fatalf("Can't find the plugin %v", sioPluginName)
  427. }
  428. sioPlug, ok := plug.(*sioPlugin)
  429. if !ok {
  430. t.Fatal("Cannot assert plugin to be type sioPlugin")
  431. }
  432. options := volume.VolumeOptions{
  433. ClusterName: "testcluster",
  434. PVName: "pvc-sio-dynamic-vol",
  435. PVC: volumetest.CreateTestPVC("100Mi", []api.PersistentVolumeAccessMode{api.ReadWriteOnce}),
  436. PersistentVolumeReclaimPolicy: api.PersistentVolumeReclaimDelete,
  437. }
  438. options.PVC.Spec.AccessModes = []api.PersistentVolumeAccessMode{
  439. api.ReadWriteOnce,
  440. }
  441. options.PVC.Namespace = "pvc-ns"
  442. options.Parameters = map[string]string{
  443. confKey.gateway: "http://test.scaleio:11111",
  444. confKey.system: "sio",
  445. confKey.protectionDomain: testSioPD,
  446. confKey.storagePool: "default",
  447. confKey.secretName: "sio-sec",
  448. confKey.secretNamespace: "sio-ns",
  449. }
  450. provisioner, _ := sioPlug.NewProvisioner(options)
  451. sio := newFakeSio()
  452. sioVol := provisioner.(*sioVolume)
  453. if err := sioVol.setSioMgrFromConfig(); err != nil {
  454. t.Fatalf("failed to create scaleio mgr from config: %v", err)
  455. }
  456. sioVol.sioMgr.client = sio
  457. spec, err := sioVol.Provision(nil, nil)
  458. if err != nil {
  459. t.Fatalf("call to Provision() failed: %v", err)
  460. }
  461. if spec.GetObjectMeta().GetNamespace() != "pvc-ns" {
  462. t.Fatalf("unexpected spec.namespace %s", spec.GetObjectMeta().GetNamespace())
  463. }
  464. if spec.Spec.ScaleIO.SecretRef.Name != "sio-sec" {
  465. t.Fatalf("unexpected spec.ScaleIOPersistentVolume.SecretRef.Name %v", spec.Spec.ScaleIO.SecretRef.Name)
  466. }
  467. if spec.Spec.ScaleIO.SecretRef.Namespace != "sio-ns" {
  468. t.Fatalf("unexpected spec.ScaleIOPersistentVolume.SecretRef.Namespace %v", spec.Spec.ScaleIO.SecretRef.Namespace)
  469. }
  470. }