github.com/sijibomii/docker@v0.0.0-20231230191044-5cf6ca554647/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 := container.NewMemoryStore() 43 store.Add(c1.ID, c1) 44 store.Add(c2.ID, c2) 45 46 d := &Daemon{root: tmpDir, containers: store} 47 db, err := graphdb.NewSqliteConn(filepath.Join(d.root, "linkgraph.db")) 48 if err != nil { 49 t.Fatal(err) 50 } 51 52 if _, err := db.Set("/"+name1, c1.ID); err != nil { 53 t.Fatal(err) 54 } 55 56 if _, err := db.Set("/"+name2, c2.ID); err != nil { 57 t.Fatal(err) 58 } 59 60 alias := "hello" 61 if _, err := db.Set(path.Join(c1.Name, alias), c2.ID); err != nil { 62 t.Fatal(err) 63 } 64 65 if err := d.migrateLegacySqliteLinks(db, c1); err != nil { 66 t.Fatal(err) 67 } 68 69 if len(c1.HostConfig.Links) != 1 { 70 t.Fatal("expected links to be populated but is empty") 71 } 72 73 expected := name2 + ":" + alias 74 actual := c1.HostConfig.Links[0] 75 if actual != expected { 76 t.Fatalf("got wrong link value, expected: %q, got: %q", expected, actual) 77 } 78 79 // ensure this is persisted 80 b, err := ioutil.ReadFile(filepath.Join(c1.Root, "hostconfig.json")) 81 if err != nil { 82 t.Fatal(err) 83 } 84 type hc struct { 85 Links []string 86 } 87 var cfg hc 88 if err := json.Unmarshal(b, &cfg); err != nil { 89 t.Fatal(err) 90 } 91 92 if len(cfg.Links) != 1 { 93 t.Fatalf("expected one entry in links, got: %d", len(cfg.Links)) 94 } 95 if cfg.Links[0] != expected { // same expected as above 96 t.Fatalf("got wrong link value, expected: %q, got: %q", expected, cfg.Links[0]) 97 } 98 }