backend_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. v1 "k8s.io/api/core/v1"
  20. "github.com/squat/kilo/pkg/k8s/apis/kilo/v1alpha1"
  21. "github.com/squat/kilo/pkg/mesh"
  22. "github.com/squat/kilo/pkg/wireguard"
  23. )
  24. func TestTranslateNode(t *testing.T) {
  25. for _, tc := range []struct {
  26. name string
  27. annotations map[string]string
  28. labels map[string]string
  29. out *mesh.Node
  30. subnet string
  31. }{
  32. {
  33. name: "empty",
  34. annotations: nil,
  35. out: &mesh.Node{},
  36. },
  37. {
  38. name: "invalid ips",
  39. annotations: map[string]string{
  40. externalIPAnnotationKey: "10.0.0.1",
  41. internalIPAnnotationKey: "foo",
  42. },
  43. out: &mesh.Node{},
  44. },
  45. {
  46. name: "valid ips",
  47. annotations: map[string]string{
  48. externalIPAnnotationKey: "10.0.0.1/24",
  49. internalIPAnnotationKey: "10.0.0.2/32",
  50. },
  51. out: &mesh.Node{
  52. ExternalIP: &net.IPNet{IP: net.ParseIP("10.0.0.1"), Mask: net.CIDRMask(24, 32)},
  53. InternalIP: &net.IPNet{IP: net.ParseIP("10.0.0.2"), Mask: net.CIDRMask(32, 32)},
  54. },
  55. },
  56. {
  57. name: "invalid subnet",
  58. annotations: map[string]string{},
  59. out: &mesh.Node{},
  60. subnet: "foo",
  61. },
  62. {
  63. name: "normalize subnet",
  64. annotations: map[string]string{},
  65. out: &mesh.Node{
  66. Subnet: &net.IPNet{IP: net.ParseIP("10.2.0.0"), Mask: net.CIDRMask(24, 32)},
  67. },
  68. subnet: "10.2.0.1/24",
  69. },
  70. {
  71. name: "valid subnet",
  72. annotations: map[string]string{},
  73. out: &mesh.Node{
  74. Subnet: &net.IPNet{IP: net.ParseIP("10.2.1.0"), Mask: net.CIDRMask(24, 32)},
  75. },
  76. subnet: "10.2.1.0/24",
  77. },
  78. {
  79. name: "region",
  80. labels: map[string]string{
  81. regionLabelKey: "a",
  82. },
  83. out: &mesh.Node{
  84. Location: "a",
  85. },
  86. },
  87. {
  88. name: "region override",
  89. annotations: map[string]string{
  90. locationAnnotationKey: "b",
  91. },
  92. labels: map[string]string{
  93. regionLabelKey: "a",
  94. },
  95. out: &mesh.Node{
  96. Location: "b",
  97. },
  98. },
  99. {
  100. name: "external IP override",
  101. annotations: map[string]string{
  102. externalIPAnnotationKey: "10.0.0.1/24",
  103. forceExternalIPAnnotationKey: "10.0.0.2/24",
  104. },
  105. out: &mesh.Node{
  106. ExternalIP: &net.IPNet{IP: net.ParseIP("10.0.0.2"), Mask: net.CIDRMask(24, 32)},
  107. },
  108. },
  109. {
  110. name: "invalid time",
  111. annotations: map[string]string{
  112. lastSeenAnnotationKey: "foo",
  113. },
  114. out: &mesh.Node{},
  115. },
  116. {
  117. name: "complete",
  118. annotations: map[string]string{
  119. externalIPAnnotationKey: "10.0.0.1/24",
  120. forceExternalIPAnnotationKey: "10.0.0.2/24",
  121. internalIPAnnotationKey: "10.0.0.2/32",
  122. keyAnnotationKey: "foo",
  123. lastSeenAnnotationKey: "1000000000",
  124. leaderAnnotationKey: "",
  125. locationAnnotationKey: "b",
  126. wireGuardIPAnnotationKey: "10.4.0.1/16",
  127. },
  128. labels: map[string]string{
  129. regionLabelKey: "a",
  130. },
  131. out: &mesh.Node{
  132. ExternalIP: &net.IPNet{IP: net.ParseIP("10.0.0.2"), Mask: net.CIDRMask(24, 32)},
  133. InternalIP: &net.IPNet{IP: net.ParseIP("10.0.0.2"), Mask: net.CIDRMask(32, 32)},
  134. Key: []byte("foo"),
  135. LastSeen: 1000000000,
  136. Leader: true,
  137. Location: "b",
  138. Subnet: &net.IPNet{IP: net.ParseIP("10.2.1.0"), Mask: net.CIDRMask(24, 32)},
  139. WireGuardIP: &net.IPNet{IP: net.ParseIP("10.4.0.1"), Mask: net.CIDRMask(16, 32)},
  140. },
  141. subnet: "10.2.1.0/24",
  142. },
  143. } {
  144. n := &v1.Node{}
  145. n.ObjectMeta.Annotations = tc.annotations
  146. n.ObjectMeta.Labels = tc.labels
  147. n.Spec.PodCIDR = tc.subnet
  148. node := translateNode(n)
  149. if diff := pretty.Compare(node, tc.out); diff != "" {
  150. t.Errorf("test case %q: got diff: %v", tc.name, diff)
  151. }
  152. }
  153. }
  154. func TestTranslatePeer(t *testing.T) {
  155. for _, tc := range []struct {
  156. name string
  157. out *mesh.Peer
  158. spec v1alpha1.PeerSpec
  159. }{
  160. {
  161. name: "empty",
  162. out: &mesh.Peer{},
  163. },
  164. {
  165. name: "invalid ips",
  166. spec: v1alpha1.PeerSpec{
  167. AllowedIPs: []string{
  168. "10.0.0.1",
  169. "foo",
  170. },
  171. },
  172. out: &mesh.Peer{},
  173. },
  174. {
  175. name: "valid ips",
  176. spec: v1alpha1.PeerSpec{
  177. AllowedIPs: []string{
  178. "10.0.0.1/24",
  179. "10.0.0.2/32",
  180. },
  181. },
  182. out: &mesh.Peer{
  183. Peer: wireguard.Peer{
  184. AllowedIPs: []*net.IPNet{
  185. {IP: net.ParseIP("10.0.0.1"), Mask: net.CIDRMask(24, 32)},
  186. {IP: net.ParseIP("10.0.0.2"), Mask: net.CIDRMask(32, 32)},
  187. },
  188. },
  189. },
  190. },
  191. {
  192. name: "invalid endpoint ip",
  193. spec: v1alpha1.PeerSpec{
  194. Endpoint: &v1alpha1.PeerEndpoint{
  195. IP: "foo",
  196. Port: mesh.DefaultKiloPort,
  197. },
  198. },
  199. out: &mesh.Peer{},
  200. },
  201. {
  202. name: "valid endpoint",
  203. spec: v1alpha1.PeerSpec{
  204. Endpoint: &v1alpha1.PeerEndpoint{
  205. IP: "10.0.0.1",
  206. Port: mesh.DefaultKiloPort,
  207. },
  208. },
  209. out: &mesh.Peer{
  210. Peer: wireguard.Peer{
  211. Endpoint: &wireguard.Endpoint{
  212. IP: net.ParseIP("10.0.0.1"),
  213. Port: mesh.DefaultKiloPort,
  214. },
  215. },
  216. },
  217. },
  218. {
  219. name: "empty key",
  220. spec: v1alpha1.PeerSpec{
  221. PublicKey: "",
  222. },
  223. out: &mesh.Peer{},
  224. },
  225. {
  226. name: "valid key",
  227. spec: v1alpha1.PeerSpec{
  228. PublicKey: "foo",
  229. },
  230. out: &mesh.Peer{
  231. Peer: wireguard.Peer{
  232. PublicKey: []byte("foo"),
  233. },
  234. },
  235. },
  236. {
  237. name: "invalid keepalive",
  238. spec: v1alpha1.PeerSpec{
  239. PersistentKeepalive: -1,
  240. },
  241. out: &mesh.Peer{},
  242. },
  243. {
  244. name: "valid keepalive",
  245. spec: v1alpha1.PeerSpec{
  246. PersistentKeepalive: 1,
  247. },
  248. out: &mesh.Peer{
  249. Peer: wireguard.Peer{
  250. PersistentKeepalive: 1,
  251. },
  252. },
  253. },
  254. } {
  255. p := &v1alpha1.Peer{}
  256. p.Spec = tc.spec
  257. peer := translatePeer(p)
  258. if diff := pretty.Compare(peer, tc.out); diff != "" {
  259. t.Errorf("test case %q: got diff: %v", tc.name, diff)
  260. }
  261. }
  262. }