strategy_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 csinode
  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 getValidCSINode(name string) *storage.CSINode {
  22. return &storage.CSINode{
  23. ObjectMeta: metav1.ObjectMeta{
  24. Name: name,
  25. },
  26. Spec: storage.CSINodeSpec{
  27. Drivers: []storage.CSINodeDriver{
  28. {
  29. Name: "valid-driver-name",
  30. NodeID: "valid-node",
  31. TopologyKeys: []string{"company.com/zone1", "company.com/zone2"},
  32. },
  33. },
  34. },
  35. }
  36. }
  37. func TestCSINodeStrategy(t *testing.T) {
  38. ctx := genericapirequest.WithRequestInfo(genericapirequest.NewContext(), &genericapirequest.RequestInfo{
  39. APIGroup: "storage.k8s.io",
  40. APIVersion: "v1beta1",
  41. Resource: "csinodes",
  42. })
  43. if Strategy.NamespaceScoped() {
  44. t.Errorf("CSINode must not be namespace scoped")
  45. }
  46. if Strategy.AllowCreateOnUpdate() {
  47. t.Errorf("CSINode should not allow create on update")
  48. }
  49. csiNode := getValidCSINode("valid-csinode")
  50. Strategy.PrepareForCreate(ctx, csiNode)
  51. errs := Strategy.Validate(ctx, csiNode)
  52. if len(errs) != 0 {
  53. t.Errorf("unexpected error validating %v", errs)
  54. }
  55. // Update of spec is allowed
  56. newCSINode := csiNode.DeepCopy()
  57. newCSINode.Spec.Drivers[0].NodeID = "valid-node-2"
  58. Strategy.PrepareForUpdate(ctx, newCSINode, csiNode)
  59. errs = Strategy.ValidateUpdate(ctx, newCSINode, csiNode)
  60. if len(errs) == 0 {
  61. t.Errorf("expected validation error")
  62. }
  63. }
  64. func TestCSINodeValidation(t *testing.T) {
  65. tests := []struct {
  66. name string
  67. csiNode *storage.CSINode
  68. expectError bool
  69. }{
  70. {
  71. "valid csinode",
  72. getValidCSINode("foo"),
  73. false,
  74. },
  75. {
  76. "invalid driver name",
  77. &storage.CSINode{
  78. ObjectMeta: metav1.ObjectMeta{
  79. Name: "foo",
  80. },
  81. Spec: storage.CSINodeSpec{
  82. Drivers: []storage.CSINodeDriver{
  83. {
  84. Name: "$csi-driver@",
  85. NodeID: "valid-node",
  86. TopologyKeys: []string{"company.com/zone1", "company.com/zone2"},
  87. },
  88. },
  89. },
  90. },
  91. true,
  92. },
  93. {
  94. "empty node id",
  95. &storage.CSINode{
  96. ObjectMeta: metav1.ObjectMeta{
  97. Name: "foo",
  98. },
  99. Spec: storage.CSINodeSpec{
  100. Drivers: []storage.CSINodeDriver{
  101. {
  102. Name: "valid-driver-name",
  103. NodeID: "",
  104. TopologyKeys: []string{"company.com/zone1", "company.com/zone2"},
  105. },
  106. },
  107. },
  108. },
  109. true,
  110. },
  111. {
  112. "invalid topology keys",
  113. &storage.CSINode{
  114. ObjectMeta: metav1.ObjectMeta{
  115. Name: "foo",
  116. },
  117. Spec: storage.CSINodeSpec{
  118. Drivers: []storage.CSINodeDriver{
  119. {
  120. Name: "valid-driver-name",
  121. NodeID: "valid-node",
  122. TopologyKeys: []string{"company.com/zone1", ""},
  123. },
  124. },
  125. },
  126. },
  127. true,
  128. },
  129. }
  130. for _, test := range tests {
  131. t.Run(test.name, func(t *testing.T) {
  132. testValidation := func(csiNode *storage.CSINode, apiVersion string) field.ErrorList {
  133. ctx := genericapirequest.WithRequestInfo(genericapirequest.NewContext(), &genericapirequest.RequestInfo{
  134. APIGroup: "storage.k8s.io",
  135. APIVersion: "v1beta1",
  136. Resource: "csinodes",
  137. })
  138. return Strategy.Validate(ctx, csiNode)
  139. }
  140. betaErr := testValidation(test.csiNode, "v1beta1")
  141. if len(betaErr) > 0 && !test.expectError {
  142. t.Errorf("Validation of v1beta1 object failed: %+v", betaErr)
  143. }
  144. if len(betaErr) == 0 && test.expectError {
  145. t.Errorf("Validation of v1beta1 object unexpectedly succeeded")
  146. }
  147. })
  148. }
  149. }