datastore_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. "github.com/stretchr/testify/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.Fatalf("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.False(t, expected.Exists())
  63. err := store.PutObjectAtomic(expected)
  64. if err != nil {
  65. t.Fatal(err)
  66. }
  67. assert.True(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. assert.True(t, newObj.Exists())
  92. err = store.PutObjectAtomic(&n)
  93. if err != nil {
  94. t.Fatal(err)
  95. }
  96. }
  97. // dummy data used to test the datastore
  98. type dummyObject struct {
  99. Name string `kv:"leaf"`
  100. NetworkType string `kv:"leaf"`
  101. EnableIPv6 bool `kv:"leaf"`
  102. Rec *recStruct `kv:"recursive"`
  103. Dict map[string]*recStruct `kv:"iterative"`
  104. Generic options.Generic `kv:"iterative"`
  105. ID string
  106. DBIndex uint64
  107. DBExists bool
  108. SkipSave bool
  109. ReturnValue bool
  110. }
  111. func (n *dummyObject) Key() []string {
  112. return []string{dummyKey, n.ID}
  113. }
  114. func (n *dummyObject) KeyPrefix() []string {
  115. return []string{dummyKey}
  116. }
  117. func (n *dummyObject) Value() []byte {
  118. if !n.ReturnValue {
  119. return nil
  120. }
  121. b, err := json.Marshal(n)
  122. if err != nil {
  123. return nil
  124. }
  125. return b
  126. }
  127. func (n *dummyObject) SetValue(value []byte) error {
  128. return json.Unmarshal(value, n)
  129. }
  130. func (n *dummyObject) Index() uint64 {
  131. return n.DBIndex
  132. }
  133. func (n *dummyObject) SetIndex(index uint64) {
  134. n.DBIndex = index
  135. n.DBExists = true
  136. }
  137. func (n *dummyObject) Exists() bool {
  138. return n.DBExists
  139. }
  140. func (n *dummyObject) Skip() bool {
  141. return n.SkipSave
  142. }
  143. func (n *dummyObject) DataScope() string {
  144. return LocalScope
  145. }
  146. func (n *dummyObject) MarshalJSON() ([]byte, error) {
  147. netMap := make(map[string]interface{})
  148. netMap["name"] = n.Name
  149. netMap["networkType"] = n.NetworkType
  150. netMap["enableIPv6"] = n.EnableIPv6
  151. netMap["generic"] = n.Generic
  152. return json.Marshal(netMap)
  153. }
  154. func (n *dummyObject) UnmarshalJSON(b []byte) (err error) {
  155. var netMap map[string]interface{}
  156. if err := json.Unmarshal(b, &netMap); err != nil {
  157. return err
  158. }
  159. n.Name = netMap["name"].(string)
  160. n.NetworkType = netMap["networkType"].(string)
  161. n.EnableIPv6 = netMap["enableIPv6"].(bool)
  162. n.Generic = netMap["generic"].(map[string]interface{})
  163. return nil
  164. }
  165. // dummy structure to test "recursive" cases
  166. type recStruct struct {
  167. Name string `kv:"leaf"`
  168. Field1 int `kv:"leaf"`
  169. Dict map[string]string `kv:"iterative"`
  170. DBIndex uint64
  171. DBExists bool
  172. SkipSave bool
  173. }
  174. func (r *recStruct) Key() []string {
  175. return []string{"recStruct"}
  176. }
  177. func (r *recStruct) Value() []byte {
  178. b, err := json.Marshal(r)
  179. if err != nil {
  180. return nil
  181. }
  182. return b
  183. }
  184. func (r *recStruct) SetValue(value []byte) error {
  185. return json.Unmarshal(value, r)
  186. }
  187. func (r *recStruct) Index() uint64 {
  188. return r.DBIndex
  189. }
  190. func (r *recStruct) SetIndex(index uint64) {
  191. r.DBIndex = index
  192. r.DBExists = true
  193. }
  194. func (r *recStruct) Exists() bool {
  195. return r.DBExists
  196. }
  197. func (r *recStruct) Skip() bool {
  198. return r.SkipSave
  199. }
  200. func dummyKVObject(id string, retValue bool) *dummyObject {
  201. cDict := make(map[string]string)
  202. cDict["foo"] = "bar"
  203. cDict["hello"] = "world"
  204. n := dummyObject{
  205. Name: "testNw",
  206. NetworkType: "bridge",
  207. EnableIPv6: true,
  208. Rec: &recStruct{"gen", 5, cDict, 0, false, false},
  209. ID: id,
  210. DBIndex: 0,
  211. ReturnValue: retValue,
  212. DBExists: false,
  213. SkipSave: false}
  214. generic := make(map[string]interface{})
  215. generic["label1"] = &recStruct{"value1", 1, cDict, 0, false, false}
  216. generic["label2"] = "subnet=10.1.1.0/16"
  217. n.Generic = generic
  218. return &n
  219. }