forked from moby/libnetwork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store_test.go
107 lines (97 loc) · 3.5 KB
/
store_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package libnetwork
import (
"fmt"
"io/ioutil"
"testing"
"github.com/docker/libkv/store"
"github.com/docker/libnetwork/config"
"github.com/docker/libnetwork/datastore"
"github.com/docker/libnetwork/netlabel"
"github.com/docker/libnetwork/options"
)
func testZooKeeperBackend(t *testing.T) {
c, err := testNewController(t, "zk", "127.0.0.1:2181/custom_prefix")
if err != nil {
t.Fatal(err)
}
c.Stop()
}
func testNewController(t *testing.T, provider, url string) (NetworkController, error) {
cfgOptions, err := OptionBoltdbWithRandomDBFile()
if err != nil {
return nil, err
}
cfgOptions = append(cfgOptions, config.OptionKVProvider(provider))
cfgOptions = append(cfgOptions, config.OptionKVProviderURL(url))
return New(cfgOptions...)
}
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))
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).getStore(datastore.LocalScope).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(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)
}
}
// OptionBoltdbWithRandomDBFile function returns a random dir for local store backend
func OptionBoltdbWithRandomDBFile() ([]config.Option, error) {
tmp, err := ioutil.TempFile("", "libnetwork-")
if err != nil {
return nil, fmt.Errorf("Error creating temp file: %v", err)
}
if err := tmp.Close(); err != nil {
return nil, fmt.Errorf("Error closing temp file: %v", err)
}
cfgOptions := []config.Option{}
cfgOptions = append(cfgOptions, config.OptionLocalKVProvider("boltdb"))
cfgOptions = append(cfgOptions, config.OptionLocalKVProviderURL(tmp.Name()))
sCfg := &store.Config{Bucket: "testBackend"}
cfgOptions = append(cfgOptions, config.OptionLocalKVProviderConfig(sCfg))
return cfgOptions, nil
}
func TestMultipleControllersWithSameStore(t *testing.T) {
cfgOptions, err := OptionBoltdbWithRandomDBFile()
if err != nil {
t.Fatalf("Error getting random boltdb configs %v", err)
}
ctrl1, err := New(cfgOptions...)
if err != nil {
t.Fatalf("Error new controller: %v", err)
}
defer ctrl1.Stop()
// Use the same boltdb file without closing the previous controller
_, err = New(cfgOptions...)
if err != nil {
t.Fatalf("Local store must support concurrent controllers")
}
}