2015-08-17 04:07:43 -04:00
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
2015-09-16 07:42:35 -04:00
|
|
|
"fmt"
|
2021-08-24 06:10:50 -04:00
|
|
|
"os"
|
2015-08-17 04:07:43 -04:00
|
|
|
"testing"
|
|
|
|
|
2021-04-05 20:24:47 -04:00
|
|
|
"github.com/docker/docker/libnetwork/config"
|
|
|
|
"github.com/docker/docker/libnetwork/datastore"
|
|
|
|
"github.com/docker/docker/libnetwork/netlabel"
|
|
|
|
"github.com/docker/docker/libnetwork/options"
|
2021-05-27 20:15:56 -04:00
|
|
|
"github.com/docker/libkv/store"
|
2015-08-17 04:07:43 -04:00
|
|
|
)
|
|
|
|
|
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()
|
2021-05-27 20:15:56 -04:00
|
|
|
if exists, err := store.Exists(datastore.Key(datastore.NetworkKeyPrefix, nw.ID())); !exists || err != nil {
|
2015-09-16 07:42:35 -04:00
|
|
|
t.Fatalf("Network key should have been created.")
|
|
|
|
}
|
2021-05-27 20:15:56 -04:00
|
|
|
if exists, err := store.Exists(datastore.Key([]string{datastore.EndpointKeyPrefix, nw.ID(), ep.ID()}...)); !exists || err != nil {
|
2015-10-07 23:01:38 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OptionBoltdbWithRandomDBFile function returns a random dir for local store backend
|
|
|
|
func OptionBoltdbWithRandomDBFile() ([]config.Option, error) {
|
2021-08-24 06:10:50 -04:00
|
|
|
tmp, err := os.CreateTemp("", "libnetwork-")
|
2015-09-16 07:42:35 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|