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"
|
2023-06-26 15:54:28 +00:00
|
|
|
store "github.com/docker/docker/libnetwork/internal/kvstore"
|
2021-04-06 00:24:47 +00:00
|
|
|
"github.com/docker/docker/libnetwork/netlabel"
|
|
|
|
"github.com/docker/docker/libnetwork/options"
|
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) {
|
2023-07-02 16:14:01 +00:00
|
|
|
cfgOptions := []config.Option{func(c *config.Config) {
|
|
|
|
c.Scope.Client.Provider = provider
|
|
|
|
c.Scope.Client.Address = url
|
|
|
|
c.Scope.Client.Config = storeConfig
|
|
|
|
}}
|
2015-09-16 11:42:35 +00:00
|
|
|
|
2023-07-02 16:14:01 +00:00
|
|
|
cfgOptions = append(cfgOptions, config.OptionDriverConfig("host", map[string]interface{}{
|
|
|
|
netlabel.GenericData: options.Generic{},
|
|
|
|
}))
|
2015-09-16 11:42:35 +00:00
|
|
|
|
2023-07-02 15:58:00 +00:00
|
|
|
testController, err := New(cfgOptions...)
|
2015-09-16 11:42:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error new controller: %v", err)
|
|
|
|
}
|
2023-07-02 15:58:00 +00:00
|
|
|
defer testController.Stop()
|
|
|
|
nw, err := testController.NewNetwork("host", "host", "")
|
2015-09-16 11:42:35 +00:00
|
|
|
if err != nil {
|
2023-07-05 10:23:03 +00:00
|
|
|
t.Fatalf(`Error creating default "host" network: %v`, err)
|
2015-09-16 11:42:35 +00:00
|
|
|
}
|
|
|
|
ep, err := nw.CreateEndpoint("newendpoint", []EndpointOption{}...)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error creating endpoint: %v", err)
|
|
|
|
}
|
2024-01-24 00:24:18 +00:00
|
|
|
|
2023-07-25 12:47:45 +00:00
|
|
|
nwKVObject := &Network{id: nw.ID()}
|
2024-01-24 00:24:18 +00:00
|
|
|
err = testController.getStore().GetObject(nwKVObject)
|
2023-07-25 12:47:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error when retrieving network key from store: %v", err)
|
|
|
|
}
|
|
|
|
if !nwKVObject.Exists() {
|
|
|
|
t.Errorf("Network key should have been created.")
|
|
|
|
}
|
|
|
|
|
|
|
|
epKVObject := &Endpoint{network: nw, id: ep.ID()}
|
2024-01-24 00:24:18 +00:00
|
|
|
err = testController.getStore().GetObject(epKVObject)
|
2023-07-25 12:47:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error when retrieving Endpoint key from store: %v", err)
|
2015-09-16 11:42:35 +00:00
|
|
|
}
|
2023-07-25 12:47:45 +00:00
|
|
|
if !epKVObject.Exists() {
|
|
|
|
t.Errorf("Endpoint key should have been created.")
|
2015-09-16 11:42:35 +00:00
|
|
|
}
|
2023-07-25 12:47:45 +00:00
|
|
|
testController.Stop()
|
2015-09-16 11:42:35 +00:00
|
|
|
|
|
|
|
// test restore of local store
|
2023-07-02 15:58:00 +00:00
|
|
|
testController, err = New(cfgOptions...)
|
2015-09-16 11:42:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error creating controller: %v", err)
|
|
|
|
}
|
2023-07-02 15:58:00 +00:00
|
|
|
defer testController.Stop()
|
|
|
|
if _, err = testController.NetworkByID(nw.ID()); err != nil {
|
2023-07-25 12:47:45 +00:00
|
|
|
t.Errorf("Error getting network %v", err)
|
2015-09-16 11:42:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2023-07-02 16:14:01 +00:00
|
|
|
c.Scope.Client.Provider = "boltdb"
|
|
|
|
c.Scope.Client.Address = tmp
|
|
|
|
c.Scope.Client.Config = &store.Config{Bucket: "testBackend"}
|
2015-09-16 11:42:35 +00:00
|
|
|
}
|
|
|
|
}
|