datastore_test.go 5.3 KB

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