datastore_test.go 5.4 KB

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