sio_volume_test.go 16 KB

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