datastore_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package datastore
  2. import (
  3. "encoding/json"
  4. "reflect"
  5. "testing"
  6. "github.com/docker/libnetwork/config"
  7. _ "github.com/docker/libnetwork/netutils"
  8. "github.com/docker/libnetwork/options"
  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 doesnt 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. ReturnValue bool
  111. }
  112. func (n *dummyObject) Key() []string {
  113. return []string{dummyKey, n.ID}
  114. }
  115. func (n *dummyObject) KeyPrefix() []string {
  116. return []string{dummyKey}
  117. }
  118. func (n *dummyObject) Value() []byte {
  119. if !n.ReturnValue {
  120. return nil
  121. }
  122. b, err := json.Marshal(n)
  123. if err != nil {
  124. return nil
  125. }
  126. return b
  127. }
  128. func (n *dummyObject) SetValue(value []byte) error {
  129. return json.Unmarshal(value, n)
  130. }
  131. func (n *dummyObject) Index() uint64 {
  132. return n.DBIndex
  133. }
  134. func (n *dummyObject) SetIndex(index uint64) {
  135. n.DBIndex = index
  136. n.DBExists = true
  137. }
  138. func (n *dummyObject) Exists() bool {
  139. return n.DBExists
  140. }
  141. func (n *dummyObject) MarshalJSON() ([]byte, error) {
  142. netMap := make(map[string]interface{})
  143. netMap["name"] = n.Name
  144. netMap["networkType"] = n.NetworkType
  145. netMap["enableIPv6"] = n.EnableIPv6
  146. netMap["generic"] = n.Generic
  147. return json.Marshal(netMap)
  148. }
  149. func (n *dummyObject) UnmarshalJSON(b []byte) (err error) {
  150. var netMap map[string]interface{}
  151. if err := json.Unmarshal(b, &netMap); err != nil {
  152. return err
  153. }
  154. n.Name = netMap["name"].(string)
  155. n.NetworkType = netMap["networkType"].(string)
  156. n.EnableIPv6 = netMap["enableIPv6"].(bool)
  157. n.Generic = netMap["generic"].(map[string]interface{})
  158. return nil
  159. }
  160. // dummy structure to test "recursive" cases
  161. type recStruct struct {
  162. Name string `kv:"leaf"`
  163. Field1 int `kv:"leaf"`
  164. Dict map[string]string `kv:"iterative"`
  165. DBIndex uint64
  166. DBExists bool
  167. }
  168. func (r *recStruct) Key() []string {
  169. return []string{"recStruct"}
  170. }
  171. func (r *recStruct) Value() []byte {
  172. b, err := json.Marshal(r)
  173. if err != nil {
  174. return nil
  175. }
  176. return b
  177. }
  178. func (r *recStruct) SetValue(value []byte) error {
  179. return json.Unmarshal(value, r)
  180. }
  181. func (r *recStruct) Index() uint64 {
  182. return r.DBIndex
  183. }
  184. func (r *recStruct) SetIndex(index uint64) {
  185. r.DBIndex = index
  186. r.DBExists = true
  187. }
  188. func (r *recStruct) Exists() bool {
  189. return r.DBExists
  190. }
  191. func dummyKVObject(id string, retValue bool) *dummyObject {
  192. cDict := make(map[string]string)
  193. cDict["foo"] = "bar"
  194. cDict["hello"] = "world"
  195. n := dummyObject{
  196. Name: "testNw",
  197. NetworkType: "bridge",
  198. EnableIPv6: true,
  199. Rec: &recStruct{"gen", 5, cDict, 0, false},
  200. ID: id,
  201. DBIndex: 0,
  202. ReturnValue: retValue,
  203. DBExists: false}
  204. generic := make(map[string]interface{})
  205. generic["label1"] = &recStruct{"value1", 1, cDict, 0, false}
  206. generic["label2"] = "subnet=10.1.1.0/16"
  207. n.Generic = generic
  208. return &n
  209. }