portworx_test.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 portworx
  14. import (
  15. "fmt"
  16. "os"
  17. "path/filepath"
  18. "testing"
  19. "k8s.io/api/core/v1"
  20. "k8s.io/apimachinery/pkg/api/resource"
  21. "k8s.io/apimachinery/pkg/types"
  22. utiltesting "k8s.io/client-go/util/testing"
  23. "k8s.io/kubernetes/pkg/util/mount"
  24. "k8s.io/kubernetes/pkg/volume"
  25. volumetest "k8s.io/kubernetes/pkg/volume/testing"
  26. )
  27. const (
  28. PortworxTestVolume = "portworx-test-vol"
  29. )
  30. func TestCanSupport(t *testing.T) {
  31. tmpDir, err := utiltesting.MkTmpdir("portworxVolumeTest")
  32. if err != nil {
  33. t.Fatalf("can't make a temp dir: %v", err)
  34. }
  35. defer os.RemoveAll(tmpDir)
  36. plugMgr := volume.VolumePluginMgr{}
  37. plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, volumetest.NewFakeVolumeHost(tmpDir, nil, nil))
  38. plug, err := plugMgr.FindPluginByName("kubernetes.io/portworx-volume")
  39. if err != nil {
  40. t.Errorf("Can't find the plugin by name")
  41. }
  42. if plug.GetPluginName() != "kubernetes.io/portworx-volume" {
  43. t.Errorf("Wrong name: %s", plug.GetPluginName())
  44. }
  45. if !plug.CanSupport(&volume.Spec{Volume: &v1.Volume{VolumeSource: v1.VolumeSource{PortworxVolume: &v1.PortworxVolumeSource{}}}}) {
  46. t.Errorf("Expected true")
  47. }
  48. if !plug.CanSupport(&volume.Spec{PersistentVolume: &v1.PersistentVolume{Spec: v1.PersistentVolumeSpec{PersistentVolumeSource: v1.PersistentVolumeSource{PortworxVolume: &v1.PortworxVolumeSource{}}}}}) {
  49. t.Errorf("Expected true")
  50. }
  51. }
  52. func TestGetAccessModes(t *testing.T) {
  53. tmpDir, err := utiltesting.MkTmpdir("portworxVolumeTest")
  54. if err != nil {
  55. t.Fatalf("can't make a temp dir: %v", err)
  56. }
  57. defer os.RemoveAll(tmpDir)
  58. plugMgr := volume.VolumePluginMgr{}
  59. plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, volumetest.NewFakeVolumeHost(tmpDir, nil, nil))
  60. plug, err := plugMgr.FindPersistentPluginByName("kubernetes.io/portworx-volume")
  61. if err != nil {
  62. t.Errorf("Can't find the plugin by name")
  63. }
  64. if !volumetest.ContainsAccessMode(plug.GetAccessModes(), v1.ReadWriteOnce) {
  65. t.Errorf("Expected to support AccessModeTypes: %s", v1.ReadWriteOnce)
  66. }
  67. if !volumetest.ContainsAccessMode(plug.GetAccessModes(), v1.ReadWriteMany) {
  68. t.Errorf("Expected to support AccessModeTypes: %s", v1.ReadWriteMany)
  69. }
  70. if volumetest.ContainsAccessMode(plug.GetAccessModes(), v1.ReadOnlyMany) {
  71. t.Errorf("Expected not to support AccessModeTypes: %s", v1.ReadOnlyMany)
  72. }
  73. }
  74. type fakePortworxManager struct {
  75. attachCalled bool
  76. mountCalled bool
  77. }
  78. func (fake *fakePortworxManager) AttachVolume(b *portworxVolumeMounter, attachOptions map[string]string) (string, error) {
  79. fake.attachCalled = true
  80. return "", nil
  81. }
  82. func (fake *fakePortworxManager) DetachVolume(c *portworxVolumeUnmounter) error {
  83. return nil
  84. }
  85. func (fake *fakePortworxManager) MountVolume(b *portworxVolumeMounter, mountPath string) error {
  86. fake.mountCalled = true
  87. return nil
  88. }
  89. func (fake *fakePortworxManager) UnmountVolume(c *portworxVolumeUnmounter, mountPath string) error {
  90. return nil
  91. }
  92. func (fake *fakePortworxManager) CreateVolume(c *portworxVolumeProvisioner) (volumeID string, volumeSizeGB int64, labels map[string]string, err error) {
  93. labels = make(map[string]string)
  94. labels["fakeportworxmanager"] = "yes"
  95. return PortworxTestVolume, 100, labels, nil
  96. }
  97. func (fake *fakePortworxManager) DeleteVolume(cd *portworxVolumeDeleter) error {
  98. if cd.volumeID != PortworxTestVolume {
  99. return fmt.Errorf("Deleter got unexpected volume name: %s", cd.volumeID)
  100. }
  101. return nil
  102. }
  103. func (fake *fakePortworxManager) ResizeVolume(spec *volume.Spec, newSize resource.Quantity, volumeHost volume.VolumeHost) error {
  104. return nil
  105. }
  106. func TestPlugin(t *testing.T) {
  107. tmpDir, err := utiltesting.MkTmpdir("portworxVolumeTest")
  108. if err != nil {
  109. t.Fatalf("can't make a temp dir: %v", err)
  110. }
  111. defer os.RemoveAll(tmpDir)
  112. plugMgr := volume.VolumePluginMgr{}
  113. plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, volumetest.NewFakeVolumeHost(tmpDir, nil, nil))
  114. plug, err := plugMgr.FindPluginByName("kubernetes.io/portworx-volume")
  115. if err != nil {
  116. t.Errorf("Can't find the plugin by name")
  117. }
  118. spec := &v1.Volume{
  119. Name: "vol1",
  120. VolumeSource: v1.VolumeSource{
  121. PortworxVolume: &v1.PortworxVolumeSource{
  122. VolumeID: PortworxTestVolume,
  123. FSType: "ext4",
  124. },
  125. },
  126. }
  127. fakeManager := &fakePortworxManager{}
  128. // Test Mounter
  129. fakeMounter := &mount.FakeMounter{}
  130. mounter, err := plug.(*portworxVolumePlugin).newMounterInternal(volume.NewSpecFromVolume(spec), types.UID("poduid"), fakeManager, fakeMounter)
  131. if err != nil {
  132. t.Errorf("Failed to make a new Mounter: %v", err)
  133. }
  134. if mounter == nil {
  135. t.Errorf("Got a nil Mounter")
  136. }
  137. volPath := filepath.Join(tmpDir, "pods/poduid/volumes/kubernetes.io~portworx-volume/vol1")
  138. path := mounter.GetPath()
  139. if path != volPath {
  140. t.Errorf("Got unexpected path: %s", path)
  141. }
  142. if err := mounter.SetUp(volume.MounterArgs{}); err != nil {
  143. t.Errorf("Expected success, got: %v", err)
  144. }
  145. if _, err := os.Stat(path); err != nil {
  146. if os.IsNotExist(err) {
  147. t.Errorf("SetUp() failed, volume path not created: %s", path)
  148. } else {
  149. t.Errorf("SetUp() failed: %v", err)
  150. }
  151. }
  152. if !fakeManager.attachCalled {
  153. t.Errorf("Attach watch not called")
  154. }
  155. if !fakeManager.mountCalled {
  156. t.Errorf("Mount watch not called")
  157. }
  158. // Test Unmounter
  159. fakeManager = &fakePortworxManager{}
  160. unmounter, err := plug.(*portworxVolumePlugin).newUnmounterInternal("vol1", types.UID("poduid"), fakeManager, fakeMounter)
  161. if err != nil {
  162. t.Errorf("Failed to make a new Unmounter: %v", err)
  163. }
  164. if unmounter == nil {
  165. t.Errorf("Got a nil Unmounter")
  166. }
  167. if err := unmounter.TearDown(); err != nil {
  168. t.Errorf("Expected success, got: %v", err)
  169. }
  170. // Test Provisioner
  171. options := volume.VolumeOptions{
  172. PVC: volumetest.CreateTestPVC("100Gi", []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce}),
  173. PersistentVolumeReclaimPolicy: v1.PersistentVolumeReclaimDelete,
  174. }
  175. provisioner, err := plug.(*portworxVolumePlugin).newProvisionerInternal(options, &fakePortworxManager{})
  176. if err != nil {
  177. t.Errorf("Error creating a new provisioner:%v", err)
  178. }
  179. persistentSpec, err := provisioner.Provision(nil, nil)
  180. if err != nil {
  181. t.Errorf("Provision() failed: %v", err)
  182. }
  183. if persistentSpec.Spec.PersistentVolumeSource.PortworxVolume.VolumeID != PortworxTestVolume {
  184. t.Errorf("Provision() returned unexpected volume ID: %s", persistentSpec.Spec.PersistentVolumeSource.PortworxVolume.VolumeID)
  185. }
  186. cap := persistentSpec.Spec.Capacity[v1.ResourceStorage]
  187. size := cap.Value()
  188. if size != 100*1024*1024*1024 {
  189. t.Errorf("Provision() returned unexpected volume size: %v", size)
  190. }
  191. if persistentSpec.Labels["fakeportworxmanager"] != "yes" {
  192. t.Errorf("Provision() returned unexpected labels: %v", persistentSpec.Labels)
  193. }
  194. // Test Deleter
  195. volSpec := &volume.Spec{
  196. PersistentVolume: persistentSpec,
  197. }
  198. deleter, err := plug.(*portworxVolumePlugin).newDeleterInternal(volSpec, &fakePortworxManager{})
  199. if err != nil {
  200. t.Errorf("Error creating a new Deleter:%v", err)
  201. }
  202. err = deleter.Delete()
  203. if err != nil {
  204. t.Errorf("Deleter() failed: %v", err)
  205. }
  206. }