forked from moby/moby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinks_test.go
98 lines (83 loc) · 2.13 KB
/
links_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
package daemon
import (
"encoding/json"
"io/ioutil"
"os"
"path"
"path/filepath"
"testing"
containertypes "github.com/docker/docker/api/types/container"
"github.com/docker/docker/container"
"github.com/docker/docker/pkg/graphdb"
"github.com/docker/docker/pkg/stringid"
)
func TestMigrateLegacySqliteLinks(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "legacy-qlite-links-test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
name1 := "test1"
c1 := &container.Container{
CommonContainer: container.CommonContainer{
ID: stringid.GenerateNonCryptoID(),
Name: name1,
HostConfig: &containertypes.HostConfig{},
},
}
c1.Root = tmpDir
name2 := "test2"
c2 := &container.Container{
CommonContainer: container.CommonContainer{
ID: stringid.GenerateNonCryptoID(),
Name: name2,
},
}
store := container.NewMemoryStore()
store.Add(c1.ID, c1)
store.Add(c2.ID, c2)
d := &Daemon{root: tmpDir, containers: store}
db, err := graphdb.NewSqliteConn(filepath.Join(d.root, "linkgraph.db"))
if err != nil {
t.Fatal(err)
}
if _, err := db.Set("/"+name1, c1.ID); err != nil {
t.Fatal(err)
}
if _, err := db.Set("/"+name2, c2.ID); err != nil {
t.Fatal(err)
}
alias := "hello"
if _, err := db.Set(path.Join(c1.Name, alias), c2.ID); err != nil {
t.Fatal(err)
}
if err := d.migrateLegacySqliteLinks(db, c1); err != nil {
t.Fatal(err)
}
if len(c1.HostConfig.Links) != 1 {
t.Fatal("expected links to be populated but is empty")
}
expected := name2 + ":" + alias
actual := c1.HostConfig.Links[0]
if actual != expected {
t.Fatalf("got wrong link value, expected: %q, got: %q", expected, actual)
}
// ensure this is persisted
b, err := ioutil.ReadFile(filepath.Join(c1.Root, "hostconfig.json"))
if err != nil {
t.Fatal(err)
}
type hc struct {
Links []string
}
var cfg hc
if err := json.Unmarshal(b, &cfg); err != nil {
t.Fatal(err)
}
if len(cfg.Links) != 1 {
t.Fatalf("expected one entry in links, got: %d", len(cfg.Links))
}
if cfg.Links[0] != expected { // same expected as above
t.Fatalf("got wrong link value, expected: %q, got: %q", expected, cfg.Links[0])
}
}