storageos_util_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. storageostypes "github.com/storageos/go-api/types"
  18. "k8s.io/api/core/v1"
  19. utiltesting "k8s.io/client-go/util/testing"
  20. "k8s.io/kubernetes/pkg/util/mount"
  21. "k8s.io/kubernetes/pkg/volume"
  22. volumetest "k8s.io/kubernetes/pkg/volume/testing"
  23. "testing"
  24. )
  25. var testAPISecretName = "storageos-api"
  26. var testVolName = "storageos-test-vol"
  27. var testPVName = "storageos-test-pv"
  28. var testNamespace = "storageos-test-namespace"
  29. var testSize = 1
  30. var testDesc = "testdescription"
  31. var testPool = "testpool"
  32. var testFSType = "ext2"
  33. var testVolUUID = "01c43d34-89f8-83d3-422b-43536a0f25e6"
  34. type fakeConfig struct {
  35. apiAddr string
  36. apiUser string
  37. apiPass string
  38. apiVersion string
  39. }
  40. func (c fakeConfig) GetAPIConfig() *storageosAPIConfig {
  41. return &storageosAPIConfig{
  42. apiAddr: "http://5.6.7.8:9999",
  43. apiUser: "abc",
  44. apiPass: "123",
  45. apiVersion: "10",
  46. }
  47. }
  48. func TestClient(t *testing.T) {
  49. util := storageosUtil{}
  50. cfg := fakeConfig{}
  51. err := util.NewAPI(cfg.GetAPIConfig())
  52. if err != nil {
  53. t.Fatalf("error getting api config: %v", err)
  54. }
  55. if util.api == nil {
  56. t.Errorf("client() unexpectedly returned nil")
  57. }
  58. }
  59. type fakeAPI struct{}
  60. func (f fakeAPI) Volume(namespace string, ref string) (*storageostypes.Volume, error) {
  61. if namespace == testNamespace && ref == testVolName {
  62. return &storageostypes.Volume{
  63. ID: "01c43d34-89f8-83d3-422b-43536a0f25e6",
  64. Name: ref,
  65. Pool: "default",
  66. Namespace: namespace,
  67. Size: 5,
  68. }, nil
  69. }
  70. return nil, fmt.Errorf("not found")
  71. }
  72. func (f fakeAPI) VolumeCreate(opts storageostypes.VolumeCreateOptions) (*storageostypes.Volume, error) {
  73. // Append a label from the api
  74. labels := opts.Labels
  75. labels["labelfromapi"] = "apilabel"
  76. return &storageostypes.Volume{
  77. ID: testVolUUID,
  78. Name: opts.Name,
  79. Namespace: opts.Namespace,
  80. Description: opts.Description,
  81. Pool: opts.Pool,
  82. Size: opts.Size,
  83. FSType: opts.FSType,
  84. Labels: labels,
  85. }, nil
  86. }
  87. func (f fakeAPI) VolumeMount(opts storageostypes.VolumeMountOptions) error {
  88. return nil
  89. }
  90. func (f fakeAPI) VolumeUnmount(opts storageostypes.VolumeUnmountOptions) error {
  91. return nil
  92. }
  93. func (f fakeAPI) VolumeDelete(opts storageostypes.DeleteOptions) error {
  94. return nil
  95. }
  96. func (f fakeAPI) Node(ref string) (*storageostypes.Node, error) {
  97. return &storageostypes.Node{}, nil
  98. }
  99. func TestCreateVolume(t *testing.T) {
  100. tmpDir, err := utiltesting.MkTmpdir("storageos_test")
  101. if err != nil {
  102. t.Fatalf("can't make a temp dir: %v", err)
  103. }
  104. defer os.RemoveAll(tmpDir)
  105. plugMgr := volume.VolumePluginMgr{}
  106. plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, volumetest.NewFakeVolumeHost(tmpDir, nil, nil))
  107. plug, _ := plugMgr.FindPluginByName("kubernetes.io/storageos")
  108. // Use real util with stubbed api
  109. util := &storageosUtil{}
  110. util.api = fakeAPI{}
  111. labels := map[string]string{
  112. "labelA": "valueA",
  113. "labelB": "valueB",
  114. }
  115. options := volume.VolumeOptions{
  116. PVName: testPVName,
  117. PVC: volumetest.CreateTestPVC(fmt.Sprintf("%dGi", testSize), []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce}),
  118. PersistentVolumeReclaimPolicy: v1.PersistentVolumeReclaimDelete,
  119. }
  120. provisioner := &storageosProvisioner{
  121. storageosMounter: &storageosMounter{
  122. storageos: &storageos{
  123. pvName: testPVName,
  124. volName: testVolName,
  125. volNamespace: testNamespace,
  126. sizeGB: testSize,
  127. pool: testPool,
  128. description: testDesc,
  129. fsType: testFSType,
  130. labels: labels,
  131. manager: util,
  132. plugin: plug.(*storageosPlugin),
  133. },
  134. },
  135. options: options,
  136. }
  137. vol, err := util.CreateVolume(provisioner)
  138. if err != nil {
  139. t.Errorf("CreateVolume() returned error: %v", err)
  140. }
  141. if vol == nil {
  142. t.Fatalf("CreateVolume() vol is empty")
  143. }
  144. if vol.ID == "" {
  145. t.Error("CreateVolume() vol ID is empty")
  146. }
  147. if vol.Name != testVolName {
  148. t.Errorf("CreateVolume() returned unexpected Name %s", vol.Name)
  149. }
  150. if vol.Namespace != testNamespace {
  151. t.Errorf("CreateVolume() returned unexpected Namespace %s", vol.Namespace)
  152. }
  153. if vol.Pool != testPool {
  154. t.Errorf("CreateVolume() returned unexpected Pool %s", vol.Pool)
  155. }
  156. if vol.FSType != testFSType {
  157. t.Errorf("CreateVolume() returned unexpected FSType %s", vol.FSType)
  158. }
  159. if vol.SizeGB != testSize {
  160. t.Errorf("CreateVolume() returned unexpected Size %d", vol.SizeGB)
  161. }
  162. if len(vol.Labels) == 0 {
  163. t.Error("CreateVolume() Labels are empty")
  164. } else {
  165. var val string
  166. var ok bool
  167. for k, v := range labels {
  168. if val, ok = vol.Labels[k]; !ok {
  169. t.Errorf("CreateVolume() Label %s not set", k)
  170. }
  171. if val != v {
  172. t.Errorf("CreateVolume() returned unexpected Label value %s", val)
  173. }
  174. }
  175. if val, ok = vol.Labels["labelfromapi"]; !ok {
  176. t.Error("CreateVolume() Label from api not set")
  177. }
  178. if val != "apilabel" {
  179. t.Errorf("CreateVolume() returned unexpected Label value %s", val)
  180. }
  181. }
  182. }
  183. func TestAttachVolume(t *testing.T) {
  184. tmpDir, err := utiltesting.MkTmpdir("storageos_test")
  185. if err != nil {
  186. t.Fatalf("can't make a temp dir: %v", err)
  187. }
  188. defer os.RemoveAll(tmpDir)
  189. plugMgr := volume.VolumePluginMgr{}
  190. plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, volumetest.NewFakeVolumeHost(tmpDir, nil, nil))
  191. plug, _ := plugMgr.FindPluginByName("kubernetes.io/storageos")
  192. // Use real util with stubbed api
  193. util := &storageosUtil{}
  194. util.api = fakeAPI{}
  195. mounter := &storageosMounter{
  196. storageos: &storageos{
  197. volName: testVolName,
  198. volNamespace: testNamespace,
  199. manager: util,
  200. mounter: &mount.FakeMounter{},
  201. plugin: plug.(*storageosPlugin),
  202. },
  203. deviceDir: tmpDir,
  204. }
  205. if err != nil {
  206. t.Errorf("Failed to make a new Mounter: %v", err)
  207. }
  208. if mounter == nil {
  209. t.Errorf("Got a nil Mounter")
  210. }
  211. }