datastore_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. package datastore
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "github.com/docker/docker/libnetwork/options"
  6. "github.com/docker/docker/libnetwork/scope"
  7. "gotest.tools/v3/assert"
  8. is "gotest.tools/v3/assert/cmp"
  9. )
  10. const dummyKey = "dummy"
  11. // NewTestDataStore can be used by other Tests in order to use custom datastore
  12. func NewTestDataStore() *Store {
  13. s := NewMockStore()
  14. return &Store{store: s, cache: newCache(s)}
  15. }
  16. func TestKey(t *testing.T) {
  17. sKey := Key("hello", "world")
  18. const expected = "docker/network/v1.0/hello/world/"
  19. assert.Check(t, is.Equal(sKey, expected))
  20. }
  21. func TestInvalidDataStore(t *testing.T) {
  22. _, err := New(ScopeCfg{
  23. Client: ScopeClientCfg{
  24. Provider: "invalid",
  25. Address: "localhost:8500",
  26. },
  27. })
  28. assert.Check(t, is.Error(err, "unsupported KV store"))
  29. }
  30. func TestKVObjectFlatKey(t *testing.T) {
  31. store := NewTestDataStore()
  32. expected := dummyKVObject("1000", true)
  33. err := store.PutObjectAtomic(expected)
  34. assert.Check(t, err)
  35. n := dummyObject{ID: "1000"} // GetObject uses KVObject.Key() for cache lookup.
  36. err = store.GetObject(Key(dummyKey, "1000"), &n)
  37. assert.Check(t, err)
  38. assert.Check(t, is.Equal(n.Name, expected.Name))
  39. }
  40. func TestAtomicKVObjectFlatKey(t *testing.T) {
  41. store := NewTestDataStore()
  42. expected := dummyKVObject("1111", true)
  43. assert.Check(t, !expected.Exists())
  44. err := store.PutObjectAtomic(expected)
  45. assert.Check(t, err)
  46. assert.Check(t, expected.Exists())
  47. // PutObjectAtomic automatically sets the Index again. Hence the following must pass.
  48. err = store.PutObjectAtomic(expected)
  49. assert.Check(t, err, "Atomic update should succeed.")
  50. // Get the latest index and try PutObjectAtomic again for the same Key
  51. // This must succeed as well
  52. n := dummyObject{ID: "1111"} // GetObject uses KVObject.Key() for cache lookup.
  53. err = store.GetObject(Key(expected.Key()...), &n)
  54. assert.Check(t, err)
  55. n.ReturnValue = true
  56. err = store.PutObjectAtomic(&n)
  57. assert.Check(t, err)
  58. // Get the Object using GetObject, then set again.
  59. newObj := dummyObject{ID: "1111"} // GetObject uses KVObject.Key() for cache lookup.
  60. err = store.GetObject(Key(expected.Key()...), &newObj)
  61. assert.Check(t, err)
  62. assert.Check(t, newObj.Exists())
  63. err = store.PutObjectAtomic(&n)
  64. assert.Check(t, err)
  65. }
  66. // dummy data used to test the datastore
  67. type dummyObject struct {
  68. Name string `kv:"leaf"`
  69. NetworkType string `kv:"leaf"`
  70. EnableIPv6 bool `kv:"leaf"`
  71. Rec *recStruct `kv:"recursive"`
  72. Dict map[string]*recStruct `kv:"iterative"`
  73. Generic options.Generic `kv:"iterative"`
  74. ID string
  75. DBIndex uint64
  76. DBExists bool
  77. SkipSave bool
  78. ReturnValue bool
  79. }
  80. func (n *dummyObject) Key() []string {
  81. return []string{dummyKey, n.ID}
  82. }
  83. func (n *dummyObject) KeyPrefix() []string {
  84. return []string{dummyKey}
  85. }
  86. func (n *dummyObject) Value() []byte {
  87. if !n.ReturnValue {
  88. return nil
  89. }
  90. b, err := json.Marshal(n)
  91. if err != nil {
  92. return nil
  93. }
  94. return b
  95. }
  96. func (n *dummyObject) SetValue(value []byte) error {
  97. return json.Unmarshal(value, n)
  98. }
  99. func (n *dummyObject) Index() uint64 {
  100. return n.DBIndex
  101. }
  102. func (n *dummyObject) SetIndex(index uint64) {
  103. n.DBIndex = index
  104. n.DBExists = true
  105. }
  106. func (n *dummyObject) Exists() bool {
  107. return n.DBExists
  108. }
  109. func (n *dummyObject) Skip() bool {
  110. return n.SkipSave
  111. }
  112. func (n *dummyObject) DataScope() string {
  113. return scope.Local
  114. }
  115. func (n *dummyObject) MarshalJSON() ([]byte, error) {
  116. return json.Marshal(map[string]interface{}{
  117. "name": n.Name,
  118. "networkType": n.NetworkType,
  119. "enableIPv6": n.EnableIPv6,
  120. "generic": n.Generic,
  121. })
  122. }
  123. func (n *dummyObject) UnmarshalJSON(b []byte) error {
  124. var netMap map[string]interface{}
  125. if err := json.Unmarshal(b, &netMap); err != nil {
  126. return err
  127. }
  128. n.Name = netMap["name"].(string)
  129. n.NetworkType = netMap["networkType"].(string)
  130. n.EnableIPv6 = netMap["enableIPv6"].(bool)
  131. n.Generic = netMap["generic"].(map[string]interface{})
  132. return nil
  133. }
  134. func (n *dummyObject) New() KVObject {
  135. return &dummyObject{}
  136. }
  137. func (n *dummyObject) CopyTo(o KVObject) error {
  138. if err := o.SetValue(n.Value()); err != nil {
  139. return err
  140. }
  141. o.SetIndex(n.Index())
  142. return nil
  143. }
  144. // dummy structure to test "recursive" cases
  145. type recStruct struct {
  146. Name string `kv:"leaf"`
  147. Field1 int `kv:"leaf"`
  148. Dict map[string]string `kv:"iterative"`
  149. DBIndex uint64
  150. DBExists bool
  151. SkipSave bool
  152. }
  153. func (r *recStruct) Key() []string {
  154. return []string{"recStruct"}
  155. }
  156. func (r *recStruct) Value() []byte {
  157. b, err := json.Marshal(r)
  158. if err != nil {
  159. return nil
  160. }
  161. return b
  162. }
  163. func (r *recStruct) SetValue(value []byte) error {
  164. return json.Unmarshal(value, r)
  165. }
  166. func (r *recStruct) Index() uint64 {
  167. return r.DBIndex
  168. }
  169. func (r *recStruct) SetIndex(index uint64) {
  170. r.DBIndex = index
  171. r.DBExists = true
  172. }
  173. func (r *recStruct) Exists() bool {
  174. return r.DBExists
  175. }
  176. func (r *recStruct) Skip() bool {
  177. return r.SkipSave
  178. }
  179. func dummyKVObject(id string, retValue bool) *dummyObject {
  180. cDict := map[string]string{
  181. "foo": "bar",
  182. "hello": "world",
  183. }
  184. return &dummyObject{
  185. Name: "testNw",
  186. NetworkType: "bridge",
  187. EnableIPv6: true,
  188. Rec: &recStruct{Name: "gen", Field1: 5, Dict: cDict},
  189. ID: id,
  190. DBIndex: 0,
  191. ReturnValue: retValue,
  192. DBExists: false,
  193. SkipSave: false,
  194. Generic: map[string]interface{}{
  195. "label1": &recStruct{Name: "value1", Field1: 1, Dict: cDict},
  196. "label2": "subnet=10.1.1.0/16",
  197. },
  198. }
  199. }