pv_validation_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 validation
  14. import (
  15. "testing"
  16. "k8s.io/apimachinery/pkg/api/resource"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. api "k8s.io/kubernetes/pkg/apis/core"
  19. )
  20. func TestValidatePersistentVolumes(t *testing.T) {
  21. scenarios := map[string]struct {
  22. isExpectedFailure bool
  23. volume *api.PersistentVolume
  24. }{
  25. "volume with valid mount option for nfs": {
  26. isExpectedFailure: false,
  27. volume: testVolumeWithMountOption("good-nfs-mount-volume", "", "ro,nfsvers=3", api.PersistentVolumeSpec{
  28. Capacity: api.ResourceList{
  29. api.ResourceName(api.ResourceStorage): resource.MustParse("10G"),
  30. },
  31. AccessModes: []api.PersistentVolumeAccessMode{api.ReadWriteOnce},
  32. PersistentVolumeSource: api.PersistentVolumeSource{
  33. NFS: &api.NFSVolumeSource{Server: "localhost", Path: "/srv", ReadOnly: false},
  34. },
  35. }),
  36. },
  37. "volume with mount option for host path": {
  38. isExpectedFailure: true,
  39. volume: testVolumeWithMountOption("bad-hostpath-mount-volume", "", "ro,nfsvers=3", api.PersistentVolumeSpec{
  40. Capacity: api.ResourceList{
  41. api.ResourceName(api.ResourceStorage): resource.MustParse("10G"),
  42. },
  43. AccessModes: []api.PersistentVolumeAccessMode{api.ReadWriteOnce},
  44. PersistentVolumeSource: api.PersistentVolumeSource{
  45. HostPath: &api.HostPathVolumeSource{Path: "/a/.."},
  46. },
  47. }),
  48. },
  49. }
  50. for name, scenario := range scenarios {
  51. errs := ValidatePersistentVolume(scenario.volume)
  52. if len(errs) == 0 && scenario.isExpectedFailure {
  53. t.Errorf("Unexpected success for scenario: %s", name)
  54. }
  55. if len(errs) > 0 && !scenario.isExpectedFailure {
  56. t.Errorf("Unexpected failure for scenario: %s - %+v", name, errs)
  57. }
  58. }
  59. }
  60. func testVolumeWithMountOption(name string, namespace string, mountOptions string, spec api.PersistentVolumeSpec) *api.PersistentVolume {
  61. annotations := map[string]string{
  62. api.MountOptionAnnotation: mountOptions,
  63. }
  64. objMeta := metav1.ObjectMeta{
  65. Name: name,
  66. Annotations: annotations,
  67. }
  68. if namespace != "" {
  69. objMeta.Namespace = namespace
  70. }
  71. return &api.PersistentVolume{
  72. ObjectMeta: objMeta,
  73. Spec: spec,
  74. }
  75. }
  76. func TestValidatePathNoBacksteps(t *testing.T) {
  77. testCases := map[string]struct {
  78. path string
  79. expectedErr bool
  80. }{
  81. "valid path": {
  82. path: "/foo/bar",
  83. },
  84. "invalid path": {
  85. path: "/foo/bar/..",
  86. expectedErr: true,
  87. },
  88. }
  89. for name, tc := range testCases {
  90. err := ValidatePathNoBacksteps(tc.path)
  91. if err == nil && tc.expectedErr {
  92. t.Fatalf("expected test `%s` to return an error but it didnt", name)
  93. }
  94. if err != nil && !tc.expectedErr {
  95. t.Fatalf("expected test `%s` to return no error but got `%v`", name, err)
  96. }
  97. }
  98. }