subnet_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 etcdv2
  15. import (
  16. "encoding/json"
  17. "reflect"
  18. "testing"
  19. "time"
  20. etcd "github.com/coreos/etcd/client"
  21. "github.com/coreos/flannel/pkg/ip"
  22. . "github.com/coreos/flannel/subnet"
  23. "github.com/jonboulle/clockwork"
  24. "golang.org/x/net/context"
  25. )
  26. func newDummyRegistry() *MockSubnetRegistry {
  27. attrs := LeaseAttrs{
  28. PublicIP: ip.MustParseIP4("1.1.1.1"),
  29. }
  30. exp := time.Time{}
  31. subnets := []Lease{
  32. // leases within SubnetMin-SubnetMax range
  33. {ip.IP4Net{ip.MustParseIP4("10.3.1.0"), 24}, attrs, exp, 10},
  34. {ip.IP4Net{ip.MustParseIP4("10.3.2.0"), 24}, attrs, exp, 11},
  35. {ip.IP4Net{ip.MustParseIP4("10.3.4.0"), 24}, attrs, exp, 12},
  36. {ip.IP4Net{ip.MustParseIP4("10.3.5.0"), 24}, attrs, exp, 13},
  37. // hand created lease outside the range of subnetMin-SubnetMax for testing removal
  38. {ip.IP4Net{ip.MustParseIP4("10.3.31.0"), 24}, attrs, exp, 13},
  39. }
  40. config := `{ "Network": "10.3.0.0/16", "SubnetMin": "10.3.1.0", "SubnetMax": "10.3.25.0" }`
  41. return NewMockRegistry(config, subnets)
  42. }
  43. func TestAcquireLease(t *testing.T) {
  44. msr := newDummyRegistry()
  45. sm := NewMockManager(msr)
  46. extIaddr, _ := ip.ParseIP4("1.2.3.4")
  47. attrs := LeaseAttrs{
  48. PublicIP: extIaddr,
  49. }
  50. l, err := sm.AcquireLease(context.Background(), &attrs)
  51. if err != nil {
  52. t.Fatal("AcquireLease failed: ", err)
  53. }
  54. if !inAllocatableRange(context.Background(), sm, l.Subnet) {
  55. t.Fatal("Subnet mismatch: expected 10.3.3.0/24, got: ", l.Subnet)
  56. }
  57. // Acquire again, should reuse
  58. l2, err := sm.AcquireLease(context.Background(), &attrs)
  59. if err != nil {
  60. t.Fatal("AcquireLease failed: ", err)
  61. }
  62. if !l.Subnet.Equal(l2.Subnet) {
  63. t.Fatalf("AcquireLease did not reuse subnet; expected %v, got %v", l.Subnet, l2.Subnet)
  64. }
  65. }
  66. func TestConfigChanged(t *testing.T) {
  67. msr := newDummyRegistry()
  68. sm := NewMockManager(msr)
  69. extIaddr, _ := ip.ParseIP4("1.2.3.4")
  70. attrs := LeaseAttrs{
  71. PublicIP: extIaddr,
  72. }
  73. l, err := sm.AcquireLease(context.Background(), &attrs)
  74. if err != nil {
  75. t.Fatal("AcquireLease failed: ", err)
  76. }
  77. if !inAllocatableRange(context.Background(), sm, l.Subnet) {
  78. t.Fatal("Acquired subnet outside of valid range: ", l.Subnet)
  79. }
  80. // Change config
  81. config := `{ "Network": "10.4.0.0/16" }`
  82. msr.setConfig(config)
  83. // Acquire again, should not reuse
  84. if l, err = sm.AcquireLease(context.Background(), &attrs); err != nil {
  85. t.Fatal("AcquireLease failed: ", err)
  86. }
  87. if !inAllocatableRange(context.Background(), sm, l.Subnet) {
  88. t.Fatal("Acquired subnet outside of valid range: ", l.Subnet)
  89. }
  90. }
  91. func newIP4Net(ipaddr string, prefix uint) ip.IP4Net {
  92. a, err := ip.ParseIP4(ipaddr)
  93. if err != nil {
  94. panic("failed to parse ipaddr")
  95. }
  96. return ip.IP4Net{
  97. IP: a,
  98. PrefixLen: prefix,
  99. }
  100. }
  101. func acquireLease(ctx context.Context, t *testing.T, sm Manager) *Lease {
  102. extIaddr, _ := ip.ParseIP4("1.2.3.4")
  103. attrs := LeaseAttrs{
  104. PublicIP: extIaddr,
  105. }
  106. l, err := sm.AcquireLease(ctx, &attrs)
  107. if err != nil {
  108. t.Fatal("AcquireLease failed: ", err)
  109. }
  110. return l
  111. }
  112. func TestWatchLeaseAdded(t *testing.T) {
  113. msr := newDummyRegistry()
  114. sm := NewMockManager(msr)
  115. ctx, cancel := context.WithCancel(context.Background())
  116. defer cancel()
  117. l := acquireLease(ctx, t, sm)
  118. events := make(chan []Event)
  119. go WatchLeases(ctx, sm, l, events)
  120. evtBatch := <-events
  121. for _, evt := range evtBatch {
  122. if evt.Lease.Key() == l.Key() {
  123. t.Errorf("WatchLeases returned our own lease")
  124. }
  125. }
  126. expected := ip.IP4Net{
  127. IP: ip.MustParseIP4("10.3.30.0"),
  128. PrefixLen: 24,
  129. }
  130. // Sanity check to make sure acquired lease is not this.
  131. // It shouldn't be as SubnetMin/SubnetMax in config is [10.3.1.0/24 to 10.3.25.0/24]
  132. if l.Subnet.Equal(expected) {
  133. t.Fatalf("Acquired lease conflicts with one about to create")
  134. }
  135. attrs := &LeaseAttrs{
  136. PublicIP: ip.MustParseIP4("1.1.1.1"),
  137. }
  138. _, err := msr.createSubnet(ctx, expected, attrs, 0)
  139. if err != nil {
  140. t.Fatalf("createSubnet filed: %v", err)
  141. }
  142. evtBatch = <-events
  143. if len(evtBatch) != 1 {
  144. t.Fatalf("WatchLeases produced wrong sized event batch: got %v, expected 1", len(evtBatch))
  145. }
  146. evt := evtBatch[0]
  147. if evt.Type != EventAdded {
  148. t.Fatalf("WatchLeases produced wrong event type")
  149. }
  150. actual := evt.Lease.Subnet
  151. if !actual.Equal(expected) {
  152. t.Errorf("WatchSubnet produced wrong subnet: expected %s, got %s", expected, actual)
  153. }
  154. }
  155. func TestWatchLeaseRemoved(t *testing.T) {
  156. msr := newDummyRegistry()
  157. sm := NewMockManager(msr)
  158. ctx, cancel := context.WithCancel(context.Background())
  159. defer cancel()
  160. l := acquireLease(ctx, t, sm)
  161. events := make(chan []Event)
  162. go WatchLeases(ctx, sm, l, events)
  163. evtBatch := <-events
  164. for _, evt := range evtBatch {
  165. if evt.Lease.Key() == l.Key() {
  166. t.Errorf("WatchLeases returned our own lease")
  167. }
  168. }
  169. expected := ip.IP4Net{ip.MustParseIP4("10.3.31.0"), 24}
  170. // Sanity check to make sure acquired lease is not this.
  171. // It shouldn't be as SubnetMin/SubnetMax in config is [10.3.1.0/24 to 10.3.25.0/24]
  172. if l.Subnet.Equal(expected) {
  173. t.Fatalf("Acquired lease conflicts with one about to create")
  174. }
  175. msr.expireSubnet("_", expected)
  176. evtBatch = <-events
  177. if len(evtBatch) != 1 {
  178. t.Fatalf("WatchLeases produced wrong sized event batch: %#v", evtBatch)
  179. }
  180. evt := evtBatch[0]
  181. if evt.Type != EventRemoved {
  182. t.Fatalf("WatchLeases produced wrong event type")
  183. }
  184. actual := evt.Lease.Subnet
  185. if !actual.Equal(expected) {
  186. t.Errorf("WatchSubnet produced wrong subnet: expected %s, got %s", expected, actual)
  187. }
  188. }
  189. type leaseData struct {
  190. Dummy string
  191. }
  192. func TestRenewLease(t *testing.T) {
  193. msr := newDummyRegistry()
  194. sm := NewMockManager(msr)
  195. now := time.Now()
  196. fakeClock := clockwork.NewFakeClockAt(now)
  197. clock = fakeClock
  198. // Create LeaseAttrs
  199. extIaddr, _ := ip.ParseIP4("1.2.3.4")
  200. attrs := LeaseAttrs{
  201. PublicIP: extIaddr,
  202. BackendType: "vxlan",
  203. }
  204. ld, err := json.Marshal(&leaseData{Dummy: "test string"})
  205. if err != nil {
  206. t.Fatalf("Failed to marshal leaseData: %v", err)
  207. }
  208. attrs.BackendData = json.RawMessage(ld)
  209. // Acquire lease
  210. ctx, cancel := context.WithCancel(context.Background())
  211. defer cancel()
  212. l, err := sm.AcquireLease(ctx, &attrs)
  213. if err != nil {
  214. t.Fatal("AcquireLease failed: ", err)
  215. }
  216. now = now.Add(subnetTTL)
  217. fakeClock.Advance(24 * time.Hour)
  218. if err := sm.RenewLease(ctx, l); err != nil {
  219. t.Fatal("RenewLease failed: ", err)
  220. }
  221. // check that it's still good
  222. n, err := msr.getNetwork(ctx)
  223. if err != nil {
  224. t.Errorf("Failed to renew lease: could not get networks: %v", err)
  225. }
  226. for _, sn := range n.subnets {
  227. if sn.Subnet.Equal(l.Subnet) {
  228. expected := now.Add(subnetTTL)
  229. if !sn.Expiration.Equal(expected) {
  230. t.Errorf("Failed to renew lease: bad expiration; expected %v, got %v", expected, sn.Expiration)
  231. }
  232. if !reflect.DeepEqual(sn.Attrs, attrs) {
  233. t.Errorf("LeaseAttrs changed: was %#v, now %#v", attrs, sn.Attrs)
  234. }
  235. return
  236. }
  237. }
  238. t.Fatal("Failed to find acquired lease")
  239. }
  240. func TestLeaseRevoked(t *testing.T) {
  241. msr := newDummyRegistry()
  242. sm := NewMockManager(msr)
  243. ctx, cancel := context.WithCancel(context.Background())
  244. defer cancel()
  245. l := acquireLease(ctx, t, sm)
  246. if err := sm.RevokeLease(ctx, l.Subnet); err != nil {
  247. t.Fatalf("RevokeLease failed: %v", err)
  248. }
  249. _, _, err := msr.getSubnet(ctx, l.Subnet)
  250. if err == nil {
  251. t.Fatalf("Revoked lease still exists")
  252. }
  253. if etcdErr, ok := err.(etcd.Error); ok && etcdErr.Code != etcd.ErrorCodeKeyNotFound {
  254. t.Fatalf("getSubnets after revoked lease returned unexpected error: %v", err)
  255. }
  256. }
  257. func TestAddReservation(t *testing.T) {
  258. msr := newDummyRegistry()
  259. sm := NewMockManager(msr)
  260. ctx, cancel := context.WithCancel(context.Background())
  261. defer cancel()
  262. r := Reservation{
  263. Subnet: newIP4Net("10.4.3.0", 24),
  264. PublicIP: ip.MustParseIP4("52.195.12.13"),
  265. }
  266. if err := sm.AddReservation(ctx, &r); err == nil {
  267. t.Fatalf("unexpectedly added a reservation outside of configured network")
  268. }
  269. r.Subnet = newIP4Net("10.3.10.0", 24)
  270. if err := sm.AddReservation(ctx, &r); err != nil {
  271. t.Fatalf("failed to add reservation: %v", err)
  272. }
  273. // Add the same reservation -- should succeed
  274. if err := sm.AddReservation(ctx, &r); err != nil {
  275. t.Fatalf("failed to add reservation: %v", err)
  276. }
  277. // Add a reservation with a different public IP -- should fail
  278. r2 := r
  279. r2.PublicIP = ip.MustParseIP4("52.195.12.17")
  280. if err := sm.AddReservation(ctx, &r2); err != ErrLeaseTaken {
  281. t.Fatalf("taken add reservation returned: %v", err)
  282. }
  283. attrs := &LeaseAttrs{
  284. PublicIP: r.PublicIP,
  285. }
  286. l, err := sm.AcquireLease(ctx, attrs)
  287. if err != nil {
  288. t.Fatalf("failed to acquire subnet: %v", err)
  289. }
  290. if !l.Subnet.Equal(r.Subnet) {
  291. t.Fatalf("acquired subnet is not the reserved one: expected %v, got %v", r.Subnet, l.Subnet)
  292. }
  293. if !l.Expiration.IsZero() {
  294. t.Fatalf("acquired lease (prev reserved) has expiration set")
  295. }
  296. }
  297. func TestRemoveReservation(t *testing.T) {
  298. msr := newDummyRegistry()
  299. sm := NewMockManager(msr)
  300. ctx, cancel := context.WithCancel(context.Background())
  301. defer cancel()
  302. r := Reservation{
  303. Subnet: newIP4Net("10.3.10.0", 24),
  304. PublicIP: ip.MustParseIP4("52.195.12.13"),
  305. }
  306. if err := sm.AddReservation(ctx, &r); err != nil {
  307. t.Fatalf("failed to add reservation: %v", err)
  308. }
  309. if err := sm.RemoveReservation(ctx, r.Subnet); err != nil {
  310. t.Fatalf("failed to remove reservation: %v", err)
  311. }
  312. // The node should have a TTL
  313. sub, _, err := msr.getSubnet(ctx, r.Subnet)
  314. if err != nil {
  315. t.Fatalf("getSubnet failed: %v", err)
  316. }
  317. if sub.Expiration.IsZero() {
  318. t.Fatalf("removed reservation resulted in no TTL")
  319. }
  320. }
  321. func TestListReservations(t *testing.T) {
  322. msr := newDummyRegistry()
  323. sm := NewMockManager(msr)
  324. ctx, cancel := context.WithCancel(context.Background())
  325. defer cancel()
  326. r1 := Reservation{
  327. Subnet: newIP4Net("10.3.10.0", 24),
  328. PublicIP: ip.MustParseIP4("52.195.12.13"),
  329. }
  330. if err := sm.AddReservation(ctx, &r1); err != nil {
  331. t.Fatalf("failed to add reservation: %v", err)
  332. }
  333. r2 := Reservation{
  334. Subnet: newIP4Net("10.3.20.0", 24),
  335. PublicIP: ip.MustParseIP4("52.195.12.14"),
  336. }
  337. if err := sm.AddReservation(ctx, &r2); err != nil {
  338. t.Fatalf("failed to add reservation: %v", err)
  339. }
  340. rs, err := sm.ListReservations(ctx)
  341. if err != nil {
  342. if len(rs) != 2 {
  343. t.Fatalf("unexpected number of reservations, expected 2, got %v", len(rs))
  344. }
  345. if !resvEqual(rs[0], r1) && !resvEqual(rs[1], r1) {
  346. t.Fatalf("reservation not found")
  347. }
  348. if !resvEqual(rs[0], r2) && !resvEqual(rs[1], r2) {
  349. t.Fatalf("reservation not found")
  350. }
  351. }
  352. }
  353. func inAllocatableRange(ctx context.Context, sm Manager, ipn ip.IP4Net) bool {
  354. cfg, err := sm.GetNetworkConfig(ctx)
  355. if err != nil {
  356. panic(err)
  357. }
  358. return ipn.IP >= cfg.SubnetMin || ipn.IP <= cfg.SubnetMax
  359. }
  360. func resvEqual(r1, r2 Reservation) bool {
  361. return r1.Subnet.Equal(r2.Subnet) && r1.PublicIP == r2.PublicIP
  362. }