PageRenderTime 74ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/pkg/volume/host_path/host_path_test.go

https://gitlab.com/0072016/Facebook-php
Go | 273 lines | 224 code | 31 blank | 18 comment | 70 complexity | 935a88e154b0b4afe2e0b43bc388db82 MD5 | raw file
  1. // +build linux
  2. /*
  3. Copyright 2014 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 host_path
  15. import (
  16. "fmt"
  17. "os"
  18. "testing"
  19. "k8s.io/kubernetes/pkg/api"
  20. "k8s.io/kubernetes/pkg/api/resource"
  21. "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
  22. "k8s.io/kubernetes/pkg/types"
  23. "k8s.io/kubernetes/pkg/util"
  24. "k8s.io/kubernetes/pkg/volume"
  25. volumetest "k8s.io/kubernetes/pkg/volume/testing"
  26. )
  27. func TestCanSupport(t *testing.T) {
  28. plugMgr := volume.VolumePluginMgr{}
  29. plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), volumetest.NewFakeVolumeHost("fake", nil, nil, "" /* rootContext */))
  30. plug, err := plugMgr.FindPluginByName("kubernetes.io/host-path")
  31. if err != nil {
  32. t.Errorf("Can't find the plugin by name")
  33. }
  34. if plug.GetPluginName() != "kubernetes.io/host-path" {
  35. t.Errorf("Wrong name: %s", plug.GetPluginName())
  36. }
  37. if !plug.CanSupport(&volume.Spec{Volume: &api.Volume{VolumeSource: api.VolumeSource{HostPath: &api.HostPathVolumeSource{}}}}) {
  38. t.Errorf("Expected true")
  39. }
  40. if !plug.CanSupport(&volume.Spec{PersistentVolume: &api.PersistentVolume{Spec: api.PersistentVolumeSpec{PersistentVolumeSource: api.PersistentVolumeSource{HostPath: &api.HostPathVolumeSource{}}}}}) {
  41. t.Errorf("Expected true")
  42. }
  43. if plug.CanSupport(&volume.Spec{Volume: &api.Volume{VolumeSource: api.VolumeSource{}}}) {
  44. t.Errorf("Expected false")
  45. }
  46. }
  47. func TestGetAccessModes(t *testing.T) {
  48. plugMgr := volume.VolumePluginMgr{}
  49. plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), volumetest.NewFakeVolumeHost("/tmp/fake", nil, nil, "" /* rootContext */))
  50. plug, err := plugMgr.FindPersistentPluginByName("kubernetes.io/host-path")
  51. if err != nil {
  52. t.Errorf("Can't find the plugin by name")
  53. }
  54. if len(plug.GetAccessModes()) != 1 || plug.GetAccessModes()[0] != api.ReadWriteOnce {
  55. t.Errorf("Expected %s PersistentVolumeAccessMode", api.ReadWriteOnce)
  56. }
  57. }
  58. func TestRecycler(t *testing.T) {
  59. plugMgr := volume.VolumePluginMgr{}
  60. pluginHost := volumetest.NewFakeVolumeHost("/tmp/fake", nil, nil, "" /* rootContext */)
  61. plugMgr.InitPlugins([]volume.VolumePlugin{&hostPathPlugin{nil, volumetest.NewFakeRecycler, nil, nil, volume.VolumeConfig{}}}, pluginHost)
  62. spec := &volume.Spec{PersistentVolume: &api.PersistentVolume{Spec: api.PersistentVolumeSpec{PersistentVolumeSource: api.PersistentVolumeSource{HostPath: &api.HostPathVolumeSource{Path: "/foo"}}}}}
  63. plug, err := plugMgr.FindRecyclablePluginBySpec(spec)
  64. if err != nil {
  65. t.Errorf("Can't find the plugin by name")
  66. }
  67. recycler, err := plug.NewRecycler("pv-name", spec)
  68. if err != nil {
  69. t.Errorf("Failed to make a new Recyler: %v", err)
  70. }
  71. if recycler.GetPath() != spec.PersistentVolume.Spec.HostPath.Path {
  72. t.Errorf("Expected %s but got %s", spec.PersistentVolume.Spec.HostPath.Path, recycler.GetPath())
  73. }
  74. if err := recycler.Recycle(); err != nil {
  75. t.Errorf("Mock Recycler expected to return nil but got %s", err)
  76. }
  77. }
  78. func TestDeleter(t *testing.T) {
  79. // Deleter has a hard-coded regex for "/tmp".
  80. tempPath := fmt.Sprintf("/tmp/hostpath/%s", util.NewUUID())
  81. defer os.RemoveAll(tempPath)
  82. err := os.MkdirAll(tempPath, 0750)
  83. if err != nil {
  84. t.Fatalf("Failed to create tmp directory for deleter: %v", err)
  85. }
  86. plugMgr := volume.VolumePluginMgr{}
  87. plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), volumetest.NewFakeVolumeHost("/tmp/fake", nil, nil, "" /* rootContext */))
  88. spec := &volume.Spec{PersistentVolume: &api.PersistentVolume{Spec: api.PersistentVolumeSpec{PersistentVolumeSource: api.PersistentVolumeSource{HostPath: &api.HostPathVolumeSource{Path: tempPath}}}}}
  89. plug, err := plugMgr.FindDeletablePluginBySpec(spec)
  90. if err != nil {
  91. t.Errorf("Can't find the plugin by name")
  92. }
  93. deleter, err := plug.NewDeleter(spec)
  94. if err != nil {
  95. t.Errorf("Failed to make a new Deleter: %v", err)
  96. }
  97. if deleter.GetPath() != tempPath {
  98. t.Errorf("Expected %s but got %s", tempPath, deleter.GetPath())
  99. }
  100. if err := deleter.Delete(); err != nil {
  101. t.Errorf("Mock Recycler expected to return nil but got %s", err)
  102. }
  103. if exists, _ := util.FileExists("foo"); exists {
  104. t.Errorf("Temp path expected to be deleted, but was found at %s", tempPath)
  105. }
  106. }
  107. func TestDeleterTempDir(t *testing.T) {
  108. tests := map[string]struct {
  109. expectedFailure bool
  110. path string
  111. }{
  112. "just-tmp": {true, "/tmp"},
  113. "not-tmp": {true, "/nottmp"},
  114. "good-tmp": {false, "/tmp/scratch"},
  115. }
  116. for name, test := range tests {
  117. plugMgr := volume.VolumePluginMgr{}
  118. plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), volumetest.NewFakeVolumeHost("/tmp/fake", nil, nil, "" /* rootContext */))
  119. spec := &volume.Spec{PersistentVolume: &api.PersistentVolume{Spec: api.PersistentVolumeSpec{PersistentVolumeSource: api.PersistentVolumeSource{HostPath: &api.HostPathVolumeSource{Path: test.path}}}}}
  120. plug, _ := plugMgr.FindDeletablePluginBySpec(spec)
  121. deleter, _ := plug.NewDeleter(spec)
  122. err := deleter.Delete()
  123. if err == nil && test.expectedFailure {
  124. t.Errorf("Expected failure for test '%s' but got nil err", name)
  125. }
  126. if err != nil && !test.expectedFailure {
  127. t.Errorf("Unexpected failure for test '%s': %v", name, err)
  128. }
  129. }
  130. }
  131. func TestProvisioner(t *testing.T) {
  132. tempPath := fmt.Sprintf("/tmp/hostpath/%s", util.NewUUID())
  133. defer os.RemoveAll(tempPath)
  134. err := os.MkdirAll(tempPath, 0750)
  135. plugMgr := volume.VolumePluginMgr{}
  136. plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), volumetest.NewFakeVolumeHost("/tmp/fake", nil, nil, "" /* rootContext */))
  137. spec := &volume.Spec{PersistentVolume: &api.PersistentVolume{Spec: api.PersistentVolumeSpec{PersistentVolumeSource: api.PersistentVolumeSource{HostPath: &api.HostPathVolumeSource{Path: tempPath}}}}}
  138. plug, err := plugMgr.FindCreatablePluginBySpec(spec)
  139. if err != nil {
  140. t.Errorf("Can't find the plugin by name")
  141. }
  142. creater, err := plug.NewProvisioner(volume.VolumeOptions{Capacity: resource.MustParse("1Gi"), PersistentVolumeReclaimPolicy: api.PersistentVolumeReclaimDelete})
  143. if err != nil {
  144. t.Errorf("Failed to make a new Provisioner: %v", err)
  145. }
  146. pv, err := creater.Provision()
  147. if err != nil {
  148. t.Errorf("Unexpected error creating volume: %v", err)
  149. }
  150. if pv.Spec.HostPath.Path == "" {
  151. t.Errorf("Expected pv.Spec.HostPath.Path to not be empty: %#v", pv)
  152. }
  153. expectedCapacity := resource.NewQuantity(1*1024*1024*1024, resource.BinarySI)
  154. actualCapacity := pv.Spec.Capacity[api.ResourceStorage]
  155. expectedAmt := expectedCapacity.Value()
  156. actualAmt := actualCapacity.Value()
  157. if expectedAmt != actualAmt {
  158. t.Errorf("Expected capacity %+v but got %+v", expectedAmt, actualAmt)
  159. }
  160. if pv.Spec.PersistentVolumeReclaimPolicy != api.PersistentVolumeReclaimDelete {
  161. t.Errorf("Expected reclaim policy %+v but got %+v", api.PersistentVolumeReclaimDelete, pv.Spec.PersistentVolumeReclaimPolicy)
  162. }
  163. os.RemoveAll(pv.Spec.HostPath.Path)
  164. }
  165. func TestPlugin(t *testing.T) {
  166. plugMgr := volume.VolumePluginMgr{}
  167. plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), volumetest.NewFakeVolumeHost("fake", nil, nil, "" /* rootContext */))
  168. plug, err := plugMgr.FindPluginByName("kubernetes.io/host-path")
  169. if err != nil {
  170. t.Errorf("Can't find the plugin by name")
  171. }
  172. spec := &api.Volume{
  173. Name: "vol1",
  174. VolumeSource: api.VolumeSource{HostPath: &api.HostPathVolumeSource{Path: "/vol1"}},
  175. }
  176. pod := &api.Pod{ObjectMeta: api.ObjectMeta{UID: types.UID("poduid")}}
  177. mounter, err := plug.NewMounter(volume.NewSpecFromVolume(spec), pod, volume.VolumeOptions{})
  178. if err != nil {
  179. t.Errorf("Failed to make a new Mounter: %v", err)
  180. }
  181. if mounter == nil {
  182. t.Errorf("Got a nil Mounter")
  183. }
  184. path := mounter.GetPath()
  185. if path != "/vol1" {
  186. t.Errorf("Got unexpected path: %s", path)
  187. }
  188. if err := mounter.SetUp(nil); err != nil {
  189. t.Errorf("Expected success, got: %v", err)
  190. }
  191. unmounter, err := plug.NewUnmounter("vol1", types.UID("poduid"))
  192. if err != nil {
  193. t.Errorf("Failed to make a new Unmounter: %v", err)
  194. }
  195. if unmounter == nil {
  196. t.Errorf("Got a nil Unmounter")
  197. }
  198. if err := unmounter.TearDown(); err != nil {
  199. t.Errorf("Expected success, got: %v", err)
  200. }
  201. }
  202. func TestPersistentClaimReadOnlyFlag(t *testing.T) {
  203. pv := &api.PersistentVolume{
  204. ObjectMeta: api.ObjectMeta{
  205. Name: "pvA",
  206. },
  207. Spec: api.PersistentVolumeSpec{
  208. PersistentVolumeSource: api.PersistentVolumeSource{
  209. HostPath: &api.HostPathVolumeSource{Path: "foo"},
  210. },
  211. ClaimRef: &api.ObjectReference{
  212. Name: "claimA",
  213. },
  214. },
  215. }
  216. claim := &api.PersistentVolumeClaim{
  217. ObjectMeta: api.ObjectMeta{
  218. Name: "claimA",
  219. Namespace: "nsA",
  220. },
  221. Spec: api.PersistentVolumeClaimSpec{
  222. VolumeName: "pvA",
  223. },
  224. Status: api.PersistentVolumeClaimStatus{
  225. Phase: api.ClaimBound,
  226. },
  227. }
  228. client := fake.NewSimpleClientset(pv, claim)
  229. plugMgr := volume.VolumePluginMgr{}
  230. plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), volumetest.NewFakeVolumeHost("/tmp/fake", client, nil, "" /* rootContext */))
  231. plug, _ := plugMgr.FindPluginByName(hostPathPluginName)
  232. // readOnly bool is supplied by persistent-claim volume source when its mounter creates other volumes
  233. spec := volume.NewSpecFromPersistentVolume(pv, true)
  234. pod := &api.Pod{ObjectMeta: api.ObjectMeta{UID: types.UID("poduid")}}
  235. mounter, _ := plug.NewMounter(spec, pod, volume.VolumeOptions{})
  236. if !mounter.GetAttributes().ReadOnly {
  237. t.Errorf("Expected true for mounter.IsReadOnly")
  238. }
  239. }