|
@@ -6,9 +6,10 @@ import (
|
|
|
|
|
|
"github.com/docker/docker/libnetwork/options"
|
|
|
"gotest.tools/v3/assert"
|
|
|
+ is "gotest.tools/v3/assert/cmp"
|
|
|
)
|
|
|
|
|
|
-var dummyKey = "dummy"
|
|
|
+const dummyKey = "dummy"
|
|
|
|
|
|
// NewTestDataStore can be used by other Tests in order to use custom datastore
|
|
|
func NewTestDataStore() *Store {
|
|
@@ -16,11 +17,9 @@ func NewTestDataStore() *Store {
|
|
|
}
|
|
|
|
|
|
func TestKey(t *testing.T) {
|
|
|
- eKey := []string{"hello", "world"}
|
|
|
- sKey := Key(eKey...)
|
|
|
- if sKey != "docker/network/v1.0/hello/world/" {
|
|
|
- t.Fatalf("unexpected key : %s", sKey)
|
|
|
- }
|
|
|
+ sKey := Key("hello", "world")
|
|
|
+ const expected = "docker/network/v1.0/hello/world/"
|
|
|
+ assert.Check(t, is.Equal(sKey, expected))
|
|
|
}
|
|
|
|
|
|
func TestInvalidDataStore(t *testing.T) {
|
|
@@ -30,28 +29,19 @@ func TestInvalidDataStore(t *testing.T) {
|
|
|
Address: "localhost:8500",
|
|
|
},
|
|
|
})
|
|
|
- if err == nil {
|
|
|
- t.Fatal("Invalid Datastore connection configuration must result in a failure")
|
|
|
- }
|
|
|
+ assert.Check(t, is.Error(err, "unsupported KV store"))
|
|
|
}
|
|
|
|
|
|
func TestKVObjectFlatKey(t *testing.T) {
|
|
|
store := NewTestDataStore()
|
|
|
expected := dummyKVObject("1000", true)
|
|
|
err := store.PutObjectAtomic(expected)
|
|
|
- if err != nil {
|
|
|
- t.Fatal(err)
|
|
|
- }
|
|
|
- keychain := []string{dummyKey, "1000"}
|
|
|
- data, err := store.KVStore().Get(Key(keychain...))
|
|
|
- if err != nil {
|
|
|
- t.Fatal(err)
|
|
|
- }
|
|
|
- var n dummyObject
|
|
|
- json.Unmarshal(data.Value, &n)
|
|
|
- if n.Name != expected.Name {
|
|
|
- t.Fatal("Dummy object doesn't match the expected object")
|
|
|
- }
|
|
|
+ assert.Check(t, err)
|
|
|
+
|
|
|
+ n := dummyObject{ID: "1000"} // GetObject uses KVObject.Key() for cache lookup.
|
|
|
+ err = store.GetObject(Key(dummyKey, "1000"), &n)
|
|
|
+ assert.Check(t, err)
|
|
|
+ assert.Check(t, is.Equal(n.Name, expected.Name))
|
|
|
}
|
|
|
|
|
|
func TestAtomicKVObjectFlatKey(t *testing.T) {
|
|
@@ -59,45 +49,30 @@ func TestAtomicKVObjectFlatKey(t *testing.T) {
|
|
|
expected := dummyKVObject("1111", true)
|
|
|
assert.Check(t, !expected.Exists())
|
|
|
err := store.PutObjectAtomic(expected)
|
|
|
- if err != nil {
|
|
|
- t.Fatal(err)
|
|
|
- }
|
|
|
+ assert.Check(t, err)
|
|
|
assert.Check(t, expected.Exists())
|
|
|
|
|
|
// PutObjectAtomic automatically sets the Index again. Hence the following must pass.
|
|
|
|
|
|
err = store.PutObjectAtomic(expected)
|
|
|
- if err != nil {
|
|
|
- t.Fatal("Atomic update should succeed.")
|
|
|
- }
|
|
|
+ assert.Check(t, err, "Atomic update should succeed.")
|
|
|
|
|
|
// Get the latest index and try PutObjectAtomic again for the same Key
|
|
|
// This must succeed as well
|
|
|
- data, err := store.KVStore().Get(Key(expected.Key()...))
|
|
|
- if err != nil {
|
|
|
- t.Fatal(err)
|
|
|
- }
|
|
|
- n := dummyObject{}
|
|
|
- json.Unmarshal(data.Value, &n)
|
|
|
- n.ID = "1111"
|
|
|
- n.SetIndex(data.LastIndex)
|
|
|
+ n := dummyObject{ID: "1111"} // GetObject uses KVObject.Key() for cache lookup.
|
|
|
+ err = store.GetObject(Key(expected.Key()...), &n)
|
|
|
+ assert.Check(t, err)
|
|
|
n.ReturnValue = true
|
|
|
err = store.PutObjectAtomic(&n)
|
|
|
- if err != nil {
|
|
|
- t.Fatal(err)
|
|
|
- }
|
|
|
+ assert.Check(t, err)
|
|
|
|
|
|
// Get the Object using GetObject, then set again.
|
|
|
- newObj := dummyObject{}
|
|
|
+ newObj := dummyObject{ID: "1111"} // GetObject uses KVObject.Key() for cache lookup.
|
|
|
err = store.GetObject(Key(expected.Key()...), &newObj)
|
|
|
- if err != nil {
|
|
|
- t.Fatal(err)
|
|
|
- }
|
|
|
+ assert.Check(t, err)
|
|
|
assert.Check(t, newObj.Exists())
|
|
|
err = store.PutObjectAtomic(&n)
|
|
|
- if err != nil {
|
|
|
- t.Fatal(err)
|
|
|
- }
|
|
|
+ assert.Check(t, err)
|
|
|
}
|
|
|
|
|
|
// dummy data used to test the datastore
|
|
@@ -161,15 +136,15 @@ func (n *dummyObject) DataScope() string {
|
|
|
}
|
|
|
|
|
|
func (n *dummyObject) MarshalJSON() ([]byte, error) {
|
|
|
- netMap := make(map[string]interface{})
|
|
|
- netMap["name"] = n.Name
|
|
|
- netMap["networkType"] = n.NetworkType
|
|
|
- netMap["enableIPv6"] = n.EnableIPv6
|
|
|
- netMap["generic"] = n.Generic
|
|
|
- return json.Marshal(netMap)
|
|
|
+ return json.Marshal(map[string]interface{}{
|
|
|
+ "name": n.Name,
|
|
|
+ "networkType": n.NetworkType,
|
|
|
+ "enableIPv6": n.EnableIPv6,
|
|
|
+ "generic": n.Generic,
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
-func (n *dummyObject) UnmarshalJSON(b []byte) (err error) {
|
|
|
+func (n *dummyObject) UnmarshalJSON(b []byte) error {
|
|
|
var netMap map[string]interface{}
|
|
|
if err := json.Unmarshal(b, &netMap); err != nil {
|
|
|
return err
|
|
@@ -225,23 +200,23 @@ func (r *recStruct) Skip() bool {
|
|
|
}
|
|
|
|
|
|
func dummyKVObject(id string, retValue bool) *dummyObject {
|
|
|
- cDict := make(map[string]string)
|
|
|
- cDict["foo"] = "bar"
|
|
|
- cDict["hello"] = "world"
|
|
|
- n := dummyObject{
|
|
|
+ cDict := map[string]string{
|
|
|
+ "foo": "bar",
|
|
|
+ "hello": "world",
|
|
|
+ }
|
|
|
+ return &dummyObject{
|
|
|
Name: "testNw",
|
|
|
NetworkType: "bridge",
|
|
|
EnableIPv6: true,
|
|
|
- Rec: &recStruct{"gen", 5, cDict, 0, false, false},
|
|
|
+ Rec: &recStruct{Name: "gen", Field1: 5, Dict: cDict},
|
|
|
ID: id,
|
|
|
DBIndex: 0,
|
|
|
ReturnValue: retValue,
|
|
|
DBExists: false,
|
|
|
SkipSave: false,
|
|
|
+ Generic: map[string]interface{}{
|
|
|
+ "label1": &recStruct{Name: "value1", Field1: 1, Dict: cDict},
|
|
|
+ "label2": "subnet=10.1.1.0/16",
|
|
|
+ },
|
|
|
}
|
|
|
- generic := make(map[string]interface{})
|
|
|
- generic["label1"] = &recStruct{"value1", 1, cDict, 0, false, false}
|
|
|
- generic["label2"] = "subnet=10.1.1.0/16"
|
|
|
- n.Generic = generic
|
|
|
- return &n
|
|
|
}
|