backend_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. },
  127. labels: map[string]string{
  128. regionLabelKey: "a",
  129. },
  130. out: &mesh.Node{
  131. ExternalIP: &net.IPNet{IP: net.ParseIP("10.0.0.2"), Mask: net.CIDRMask(24, 32)},
  132. InternalIP: &net.IPNet{IP: net.ParseIP("10.0.0.2"), Mask: net.CIDRMask(32, 32)},
  133. Key: []byte("foo"),
  134. LastSeen: 1000000000,
  135. Leader: true,
  136. Location: "b",
  137. Subnet: &net.IPNet{IP: net.ParseIP("10.2.1.0"), Mask: net.CIDRMask(24, 32)},
  138. },
  139. subnet: "10.2.1.0/24",
  140. },
  141. } {
  142. n := &v1.Node{}
  143. n.ObjectMeta.Annotations = tc.annotations
  144. n.ObjectMeta.Labels = tc.labels
  145. n.Spec.PodCIDR = tc.subnet
  146. node := translateNode(n)
  147. if diff := pretty.Compare(node, tc.out); diff != "" {
  148. t.Errorf("test case %q: got diff: %v", tc.name, diff)
  149. }
  150. }
  151. }
  152. func TestTranslatePeer(t *testing.T) {
  153. for _, tc := range []struct {
  154. name string
  155. out *mesh.Peer
  156. spec v1alpha1.PeerSpec
  157. }{
  158. {
  159. name: "empty",
  160. out: &mesh.Peer{},
  161. },
  162. {
  163. name: "invalid ips",
  164. spec: v1alpha1.PeerSpec{
  165. AllowedIPs: []string{
  166. "10.0.0.1",
  167. "foo",
  168. },
  169. },
  170. out: &mesh.Peer{},
  171. },
  172. {
  173. name: "valid ips",
  174. spec: v1alpha1.PeerSpec{
  175. AllowedIPs: []string{
  176. "10.0.0.1/24",
  177. "10.0.0.2/32",
  178. },
  179. },
  180. out: &mesh.Peer{
  181. Peer: wireguard.Peer{
  182. AllowedIPs: []*net.IPNet{
  183. {IP: net.ParseIP("10.0.0.1"), Mask: net.CIDRMask(24, 32)},
  184. {IP: net.ParseIP("10.0.0.2"), Mask: net.CIDRMask(32, 32)},
  185. },
  186. },
  187. },
  188. },
  189. {
  190. name: "invalid endpoint ip",
  191. spec: v1alpha1.PeerSpec{
  192. Endpoint: &v1alpha1.PeerEndpoint{
  193. IP: "foo",
  194. Port: mesh.DefaultKiloPort,
  195. },
  196. },
  197. out: &mesh.Peer{},
  198. },
  199. {
  200. name: "valid endpoint",
  201. spec: v1alpha1.PeerSpec{
  202. Endpoint: &v1alpha1.PeerEndpoint{
  203. IP: "10.0.0.1",
  204. Port: mesh.DefaultKiloPort,
  205. },
  206. },
  207. out: &mesh.Peer{
  208. Peer: wireguard.Peer{
  209. Endpoint: &wireguard.Endpoint{
  210. IP: net.ParseIP("10.0.0.1"),
  211. Port: mesh.DefaultKiloPort,
  212. },
  213. },
  214. },
  215. },
  216. {
  217. name: "empty key",
  218. spec: v1alpha1.PeerSpec{
  219. PublicKey: "",
  220. },
  221. out: &mesh.Peer{},
  222. },
  223. {
  224. name: "valid key",
  225. spec: v1alpha1.PeerSpec{
  226. PublicKey: "foo",
  227. },
  228. out: &mesh.Peer{
  229. Peer: wireguard.Peer{
  230. PublicKey: []byte("foo"),
  231. },
  232. },
  233. },
  234. {
  235. name: "invalid keepalive",
  236. spec: v1alpha1.PeerSpec{
  237. PersistentKeepalive: -1,
  238. },
  239. out: &mesh.Peer{},
  240. },
  241. {
  242. name: "valid keepalive",
  243. spec: v1alpha1.PeerSpec{
  244. PersistentKeepalive: 1,
  245. },
  246. out: &mesh.Peer{
  247. Peer: wireguard.Peer{
  248. PersistentKeepalive: 1,
  249. },
  250. },
  251. },
  252. } {
  253. p := &v1alpha1.Peer{}
  254. p.Spec = tc.spec
  255. peer := translatePeer(p)
  256. if diff := pretty.Compare(peer, tc.out); diff != "" {
  257. t.Errorf("test case %q: got diff: %v", tc.name, diff)
  258. }
  259. }
  260. }