PageRenderTime 1019ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/github.com/hashicorp/consul/consul/util_test.go

https://github.com/backstage/backstage
Go | 327 lines | 291 code | 27 blank | 9 comment | 83 complexity | d25a2925b975b823aa1284cde468a9cf MD5 | raw file
Possible License(s): Apache-2.0, MIT, BSD-3-Clause, MPL-2.0-no-copyleft-exception
  1. package consul
  2. import (
  3. "errors"
  4. "fmt"
  5. "net"
  6. "regexp"
  7. "testing"
  8. "github.com/hashicorp/serf/serf"
  9. )
  10. func TestGetPrivateIP(t *testing.T) {
  11. ip, _, err := net.ParseCIDR("10.1.2.3/32")
  12. if err != nil {
  13. t.Fatalf("failed to parse private cidr: %v", err)
  14. }
  15. pubIP, _, err := net.ParseCIDR("8.8.8.8/32")
  16. if err != nil {
  17. t.Fatalf("failed to parse public cidr: %v", err)
  18. }
  19. tests := []struct {
  20. addrs []net.Addr
  21. expected net.IP
  22. err error
  23. }{
  24. {
  25. addrs: []net.Addr{
  26. &net.IPAddr{
  27. IP: ip,
  28. },
  29. &net.IPAddr{
  30. IP: pubIP,
  31. },
  32. },
  33. expected: ip,
  34. },
  35. {
  36. addrs: []net.Addr{
  37. &net.IPAddr{
  38. IP: pubIP,
  39. },
  40. },
  41. err: errors.New("No private IP address found"),
  42. },
  43. {
  44. addrs: []net.Addr{
  45. &net.IPAddr{
  46. IP: ip,
  47. },
  48. &net.IPAddr{
  49. IP: ip,
  50. },
  51. &net.IPAddr{
  52. IP: pubIP,
  53. },
  54. },
  55. err: errors.New("Multiple private IPs found. Please configure one."),
  56. },
  57. }
  58. for _, test := range tests {
  59. ip, err := getPrivateIP(test.addrs)
  60. switch {
  61. case test.err != nil && err != nil:
  62. if err.Error() != test.err.Error() {
  63. t.Fatalf("unexpected error: %v != %v", test.err, err)
  64. }
  65. case (test.err == nil && err != nil) || (test.err != nil && err == nil):
  66. t.Fatalf("unexpected error: %v != %v", test.err, err)
  67. default:
  68. if !test.expected.Equal(ip) {
  69. t.Fatalf("unexpected ip: %v != %v", ip, test.expected)
  70. }
  71. }
  72. }
  73. }
  74. func TestIsPrivateIP(t *testing.T) {
  75. if !isPrivateIP("192.168.1.1") {
  76. t.Fatalf("bad")
  77. }
  78. if !isPrivateIP("172.16.45.100") {
  79. t.Fatalf("bad")
  80. }
  81. if !isPrivateIP("10.1.2.3") {
  82. t.Fatalf("bad")
  83. }
  84. if !isPrivateIP("100.115.110.19") {
  85. t.Fatalf("bad")
  86. }
  87. if isPrivateIP("8.8.8.8") {
  88. t.Fatalf("bad")
  89. }
  90. if !isPrivateIP("127.0.0.1") {
  91. t.Fatalf("bad")
  92. }
  93. }
  94. func TestUtil_CanServersUnderstandProtocol(t *testing.T) {
  95. var members []serf.Member
  96. // All empty list cases should return false.
  97. for v := ProtocolVersionMin; v <= ProtocolVersionMax; v++ {
  98. grok, err := CanServersUnderstandProtocol(members, v)
  99. if err != nil {
  100. t.Fatalf("err: %v", err)
  101. }
  102. if grok {
  103. t.Fatalf("empty list should always return false")
  104. }
  105. }
  106. // Add a non-server member.
  107. members = append(members, serf.Member{
  108. Tags: map[string]string{
  109. "vsn_min": fmt.Sprintf("%d", ProtocolVersionMin),
  110. "vsn_max": fmt.Sprintf("%d", ProtocolVersionMax),
  111. },
  112. })
  113. // Make sure it doesn't get counted.
  114. for v := ProtocolVersionMin; v <= ProtocolVersionMax; v++ {
  115. grok, err := CanServersUnderstandProtocol(members, v)
  116. if err != nil {
  117. t.Fatalf("err: %v", err)
  118. }
  119. if grok {
  120. t.Fatalf("non-server members should not be counted")
  121. }
  122. }
  123. // Add a server member.
  124. members = append(members, serf.Member{
  125. Tags: map[string]string{
  126. "role": "consul",
  127. "vsn_min": fmt.Sprintf("%d", ProtocolVersionMin),
  128. "vsn_max": fmt.Sprintf("%d", ProtocolVersionMax),
  129. },
  130. })
  131. // Now it should report that it understands.
  132. for v := ProtocolVersionMin; v <= ProtocolVersionMax; v++ {
  133. grok, err := CanServersUnderstandProtocol(members, v)
  134. if err != nil {
  135. t.Fatalf("err: %v", err)
  136. }
  137. if !grok {
  138. t.Fatalf("server should grok")
  139. }
  140. }
  141. // Nobody should understand anything from the future.
  142. for v := uint8(ProtocolVersionMax + 1); v <= uint8(ProtocolVersionMax+10); v++ {
  143. grok, err := CanServersUnderstandProtocol(members, v)
  144. if err != nil {
  145. t.Fatalf("err: %v", err)
  146. }
  147. if grok {
  148. t.Fatalf("server should not grok")
  149. }
  150. }
  151. // Add an older server.
  152. members = append(members, serf.Member{
  153. Tags: map[string]string{
  154. "role": "consul",
  155. "vsn_min": fmt.Sprintf("%d", ProtocolVersionMin),
  156. "vsn_max": fmt.Sprintf("%d", ProtocolVersionMax-1),
  157. },
  158. })
  159. // The servers should no longer understand the max version.
  160. for v := ProtocolVersionMin; v <= ProtocolVersionMax; v++ {
  161. grok, err := CanServersUnderstandProtocol(members, v)
  162. if err != nil {
  163. t.Fatalf("err: %v", err)
  164. }
  165. expected := v < ProtocolVersionMax
  166. if grok != expected {
  167. t.Fatalf("bad: %v != %v", grok, expected)
  168. }
  169. }
  170. // Try a version that's too low for the minimum.
  171. {
  172. grok, err := CanServersUnderstandProtocol(members, 0)
  173. if err != nil {
  174. t.Fatalf("err: %v", err)
  175. }
  176. if grok {
  177. t.Fatalf("server should not grok")
  178. }
  179. }
  180. }
  181. func TestIsConsulNode(t *testing.T) {
  182. m := serf.Member{
  183. Tags: map[string]string{
  184. "role": "node",
  185. "dc": "east-aws",
  186. },
  187. }
  188. valid, dc := isConsulNode(m)
  189. if !valid || dc != "east-aws" {
  190. t.Fatalf("bad: %v %v", valid, dc)
  191. }
  192. }
  193. func TestByteConversion(t *testing.T) {
  194. var val uint64 = 2 << 50
  195. raw := uint64ToBytes(val)
  196. if bytesToUint64(raw) != val {
  197. t.Fatalf("no match")
  198. }
  199. }
  200. func TestGenerateUUID(t *testing.T) {
  201. prev := generateUUID()
  202. for i := 0; i < 100; i++ {
  203. id := generateUUID()
  204. if prev == id {
  205. t.Fatalf("Should get a new ID!")
  206. }
  207. matched, err := regexp.MatchString(
  208. "[\\da-f]{8}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{12}", id)
  209. if !matched || err != nil {
  210. t.Fatalf("expected match %s %v %s", id, matched, err)
  211. }
  212. }
  213. }
  214. func TestGetPublicIPv6(t *testing.T) {
  215. ip, _, err := net.ParseCIDR("fe80::1/128")
  216. if err != nil {
  217. t.Fatalf("failed to parse link-local cidr: %v", err)
  218. }
  219. ip2, _, err := net.ParseCIDR("::1/128")
  220. if err != nil {
  221. t.Fatalf("failed to parse loopback cidr: %v", err)
  222. }
  223. ip3, _, err := net.ParseCIDR("fc00::1/128")
  224. if err != nil {
  225. t.Fatalf("failed to parse ULA cidr: %v", err)
  226. }
  227. pubIP, _, err := net.ParseCIDR("2001:0db8:85a3::8a2e:0370:7334/128")
  228. if err != nil {
  229. t.Fatalf("failed to parse public cidr: %v", err)
  230. }
  231. tests := []struct {
  232. addrs []net.Addr
  233. expected net.IP
  234. err error
  235. }{
  236. {
  237. addrs: []net.Addr{
  238. &net.IPAddr{
  239. IP: ip,
  240. },
  241. &net.IPAddr{
  242. IP: ip2,
  243. },
  244. &net.IPAddr{
  245. IP: ip3,
  246. },
  247. &net.IPAddr{
  248. IP: pubIP,
  249. },
  250. },
  251. expected: pubIP,
  252. },
  253. {
  254. addrs: []net.Addr{
  255. &net.IPAddr{
  256. IP: ip,
  257. },
  258. &net.IPAddr{
  259. IP: ip2,
  260. },
  261. &net.IPAddr{
  262. IP: ip3,
  263. },
  264. },
  265. err: errors.New("No public IPv6 address found"),
  266. },
  267. {
  268. addrs: []net.Addr{
  269. &net.IPAddr{
  270. IP: ip,
  271. },
  272. &net.IPAddr{
  273. IP: ip,
  274. },
  275. &net.IPAddr{
  276. IP: pubIP,
  277. },
  278. &net.IPAddr{
  279. IP: pubIP,
  280. },
  281. },
  282. err: errors.New("Multiple public IPv6 addresses found. Please configure one."),
  283. },
  284. }
  285. for _, test := range tests {
  286. ip, err := getPublicIPv6(test.addrs)
  287. switch {
  288. case test.err != nil && err != nil:
  289. if err.Error() != test.err.Error() {
  290. t.Fatalf("unexpected error: %v != %v", test.err, err)
  291. }
  292. case (test.err == nil && err != nil) || (test.err != nil && err == nil):
  293. t.Fatalf("unexpected error: %v != %v", test.err, err)
  294. default:
  295. if !test.expected.Equal(ip) {
  296. t.Fatalf("unexpected ip: %v != %v", ip, test.expected)
  297. }
  298. }
  299. }
  300. }