github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/libnetwork/store_test.go (about)

     1  package libnetwork
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"testing"
     7  
     8  	"github.com/docker/libkv/store"
     9  	"github.com/docker/libnetwork/config"
    10  	"github.com/docker/libnetwork/datastore"
    11  	"github.com/docker/libnetwork/netlabel"
    12  	"github.com/docker/libnetwork/options"
    13  )
    14  
    15  func testZooKeeperBackend(t *testing.T) {
    16  	c, err := testNewController(t, "zk", "127.0.0.1:2181/custom_prefix")
    17  	if err != nil {
    18  		t.Fatal(err)
    19  	}
    20  	c.Stop()
    21  }
    22  
    23  func testNewController(t *testing.T, provider, url string) (NetworkController, error) {
    24  	cfgOptions, err := OptionBoltdbWithRandomDBFile()
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  	cfgOptions = append(cfgOptions, config.OptionKVProvider(provider))
    29  	cfgOptions = append(cfgOptions, config.OptionKVProviderURL(url))
    30  	return New(cfgOptions...)
    31  }
    32  
    33  func testLocalBackend(t *testing.T, provider, url string, storeConfig *store.Config) {
    34  	cfgOptions := []config.Option{}
    35  	cfgOptions = append(cfgOptions, config.OptionLocalKVProvider(provider))
    36  	cfgOptions = append(cfgOptions, config.OptionLocalKVProviderURL(url))
    37  	cfgOptions = append(cfgOptions, config.OptionLocalKVProviderConfig(storeConfig))
    38  
    39  	driverOptions := options.Generic{}
    40  	genericOption := make(map[string]interface{})
    41  	genericOption[netlabel.GenericData] = driverOptions
    42  	cfgOptions = append(cfgOptions, config.OptionDriverConfig("host", genericOption))
    43  
    44  	ctrl, err := New(cfgOptions...)
    45  	if err != nil {
    46  		t.Fatalf("Error new controller: %v", err)
    47  	}
    48  	nw, err := ctrl.NewNetwork("host", "host", "")
    49  	if err != nil {
    50  		t.Fatalf("Error creating default \"host\" network: %v", err)
    51  	}
    52  	ep, err := nw.CreateEndpoint("newendpoint", []EndpointOption{}...)
    53  	if err != nil {
    54  		t.Fatalf("Error creating endpoint: %v", err)
    55  	}
    56  	store := ctrl.(*controller).getStore(datastore.LocalScope).KVStore()
    57  	if exists, err := store.Exists(datastore.Key(datastore.NetworkKeyPrefix, string(nw.ID()))); !exists || err != nil {
    58  		t.Fatalf("Network key should have been created.")
    59  	}
    60  	if exists, err := store.Exists(datastore.Key([]string{datastore.EndpointKeyPrefix, string(nw.ID()), string(ep.ID())}...)); !exists || err != nil {
    61  		t.Fatalf("Endpoint key should have been created.")
    62  	}
    63  	store.Close()
    64  
    65  	// test restore of local store
    66  	ctrl, err = New(cfgOptions...)
    67  	if err != nil {
    68  		t.Fatalf("Error creating controller: %v", err)
    69  	}
    70  	if _, err = ctrl.NetworkByID(nw.ID()); err != nil {
    71  		t.Fatalf("Error getting network %v", err)
    72  	}
    73  }
    74  
    75  // OptionBoltdbWithRandomDBFile function returns a random dir for local store backend
    76  func OptionBoltdbWithRandomDBFile() ([]config.Option, error) {
    77  	tmp, err := ioutil.TempFile("", "libnetwork-")
    78  	if err != nil {
    79  		return nil, fmt.Errorf("Error creating temp file: %v", err)
    80  	}
    81  	if err := tmp.Close(); err != nil {
    82  		return nil, fmt.Errorf("Error closing temp file: %v", err)
    83  	}
    84  	cfgOptions := []config.Option{}
    85  	cfgOptions = append(cfgOptions, config.OptionLocalKVProvider("boltdb"))
    86  	cfgOptions = append(cfgOptions, config.OptionLocalKVProviderURL(tmp.Name()))
    87  	sCfg := &store.Config{Bucket: "testBackend"}
    88  	cfgOptions = append(cfgOptions, config.OptionLocalKVProviderConfig(sCfg))
    89  	return cfgOptions, nil
    90  }
    91  
    92  func TestMultipleControllersWithSameStore(t *testing.T) {
    93  	cfgOptions, err := OptionBoltdbWithRandomDBFile()
    94  	if err != nil {
    95  		t.Fatalf("Error getting random boltdb configs %v", err)
    96  	}
    97  	ctrl1, err := New(cfgOptions...)
    98  	if err != nil {
    99  		t.Fatalf("Error new controller: %v", err)
   100  	}
   101  	defer ctrl1.Stop()
   102  	// Use the same boltdb file without closing the previous controller
   103  	_, err = New(cfgOptions...)
   104  	if err != nil {
   105  		t.Fatalf("Local store must support concurrent controllers")
   106  	}
   107  }