subnet_test.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. // Copyright 2015 flannel 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 subnet
  15. import (
  16. "encoding/json"
  17. "reflect"
  18. "testing"
  19. "time"
  20. "github.com/coreos/flannel/Godeps/_workspace/src/github.com/jonboulle/clockwork"
  21. "github.com/coreos/flannel/Godeps/_workspace/src/golang.org/x/net/context"
  22. "github.com/coreos/flannel/pkg/ip"
  23. )
  24. func newDummyRegistry() *MockSubnetRegistry {
  25. attrs := LeaseAttrs{
  26. PublicIP: ip.MustParseIP4("1.1.1.1"),
  27. }
  28. exp := time.Time{}
  29. subnets := []Lease{
  30. {ip.IP4Net{ip.MustParseIP4("10.3.1.0"), 24}, attrs, exp, 10},
  31. {ip.IP4Net{ip.MustParseIP4("10.3.2.0"), 24}, attrs, exp, 11},
  32. {ip.IP4Net{ip.MustParseIP4("10.3.4.0"), 24}, attrs, exp, 12},
  33. {ip.IP4Net{ip.MustParseIP4("10.3.5.0"), 24}, attrs, exp, 13},
  34. }
  35. config := `{ "Network": "10.3.0.0/16", "SubnetMin": "10.3.1.0", "SubnetMax": "10.3.5.0" }`
  36. return NewMockRegistry("_", config, subnets)
  37. }
  38. func TestAcquireLease(t *testing.T) {
  39. msr := newDummyRegistry()
  40. sm := NewMockManager(msr)
  41. extIaddr, _ := ip.ParseIP4("1.2.3.4")
  42. attrs := LeaseAttrs{
  43. PublicIP: extIaddr,
  44. }
  45. l, err := sm.AcquireLease(context.Background(), "_", &attrs)
  46. if err != nil {
  47. t.Fatal("AcquireLease failed: ", err)
  48. }
  49. if l.Subnet.String() != "10.3.3.0/24" {
  50. t.Fatal("Subnet mismatch: expected 10.3.3.0/24, got: ", l.Subnet)
  51. }
  52. // Acquire again, should reuse
  53. if l, err = sm.AcquireLease(context.Background(), "_", &attrs); err != nil {
  54. t.Fatal("AcquireLease failed: ", err)
  55. }
  56. if l.Subnet.String() != "10.3.3.0/24" {
  57. t.Fatal("Subnet mismatch: expected 10.3.3.0/24, got: ", l.Subnet)
  58. }
  59. }
  60. func TestConfigChanged(t *testing.T) {
  61. msr := newDummyRegistry()
  62. sm := NewMockManager(msr)
  63. extIaddr, _ := ip.ParseIP4("1.2.3.4")
  64. attrs := LeaseAttrs{
  65. PublicIP: extIaddr,
  66. }
  67. l, err := sm.AcquireLease(context.Background(), "_", &attrs)
  68. if err != nil {
  69. t.Fatal("AcquireLease failed: ", err)
  70. }
  71. if l.Subnet.String() != "10.3.3.0/24" {
  72. t.Fatal("Subnet mismatch: expected 10.3.3.0/24, got: ", l.Subnet)
  73. }
  74. // Change config
  75. config := `{ "Network": "10.4.0.0/16" }`
  76. msr.setConfig("_", config)
  77. // Acquire again, should not reuse
  78. if l, err = sm.AcquireLease(context.Background(), "_", &attrs); err != nil {
  79. t.Fatal("AcquireLease failed: ", err)
  80. }
  81. newNet := newIP4Net("10.4.0.0", 16)
  82. if !newNet.Contains(l.Subnet.IP) {
  83. t.Fatalf("Subnet mismatch: expected within %v, got: %v", newNet, l.Subnet)
  84. }
  85. }
  86. func newIP4Net(ipaddr string, prefix uint) ip.IP4Net {
  87. a, err := ip.ParseIP4(ipaddr)
  88. if err != nil {
  89. panic("failed to parse ipaddr")
  90. }
  91. return ip.IP4Net{
  92. IP: a,
  93. PrefixLen: prefix,
  94. }
  95. }
  96. func acquireLease(ctx context.Context, t *testing.T, sm Manager) *Lease {
  97. extIaddr, _ := ip.ParseIP4("1.2.3.4")
  98. attrs := LeaseAttrs{
  99. PublicIP: extIaddr,
  100. }
  101. l, err := sm.AcquireLease(ctx, "_", &attrs)
  102. if err != nil {
  103. t.Fatal("AcquireLease failed: ", err)
  104. }
  105. return l
  106. }
  107. func TestWatchLeaseAdded(t *testing.T) {
  108. msr := newDummyRegistry()
  109. sm := NewMockManager(msr)
  110. ctx, cancel := context.WithCancel(context.Background())
  111. defer cancel()
  112. l := acquireLease(ctx, t, sm)
  113. events := make(chan []Event)
  114. go WatchLeases(ctx, sm, "_", l, events)
  115. evtBatch := <-events
  116. for _, evt := range evtBatch {
  117. if evt.Lease.Key() == l.Key() {
  118. t.Errorf("WatchLeases returned our own lease")
  119. }
  120. }
  121. expected := ip.IP4Net{
  122. IP: ip.MustParseIP4("10.3.6.0"),
  123. PrefixLen: 24,
  124. }
  125. attrs := &LeaseAttrs{
  126. PublicIP: ip.MustParseIP4("1.1.1.1"),
  127. }
  128. msr.createSubnet(ctx, "_", expected, attrs, 0)
  129. evtBatch = <-events
  130. if len(evtBatch) != 1 {
  131. t.Fatalf("WatchLeases produced wrong sized event batch")
  132. }
  133. evt := evtBatch[0]
  134. if evt.Type != EventAdded {
  135. t.Fatalf("WatchLeases produced wrong event type")
  136. }
  137. actual := evt.Lease.Subnet
  138. if !actual.Equal(expected) {
  139. t.Errorf("WatchSubnet produced wrong subnet: expected %s, got %s", expected, actual)
  140. }
  141. }
  142. func TestWatchLeaseRemoved(t *testing.T) {
  143. msr := newDummyRegistry()
  144. sm := NewMockManager(msr)
  145. ctx, cancel := context.WithCancel(context.Background())
  146. defer cancel()
  147. l := acquireLease(ctx, t, sm)
  148. events := make(chan []Event)
  149. go WatchLeases(ctx, sm, "_", l, events)
  150. evtBatch := <-events
  151. for _, evt := range evtBatch {
  152. if evt.Lease.Key() == l.Key() {
  153. t.Errorf("WatchLeases returned our own lease")
  154. }
  155. }
  156. expected := ip.IP4Net{ip.MustParseIP4("10.3.4.0"), 24}
  157. msr.expireSubnet("_", expected)
  158. evtBatch = <-events
  159. if len(evtBatch) != 1 {
  160. t.Fatalf("WatchLeases produced wrong sized event batch")
  161. }
  162. evt := evtBatch[0]
  163. if evt.Type != EventRemoved {
  164. t.Fatalf("WatchLeases produced wrong event type")
  165. }
  166. actual := evt.Lease.Subnet
  167. if !actual.Equal(expected) {
  168. t.Errorf("WatchSubnet produced wrong subnet: expected %s, got %s", expected, actual)
  169. }
  170. }
  171. type leaseData struct {
  172. Dummy string
  173. }
  174. func TestRenewLease(t *testing.T) {
  175. msr := newDummyRegistry()
  176. sm := NewMockManager(msr)
  177. now := time.Now()
  178. fakeClock := clockwork.NewFakeClockAt(now)
  179. clock = fakeClock
  180. // Create LeaseAttrs
  181. extIaddr, _ := ip.ParseIP4("1.2.3.4")
  182. attrs := LeaseAttrs{
  183. PublicIP: extIaddr,
  184. BackendType: "vxlan",
  185. }
  186. ld, err := json.Marshal(&leaseData{Dummy: "test string"})
  187. if err != nil {
  188. t.Fatalf("Failed to marshal leaseData: %v", err)
  189. }
  190. attrs.BackendData = json.RawMessage(ld)
  191. // Acquire lease
  192. ctx, cancel := context.WithCancel(context.Background())
  193. defer cancel()
  194. l, err := sm.AcquireLease(ctx, "_", &attrs)
  195. if err != nil {
  196. t.Fatal("AcquireLease failed: ", err)
  197. }
  198. go LeaseRenewer(ctx, sm, "_", l)
  199. fakeClock.Advance(24 * time.Hour)
  200. // check that it's still good
  201. n, err := msr.getNetwork(ctx, "_")
  202. if err != nil {
  203. t.Error("Failed to renew lease: could not get networks: %v", err)
  204. }
  205. for _, sn := range n.subnets {
  206. if sn.Subnet.Equal(l.Subnet) {
  207. if !sn.Expiration.Equal(now.Add(subnetTTL)) {
  208. t.Errorf("Failed to renew lease: bad expiration; expected %v, got %v", now.Add(subnetTTL), sn.Expiration)
  209. }
  210. if !reflect.DeepEqual(sn.Attrs, attrs) {
  211. t.Errorf("LeaseAttrs changed: was %#v, now %#v", attrs, sn.Attrs)
  212. }
  213. return
  214. }
  215. }
  216. t.Fatalf("Failed to find acquired lease")
  217. }
  218. func TestWatchGetNetworks(t *testing.T) {
  219. msr := newDummyRegistry()
  220. sm := NewMockManager(msr)
  221. ctx, cancel := context.WithCancel(context.Background())
  222. defer cancel()
  223. // Kill the previously added "_" network
  224. msr.DeleteNetwork(ctx, "_")
  225. expected := "foobar"
  226. msr.CreateNetwork(ctx, expected, `{"Network": "10.1.1.0/16", "Backend": {"Type": "bridge"}}`)
  227. resp, err := sm.WatchNetworks(ctx, nil)
  228. if err != nil {
  229. t.Errorf("WatchNetworks(nil) failed:", err)
  230. }
  231. if len(resp.Snapshot) != 1 {
  232. t.Errorf("WatchNetworks(nil) produced wrong number of networks: expected 1, got %d", len(resp.Snapshot))
  233. }
  234. if resp.Snapshot[0] != expected {
  235. t.Errorf("WatchNetworks(nil) produced wrong network: expected %s, got %s", expected, resp.Snapshot[0])
  236. }
  237. }
  238. func TestWatchNetworkAdded(t *testing.T) {
  239. msr := newDummyRegistry()
  240. sm := NewMockManager(msr)
  241. ctx, cancel := context.WithCancel(context.Background())
  242. defer cancel()
  243. events := make(chan []Event)
  244. go WatchNetworks(ctx, sm, events)
  245. // skip over the initial snapshot
  246. <-events
  247. expected := "foobar"
  248. msr.CreateNetwork(ctx, expected, `{"Network": "10.1.1.0/16", "Backend": {"Type": "bridge"}}`)
  249. evtBatch := <-events
  250. if len(evtBatch) != 1 {
  251. t.Fatalf("WatchNetworks produced wrong sized event batch")
  252. }
  253. evt := evtBatch[0]
  254. if evt.Type != EventAdded {
  255. t.Fatalf("WatchNetworks produced wrong event type")
  256. }
  257. actual := evt.Network
  258. if actual != expected {
  259. t.Errorf("WatchNetworks produced wrong network: expected %s, got %s", expected, actual)
  260. }
  261. }
  262. func TestWatchNetworkRemoved(t *testing.T) {
  263. msr := newDummyRegistry()
  264. sm := NewMockManager(msr)
  265. ctx, cancel := context.WithCancel(context.Background())
  266. defer cancel()
  267. events := make(chan []Event)
  268. go WatchNetworks(ctx, sm, events)
  269. // skip over the initial snapshot
  270. <-events
  271. expected := "blah"
  272. msr.CreateNetwork(ctx, expected, `{"Network": "10.1.1.0/16", "Backend": {"Type": "bridge"}}`)
  273. // skip over the create event
  274. <-events
  275. err := msr.DeleteNetwork(ctx, expected)
  276. if err != nil {
  277. t.Fatalf("WatchNetworks failed to delete the network")
  278. }
  279. evtBatch := <-events
  280. if len(evtBatch) != 1 {
  281. t.Fatalf("WatchNetworks produced wrong sized event batch")
  282. }
  283. evt := evtBatch[0]
  284. if evt.Type != EventRemoved {
  285. t.Fatalf("WatchNetworks produced wrong event type")
  286. }
  287. actual := evt.Network
  288. if actual != expected {
  289. t.Errorf("WatchNetwork produced wrong network: expected %s, got %s", expected, actual)
  290. }
  291. }