github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/engine/container/view_test.go (about) 1 package container // import "github.com/docker/docker/container" 2 3 import ( 4 "os" 5 "path/filepath" 6 "testing" 7 8 "github.com/docker/docker/api/types" 9 containertypes "github.com/docker/docker/api/types/container" 10 "github.com/google/uuid" 11 "gotest.tools/v3/assert" 12 is "gotest.tools/v3/assert/cmp" 13 ) 14 15 var root string 16 17 func TestMain(m *testing.M) { 18 var err error 19 root, err = os.MkdirTemp("", "docker-container-test-") 20 if err != nil { 21 panic(err) 22 } 23 defer os.RemoveAll(root) 24 25 os.Exit(m.Run()) 26 } 27 28 func newContainer(t *testing.T) *Container { 29 var ( 30 id = uuid.New().String() 31 cRoot = filepath.Join(root, id) 32 ) 33 if err := os.MkdirAll(cRoot, 0755); err != nil { 34 t.Fatal(err) 35 } 36 c := NewBaseContainer(id, cRoot) 37 c.HostConfig = &containertypes.HostConfig{} 38 return c 39 } 40 41 func TestViewSaveDelete(t *testing.T) { 42 db, err := NewViewDB() 43 if err != nil { 44 t.Fatal(err) 45 } 46 c := newContainer(t) 47 if err := c.CheckpointTo(db); err != nil { 48 t.Fatal(err) 49 } 50 if err := db.Delete(c); err != nil { 51 t.Fatal(err) 52 } 53 } 54 55 func TestViewAll(t *testing.T) { 56 var ( 57 db, _ = NewViewDB() 58 one = newContainer(t) 59 two = newContainer(t) 60 ) 61 one.Pid = 10 62 if err := one.CheckpointTo(db); err != nil { 63 t.Fatal(err) 64 } 65 two.Pid = 20 66 if err := two.CheckpointTo(db); err != nil { 67 t.Fatal(err) 68 } 69 70 all, err := db.Snapshot().All() 71 if err != nil { 72 t.Fatal(err) 73 } 74 if l := len(all); l != 2 { 75 t.Fatalf("expected 2 items, got %d", l) 76 } 77 byID := make(map[string]Snapshot) 78 for i := range all { 79 byID[all[i].ID] = all[i] 80 } 81 if s, ok := byID[one.ID]; !ok || s.Pid != 10 { 82 t.Fatalf("expected something different with for id=%s: %v", one.ID, s) 83 } 84 if s, ok := byID[two.ID]; !ok || s.Pid != 20 { 85 t.Fatalf("expected something different with for id=%s: %v", two.ID, s) 86 } 87 } 88 89 func TestViewGet(t *testing.T) { 90 var ( 91 db, _ = NewViewDB() 92 one = newContainer(t) 93 ) 94 one.ImageID = "some-image-123" 95 if err := one.CheckpointTo(db); err != nil { 96 t.Fatal(err) 97 } 98 s, err := db.Snapshot().Get(one.ID) 99 if err != nil { 100 t.Fatal(err) 101 } 102 if s == nil || s.ImageID != "some-image-123" { 103 t.Fatalf("expected ImageID=some-image-123. Got: %v", s) 104 } 105 } 106 107 func TestNames(t *testing.T) { 108 db, err := NewViewDB() 109 if err != nil { 110 t.Fatal(err) 111 } 112 assert.Check(t, db.ReserveName("name1", "containerid1")) 113 assert.Check(t, db.ReserveName("name1", "containerid1")) // idempotent 114 assert.Check(t, db.ReserveName("name2", "containerid2")) 115 assert.Check(t, is.Error(db.ReserveName("name2", "containerid3"), ErrNameReserved.Error())) 116 117 // Releasing a name allows the name to point to something else later. 118 assert.Check(t, db.ReleaseName("name2")) 119 assert.Check(t, db.ReserveName("name2", "containerid3")) 120 121 view := db.Snapshot() 122 123 id, err := view.GetID("name1") 124 assert.Check(t, err) 125 assert.Check(t, is.Equal("containerid1", id)) 126 127 id, err = view.GetID("name2") 128 assert.Check(t, err) 129 assert.Check(t, is.Equal("containerid3", id)) 130 131 _, err = view.GetID("notreserved") 132 assert.Check(t, is.Error(err, ErrNameNotReserved.Error())) 133 134 // Releasing and re-reserving a name doesn't affect the snapshot. 135 assert.Check(t, db.ReleaseName("name2")) 136 assert.Check(t, db.ReserveName("name2", "containerid4")) 137 138 id, err = view.GetID("name1") 139 assert.Check(t, err) 140 assert.Check(t, is.Equal("containerid1", id)) 141 142 id, err = view.GetID("name2") 143 assert.Check(t, err) 144 assert.Check(t, is.Equal("containerid3", id)) 145 146 // GetAllNames 147 assert.Check(t, is.DeepEqual(map[string][]string{"containerid1": {"name1"}, "containerid3": {"name2"}}, view.GetAllNames())) 148 149 assert.Check(t, db.ReserveName("name3", "containerid1")) 150 assert.Check(t, db.ReserveName("name4", "containerid1")) 151 152 view = db.Snapshot() 153 assert.Check(t, is.DeepEqual(map[string][]string{"containerid1": {"name1", "name3", "name4"}, "containerid4": {"name2"}}, view.GetAllNames())) 154 155 // Release containerid1's names with Delete even though no container exists 156 assert.Check(t, db.Delete(&Container{ID: "containerid1"})) 157 158 // Reusing one of those names should work 159 assert.Check(t, db.ReserveName("name1", "containerid4")) 160 view = db.Snapshot() 161 assert.Check(t, is.DeepEqual(map[string][]string{"containerid4": {"name1", "name2"}}, view.GetAllNames())) 162 } 163 164 // Test case for GitHub issue 35920 165 func TestViewWithHealthCheck(t *testing.T) { 166 var ( 167 db, _ = NewViewDB() 168 one = newContainer(t) 169 ) 170 one.Health = &Health{ 171 Health: types.Health{ 172 Status: "starting", 173 }, 174 } 175 if err := one.CheckpointTo(db); err != nil { 176 t.Fatal(err) 177 } 178 s, err := db.Snapshot().Get(one.ID) 179 if err != nil { 180 t.Fatal(err) 181 } 182 if s == nil || s.Health != "starting" { 183 t.Fatalf("expected Health=starting. Got: %+v", s) 184 } 185 }