datastore_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package datastore
  2. import (
  3. "encoding/json"
  4. "reflect"
  5. "testing"
  6. "github.com/docker/libnetwork/config"
  7. "github.com/docker/libnetwork/options"
  8. _ "github.com/docker/libnetwork/testutils"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. var dummyKey = "dummy"
  12. // NewCustomDataStore can be used by other Tests in order to use custom datastore
  13. func NewTestDataStore() DataStore {
  14. return &datastore{store: NewMockStore()}
  15. }
  16. func TestKey(t *testing.T) {
  17. eKey := []string{"hello", "world"}
  18. sKey := Key(eKey...)
  19. if sKey != "docker/libnetwork/hello/world/" {
  20. t.Fatalf("unexpected key : %s", sKey)
  21. }
  22. }
  23. func TestParseKey(t *testing.T) {
  24. keySlice, err := ParseKey("/docker/libnetwork/hello/world/")
  25. if err != nil {
  26. t.Fatal(err)
  27. }
  28. eKey := []string{"hello", "world"}
  29. if len(keySlice) < 2 || !reflect.DeepEqual(eKey, keySlice) {
  30. t.Fatalf("unexpected unkey : %s", keySlice)
  31. }
  32. }
  33. func TestInvalidDataStore(t *testing.T) {
  34. config := &config.DatastoreCfg{}
  35. config.Embedded = false
  36. config.Client.Provider = "invalid"
  37. config.Client.Address = "localhost:8500"
  38. _, err := NewDataStore(config)
  39. if err == nil {
  40. t.Fatal("Invalid Datastore connection configuration must result in a failure")
  41. }
  42. }
  43. func TestKVObjectFlatKey(t *testing.T) {
  44. store := NewTestDataStore()
  45. expected := dummyKVObject("1000", true)
  46. err := store.PutObject(expected)
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. keychain := []string{dummyKey, "1000"}
  51. data, err := store.KVStore().Get(Key(keychain...))
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. var n dummyObject
  56. json.Unmarshal(data.Value, &n)
  57. if n.Name != expected.Name {
  58. t.Fatalf("Dummy object doesn't match the expected object")
  59. }
  60. }
  61. func TestAtomicKVObjectFlatKey(t *testing.T) {
  62. store := NewTestDataStore()
  63. expected := dummyKVObject("1111", true)
  64. assert.False(t, expected.Exists())
  65. err := store.PutObjectAtomic(expected)
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. assert.True(t, expected.Exists())
  70. // PutObjectAtomic automatically sets the Index again. Hence the following must pass.
  71. err = store.PutObjectAtomic(expected)
  72. if err != nil {
  73. t.Fatal("Atomic update should succeed.")
  74. }
  75. // Get the latest index and try PutObjectAtomic again for the same Key
  76. // This must succeed as well
  77. data, err := store.KVStore().Get(Key(expected.Key()...))
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. n := dummyObject{}
  82. json.Unmarshal(data.Value, &n)
  83. n.ID = "1111"
  84. n.SetIndex(data.LastIndex)
  85. n.ReturnValue = true
  86. err = store.PutObjectAtomic(&n)
  87. if err != nil {
  88. t.Fatal(err)
  89. }
  90. // Get the Object using GetObject, then set again.
  91. newObj := dummyObject{}
  92. err = store.GetObject(Key(expected.Key()...), &newObj)
  93. assert.True(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() DataScope {
  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. }