plugins_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. Copyright 2015 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 volume
  14. import (
  15. "testing"
  16. "k8s.io/api/core/v1"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/apimachinery/pkg/types"
  19. )
  20. const testPluginName = "kubernetes.io/testPlugin"
  21. func TestSpecSourceConverters(t *testing.T) {
  22. v := &v1.Volume{
  23. Name: "foo",
  24. VolumeSource: v1.VolumeSource{EmptyDir: &v1.EmptyDirVolumeSource{}},
  25. }
  26. converted := NewSpecFromVolume(v)
  27. if converted.Volume.EmptyDir == nil {
  28. t.Errorf("Unexpected nil EmptyDir: %#v", converted)
  29. }
  30. if v.Name != converted.Name() {
  31. t.Errorf("Expected %v but got %v", converted.Name(), v.Name)
  32. }
  33. pv := &v1.PersistentVolume{
  34. ObjectMeta: metav1.ObjectMeta{Name: "bar"},
  35. Spec: v1.PersistentVolumeSpec{
  36. PersistentVolumeSource: v1.PersistentVolumeSource{AWSElasticBlockStore: &v1.AWSElasticBlockStoreVolumeSource{}},
  37. },
  38. }
  39. converted = NewSpecFromPersistentVolume(pv, false)
  40. if converted.PersistentVolume.Spec.AWSElasticBlockStore == nil {
  41. t.Errorf("Unexpected nil AWSElasticBlockStore: %#v", converted)
  42. }
  43. if pv.Name != converted.Name() {
  44. t.Errorf("Expected %v but got %v", converted.Name(), pv.Name)
  45. }
  46. }
  47. type testPlugins struct {
  48. }
  49. func (plugin *testPlugins) Init(host VolumeHost) error {
  50. return nil
  51. }
  52. func (plugin *testPlugins) GetPluginName() string {
  53. return testPluginName
  54. }
  55. func (plugin *testPlugins) GetVolumeName(spec *Spec) (string, error) {
  56. return "", nil
  57. }
  58. func (plugin *testPlugins) CanSupport(spec *Spec) bool {
  59. return true
  60. }
  61. func (plugin *testPlugins) IsMigratedToCSI() bool {
  62. return false
  63. }
  64. func (plugin *testPlugins) RequiresRemount() bool {
  65. return false
  66. }
  67. func (plugin *testPlugins) SupportsMountOption() bool {
  68. return false
  69. }
  70. func (plugin *testPlugins) SupportsBulkVolumeVerification() bool {
  71. return false
  72. }
  73. func (plugin *testPlugins) NewMounter(spec *Spec, podRef *v1.Pod, opts VolumeOptions) (Mounter, error) {
  74. return nil, nil
  75. }
  76. func (plugin *testPlugins) NewUnmounter(name string, podUID types.UID) (Unmounter, error) {
  77. return nil, nil
  78. }
  79. func (plugin *testPlugins) ConstructVolumeSpec(volumeName, mountPath string) (*Spec, error) {
  80. return nil, nil
  81. }
  82. func newTestPlugin() []VolumePlugin {
  83. return []VolumePlugin{&testPlugins{}}
  84. }
  85. func TestVolumePluginMgrFunc(t *testing.T) {
  86. vpm := VolumePluginMgr{}
  87. var prober DynamicPluginProber = nil // TODO (#51147) inject mock
  88. vpm.InitPlugins(newTestPlugin(), prober, nil)
  89. plug, err := vpm.FindPluginByName(testPluginName)
  90. if err != nil {
  91. t.Errorf("Can't find the plugin by name")
  92. }
  93. if plug.GetPluginName() != testPluginName {
  94. t.Errorf("Wrong name: %s", plug.GetPluginName())
  95. }
  96. plug, err = vpm.FindPluginBySpec(nil)
  97. if err == nil {
  98. t.Errorf("Should return error if volume spec is nil")
  99. }
  100. volumeSpec := &Spec{}
  101. plug, err = vpm.FindPluginBySpec(volumeSpec)
  102. if err != nil {
  103. t.Errorf("Should return test plugin if volume spec is not nil")
  104. }
  105. }
  106. func Test_ValidatePodTemplate(t *testing.T) {
  107. pod := &v1.Pod{
  108. Spec: v1.PodSpec{
  109. Volumes: []v1.Volume{
  110. {
  111. Name: "vol",
  112. VolumeSource: v1.VolumeSource{},
  113. },
  114. },
  115. },
  116. }
  117. var want error
  118. if got := ValidateRecyclerPodTemplate(pod); got != want {
  119. t.Errorf("isPodTemplateValid(%v) returned (%v), want (%v)", pod.String(), got.Error(), want)
  120. }
  121. // Check that the default recycle pod template is valid
  122. pod = NewPersistentVolumeRecyclerPodTemplate()
  123. want = nil
  124. if got := ValidateRecyclerPodTemplate(pod); got != want {
  125. t.Errorf("isPodTemplateValid(%v) returned (%v), want (%v)", pod.String(), got.Error(), want)
  126. }
  127. pod = &v1.Pod{
  128. Spec: v1.PodSpec{
  129. Containers: []v1.Container{
  130. {
  131. Name: "pv-recycler",
  132. },
  133. },
  134. },
  135. }
  136. // want = an error
  137. if got := ValidateRecyclerPodTemplate(pod); got == nil {
  138. t.Errorf("isPodTemplateValid(%v) returned (%v), want (%v)", pod.String(), got, "Error: pod specification does not contain any volume(s).")
  139. }
  140. }