mock_registry.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright 2015 CoreOS, Inc.
  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. "fmt"
  17. "time"
  18. etcd "github.com/coreos/flannel/Godeps/_workspace/src/github.com/coreos/etcd/client"
  19. "github.com/coreos/flannel/Godeps/_workspace/src/golang.org/x/net/context"
  20. )
  21. type mockSubnetRegistry struct {
  22. config *etcd.Node
  23. subnets *etcd.Node
  24. events chan *etcd.Response
  25. index uint64
  26. ttl time.Duration
  27. }
  28. func newMockRegistry(ttlOverride time.Duration, config string, initialSubnets []*etcd.Node) *mockSubnetRegistry {
  29. index := uint64(0)
  30. for _, n := range initialSubnets {
  31. if n.ModifiedIndex > index {
  32. index = n.ModifiedIndex
  33. }
  34. }
  35. return &mockSubnetRegistry{
  36. config: &etcd.Node{
  37. Value: config,
  38. },
  39. subnets: &etcd.Node{
  40. Nodes: initialSubnets,
  41. },
  42. events: make(chan *etcd.Response, 1000),
  43. index: index + 1,
  44. ttl: ttlOverride,
  45. }
  46. }
  47. func (msr *mockSubnetRegistry) getConfig(ctx context.Context, network string) (*etcd.Response, error) {
  48. return &etcd.Response{
  49. Index: msr.index,
  50. Node: msr.config,
  51. }, nil
  52. }
  53. func (msr *mockSubnetRegistry) setConfig(config string) {
  54. msr.config = &etcd.Node{
  55. Key: "config",
  56. Value: config,
  57. }
  58. }
  59. func (msr *mockSubnetRegistry) getSubnets(ctx context.Context, network string) (*etcd.Response, error) {
  60. return &etcd.Response{
  61. Node: msr.subnets,
  62. Index: msr.index,
  63. }, nil
  64. }
  65. func (msr *mockSubnetRegistry) createSubnet(ctx context.Context, network, sn, data string, ttl time.Duration) (*etcd.Response, error) {
  66. msr.index += 1
  67. if msr.ttl > 0 {
  68. ttl = msr.ttl
  69. }
  70. exp := time.Now().Add(ttl)
  71. node := &etcd.Node{
  72. Key: sn,
  73. Value: data,
  74. ModifiedIndex: msr.index,
  75. Expiration: &exp,
  76. }
  77. msr.subnets.Nodes = append(msr.subnets.Nodes, node)
  78. msr.events <- &etcd.Response{
  79. Action: "add",
  80. Node: node,
  81. }
  82. return &etcd.Response{
  83. Node: node,
  84. Index: msr.index,
  85. }, nil
  86. }
  87. func (msr *mockSubnetRegistry) updateSubnet(ctx context.Context, network, sn, data string, ttl time.Duration) (*etcd.Response, error) {
  88. msr.index += 1
  89. exp := time.Now().Add(ttl)
  90. for _, n := range msr.subnets.Nodes {
  91. if n.Key == sn {
  92. n.Value = data
  93. n.ModifiedIndex = msr.index
  94. n.Expiration = &exp
  95. msr.events <- &etcd.Response{
  96. Action: "add",
  97. Node: n,
  98. }
  99. return &etcd.Response{
  100. Node: n,
  101. Index: msr.index,
  102. }, nil
  103. }
  104. }
  105. return nil, fmt.Errorf("Subnet not found")
  106. }
  107. func (msr *mockSubnetRegistry) deleteSubnet(ctx context.Context, network, sn string) (*etcd.Response, error) {
  108. msr.index += 1
  109. for i, n := range msr.subnets.Nodes {
  110. if n.Key == sn {
  111. msr.subnets.Nodes[i] = msr.subnets.Nodes[len(msr.subnets.Nodes)-1]
  112. msr.subnets.Nodes = msr.subnets.Nodes[:len(msr.subnets.Nodes)-1]
  113. msr.events <- &etcd.Response{
  114. Action: "delete",
  115. Node: n,
  116. }
  117. return &etcd.Response{
  118. Node: n,
  119. Index: msr.index,
  120. }, nil
  121. }
  122. }
  123. return nil, fmt.Errorf("Subnet not found")
  124. }
  125. func (msr *mockSubnetRegistry) watchSubnets(ctx context.Context, network string, since uint64) (*etcd.Response, error) {
  126. for {
  127. select {
  128. case <-ctx.Done():
  129. return nil, ctx.Err()
  130. case r := <-msr.events:
  131. if r.Node.ModifiedIndex <= since {
  132. continue
  133. }
  134. return r, nil
  135. }
  136. }
  137. }
  138. func (msr *mockSubnetRegistry) hasSubnet(sn string) bool {
  139. for _, n := range msr.subnets.Nodes {
  140. if n.Key == sn {
  141. return true
  142. }
  143. }
  144. return false
  145. }
  146. func (msr *mockSubnetRegistry) expireSubnet(sn string) {
  147. for i, n := range msr.subnets.Nodes {
  148. if n.Key == sn {
  149. msr.index += 1
  150. msr.subnets.Nodes[i] = msr.subnets.Nodes[len(msr.subnets.Nodes)-1]
  151. msr.subnets.Nodes = msr.subnets.Nodes[:len(msr.subnets.Nodes)-2]
  152. n.ModifiedIndex = msr.index
  153. msr.events <- &etcd.Response{
  154. Action: "expire",
  155. Node: n,
  156. }
  157. return
  158. }
  159. }
  160. }