2015-08-17 08:07:43 +00:00
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
2021-08-24 10:10:50 +00:00
|
|
|
"os"
|
2022-11-08 23:06:30 +00:00
|
|
|
"path/filepath"
|
2015-08-17 08:07:43 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-04-06 00:24:47 +00: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-28 00:15:56 +00:00
|
|
|
"github.com/docker/libkv/store"
|
2015-08-17 08:07:43 +00:00
|
|
|
)
|
|
|
|
|
2015-09-16 11:42:35 +00:00
|
|
|
func testLocalBackend(t *testing.T, provider, url string, storeConfig *store.Config) {
|
|
|
|
cfgOptions := []config.Option{}
|
2015-10-08 22:23:19 +00:00
|
|
|
cfgOptions = append(cfgOptions, config.OptionLocalKVProvider(provider))
|
|
|
|
cfgOptions = append(cfgOptions, config.OptionLocalKVProviderURL(url))
|
|
|
|
cfgOptions = append(cfgOptions, config.OptionLocalKVProviderConfig(storeConfig))
|
2015-09-16 11:42:35 +00: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)
|
|
|
|
}
|
2022-11-08 22:49:52 +00:00
|
|
|
defer ctrl.Stop()
|
2016-02-29 19:49:04 +00:00
|
|
|
nw, err := ctrl.NewNetwork("host", "host", "")
|
2015-09-16 11:42:35 +00: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)
|
|
|
|
}
|
2023-01-14 00:04:43 +00:00
|
|
|
store := ctrl.getStore().KVStore()
|
2021-05-28 00:15:56 +00:00
|
|
|
if exists, err := store.Exists(datastore.Key(datastore.NetworkKeyPrefix, nw.ID())); !exists || err != nil {
|
2015-09-16 11:42:35 +00:00
|
|
|
t.Fatalf("Network key should have been created.")
|
|
|
|
}
|
2021-05-28 00:15:56 +00:00
|
|
|
if exists, err := store.Exists(datastore.Key([]string{datastore.EndpointKeyPrefix, nw.ID(), ep.ID()}...)); !exists || err != nil {
|
2015-10-08 03:01:38 +00:00
|
|
|
t.Fatalf("Endpoint key should have been created.")
|
2015-09-16 11:42:35 +00:00
|
|
|
}
|
2015-10-08 22:23:19 +00:00
|
|
|
store.Close()
|
2015-09-16 11:42:35 +00:00
|
|
|
|
|
|
|
// test restore of local store
|
|
|
|
ctrl, err = New(cfgOptions...)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error creating controller: %v", err)
|
|
|
|
}
|
2022-11-08 22:49:52 +00:00
|
|
|
defer ctrl.Stop()
|
2015-09-16 11:42:35 +00:00
|
|
|
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
|
2022-11-08 23:06:30 +00:00
|
|
|
func OptionBoltdbWithRandomDBFile(t *testing.T) config.Option {
|
|
|
|
t.Helper()
|
|
|
|
tmp := filepath.Join(t.TempDir(), "bolt.db")
|
|
|
|
if err := os.WriteFile(tmp, nil, 0o600); err != nil {
|
|
|
|
t.Fatal(err)
|
2015-09-16 11:42:35 +00:00
|
|
|
}
|
2022-11-08 23:06:30 +00:00
|
|
|
|
|
|
|
return func(c *config.Config) {
|
|
|
|
config.OptionLocalKVProvider("boltdb")(c)
|
|
|
|
config.OptionLocalKVProviderURL(tmp)(c)
|
|
|
|
config.OptionLocalKVProviderConfig(&store.Config{Bucket: "testBackend"})(c)
|
2015-09-16 11:42:35 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-22 17:49:15 +00:00
|
|
|
|
2015-10-08 22:02:03 +00:00
|
|
|
func TestMultipleControllersWithSameStore(t *testing.T) {
|
2022-11-08 23:06:30 +00:00
|
|
|
cfgOptions := OptionBoltdbWithRandomDBFile(t)
|
|
|
|
ctrl1, err := New(cfgOptions)
|
2015-09-22 17:49:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error new controller: %v", err)
|
|
|
|
}
|
2015-09-25 16:02:18 +00:00
|
|
|
defer ctrl1.Stop()
|
2015-09-22 17:49:15 +00:00
|
|
|
// Use the same boltdb file without closing the previous controller
|
2022-11-08 23:06:30 +00:00
|
|
|
ctrl2, err := New(cfgOptions)
|
2015-10-08 22:02:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Local store must support concurrent controllers")
|
2015-09-22 17:49:15 +00:00
|
|
|
}
|
2022-11-08 22:49:52 +00:00
|
|
|
ctrl2.Stop()
|
2015-09-22 17:49:15 +00:00
|
|
|
}
|