mesh_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 mesh
  15. import (
  16. "net"
  17. "testing"
  18. "time"
  19. )
  20. func TestNewAllocator(t *testing.T) {
  21. _, c1, err := net.ParseCIDR("10.1.0.0/16")
  22. if err != nil {
  23. t.Fatalf("failed to parse CIDR: %v", err)
  24. }
  25. a1 := newAllocator(*c1)
  26. _, c2, err := net.ParseCIDR("10.1.0.0/32")
  27. if err != nil {
  28. t.Fatalf("failed to parse CIDR: %v", err)
  29. }
  30. a2 := newAllocator(*c2)
  31. _, c3, err := net.ParseCIDR("10.1.0.0/31")
  32. if err != nil {
  33. t.Fatalf("failed to parse CIDR: %v", err)
  34. }
  35. a3 := newAllocator(*c3)
  36. for _, tc := range []struct {
  37. name string
  38. a *allocator
  39. next string
  40. }{
  41. {
  42. name: "10.1.0.0/16 first",
  43. a: a1,
  44. next: "10.1.0.1/32",
  45. },
  46. {
  47. name: "10.1.0.0/16 second",
  48. a: a1,
  49. next: "10.1.0.2/32",
  50. },
  51. {
  52. name: "10.1.0.0/32",
  53. a: a2,
  54. next: "<nil>",
  55. },
  56. {
  57. name: "10.1.0.0/31 first",
  58. a: a3,
  59. next: "10.1.0.1/32",
  60. },
  61. {
  62. name: "10.1.0.0/31 second",
  63. a: a3,
  64. next: "<nil>",
  65. },
  66. } {
  67. next := tc.a.next()
  68. if next.String() != tc.next {
  69. t.Errorf("test case %q: expected %s, got %s", tc.name, tc.next, next.String())
  70. }
  71. }
  72. }
  73. func TestReady(t *testing.T) {
  74. internalIP := oneAddressCIDR(net.ParseIP("1.1.1.1"))
  75. externalIP := oneAddressCIDR(net.ParseIP("2.2.2.2"))
  76. for _, tc := range []struct {
  77. name string
  78. node *Node
  79. ready bool
  80. }{
  81. {
  82. name: "nil",
  83. node: nil,
  84. ready: false,
  85. },
  86. {
  87. name: "empty fields",
  88. node: &Node{},
  89. ready: false,
  90. },
  91. {
  92. name: "empty external IP",
  93. node: &Node{
  94. InternalIP: internalIP,
  95. Key: []byte{},
  96. Subnet: &net.IPNet{IP: net.ParseIP("10.2.0.0"), Mask: net.CIDRMask(16, 32)},
  97. },
  98. ready: false,
  99. },
  100. {
  101. name: "empty internal IP",
  102. node: &Node{
  103. ExternalIP: externalIP,
  104. Key: []byte{},
  105. Subnet: &net.IPNet{IP: net.ParseIP("10.2.0.0"), Mask: net.CIDRMask(16, 32)},
  106. },
  107. ready: false,
  108. },
  109. {
  110. name: "empty key",
  111. node: &Node{
  112. ExternalIP: externalIP,
  113. InternalIP: internalIP,
  114. Subnet: &net.IPNet{IP: net.ParseIP("10.2.0.0"), Mask: net.CIDRMask(16, 32)},
  115. },
  116. ready: false,
  117. },
  118. {
  119. name: "empty subnet",
  120. node: &Node{
  121. ExternalIP: externalIP,
  122. InternalIP: internalIP,
  123. Key: []byte{},
  124. },
  125. ready: false,
  126. },
  127. {
  128. name: "valid",
  129. node: &Node{
  130. ExternalIP: externalIP,
  131. InternalIP: internalIP,
  132. Key: []byte{},
  133. LastSeen: time.Now().Unix(),
  134. Subnet: &net.IPNet{IP: net.ParseIP("10.2.0.0"), Mask: net.CIDRMask(16, 32)},
  135. },
  136. ready: true,
  137. },
  138. } {
  139. ready := tc.node.Ready()
  140. if ready != tc.ready {
  141. t.Errorf("test case %q: expected %t, got %t", tc.name, tc.ready, ready)
  142. }
  143. }
  144. }