annotations_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2018 flannel authors
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package kube
  15. import "testing"
  16. func Test_newAnnotations(t *testing.T) {
  17. testCases := []struct {
  18. prefix string
  19. expectedBackendType string
  20. hasError bool
  21. }{
  22. {
  23. prefix: "flannel.alpha.coreos.com",
  24. expectedBackendType: "flannel.alpha.coreos.com/backend-type",
  25. },
  26. {
  27. prefix: "flannel.alpha.coreos.com/",
  28. expectedBackendType: "flannel.alpha.coreos.com/backend-type",
  29. },
  30. {
  31. prefix: "flannel.alpha.coreos.com/prefix",
  32. expectedBackendType: "flannel.alpha.coreos.com/prefix-backend-type",
  33. },
  34. {
  35. prefix: "flannel.alpha.coreos.com/prefix-",
  36. expectedBackendType: "flannel.alpha.coreos.com/prefix-backend-type",
  37. },
  38. {
  39. prefix: "org.com",
  40. expectedBackendType: "org.com/backend-type",
  41. },
  42. {
  43. prefix: "org9.com",
  44. expectedBackendType: "org9.com/backend-type",
  45. },
  46. {
  47. prefix: "org.com/9",
  48. expectedBackendType: "org.com/9-backend-type",
  49. },
  50. {
  51. // Not a fqdn.
  52. prefix: "org",
  53. hasError: true,
  54. },
  55. {
  56. // Not a fqdn before /.
  57. prefix: "org/",
  58. hasError: true,
  59. },
  60. {
  61. // Not a fqdn before /.
  62. prefix: "org/prefix",
  63. hasError: true,
  64. },
  65. {
  66. // Uppercase letters.
  67. prefix: "org.COM",
  68. hasError: true,
  69. },
  70. {
  71. // Uppercase letters.
  72. prefix: "org.com/PREFIX",
  73. hasError: true,
  74. },
  75. }
  76. for i, tc := range testCases {
  77. as, err := newAnnotations(tc.prefix)
  78. if err != nil && !tc.hasError {
  79. t.Errorf("#%d: error = %s, want nil", i, err)
  80. continue
  81. }
  82. if err == nil && tc.hasError {
  83. t.Errorf("#%d: error = nil, want non-nil", i)
  84. continue
  85. }
  86. if as.BackendType != tc.expectedBackendType {
  87. t.Errorf("#%d: BackendType = %s, want %s", i, as.BackendType, tc.expectedBackendType)
  88. }
  89. }
  90. }