func TestNoPersist(t *testing.T) { cfgOptions, err := OptionBoltdbWithRandomDBFile() if err != nil { t.Fatalf("Error creating random boltdb file : %v", err) } ctrl, err := New(cfgOptions...) if err != nil { t.Fatalf("Error new controller: %v", err) } nw, err := ctrl.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) } store := ctrl.(*controller).getStore(datastore.LocalScope).KVStore() if exists, _ := store.Exists(datastore.Key(datastore.NetworkKeyPrefix, string(nw.ID()))); exists { t.Fatalf("Network with persist=false should not be stored in KV Store") } if exists, _ := store.Exists(datastore.Key([]string{datastore.EndpointKeyPrefix, string(nw.ID()), string(ep.ID())}...)); exists { t.Fatalf("Endpoint in Network with persist=false should not be stored in KV Store") } store.Close() }
func New(config *config.BalancerConfig) (Store, error) { u, err := url.Parse(config.StoreAddress) if err != nil { return nil, errors.Wrap(err, "error paring store address") } scheme := u.Scheme if scheme != "consul" && scheme != "etcd" { return nil, ErrUnsupportedStore } //Validating open connection _, err = net.Dial("tcp", u.Host) if err != nil { return nil, errors.Wrap(err, "Store connection failed. Make sure your store is up and running.") } kv, err := libkv.NewStore( kv.Backend(scheme), []string{u.Host}, nil, ) if err != nil { kv.Close() return nil, errors.Wrap(err, "Cannot create store consul") } svcsChs := []chan []types.Service{} dstsChs := []chan []types.Destination{} checksChs := []chan []types.CheckSpec{} validate := validator.New() // Registering custom validations validate.RegisterValidation("protocols", validateValues(types.Protocols)) validate.RegisterValidation("schedulers", validateValues(types.Schedulers)) fusisStore := &FusisStore{ kv: kv, prefix: config.StorePrefix, validate: validate, servicesChannels: svcsChs, destinationChannels: dstsChs, checksChannels: checksChs, } go fusisStore.WatchServices() go fusisStore.WatchDestinations() go fusisStore.WatchChecks() return fusisStore, nil }
func testLocalBackend(t *testing.T, provider, url string, storeConfig *store.Config) { netOptions := []config.Option{} netOptions = append(netOptions, config.OptionLocalKVProvider(provider)) netOptions = append(netOptions, config.OptionLocalKVProviderURL(url)) netOptions = append(netOptions, config.OptionLocalKVProviderConfig(storeConfig)) ctrl, err := New(netOptions...) if err != nil { t.Fatalf("Error new controller: %v", err) } if err := ctrl.ConfigureNetworkDriver("host", options.NewGeneric()); err != nil { t.Fatalf("Error initializing host driver: %v", err) } nw, err := ctrl.NewNetwork("host", "host") 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) } store := ctrl.(*controller).localStore.KVStore() if exists, err := store.Exists(datastore.Key(datastore.NetworkKeyPrefix, string(nw.ID()))); !exists || err != nil { t.Fatalf("Network key should have been created.") } if exists, err := store.Exists(datastore.Key([]string{datastore.EndpointKeyPrefix, string(nw.ID()), string(ep.ID())}...)); !exists || err != nil { t.Fatalf("Endpoint key should have been created.") } store.Close() // test restore of local store ctrl, err = New(netOptions...) if nw, err = ctrl.NetworkByID(nw.ID()); err != nil { t.Fatalf("Error get network %v", err) } if ep, err = nw.EndpointByID(ep.ID()); err != nil { t.Fatalf("Error get endpoint %v", err) } if err := ep.Delete(); err != nil { t.Fatalf("Error delete endpoint %v", err) } store = ctrl.(*controller).localStore.KVStore() if exists, err := store.Exists(datastore.Key([]string{datastore.EndpointKeyPrefix, string(nw.ID()), string(ep.ID())}...)); exists || err != nil { t.Fatalf("Endpoint key should have been deleted. ") } }
func testLocalBackend(t *testing.T, provider, url string, storeConfig *store.Config) { cfgOptions := []config.Option{} cfgOptions = append(cfgOptions, config.OptionLocalKVProvider(provider)) cfgOptions = append(cfgOptions, config.OptionLocalKVProviderURL(url)) cfgOptions = append(cfgOptions, config.OptionLocalKVProviderConfig(storeConfig)) driverOptions := options.Generic{} genericOption := make(map[string]interface{}) genericOption[netlabel.GenericData] = driverOptions cfgOptions = append(cfgOptions, config.OptionDriverConfig("host", genericOption)) fmt.Printf("URL : %s\n", url) ctrl, err := New(cfgOptions...) if err != nil { t.Fatalf("Error new controller: %v", err) } nw, err := ctrl.NewNetwork("host", "host") 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) } store := ctrl.(*controller).localStore.KVStore() if exists, err := store.Exists(datastore.Key(datastore.NetworkKeyPrefix, string(nw.ID()))); !exists || err != nil { t.Fatalf("Network key should have been created.") } if exists, err := store.Exists(datastore.Key([]string{datastore.EndpointKeyPrefix, string(nw.ID()), string(ep.ID())}...)); exists || err != nil { t.Fatalf("Endpoint key shouldn't have been created.") } store.Close() // test restore of local store ctrl, err = New(cfgOptions...) if err != nil { t.Fatalf("Error creating controller: %v", err) } if _, err = ctrl.NetworkByID(nw.ID()); err != nil { t.Fatalf("Error getting network %v", err) } }