moby/libnetwork/store_linux_test.go
Cory Snider d21d0884ae libnetwork: share a single datastore with drivers
The bbolt library wants exclusive access to the boltdb file and uses
file locking to assure that is the case. The controller and each network
driver that needs persistent storage instantiates its own unique
datastore instance, backed by the same boltdb file. The boltdb kvstore
implementation works around multiple access to the same boltdb file by
aggressively closing the boltdb file between each transaction. This is
very inefficient. Have the controller pass its datastore instance into
the drivers and enable the PersistConnection option to disable closing
the boltdb between transactions.

Set data-dir in unit tests which instantiate libnetwork controllers so
they don't hang trying to lock the default boltdb database file.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2024-01-31 21:08:34 -05:00

60 lines
1.8 KiB
Go

package libnetwork
import (
"errors"
"path/filepath"
"testing"
store "github.com/docker/docker/libnetwork/internal/kvstore"
)
func TestBoltdbBackend(t *testing.T) {
tmpPath := filepath.Join(t.TempDir(), "boltdb.db")
testLocalBackend(t, "boltdb", tmpPath, &store.Config{
Bucket: "testBackend",
})
}
func TestNoPersist(t *testing.T) {
configOption := OptionBoltdbWithRandomDBFile(t)
testController, err := New(configOption)
if err != nil {
t.Fatalf("Error creating new controller: %v", err)
}
defer testController.Stop()
nw, err := testController.NewNetwork("host", "host", "", NetworkOptionPersist(false))
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)
}
testController.Stop()
// Create a new controller using the same database-file. The network
// should not have persisted.
testController, err = New(configOption)
if err != nil {
t.Fatalf("Error creating new controller: %v", err)
}
defer testController.Stop()
nwKVObject := &Network{id: nw.ID()}
err = testController.getStore().GetObject(nwKVObject)
if !errors.Is(err, store.ErrKeyNotFound) {
t.Errorf("Expected %q error when retrieving network from store, got: %q", store.ErrKeyNotFound, err)
}
if nwKVObject.Exists() {
t.Errorf("Network with persist=false should not be stored in KV Store")
}
epKVObject := &Endpoint{network: nw, id: ep.ID()}
err = testController.getStore().GetObject(epKVObject)
if !errors.Is(err, store.ErrKeyNotFound) {
t.Errorf("Expected %v error when retrieving endpoint from store, got: %v", store.ErrKeyNotFound, err)
}
if epKVObject.Exists() {
t.Errorf("Endpoint in Network with persist=false should not be stored in KV Store")
}
}