cinder_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. // +build !providerless
  2. /*
  3. Copyright 2015 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package cinder
  15. import (
  16. "fmt"
  17. "os"
  18. "path/filepath"
  19. "testing"
  20. "time"
  21. v1 "k8s.io/api/core/v1"
  22. "k8s.io/apimachinery/pkg/types"
  23. utiltesting "k8s.io/client-go/util/testing"
  24. "k8s.io/utils/mount"
  25. "k8s.io/kubernetes/pkg/volume"
  26. volumetest "k8s.io/kubernetes/pkg/volume/testing"
  27. "k8s.io/kubernetes/pkg/volume/util"
  28. "k8s.io/legacy-cloud-providers/openstack"
  29. )
  30. func TestCanSupport(t *testing.T) {
  31. tmpDir, err := utiltesting.MkTmpdir("cinderTest")
  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(t, tmpDir, nil, nil))
  38. plug, err := plugMgr.FindPluginByName("kubernetes.io/cinder")
  39. if err != nil {
  40. t.Errorf("Can't find the plugin by name")
  41. }
  42. if plug.GetPluginName() != "kubernetes.io/cinder" {
  43. t.Errorf("Wrong name: %s", plug.GetPluginName())
  44. }
  45. if !plug.CanSupport(&volume.Spec{Volume: &v1.Volume{VolumeSource: v1.VolumeSource{Cinder: &v1.CinderVolumeSource{}}}}) {
  46. t.Errorf("Expected true")
  47. }
  48. if !plug.CanSupport(&volume.Spec{PersistentVolume: &v1.PersistentVolume{Spec: v1.PersistentVolumeSpec{PersistentVolumeSource: v1.PersistentVolumeSource{Cinder: &v1.CinderPersistentVolumeSource{}}}}}) {
  49. t.Errorf("Expected true")
  50. }
  51. }
  52. type fakePDManager struct {
  53. // How long should AttachDisk/DetachDisk take - we need slower AttachDisk in a test.
  54. attachDetachDuration time.Duration
  55. }
  56. func getFakeDeviceName(host volume.VolumeHost, pdName string) string {
  57. return filepath.Join(host.GetPluginDir(cinderVolumePluginName), "device", pdName)
  58. }
  59. // Real Cinder AttachDisk attaches a cinder volume. If it is not yet mounted,
  60. // it mounts it to globalPDPath.
  61. // We create a dummy directory (="device") and bind-mount it to globalPDPath
  62. func (fake *fakePDManager) AttachDisk(b *cinderVolumeMounter, globalPDPath string) error {
  63. globalPath := makeGlobalPDName(b.plugin.host, b.pdName)
  64. fakeDeviceName := getFakeDeviceName(b.plugin.host, b.pdName)
  65. err := os.MkdirAll(fakeDeviceName, 0750)
  66. if err != nil {
  67. return err
  68. }
  69. // Attaching a Cinder volume can be slow...
  70. time.Sleep(fake.attachDetachDuration)
  71. // The volume is "attached", bind-mount it if it's not mounted yet.
  72. notmnt, err := b.mounter.IsLikelyNotMountPoint(globalPath)
  73. if err != nil {
  74. if os.IsNotExist(err) {
  75. if err := os.MkdirAll(globalPath, 0750); err != nil {
  76. return err
  77. }
  78. notmnt = true
  79. } else {
  80. return err
  81. }
  82. }
  83. if notmnt {
  84. err = b.mounter.Mount(fakeDeviceName, globalPath, "", []string{"bind"})
  85. if err != nil {
  86. return err
  87. }
  88. }
  89. return nil
  90. }
  91. func (fake *fakePDManager) DetachDisk(c *cinderVolumeUnmounter) error {
  92. globalPath := makeGlobalPDName(c.plugin.host, c.pdName)
  93. fakeDeviceName := getFakeDeviceName(c.plugin.host, c.pdName)
  94. // unmount the bind-mount - should be fast
  95. err := c.mounter.Unmount(globalPath)
  96. if err != nil {
  97. return err
  98. }
  99. // "Detach" the fake "device"
  100. err = os.RemoveAll(fakeDeviceName)
  101. if err != nil {
  102. return err
  103. }
  104. return nil
  105. }
  106. func (fake *fakePDManager) CreateVolume(c *cinderVolumeProvisioner, node *v1.Node, allowedTopologies []v1.TopologySelectorTerm) (volumeID string, volumeSizeGB int, labels map[string]string, fstype string, err error) {
  107. labels = make(map[string]string)
  108. labels[v1.LabelZoneFailureDomain] = "nova"
  109. return "test-volume-name", 1, labels, "", nil
  110. }
  111. func (fake *fakePDManager) DeleteVolume(cd *cinderVolumeDeleter) error {
  112. if cd.pdName != "test-volume-name" {
  113. return fmt.Errorf("Deleter got unexpected volume name: %s", cd.pdName)
  114. }
  115. return nil
  116. }
  117. func TestPlugin(t *testing.T) {
  118. tmpDir, err := utiltesting.MkTmpdir("cinderTest")
  119. if err != nil {
  120. t.Fatalf("can't make a temp dir: %v", err)
  121. }
  122. defer os.RemoveAll(tmpDir)
  123. plugMgr := volume.VolumePluginMgr{}
  124. plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, volumetest.NewFakeVolumeHost(t, tmpDir, nil, nil))
  125. plug, err := plugMgr.FindPluginByName("kubernetes.io/cinder")
  126. if err != nil {
  127. t.Errorf("Can't find the plugin by name")
  128. }
  129. spec := &v1.Volume{
  130. Name: "vol1",
  131. VolumeSource: v1.VolumeSource{
  132. Cinder: &v1.CinderVolumeSource{
  133. VolumeID: "pd",
  134. FSType: "ext4",
  135. },
  136. },
  137. }
  138. mounter, err := plug.(*cinderPlugin).newMounterInternal(volume.NewSpecFromVolume(spec), types.UID("poduid"), &fakePDManager{0}, mount.NewFakeMounter(nil))
  139. if err != nil {
  140. t.Errorf("Failed to make a new Mounter: %v", err)
  141. }
  142. if mounter == nil {
  143. t.Errorf("Got a nil Mounter")
  144. }
  145. volPath := filepath.Join(tmpDir, "pods/poduid/volumes/kubernetes.io~cinder/vol1")
  146. path := mounter.GetPath()
  147. if path != volPath {
  148. t.Errorf("Got unexpected path: %s", path)
  149. }
  150. if err := mounter.SetUp(volume.MounterArgs{}); err != nil {
  151. t.Errorf("Expected success, got: %v", err)
  152. }
  153. if _, err := os.Stat(path); err != nil {
  154. if os.IsNotExist(err) {
  155. t.Errorf("SetUp() failed, volume path not created: %s", path)
  156. } else {
  157. t.Errorf("SetUp() failed: %v", err)
  158. }
  159. }
  160. unmounter, err := plug.(*cinderPlugin).newUnmounterInternal("vol1", types.UID("poduid"), &fakePDManager{0}, mount.NewFakeMounter(nil))
  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. if _, err := os.Stat(path); err == nil {
  171. t.Errorf("TearDown() failed, volume path still exists: %s", path)
  172. } else if !os.IsNotExist(err) {
  173. t.Errorf("TearDown() failed: %v", err)
  174. }
  175. // Test Provisioner
  176. options := volume.VolumeOptions{
  177. PVC: volumetest.CreateTestPVC("100Mi", []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce}),
  178. PersistentVolumeReclaimPolicy: v1.PersistentVolumeReclaimDelete,
  179. }
  180. provisioner, err := plug.(*cinderPlugin).newProvisionerInternal(options, &fakePDManager{0})
  181. if err != nil {
  182. t.Errorf("ProvisionerInternal() failed: %v", err)
  183. }
  184. persistentSpec, err := provisioner.Provision(nil, nil)
  185. if err != nil {
  186. t.Errorf("Provision() failed: %v", err)
  187. }
  188. if persistentSpec.Spec.PersistentVolumeSource.Cinder.VolumeID != "test-volume-name" {
  189. t.Errorf("Provision() returned unexpected volume ID: %s", persistentSpec.Spec.PersistentVolumeSource.Cinder.VolumeID)
  190. }
  191. cap := persistentSpec.Spec.Capacity[v1.ResourceStorage]
  192. size := cap.Value()
  193. if size != 1024*1024*1024 {
  194. t.Errorf("Provision() returned unexpected volume size: %v", size)
  195. }
  196. // check nodeaffinity members
  197. if persistentSpec.Spec.NodeAffinity == nil {
  198. t.Errorf("Provision() returned unexpected nil NodeAffinity")
  199. }
  200. if persistentSpec.Spec.NodeAffinity.Required == nil {
  201. t.Errorf("Provision() returned unexpected nil NodeAffinity.Required")
  202. }
  203. n := len(persistentSpec.Spec.NodeAffinity.Required.NodeSelectorTerms)
  204. if n != 1 {
  205. t.Errorf("Provision() returned unexpected number of NodeSelectorTerms %d. Expected %d", n, 1)
  206. }
  207. n = len(persistentSpec.Spec.NodeAffinity.Required.NodeSelectorTerms[0].MatchExpressions)
  208. if n != 1 {
  209. t.Errorf("Provision() returned unexpected number of MatchExpressions %d. Expected %d", n, 1)
  210. }
  211. req := persistentSpec.Spec.NodeAffinity.Required.NodeSelectorTerms[0].MatchExpressions[0]
  212. if req.Key != v1.LabelZoneFailureDomain {
  213. t.Errorf("Provision() returned unexpected requirement key in NodeAffinity %v", req.Key)
  214. }
  215. if req.Operator != v1.NodeSelectorOpIn {
  216. t.Errorf("Provision() returned unexpected requirement operator in NodeAffinity %v", req.Operator)
  217. }
  218. if len(req.Values) != 1 || req.Values[0] != "nova" {
  219. t.Errorf("Provision() returned unexpected requirement value in NodeAffinity %v", req.Values)
  220. }
  221. // Test Deleter
  222. volSpec := &volume.Spec{
  223. PersistentVolume: persistentSpec,
  224. }
  225. deleter, err := plug.(*cinderPlugin).newDeleterInternal(volSpec, &fakePDManager{0})
  226. if err != nil {
  227. t.Errorf("DeleterInternal() failed: %v", err)
  228. }
  229. err = deleter.Delete()
  230. if err != nil {
  231. t.Errorf("Deleter() failed: %v", err)
  232. }
  233. }
  234. func TestGetVolumeLimit(t *testing.T) {
  235. tmpDir, err := utiltesting.MkTmpdir("cinderTest")
  236. if err != nil {
  237. t.Fatalf("can't make a temp dir: %v", err)
  238. }
  239. cloud, err := getOpenstackCloudProvider()
  240. if err != nil {
  241. t.Fatalf("can not instantiate openstack cloudprovider : %v", err)
  242. }
  243. defer os.RemoveAll(tmpDir)
  244. plugMgr := volume.VolumePluginMgr{}
  245. volumeHost := volumetest.NewFakeVolumeHostWithCloudProvider(t, tmpDir, nil, nil, cloud)
  246. plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, volumeHost)
  247. plug, err := plugMgr.FindPluginByName("kubernetes.io/cinder")
  248. if err != nil {
  249. t.Fatalf("Can't find the plugin by name")
  250. }
  251. attachablePlugin, ok := plug.(volume.VolumePluginWithAttachLimits)
  252. if !ok {
  253. t.Fatalf("plugin %s is not of attachable type", plug.GetPluginName())
  254. }
  255. limits, err := attachablePlugin.GetVolumeLimits()
  256. if err != nil {
  257. t.Errorf("error fetching limits : %v", err)
  258. }
  259. if len(limits) == 0 {
  260. t.Fatalf("expecting limit from openstack got none")
  261. }
  262. limit, _ := limits[util.CinderVolumeLimitKey]
  263. if limit != 10 {
  264. t.Fatalf("expected volume limit to be 10 got %d", limit)
  265. }
  266. }
  267. func getOpenstackCloudProvider() (*openstack.OpenStack, error) {
  268. cfg := getOpenstackConfig()
  269. return openstack.NewFakeOpenStackCloud(cfg)
  270. }
  271. func getOpenstackConfig() openstack.Config {
  272. cfg := openstack.Config{
  273. Global: struct {
  274. AuthURL string `gcfg:"auth-url"`
  275. Username string
  276. UserID string `gcfg:"user-id"`
  277. Password string
  278. TenantID string `gcfg:"tenant-id"`
  279. TenantName string `gcfg:"tenant-name"`
  280. TrustID string `gcfg:"trust-id"`
  281. DomainID string `gcfg:"domain-id"`
  282. DomainName string `gcfg:"domain-name"`
  283. Region string
  284. CAFile string `gcfg:"ca-file"`
  285. SecretName string `gcfg:"secret-name"`
  286. SecretNamespace string `gcfg:"secret-namespace"`
  287. KubeconfigPath string `gcfg:"kubeconfig-path"`
  288. }{
  289. Username: "user",
  290. Password: "pass",
  291. TenantID: "foobar",
  292. DomainID: "2a73b8f597c04551a0fdc8e95544be8a",
  293. DomainName: "local",
  294. AuthURL: "http://auth.url",
  295. UserID: "user",
  296. },
  297. BlockStorage: openstack.BlockStorageOpts{
  298. NodeVolumeAttachLimit: 10,
  299. },
  300. }
  301. return cfg
  302. }