mock_registry.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. "github.com/coreos/flannel/Godeps/_workspace/src/github.com/coreos/go-etcd/etcd"
  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 uint64
  27. }
  28. func newMockRegistry(ttlOverride uint64, 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. EtcdIndex: msr.index,
  50. Node: msr.config,
  51. }, nil
  52. }
  53. func (msr *mockSubnetRegistry) getSubnets(ctx context.Context, network string) (*etcd.Response, error) {
  54. return &etcd.Response{
  55. Node: msr.subnets,
  56. EtcdIndex: msr.index,
  57. }, nil
  58. }
  59. func (msr *mockSubnetRegistry) createSubnet(ctx context.Context, network, sn, data string, ttl uint64) (*etcd.Response, error) {
  60. msr.index += 1
  61. if msr.ttl > 0 {
  62. ttl = msr.ttl
  63. }
  64. // add squared durations :)
  65. exp := time.Now().Add(time.Duration(ttl) * time.Second)
  66. node := &etcd.Node{
  67. Key: sn,
  68. Value: data,
  69. ModifiedIndex: msr.index,
  70. Expiration: &exp,
  71. }
  72. msr.subnets.Nodes = append(msr.subnets.Nodes, node)
  73. msr.events <- &etcd.Response{
  74. Action: "add",
  75. Node: node,
  76. }
  77. return &etcd.Response{
  78. Node: node,
  79. EtcdIndex: msr.index,
  80. }, nil
  81. }
  82. func (msr *mockSubnetRegistry) updateSubnet(ctx context.Context, network, sn, data string, ttl uint64) (*etcd.Response, error) {
  83. msr.index += 1
  84. // add squared durations :)
  85. exp := time.Now().Add(time.Duration(ttl) * time.Second)
  86. for _, n := range msr.subnets.Nodes {
  87. if n.Key == sn {
  88. n.Value = data
  89. n.ModifiedIndex = msr.index
  90. n.Expiration = &exp
  91. msr.events <- &etcd.Response{
  92. Action: "add",
  93. Node: n,
  94. }
  95. return &etcd.Response{
  96. Node: n,
  97. EtcdIndex: msr.index,
  98. }, nil
  99. }
  100. }
  101. return nil, fmt.Errorf("Subnet not found")
  102. }
  103. func (msr *mockSubnetRegistry) watchSubnets(ctx context.Context, network string, since uint64) (*etcd.Response, error) {
  104. select {
  105. case <-ctx.Done():
  106. return nil, ctx.Err()
  107. case r := <-msr.events:
  108. return r, nil
  109. }
  110. }
  111. func (msr *mockSubnetRegistry) hasSubnet(sn string) bool {
  112. for _, n := range msr.subnets.Nodes {
  113. if n.Key == sn {
  114. return true
  115. }
  116. }
  117. return false
  118. }
  119. func (msr *mockSubnetRegistry) expireSubnet(sn string) {
  120. for i, n := range msr.subnets.Nodes {
  121. if n.Key == sn {
  122. msr.subnets.Nodes[i] = msr.subnets.Nodes[len(msr.subnets.Nodes)-1]
  123. msr.subnets.Nodes = msr.subnets.Nodes[:len(msr.subnets.Nodes)-2]
  124. msr.events <- &etcd.Response{
  125. Action: "expire",
  126. Node: n,
  127. }
  128. return
  129. }
  130. }
  131. }