strategy_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. Copyright 2019 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 csidriver
  14. import (
  15. "testing"
  16. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  17. "k8s.io/apimachinery/pkg/util/validation/field"
  18. genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
  19. "k8s.io/kubernetes/pkg/apis/storage"
  20. )
  21. func getValidCSIDriver(name string) *storage.CSIDriver {
  22. attachRequired := true
  23. podInfoOnMount := true
  24. return &storage.CSIDriver{
  25. ObjectMeta: metav1.ObjectMeta{
  26. Name: name,
  27. },
  28. Spec: storage.CSIDriverSpec{
  29. AttachRequired: &attachRequired,
  30. PodInfoOnMount: &podInfoOnMount,
  31. },
  32. }
  33. }
  34. func TestCSIDriverStrategy(t *testing.T) {
  35. ctx := genericapirequest.WithRequestInfo(genericapirequest.NewContext(), &genericapirequest.RequestInfo{
  36. APIGroup: "storage.k8s.io",
  37. APIVersion: "v1beta1",
  38. Resource: "csidrivers",
  39. })
  40. if Strategy.NamespaceScoped() {
  41. t.Errorf("CSIDriver must not be namespace scoped")
  42. }
  43. if Strategy.AllowCreateOnUpdate() {
  44. t.Errorf("CSIDriver should not allow create on update")
  45. }
  46. csiDriver := getValidCSIDriver("valid-csidriver")
  47. Strategy.PrepareForCreate(ctx, csiDriver)
  48. errs := Strategy.Validate(ctx, csiDriver)
  49. if len(errs) != 0 {
  50. t.Errorf("unexpected error validating %v", errs)
  51. }
  52. // Update of spec is disallowed
  53. newCSIDriver := csiDriver.DeepCopy()
  54. attachNotRequired := false
  55. newCSIDriver.Spec.AttachRequired = &attachNotRequired
  56. Strategy.PrepareForUpdate(ctx, newCSIDriver, csiDriver)
  57. errs = Strategy.ValidateUpdate(ctx, newCSIDriver, csiDriver)
  58. if len(errs) == 0 {
  59. t.Errorf("Expected a validation error")
  60. }
  61. }
  62. func TestCSIDriverValidation(t *testing.T) {
  63. attachRequired := true
  64. notAttachRequired := false
  65. podInfoOnMount := true
  66. notPodInfoOnMount := false
  67. tests := []struct {
  68. name string
  69. csiDriver *storage.CSIDriver
  70. expectError bool
  71. }{
  72. {
  73. "valid csidriver",
  74. getValidCSIDriver("foo"),
  75. false,
  76. },
  77. {
  78. "true PodInfoOnMount and AttachRequired",
  79. &storage.CSIDriver{
  80. ObjectMeta: metav1.ObjectMeta{
  81. Name: "foo",
  82. },
  83. Spec: storage.CSIDriverSpec{
  84. AttachRequired: &attachRequired,
  85. PodInfoOnMount: &podInfoOnMount,
  86. },
  87. },
  88. false,
  89. },
  90. {
  91. "false PodInfoOnMount and AttachRequired",
  92. &storage.CSIDriver{
  93. ObjectMeta: metav1.ObjectMeta{
  94. Name: "foo",
  95. },
  96. Spec: storage.CSIDriverSpec{
  97. AttachRequired: &notAttachRequired,
  98. PodInfoOnMount: &notPodInfoOnMount,
  99. },
  100. },
  101. false,
  102. },
  103. {
  104. "invalid driver name",
  105. &storage.CSIDriver{
  106. ObjectMeta: metav1.ObjectMeta{
  107. Name: "*foo#",
  108. },
  109. Spec: storage.CSIDriverSpec{
  110. AttachRequired: &attachRequired,
  111. PodInfoOnMount: &podInfoOnMount,
  112. },
  113. },
  114. true,
  115. },
  116. }
  117. for _, test := range tests {
  118. t.Run(test.name, func(t *testing.T) {
  119. testValidation := func(csiDriver *storage.CSIDriver, apiVersion string) field.ErrorList {
  120. ctx := genericapirequest.WithRequestInfo(genericapirequest.NewContext(), &genericapirequest.RequestInfo{
  121. APIGroup: "storage.k8s.io",
  122. APIVersion: "v1beta1",
  123. Resource: "csidrivers",
  124. })
  125. return Strategy.Validate(ctx, csiDriver)
  126. }
  127. betaErr := testValidation(test.csiDriver, "v1beta1")
  128. if len(betaErr) > 0 && !test.expectError {
  129. t.Errorf("Validation of v1beta1 object failed: %+v", betaErr)
  130. }
  131. if len(betaErr) == 0 && test.expectError {
  132. t.Errorf("Validation of v1beta1 object unexpectedly succeeded")
  133. }
  134. })
  135. }
  136. }