datastore_test.go 5.7 KB

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