github.com/tonistiigi/docker@v0.10.1-0.20240229224939-974013b0dc6a/libnetwork/store_test.go (about)

     1  package libnetwork
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/docker/docker/libnetwork/config"
     9  	store "github.com/docker/docker/libnetwork/internal/kvstore"
    10  	"github.com/docker/docker/libnetwork/netlabel"
    11  	"github.com/docker/docker/libnetwork/options"
    12  )
    13  
    14  func testLocalBackend(t *testing.T, provider, url string, storeConfig *store.Config) {
    15  	cfgOptions := []config.Option{func(c *config.Config) {
    16  		c.Scope.Client.Provider = provider
    17  		c.Scope.Client.Address = url
    18  		c.Scope.Client.Config = storeConfig
    19  	}}
    20  
    21  	cfgOptions = append(cfgOptions, config.OptionDriverConfig("host", map[string]interface{}{
    22  		netlabel.GenericData: options.Generic{},
    23  	}))
    24  
    25  	testController, err := New(cfgOptions...)
    26  	if err != nil {
    27  		t.Fatalf("Error new controller: %v", err)
    28  	}
    29  	defer testController.Stop()
    30  	nw, err := testController.NewNetwork("host", "host", "")
    31  	if err != nil {
    32  		t.Fatalf(`Error creating default "host" network: %v`, err)
    33  	}
    34  	ep, err := nw.CreateEndpoint("newendpoint", []EndpointOption{}...)
    35  	if err != nil {
    36  		t.Fatalf("Error creating endpoint: %v", err)
    37  	}
    38  
    39  	nwKVObject := &Network{id: nw.ID()}
    40  	err = testController.getStore().GetObject(nwKVObject)
    41  	if err != nil {
    42  		t.Errorf("Error when retrieving network key from store: %v", err)
    43  	}
    44  	if !nwKVObject.Exists() {
    45  		t.Errorf("Network key should have been created.")
    46  	}
    47  
    48  	epKVObject := &Endpoint{network: nw, id: ep.ID()}
    49  	err = testController.getStore().GetObject(epKVObject)
    50  	if err != nil {
    51  		t.Errorf("Error when retrieving Endpoint key from store: %v", err)
    52  	}
    53  	if !epKVObject.Exists() {
    54  		t.Errorf("Endpoint key should have been created.")
    55  	}
    56  	testController.Stop()
    57  
    58  	// test restore of local store
    59  	testController, err = New(cfgOptions...)
    60  	if err != nil {
    61  		t.Fatalf("Error creating controller: %v", err)
    62  	}
    63  	defer testController.Stop()
    64  	if _, err = testController.NetworkByID(nw.ID()); err != nil {
    65  		t.Errorf("Error getting network %v", err)
    66  	}
    67  }
    68  
    69  // OptionBoltdbWithRandomDBFile function returns a random dir for local store backend
    70  func OptionBoltdbWithRandomDBFile(t *testing.T) config.Option {
    71  	t.Helper()
    72  	tmp := filepath.Join(t.TempDir(), "bolt.db")
    73  	if err := os.WriteFile(tmp, nil, 0o600); err != nil {
    74  		t.Fatal(err)
    75  	}
    76  
    77  	return func(c *config.Config) {
    78  		c.Scope.Client.Provider = "boltdb"
    79  		c.Scope.Client.Address = tmp
    80  		c.Scope.Client.Config = &store.Config{Bucket: "testBackend"}
    81  	}
    82  }