storageos_test.go 12 KB

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