storageos_util_test.go 6.2 KB

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