Beispiel #1
0
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()
}
Beispiel #2
0
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. ")
	}
}
Beispiel #3
0
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)
	}
}
Beispiel #4
0
func TestLocalRestore(t *testing.T) {
	netOptions := []config.Option{}
	netOptions = append(netOptions, config.OptionLocalKVProvider(""))

	ctrl, err := New(netOptions...)
	if err != nil {
		t.Fatalf("Error new controller: %v", err)
	}

	if err := ctrl.ConfigureNetworkDriver("bridge", options.Generic{netlabel.GenericData: options.Generic{}}); err != nil {
		t.Fatalf("Error initializing bridge driver: %v", err)
	}
	_, bipNet, err := net.ParseCIDR("172.18.42.1/16")
	if err != nil {
		t.Fatalf("Erorr %v", err)
	}
	netOption := options.Generic{
		"BridgeName":         "docker0",
		"EnableIPMasquerade": true,
		"EnableICC":          true,
		"AddressIPv4":        bipNet,
	}
	// Initialize default network on "bridge" with the same name
	nw, err := ctrl.NewNetwork("bridge", "bridge", NetworkOptionGeneric(options.Generic{netlabel.GenericData: netOption}))
	if err != nil {
		t.Fatalf("Error creating default \"bridge\" network: %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 err := ctrl.ConfigureNetworkDriver("bridge", options.Generic{netlabel.GenericData: options.Generic{}}); err != nil {
		t.Fatalf("Error initializing bridge driver: %v", err)
	}
	_, err = ctrl.NewNetwork("bridge", "bridge", NetworkOptionGeneric(options.Generic{netlabel.GenericData: netOption}))
	if err != nil {
		t.Fatalf("Error creating default \"bridge\" network: %v", err)
	}
}