2016-05-20 20:23:53 +00:00
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
2023-07-25 12:47:45 +00:00
|
|
|
"errors"
|
2023-07-02 16:14:24 +00:00
|
|
|
"path/filepath"
|
2016-05-20 20:23:53 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-06-26 15:54:28 +00:00
|
|
|
store "github.com/docker/docker/libnetwork/internal/kvstore"
|
2016-05-20 20:23:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBoltdbBackend(t *testing.T) {
|
2023-07-02 16:14:24 +00:00
|
|
|
tmpPath := filepath.Join(t.TempDir(), "boltdb.db")
|
|
|
|
testLocalBackend(t, "boltdb", tmpPath, &store.Config{
|
|
|
|
Bucket: "testBackend",
|
|
|
|
})
|
2016-05-20 20:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoPersist(t *testing.T) {
|
2024-01-31 22:24:47 +00:00
|
|
|
configOption := OptionBoltdbWithRandomDBFile(t)
|
2023-07-25 12:50:11 +00:00
|
|
|
testController, err := New(configOption)
|
2016-05-20 20:23:53 +00:00
|
|
|
if err != nil {
|
2023-07-25 12:50:11 +00:00
|
|
|
t.Fatalf("Error creating new controller: %v", err)
|
2016-05-20 20:23:53 +00:00
|
|
|
}
|
2023-07-02 15:58:00 +00:00
|
|
|
defer testController.Stop()
|
|
|
|
nw, err := testController.NewNetwork("host", "host", "", NetworkOptionPersist(false))
|
2016-05-20 20:23:53 +00:00
|
|
|
if err != nil {
|
2023-07-05 10:23:03 +00:00
|
|
|
t.Fatalf(`Error creating default "host" network: %v`, err)
|
2016-05-20 20:23:53 +00:00
|
|
|
}
|
|
|
|
ep, err := nw.CreateEndpoint("newendpoint", []EndpointOption{}...)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error creating endpoint: %v", err)
|
|
|
|
}
|
2023-07-25 12:50:11 +00:00
|
|
|
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()
|
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 !errors.Is(err, store.ErrKeyNotFound) {
|
2023-07-25 12:50:11 +00:00
|
|
|
t.Errorf("Expected %q error when retrieving network from store, got: %q", store.ErrKeyNotFound, err)
|
2023-07-25 12:47:45 +00:00
|
|
|
}
|
|
|
|
if nwKVObject.Exists() {
|
|
|
|
t.Errorf("Network with persist=false should not be stored in KV Store")
|
|
|
|
}
|
|
|
|
|
|
|
|
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 !errors.Is(err, store.ErrKeyNotFound) {
|
|
|
|
t.Errorf("Expected %v error when retrieving endpoint from store, got: %v", store.ErrKeyNotFound, err)
|
2016-05-20 20:23:53 +00:00
|
|
|
}
|
2023-07-25 12:47:45 +00:00
|
|
|
if epKVObject.Exists() {
|
|
|
|
t.Errorf("Endpoint in Network with persist=false should not be stored in KV Store")
|
2016-05-20 20:23:53 +00:00
|
|
|
}
|
|
|
|
}
|