datastore_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package datastore
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "github.com/docker/libnetwork/config"
  6. _ "github.com/docker/libnetwork/netutils"
  7. "github.com/docker/libnetwork/options"
  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{store: NewMockStore()}
  13. }
  14. func TestInvalidDataStore(t *testing.T) {
  15. config := &config.DatastoreCfg{}
  16. config.Embedded = false
  17. config.Client.Provider = "invalid"
  18. config.Client.Address = "localhost:8500"
  19. _, err := NewDataStore(config)
  20. if err == nil {
  21. t.Fatal("Invalid Datastore connection configuration must result in a failure")
  22. }
  23. }
  24. func TestKVObjectFlatKey(t *testing.T) {
  25. store := NewTestDataStore()
  26. expected := dummyKVObject("1000", true)
  27. err := store.PutObject(expected)
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. keychain := []string{dummyKey, "1000"}
  32. data, _, err := store.KVStore().Get(Key(keychain...))
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. var n dummyObject
  37. json.Unmarshal(data, &n)
  38. if n.Name != expected.Name {
  39. t.Fatalf("Dummy object doesnt match the expected object")
  40. }
  41. }
  42. func TestAtomicKVObjectFlatKey(t *testing.T) {
  43. store := NewTestDataStore()
  44. expected := dummyKVObject("1111", true)
  45. err := store.PutObjectAtomic(expected)
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. // PutObjectAtomic automatically sets the Index again. Hence the following must pass.
  50. err = store.PutObjectAtomic(expected)
  51. if err != nil {
  52. t.Fatal("Atomic update with an older Index must fail")
  53. }
  54. // Get the latest index and try PutObjectAtomic again for the same Key
  55. // This must succeed as well
  56. data, index, err := store.KVStore().Get(Key(expected.Key()...))
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. n := dummyObject{}
  61. json.Unmarshal(data, &n)
  62. n.ID = "1111"
  63. n.DBIndex = index
  64. n.ReturnValue = true
  65. err = store.PutObjectAtomic(&n)
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. }
  70. // dummy data used to test the datastore
  71. type dummyObject struct {
  72. Name string `kv:"leaf"`
  73. NetworkType string `kv:"leaf"`
  74. EnableIPv6 bool `kv:"leaf"`
  75. Rec *recStruct `kv:"recursive"`
  76. Dict map[string]*recStruct `kv:"iterative"`
  77. Generic options.Generic `kv:"iterative"`
  78. ID string
  79. DBIndex uint64
  80. ReturnValue bool
  81. }
  82. func (n *dummyObject) Key() []string {
  83. return []string{dummyKey, n.ID}
  84. }
  85. func (n *dummyObject) Value() []byte {
  86. if !n.ReturnValue {
  87. return nil
  88. }
  89. b, err := json.Marshal(n)
  90. if err != nil {
  91. return nil
  92. }
  93. return b
  94. }
  95. func (n *dummyObject) Index() uint64 {
  96. return n.DBIndex
  97. }
  98. func (n *dummyObject) SetIndex(index uint64) {
  99. n.DBIndex = index
  100. }
  101. func (n *dummyObject) MarshalJSON() ([]byte, error) {
  102. netMap := make(map[string]interface{})
  103. netMap["name"] = n.Name
  104. netMap["networkType"] = n.NetworkType
  105. netMap["enableIPv6"] = n.EnableIPv6
  106. netMap["generic"] = n.Generic
  107. return json.Marshal(netMap)
  108. }
  109. func (n *dummyObject) UnmarshalJSON(b []byte) (err error) {
  110. var netMap map[string]interface{}
  111. if err := json.Unmarshal(b, &netMap); err != nil {
  112. return err
  113. }
  114. n.Name = netMap["name"].(string)
  115. n.NetworkType = netMap["networkType"].(string)
  116. n.EnableIPv6 = netMap["enableIPv6"].(bool)
  117. n.Generic = netMap["generic"].(map[string]interface{})
  118. return nil
  119. }
  120. // dummy structure to test "recursive" cases
  121. type recStruct struct {
  122. Name string `kv:"leaf"`
  123. Field1 int `kv:"leaf"`
  124. Dict map[string]string `kv:"iterative"`
  125. DBIndex uint64
  126. }
  127. func (r *recStruct) Key() []string {
  128. return []string{"recStruct"}
  129. }
  130. func (r *recStruct) Value() []byte {
  131. b, err := json.Marshal(r)
  132. if err != nil {
  133. return nil
  134. }
  135. return b
  136. }
  137. func (r *recStruct) Index() uint64 {
  138. return r.DBIndex
  139. }
  140. func (r *recStruct) SetIndex(index uint64) {
  141. r.DBIndex = index
  142. }
  143. func dummyKVObject(id string, retValue bool) *dummyObject {
  144. cDict := make(map[string]string)
  145. cDict["foo"] = "bar"
  146. cDict["hello"] = "world"
  147. n := dummyObject{
  148. Name: "testNw",
  149. NetworkType: "bridge",
  150. EnableIPv6: true,
  151. Rec: &recStruct{"gen", 5, cDict, 0},
  152. ID: id,
  153. DBIndex: 0,
  154. ReturnValue: retValue}
  155. generic := make(map[string]interface{})
  156. generic["label1"] = &recStruct{"value1", 1, cDict, 0}
  157. generic["label2"] = "subnet=10.1.1.0/16"
  158. n.Generic = generic
  159. return &n
  160. }