backend_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright 2019 the Kilo 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 k8s
  15. import (
  16. "net"
  17. "testing"
  18. "github.com/kylelemons/godebug/pretty"
  19. "k8s.io/api/core/v1"
  20. "github.com/squat/kilo/pkg/mesh"
  21. )
  22. func TestTranslateNode(t *testing.T) {
  23. for _, tc := range []struct {
  24. name string
  25. annotations map[string]string
  26. labels map[string]string
  27. out *mesh.Node
  28. subnet string
  29. }{
  30. {
  31. name: "empty",
  32. annotations: nil,
  33. out: &mesh.Node{},
  34. },
  35. {
  36. name: "invalid ip",
  37. annotations: map[string]string{
  38. externalIPAnnotationKey: "10.0.0.1",
  39. internalIPAnnotationKey: "10.0.0.1",
  40. },
  41. out: &mesh.Node{},
  42. },
  43. {
  44. name: "valid ip",
  45. annotations: map[string]string{
  46. externalIPAnnotationKey: "10.0.0.1/24",
  47. internalIPAnnotationKey: "10.0.0.2/32",
  48. },
  49. out: &mesh.Node{
  50. ExternalIP: &net.IPNet{IP: net.ParseIP("10.0.0.1"), Mask: net.CIDRMask(24, 32)},
  51. InternalIP: &net.IPNet{IP: net.ParseIP("10.0.0.2"), Mask: net.CIDRMask(32, 32)},
  52. },
  53. },
  54. {
  55. name: "invalid subnet",
  56. annotations: map[string]string{},
  57. out: &mesh.Node{},
  58. subnet: "foo",
  59. },
  60. {
  61. name: "normalize subnet",
  62. annotations: map[string]string{},
  63. out: &mesh.Node{
  64. Subnet: &net.IPNet{IP: net.ParseIP("10.2.0.0"), Mask: net.CIDRMask(24, 32)},
  65. },
  66. subnet: "10.2.0.1/24",
  67. },
  68. {
  69. name: "valid subnet",
  70. annotations: map[string]string{},
  71. out: &mesh.Node{
  72. Subnet: &net.IPNet{IP: net.ParseIP("10.2.1.0"), Mask: net.CIDRMask(24, 32)},
  73. },
  74. subnet: "10.2.1.0/24",
  75. },
  76. {
  77. name: "region",
  78. labels: map[string]string{
  79. regionLabelKey: "a",
  80. },
  81. out: &mesh.Node{
  82. Location: "a",
  83. },
  84. },
  85. {
  86. name: "region override",
  87. annotations: map[string]string{
  88. locationAnnotationKey: "b",
  89. },
  90. labels: map[string]string{
  91. regionLabelKey: "a",
  92. },
  93. out: &mesh.Node{
  94. Location: "b",
  95. },
  96. },
  97. {
  98. name: "external IP override",
  99. annotations: map[string]string{
  100. externalIPAnnotationKey: "10.0.0.1/24",
  101. forceExternalIPAnnotationKey: "10.0.0.2/24",
  102. },
  103. out: &mesh.Node{
  104. ExternalIP: &net.IPNet{IP: net.ParseIP("10.0.0.2"), Mask: net.CIDRMask(24, 32)},
  105. },
  106. },
  107. {
  108. name: "complete",
  109. annotations: map[string]string{
  110. externalIPAnnotationKey: "10.0.0.1/24",
  111. forceExternalIPAnnotationKey: "10.0.0.2/24",
  112. internalIPAnnotationKey: "10.0.0.2/32",
  113. keyAnnotationKey: "foo",
  114. leaderAnnotationKey: "",
  115. locationAnnotationKey: "b",
  116. },
  117. labels: map[string]string{
  118. regionLabelKey: "a",
  119. },
  120. out: &mesh.Node{
  121. ExternalIP: &net.IPNet{IP: net.ParseIP("10.0.0.2"), Mask: net.CIDRMask(24, 32)},
  122. InternalIP: &net.IPNet{IP: net.ParseIP("10.0.0.2"), Mask: net.CIDRMask(32, 32)},
  123. Key: []byte("foo"),
  124. Leader: true,
  125. Location: "b",
  126. Subnet: &net.IPNet{IP: net.ParseIP("10.2.1.0"), Mask: net.CIDRMask(24, 32)},
  127. },
  128. subnet: "10.2.1.0/24",
  129. },
  130. } {
  131. n := &v1.Node{}
  132. n.ObjectMeta.Annotations = tc.annotations
  133. n.ObjectMeta.Labels = tc.labels
  134. n.Spec.PodCIDR = tc.subnet
  135. node := translateNode(n)
  136. if diff := pretty.Compare(node, tc.out); diff != "" {
  137. t.Errorf("test case %q: got diff: %v", tc.name, diff)
  138. }
  139. }
  140. }