/apis/metadata_validation_test.go

https://github.com/knative/pkg · Go · 177 lines · 153 code · 9 blank · 15 comment · 4 complexity · 3332d674c1293317b73ebd4b2fba8ea4 MD5 · raw file

  1. /*
  2. Copyright 2019 The Knative 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 apis
  14. import (
  15. "strings"
  16. "testing"
  17. "github.com/google/go-cmp/cmp"
  18. corev1 "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. )
  21. func TestValidateObjectMetadata(t *testing.T) {
  22. tests := []struct {
  23. name string
  24. objectMeta metav1.Object
  25. want error
  26. }{{
  27. name: "invalid name - dots",
  28. objectMeta: &metav1.ObjectMeta{
  29. Name: "do.not.use.dots",
  30. },
  31. want: &FieldError{
  32. Message: "not a DNS 1035 label: [a DNS-1035 label must consist of lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character (e.g. 'my-name', or 'abc-123', regex used for validation is '[a-z]([-a-z0-9]*[a-z0-9])?')]",
  33. Paths: []string{"name"},
  34. },
  35. }, {
  36. name: "invalid name - too long",
  37. objectMeta: &metav1.ObjectMeta{
  38. Name: strings.Repeat("a", 64),
  39. },
  40. want: &FieldError{
  41. Message: "not a DNS 1035 label: [must be no more than 63 characters]",
  42. Paths: []string{"name"},
  43. },
  44. }, {
  45. name: "invalid name - trailing dash",
  46. objectMeta: &metav1.ObjectMeta{
  47. Name: "some-name-",
  48. },
  49. want: &FieldError{
  50. Message: "not a DNS 1035 label: [a DNS-1035 label must consist of lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character (e.g. 'my-name', or 'abc-123', regex used for validation is '[a-z]([-a-z0-9]*[a-z0-9])?')]",
  51. Paths: []string{"name"},
  52. },
  53. }, {
  54. name: "valid generateName",
  55. objectMeta: &metav1.ObjectMeta{
  56. GenerateName: "some-name",
  57. },
  58. want: (*FieldError)(nil),
  59. }, {
  60. name: "valid generateName - trailing dash",
  61. objectMeta: &metav1.ObjectMeta{
  62. GenerateName: "some-name-",
  63. },
  64. want: (*FieldError)(nil),
  65. }, {
  66. name: "invalid generateName - dots",
  67. objectMeta: &metav1.ObjectMeta{
  68. GenerateName: "do.not.use.dots",
  69. },
  70. want: &FieldError{
  71. Message: "not a DNS 1035 label prefix: [a DNS-1035 label must consist of lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character (e.g. 'my-name', or 'abc-123', regex used for validation is '[a-z]([-a-z0-9]*[a-z0-9])?')]",
  72. Paths: []string{"generateName"},
  73. },
  74. }, {
  75. name: "invalid generateName - too long",
  76. objectMeta: &metav1.ObjectMeta{
  77. GenerateName: strings.Repeat("a", 64),
  78. },
  79. want: &FieldError{
  80. Message: "not a DNS 1035 label prefix: [must be no more than 63 characters]",
  81. Paths: []string{"generateName"},
  82. },
  83. }, {
  84. name: "missing name and generateName",
  85. objectMeta: &metav1.ObjectMeta{},
  86. want: &FieldError{
  87. Message: "name or generateName is required",
  88. Paths: []string{"name"},
  89. },
  90. }}
  91. for _, tc := range tests {
  92. t.Run(tc.name, func(t *testing.T) {
  93. if err := ValidateObjectMetadata(tc.objectMeta); !cmp.Equal(tc.want.Error(), err.Error()) {
  94. t.Errorf("Expected: '%#v', Got: '%#v', diff(-want,+got)\n%s", tc.want, err,
  95. cmp.Diff(tc.want.Error(), err.Error()))
  96. }
  97. })
  98. }
  99. }
  100. type WithPod struct {
  101. metav1.TypeMeta `json:",inline"`
  102. metav1.ObjectMeta `json:"metadata,omitempty"`
  103. Spec corev1.PodSpec `json:"spec,omitempty"`
  104. }
  105. func getSpec(image string) corev1.PodSpec {
  106. return corev1.PodSpec{
  107. Containers: []corev1.Container{{
  108. Image: image,
  109. }},
  110. }
  111. }
  112. func getAnnotation(groupName, suffix, user string) map[string]string {
  113. return map[string]string{
  114. groupName + suffix: user,
  115. }
  116. }
  117. func TestServiceAnnotationUpdate(t *testing.T) {
  118. const (
  119. u1 = "oveja@knative.dev"
  120. u2 = "cabra@knative.dev"
  121. groupName = "pkg.knative.dev"
  122. )
  123. tests := []struct {
  124. name string
  125. prev *WithPod
  126. this *WithPod
  127. oldAnnotation map[string]string
  128. newAnnotation map[string]string
  129. want *FieldError
  130. }{{
  131. name: "update creator annotation",
  132. prev: nil,
  133. this: nil,
  134. oldAnnotation: getAnnotation(groupName, CreatorAnnotationSuffix, u1),
  135. newAnnotation: getAnnotation(groupName, CreatorAnnotationSuffix, u2),
  136. want: &FieldError{Message: "annotation value is immutable",
  137. Paths: []string{groupName + CreatorAnnotationSuffix}},
  138. }, {
  139. name: "update lastModifier without spec changes",
  140. prev: nil,
  141. this: nil,
  142. oldAnnotation: getAnnotation(groupName, UpdaterAnnotationSuffix, u1),
  143. newAnnotation: getAnnotation(groupName, UpdaterAnnotationSuffix, u2),
  144. want: ErrInvalidValue(u2, groupName+UpdaterAnnotationSuffix),
  145. }, {
  146. name: "update lastModifier with spec changes",
  147. prev: &WithPod{
  148. Spec: getSpec("new-image"),
  149. },
  150. this: &WithPod{
  151. Spec: getSpec("old-image"),
  152. },
  153. oldAnnotation: getAnnotation(groupName, UpdaterAnnotationSuffix, u1),
  154. newAnnotation: getAnnotation(groupName, UpdaterAnnotationSuffix, u2),
  155. want: nil,
  156. }}
  157. for _, test := range tests {
  158. t.Run(test.name, func(t *testing.T) {
  159. err := ValidateCreatorAndModifier(test.prev, test.this, test.oldAnnotation, test.newAnnotation, groupName)
  160. if !cmp.Equal(test.want.Error(), err.Error()) {
  161. t.Errorf("Expected: '%#v', Got: '%#v', diff(-want,+got)\n%s", test.want, err, cmp.Diff(test.want, err))
  162. }
  163. })
  164. }
  165. }