datastore_test.go 5.7 KB

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