backend_test.go 6.5 KB

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