2015-08-17 04:07:43 -04:00
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
2015-09-16 07:42:35 -04:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2015-08-17 04:07:43 -04:00
|
|
|
"testing"
|
|
|
|
|
2015-09-16 07:42:35 -04:00
|
|
|
"github.com/docker/libkv/store"
|
2015-08-17 04:07:43 -04:00
|
|
|
"github.com/docker/libnetwork/config"
|
2015-09-16 07:42:35 -04:00
|
|
|
"github.com/docker/libnetwork/datastore"
|
|
|
|
"github.com/docker/libnetwork/netlabel"
|
|
|
|
"github.com/docker/libnetwork/options"
|
2015-08-17 04:07:43 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestZooKeeperBackend(t *testing.T) {
|
2015-10-26 05:48:52 -04:00
|
|
|
c, err := testNewController(t, "zk", "127.0.0.1:2181/custom_prefix")
|
2015-09-22 17:07:23 -04:00
|
|
|
if err != nil {
|
2015-09-16 07:42:35 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-09-22 17:07:23 -04:00
|
|
|
c.Stop()
|
2015-08-17 04:07:43 -04:00
|
|
|
}
|
|
|
|
|
2015-09-22 17:07:23 -04:00
|
|
|
func testNewController(t *testing.T, provider, url string) (NetworkController, error) {
|
2015-09-16 07:42:35 -04:00
|
|
|
cfgOptions, err := OptionBoltdbWithRandomDBFile()
|
|
|
|
if err != nil {
|
2015-09-22 17:07:23 -04:00
|
|
|
return nil, err
|
2015-09-16 07:42:35 -04:00
|
|
|
}
|
|
|
|
cfgOptions = append(cfgOptions, config.OptionKVProvider(provider))
|
|
|
|
cfgOptions = append(cfgOptions, config.OptionKVProviderURL(url))
|
2015-09-22 17:07:23 -04:00
|
|
|
return New(cfgOptions...)
|
2015-08-17 04:07:43 -04:00
|
|
|
}
|
2015-09-16 07:42:35 -04:00
|
|
|
|
|
|
|
func TestBoltdbBackend(t *testing.T) {
|
2015-10-05 07:21:15 -04:00
|
|
|
defer os.Remove(datastore.DefaultScopes("")[datastore.LocalScope].Client.Address)
|
2015-09-16 07:42:35 -04:00
|
|
|
testLocalBackend(t, "", "", nil)
|
|
|
|
defer os.Remove("/tmp/boltdb.db")
|
2015-10-08 18:02:03 -04:00
|
|
|
config := &store.Config{Bucket: "testBackend"}
|
2015-09-22 13:49:15 -04:00
|
|
|
testLocalBackend(t, "boltdb", "/tmp/boltdb.db", config)
|
|
|
|
|
2015-09-16 07:42:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func testLocalBackend(t *testing.T, provider, url string, storeConfig *store.Config) {
|
|
|
|
cfgOptions := []config.Option{}
|
2015-10-08 18:23:19 -04:00
|
|
|
cfgOptions = append(cfgOptions, config.OptionLocalKVProvider(provider))
|
|
|
|
cfgOptions = append(cfgOptions, config.OptionLocalKVProviderURL(url))
|
|
|
|
cfgOptions = append(cfgOptions, config.OptionLocalKVProviderConfig(storeConfig))
|
2015-09-16 07:42:35 -04:00
|
|
|
|
|
|
|
driverOptions := options.Generic{}
|
|
|
|
genericOption := make(map[string]interface{})
|
|
|
|
genericOption[netlabel.GenericData] = driverOptions
|
|
|
|
cfgOptions = append(cfgOptions, config.OptionDriverConfig("host", genericOption))
|
|
|
|
|
|
|
|
ctrl, err := New(cfgOptions...)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error new controller: %v", err)
|
|
|
|
}
|
2016-02-29 14:49:04 -05:00
|
|
|
nw, err := ctrl.NewNetwork("host", "host", "")
|
2015-09-16 07:42:35 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error creating default \"host\" network: %v", err)
|
|
|
|
}
|
|
|
|
ep, err := nw.CreateEndpoint("newendpoint", []EndpointOption{}...)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error creating endpoint: %v", err)
|
|
|
|
}
|
2015-10-05 07:21:15 -04:00
|
|
|
store := ctrl.(*controller).getStore(datastore.LocalScope).KVStore()
|
2015-09-16 07:42:35 -04:00
|
|
|
if exists, err := store.Exists(datastore.Key(datastore.NetworkKeyPrefix, string(nw.ID()))); !exists || err != nil {
|
|
|
|
t.Fatalf("Network key should have been created.")
|
|
|
|
}
|
2015-10-07 23:01:38 -04:00
|
|
|
if exists, err := store.Exists(datastore.Key([]string{datastore.EndpointKeyPrefix, string(nw.ID()), string(ep.ID())}...)); !exists || err != nil {
|
|
|
|
t.Fatalf("Endpoint key should have been created.")
|
2015-09-16 07:42:35 -04:00
|
|
|
}
|
2015-10-08 18:23:19 -04:00
|
|
|
store.Close()
|
2015-09-16 07:42:35 -04:00
|
|
|
|
|
|
|
// test restore of local store
|
|
|
|
ctrl, err = New(cfgOptions...)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error creating controller: %v", err)
|
|
|
|
}
|
|
|
|
if _, err = ctrl.NetworkByID(nw.ID()); err != nil {
|
|
|
|
t.Fatalf("Error getting network %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-22 10:09:39 -04:00
|
|
|
func TestNoPersist(t *testing.T) {
|
|
|
|
cfgOptions, err := OptionBoltdbWithRandomDBFile()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error creating random boltdb file : %v", err)
|
|
|
|
}
|
|
|
|
ctrl, err := New(cfgOptions...)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error new controller: %v", err)
|
|
|
|
}
|
2016-02-29 14:49:04 -05:00
|
|
|
nw, err := ctrl.NewNetwork("host", "host", "", NetworkOptionPersist(false))
|
2015-09-22 10:09:39 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error creating default \"host\" network: %v", err)
|
|
|
|
}
|
|
|
|
ep, err := nw.CreateEndpoint("newendpoint", []EndpointOption{}...)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error creating endpoint: %v", err)
|
|
|
|
}
|
2015-10-05 07:21:15 -04:00
|
|
|
store := ctrl.(*controller).getStore(datastore.LocalScope).KVStore()
|
2015-09-22 10:09:39 -04:00
|
|
|
if exists, _ := store.Exists(datastore.Key(datastore.NetworkKeyPrefix, string(nw.ID()))); exists {
|
|
|
|
t.Fatalf("Network with persist=false should not be stored in KV Store")
|
|
|
|
}
|
|
|
|
if exists, _ := store.Exists(datastore.Key([]string{datastore.EndpointKeyPrefix, string(nw.ID()), string(ep.ID())}...)); exists {
|
|
|
|
t.Fatalf("Endpoint in Network with persist=false should not be stored in KV Store")
|
|
|
|
}
|
|
|
|
store.Close()
|
|
|
|
}
|
|
|
|
|
2015-09-16 07:42:35 -04:00
|
|
|
// OptionBoltdbWithRandomDBFile function returns a random dir for local store backend
|
|
|
|
func OptionBoltdbWithRandomDBFile() ([]config.Option, error) {
|
|
|
|
tmp, err := ioutil.TempFile("", "libnetwork-")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error creating temp file: %v", err)
|
|
|
|
}
|
|
|
|
if err := tmp.Close(); err != nil {
|
|
|
|
return nil, fmt.Errorf("Error closing temp file: %v", err)
|
|
|
|
}
|
|
|
|
cfgOptions := []config.Option{}
|
|
|
|
cfgOptions = append(cfgOptions, config.OptionLocalKVProvider("boltdb"))
|
|
|
|
cfgOptions = append(cfgOptions, config.OptionLocalKVProviderURL(tmp.Name()))
|
2015-10-08 18:02:03 -04:00
|
|
|
sCfg := &store.Config{Bucket: "testBackend"}
|
2015-09-22 13:49:15 -04:00
|
|
|
cfgOptions = append(cfgOptions, config.OptionLocalKVProviderConfig(sCfg))
|
2015-09-16 07:42:35 -04:00
|
|
|
return cfgOptions, nil
|
|
|
|
}
|
2015-09-22 13:49:15 -04:00
|
|
|
|
2015-10-08 18:02:03 -04:00
|
|
|
func TestMultipleControllersWithSameStore(t *testing.T) {
|
2015-09-22 13:49:15 -04:00
|
|
|
cfgOptions, err := OptionBoltdbWithRandomDBFile()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error getting random boltdb configs %v", err)
|
|
|
|
}
|
2015-09-25 12:02:18 -04:00
|
|
|
ctrl1, err := New(cfgOptions...)
|
2015-09-22 13:49:15 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error new controller: %v", err)
|
|
|
|
}
|
2015-09-25 12:02:18 -04:00
|
|
|
defer ctrl1.Stop()
|
2015-09-22 13:49:15 -04:00
|
|
|
// Use the same boltdb file without closing the previous controller
|
2015-10-08 18:02:03 -04:00
|
|
|
_, err = New(cfgOptions...)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Local store must support concurrent controllers")
|
2015-09-22 13:49:15 -04:00
|
|
|
}
|
|
|
|
}
|