github.com/vincentwoo/docker@v0.7.3-0.20160116130405-82401a4b13c0/daemon/links_test.go (about) 1 package daemon 2 3 import ( 4 "encoding/json" 5 "io/ioutil" 6 "os" 7 "path" 8 "path/filepath" 9 "testing" 10 11 "github.com/docker/docker/container" 12 "github.com/docker/docker/pkg/graphdb" 13 "github.com/docker/docker/pkg/stringid" 14 containertypes "github.com/docker/engine-api/types/container" 15 ) 16 17 func TestMigrateLegacySqliteLinks(t *testing.T) { 18 tmpDir, err := ioutil.TempDir("", "legacy-qlite-links-test") 19 if err != nil { 20 t.Fatal(err) 21 } 22 defer os.RemoveAll(tmpDir) 23 24 name1 := "test1" 25 c1 := &container.Container{ 26 CommonContainer: container.CommonContainer{ 27 ID: stringid.GenerateNonCryptoID(), 28 Name: name1, 29 HostConfig: &containertypes.HostConfig{}, 30 }, 31 } 32 c1.Root = tmpDir 33 34 name2 := "test2" 35 c2 := &container.Container{ 36 CommonContainer: container.CommonContainer{ 37 ID: stringid.GenerateNonCryptoID(), 38 Name: name2, 39 }, 40 } 41 42 store := &contStore{ 43 s: map[string]*container.Container{ 44 c1.ID: c1, 45 c2.ID: c2, 46 }, 47 } 48 49 d := &Daemon{root: tmpDir, containers: store} 50 db, err := graphdb.NewSqliteConn(filepath.Join(d.root, "linkgraph.db")) 51 if err != nil { 52 t.Fatal(err) 53 } 54 55 if _, err := db.Set("/"+name1, c1.ID); err != nil { 56 t.Fatal(err) 57 } 58 59 if _, err := db.Set("/"+name2, c2.ID); err != nil { 60 t.Fatal(err) 61 } 62 63 alias := "hello" 64 if _, err := db.Set(path.Join(c1.Name, alias), c2.ID); err != nil { 65 t.Fatal(err) 66 } 67 68 if err := d.migrateLegacySqliteLinks(db, c1); err != nil { 69 t.Fatal(err) 70 } 71 72 if len(c1.HostConfig.Links) != 1 { 73 t.Fatal("expected links to be populated but is empty") 74 } 75 76 expected := name2 + ":" + alias 77 actual := c1.HostConfig.Links[0] 78 if actual != expected { 79 t.Fatalf("got wrong link value, expected: %q, got: %q", expected, actual) 80 } 81 82 // ensure this is persisted 83 b, err := ioutil.ReadFile(filepath.Join(c1.Root, "hostconfig.json")) 84 if err != nil { 85 t.Fatal(err) 86 } 87 type hc struct { 88 Links []string 89 } 90 var cfg hc 91 if err := json.Unmarshal(b, &cfg); err != nil { 92 t.Fatal(err) 93 } 94 95 if len(cfg.Links) != 1 { 96 t.Fatalf("expected one entry in links, got: %d", len(cfg.Links)) 97 } 98 if cfg.Links[0] != expected { // same expected as above 99 t.Fatalf("got wrong link value, expected: %q, got: %q", expected, cfg.Links[0]) 100 } 101 }