storageos_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 storageos
  14. import (
  15. "fmt"
  16. "os"
  17. "path/filepath"
  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("storageos_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/storageos")
  37. if err != nil {
  38. t.Errorf("Can't find the plugin by name")
  39. }
  40. if plug.GetPluginName() != "kubernetes.io/storageos" {
  41. t.Errorf("Wrong name: %s", plug.GetPluginName())
  42. }
  43. if !plug.CanSupport(&volume.Spec{Volume: &v1.Volume{VolumeSource: v1.VolumeSource{StorageOS: &v1.StorageOSVolumeSource{}}}}) {
  44. t.Errorf("Expected true")
  45. }
  46. if !plug.CanSupport(&volume.Spec{PersistentVolume: &v1.PersistentVolume{Spec: v1.PersistentVolumeSpec{PersistentVolumeSource: v1.PersistentVolumeSource{StorageOS: &v1.StorageOSPersistentVolumeSource{}}}}}) {
  47. t.Errorf("Expected true")
  48. }
  49. }
  50. func TestGetAccessModes(t *testing.T) {
  51. tmpDir, err := utiltesting.MkTmpdir("storageos_test")
  52. if err != nil {
  53. t.Fatalf("error creating temp dir: %v", err)
  54. }
  55. defer os.RemoveAll(tmpDir)
  56. plugMgr := volume.VolumePluginMgr{}
  57. plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, volumetest.NewFakeVolumeHost(tmpDir, nil, nil))
  58. plug, err := plugMgr.FindPersistentPluginByName("kubernetes.io/storageos")
  59. if err != nil {
  60. t.Errorf("Can't find the plugin by name")
  61. }
  62. if !volumetest.ContainsAccessMode(plug.GetAccessModes(), v1.ReadWriteOnce) || !volumetest.ContainsAccessMode(plug.GetAccessModes(), v1.ReadOnlyMany) {
  63. t.Errorf("Expected two AccessModeTypes: %s and %s", v1.ReadWriteOnce, v1.ReadOnlyMany)
  64. }
  65. }
  66. type fakePDManager struct {
  67. api apiImplementer
  68. attachCalled bool
  69. attachDeviceCalled bool
  70. detachCalled bool
  71. mountCalled bool
  72. unmountCalled bool
  73. createCalled bool
  74. deleteCalled bool
  75. }
  76. func (fake *fakePDManager) NewAPI(apiCfg *storageosAPIConfig) error {
  77. fake.api = fakeAPI{}
  78. return nil
  79. }
  80. func (fake *fakePDManager) CreateVolume(p *storageosProvisioner) (*storageosVolume, error) {
  81. fake.createCalled = true
  82. labels := make(map[string]string)
  83. labels["fakepdmanager"] = "yes"
  84. return &storageosVolume{
  85. Name: "test-storageos-name",
  86. Namespace: "test-storageos-namespace",
  87. Pool: "test-storageos-pool",
  88. SizeGB: 100,
  89. Labels: labels,
  90. FSType: "ext2",
  91. }, nil
  92. }
  93. func (fake *fakePDManager) AttachVolume(b *storageosMounter) (string, error) {
  94. fake.attachCalled = true
  95. return "", nil
  96. }
  97. func (fake *fakePDManager) AttachDevice(b *storageosMounter, dir string) error {
  98. fake.attachDeviceCalled = true
  99. return nil
  100. }
  101. func (fake *fakePDManager) DetachVolume(b *storageosUnmounter, loopDevice string) error {
  102. fake.detachCalled = true
  103. return nil
  104. }
  105. func (fake *fakePDManager) MountVolume(b *storageosMounter, mntDevice, deviceMountPath string) error {
  106. fake.mountCalled = true
  107. return nil
  108. }
  109. func (fake *fakePDManager) UnmountVolume(b *storageosUnmounter) error {
  110. fake.unmountCalled = true
  111. return nil
  112. }
  113. func (fake *fakePDManager) DeleteVolume(d *storageosDeleter) error {
  114. fake.deleteCalled = true
  115. if d.volName != "test-storageos-name" {
  116. return fmt.Errorf("Deleter got unexpected volume name: %s", d.volName)
  117. }
  118. return nil
  119. }
  120. func (fake *fakePDManager) DeviceDir(mounter *storageosMounter) string {
  121. return defaultDeviceDir
  122. }
  123. func TestPlugin(t *testing.T) {
  124. tmpDir, err := utiltesting.MkTmpdir("storageos_test")
  125. if err != nil {
  126. t.Fatalf("can't make a temp dir: %v", err)
  127. }
  128. defer os.RemoveAll(tmpDir)
  129. plugMgr := volume.VolumePluginMgr{}
  130. plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, volumetest.NewFakeVolumeHost(tmpDir, nil, nil))
  131. plug, err := plugMgr.FindPluginByName("kubernetes.io/storageos")
  132. if err != nil {
  133. t.Errorf("Can't find the plugin by name")
  134. }
  135. secretName := "very-secret"
  136. spec := &v1.Volume{
  137. Name: "vol1-pvname",
  138. VolumeSource: v1.VolumeSource{
  139. StorageOS: &v1.StorageOSVolumeSource{
  140. VolumeName: "vol1",
  141. VolumeNamespace: "ns1",
  142. FSType: "ext3",
  143. SecretRef: &v1.LocalObjectReference{
  144. Name: secretName,
  145. },
  146. },
  147. },
  148. }
  149. client := fake.NewSimpleClientset()
  150. client.CoreV1().Secrets("default").Create(&v1.Secret{
  151. ObjectMeta: metav1.ObjectMeta{
  152. Name: secretName,
  153. Namespace: "default",
  154. },
  155. Type: "kubernetes.io/storageos",
  156. Data: map[string][]byte{
  157. "apiUsername": []byte("storageos"),
  158. "apiPassword": []byte("storageos"),
  159. "apiAddr": []byte("tcp://localhost:5705"),
  160. }})
  161. plug.(*storageosPlugin).host = volumetest.NewFakeVolumeHost(tmpDir, client, nil)
  162. // Test Mounter
  163. pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: types.UID("poduid"), Namespace: "default"}}
  164. fakeManager := &fakePDManager{}
  165. apiCfg, err := parsePodSecret(pod, secretName, plug.(*storageosPlugin).host.GetKubeClient())
  166. if err != nil {
  167. t.Errorf("Couldn't get secret from %v/%v", pod.Namespace, secretName)
  168. }
  169. mounter, err := plug.(*storageosPlugin).newMounterInternal(volume.NewSpecFromVolume(spec), pod, apiCfg, fakeManager, &mount.FakeMounter{}, mount.NewFakeExec(nil))
  170. if err != nil {
  171. t.Fatalf("Failed to make a new Mounter: %v", err)
  172. }
  173. if mounter == nil {
  174. t.Fatalf("Got a nil Mounter")
  175. }
  176. expectedPath := filepath.Join(tmpDir, "pods/poduid/volumes/kubernetes.io~storageos/vol1-pvname.ns1.vol1")
  177. volPath := mounter.GetPath()
  178. if volPath != expectedPath {
  179. t.Errorf("Expected path: '%s' got: '%s'", expectedPath, volPath)
  180. }
  181. if err := mounter.SetUp(volume.MounterArgs{}); err != nil {
  182. t.Errorf("Expected success, got: %v", err)
  183. }
  184. if _, err := os.Stat(volPath); err != nil {
  185. if os.IsNotExist(err) {
  186. t.Errorf("SetUp() failed, volume path not created: %s", volPath)
  187. } else {
  188. t.Errorf("SetUp() failed: %v", err)
  189. }
  190. }
  191. if !fakeManager.attachDeviceCalled {
  192. t.Errorf("AttachDevice not called")
  193. }
  194. if !fakeManager.attachCalled {
  195. t.Errorf("Attach not called")
  196. }
  197. if !fakeManager.mountCalled {
  198. t.Errorf("Mount not called")
  199. }
  200. // Test Unmounter
  201. fakeManager = &fakePDManager{}
  202. unmounter, err := plug.(*storageosPlugin).newUnmounterInternal("vol1-pvname", types.UID("poduid"), fakeManager, &mount.FakeMounter{}, mount.NewFakeExec(nil))
  203. if err != nil {
  204. t.Errorf("Failed to make a new Unmounter: %v", err)
  205. }
  206. if unmounter == nil {
  207. t.Errorf("Got a nil Unmounter")
  208. }
  209. volPath = unmounter.GetPath()
  210. if volPath != expectedPath {
  211. t.Errorf("Expected path: '%s' got: '%s'", expectedPath, volPath)
  212. }
  213. if err := unmounter.TearDown(); err != nil {
  214. t.Errorf("Expected success, got: %v", err)
  215. }
  216. if _, err := os.Stat(volPath); err == nil {
  217. t.Errorf("TearDown() failed, volume path still exists: %s", volPath)
  218. } else if !os.IsNotExist(err) {
  219. t.Errorf("TearDown() failed: %v", err)
  220. }
  221. if !fakeManager.unmountCalled {
  222. t.Errorf("Unmount not called")
  223. }
  224. if !fakeManager.detachCalled {
  225. t.Errorf("Detach not called")
  226. }
  227. // Test Provisioner
  228. fakeManager = &fakePDManager{}
  229. mountOptions := []string{"sync", "noatime"}
  230. options := volume.VolumeOptions{
  231. PVC: volumetest.CreateTestPVC("100Mi", []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce}),
  232. // PVName: "test-volume-name",
  233. PersistentVolumeReclaimPolicy: v1.PersistentVolumeReclaimDelete,
  234. Parameters: map[string]string{
  235. "VolumeNamespace": "test-volume-namespace",
  236. "adminSecretName": secretName,
  237. },
  238. MountOptions: mountOptions,
  239. }
  240. provisioner, err := plug.(*storageosPlugin).newProvisionerInternal(options, fakeManager)
  241. if err != nil {
  242. t.Errorf("newProvisionerInternal() failed: %v", err)
  243. }
  244. persistentSpec, err := provisioner.Provision(nil, nil)
  245. if err != nil {
  246. t.Fatalf("Provision() failed: %v", err)
  247. }
  248. if persistentSpec.Spec.PersistentVolumeSource.StorageOS.VolumeName != "test-storageos-name" {
  249. t.Errorf("Provision() returned unexpected volume Name: %s, expected test-storageos-name", persistentSpec.Spec.PersistentVolumeSource.StorageOS.VolumeName)
  250. }
  251. if persistentSpec.Spec.PersistentVolumeSource.StorageOS.VolumeNamespace != "test-storageos-namespace" {
  252. t.Errorf("Provision() returned unexpected volume Namespace: %s", persistentSpec.Spec.PersistentVolumeSource.StorageOS.VolumeNamespace)
  253. }
  254. cap := persistentSpec.Spec.Capacity[v1.ResourceStorage]
  255. size := cap.Value()
  256. if size != 100*1024*1024*1024 {
  257. t.Errorf("Provision() returned unexpected volume size: %v", size)
  258. }
  259. if persistentSpec.Spec.PersistentVolumeSource.StorageOS.FSType != "ext2" {
  260. t.Errorf("Provision() returned unexpected volume FSType: %s", persistentSpec.Spec.PersistentVolumeSource.StorageOS.FSType)
  261. }
  262. if len(persistentSpec.Spec.MountOptions) != 2 {
  263. t.Errorf("Provision() returned unexpected volume mount options: %v", persistentSpec.Spec.MountOptions)
  264. }
  265. if persistentSpec.Labels["fakepdmanager"] != "yes" {
  266. t.Errorf("Provision() returned unexpected labels: %v", persistentSpec.Labels)
  267. }
  268. if !fakeManager.createCalled {
  269. t.Errorf("Create not called")
  270. }
  271. // Test Deleter
  272. fakeManager = &fakePDManager{}
  273. volSpec := &volume.Spec{
  274. PersistentVolume: persistentSpec,
  275. }
  276. deleter, err := plug.(*storageosPlugin).newDeleterInternal(volSpec, apiCfg, fakeManager)
  277. if err != nil {
  278. t.Errorf("newDeleterInternal() failed: %v", err)
  279. }
  280. err = deleter.Delete()
  281. if err != nil {
  282. t.Errorf("Deleter() failed: %v", err)
  283. }
  284. if !fakeManager.deleteCalled {
  285. t.Errorf("Delete not called")
  286. }
  287. }
  288. func TestPersistentClaimReadOnlyFlag(t *testing.T) {
  289. tmpDir, err := utiltesting.MkTmpdir("storageos_test")
  290. if err != nil {
  291. t.Fatalf("error creating temp dir: %v", err)
  292. }
  293. defer os.RemoveAll(tmpDir)
  294. pv := &v1.PersistentVolume{
  295. ObjectMeta: metav1.ObjectMeta{
  296. Name: "pvA",
  297. },
  298. Spec: v1.PersistentVolumeSpec{
  299. PersistentVolumeSource: v1.PersistentVolumeSource{
  300. StorageOS: &v1.StorageOSPersistentVolumeSource{VolumeName: "pvA", VolumeNamespace: "vnsA", ReadOnly: false},
  301. },
  302. ClaimRef: &v1.ObjectReference{
  303. Name: "claimA",
  304. },
  305. },
  306. }
  307. claim := &v1.PersistentVolumeClaim{
  308. ObjectMeta: metav1.ObjectMeta{
  309. Name: "claimA",
  310. Namespace: "nsA",
  311. },
  312. Spec: v1.PersistentVolumeClaimSpec{
  313. VolumeName: "pvA",
  314. },
  315. Status: v1.PersistentVolumeClaimStatus{
  316. Phase: v1.ClaimBound,
  317. },
  318. }
  319. client := fake.NewSimpleClientset(pv, claim)
  320. plugMgr := volume.VolumePluginMgr{}
  321. plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, volumetest.NewFakeVolumeHost(tmpDir, client, nil))
  322. plug, _ := plugMgr.FindPluginByName(storageosPluginName)
  323. // readOnly bool is supplied by persistent-claim volume source when its mounter creates other volumes
  324. spec := volume.NewSpecFromPersistentVolume(pv, true)
  325. pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: "nsA", UID: types.UID("poduid")}}
  326. fakeManager := &fakePDManager{}
  327. fakeConfig := &fakeConfig{}
  328. apiCfg := fakeConfig.GetAPIConfig()
  329. mounter, err := plug.(*storageosPlugin).newMounterInternal(spec, pod, apiCfg, fakeManager, &mount.FakeMounter{}, mount.NewFakeExec(nil))
  330. if err != nil {
  331. t.Fatalf("error creating a new internal mounter:%v", err)
  332. }
  333. if !mounter.GetAttributes().ReadOnly {
  334. t.Errorf("Expected true for mounter.IsReadOnly")
  335. }
  336. }