github.com/ruphin/docker@v1.10.1/container/memory_store_test.go (about) 1 package container 2 3 import ( 4 "testing" 5 "time" 6 ) 7 8 func TestNewMemoryStore(t *testing.T) { 9 s := NewMemoryStore() 10 m, ok := s.(*memoryStore) 11 if !ok { 12 t.Fatalf("store is not a memory store %v", s) 13 } 14 if m.s == nil { 15 t.Fatal("expected store map to not be nil") 16 } 17 } 18 19 func TestAddContainers(t *testing.T) { 20 s := NewMemoryStore() 21 s.Add("id", NewBaseContainer("id", "root")) 22 if s.Size() != 1 { 23 t.Fatalf("expected store size 1, got %v", s.Size()) 24 } 25 } 26 27 func TestGetContainer(t *testing.T) { 28 s := NewMemoryStore() 29 s.Add("id", NewBaseContainer("id", "root")) 30 c := s.Get("id") 31 if c == nil { 32 t.Fatal("expected container to not be nil") 33 } 34 } 35 36 func TestDeleteContainer(t *testing.T) { 37 s := NewMemoryStore() 38 s.Add("id", NewBaseContainer("id", "root")) 39 s.Delete("id") 40 if c := s.Get("id"); c != nil { 41 t.Fatalf("expected container to be nil after removal, got %v", c) 42 } 43 44 if s.Size() != 0 { 45 t.Fatalf("expected store size to be 0, got %v", s.Size()) 46 } 47 } 48 49 func TestListContainers(t *testing.T) { 50 s := NewMemoryStore() 51 52 cont := NewBaseContainer("id", "root") 53 cont.Created = time.Now() 54 cont2 := NewBaseContainer("id2", "root") 55 cont2.Created = time.Now().Add(24 * time.Hour) 56 57 s.Add("id", cont) 58 s.Add("id2", cont2) 59 60 list := s.List() 61 if len(list) != 2 { 62 t.Fatalf("expected list size 2, got %v", len(list)) 63 } 64 if list[0].ID != "id2" { 65 t.Fatalf("expected older container to be first, got %v", list[0].ID) 66 } 67 } 68 69 func TestFirstContainer(t *testing.T) { 70 s := NewMemoryStore() 71 72 s.Add("id", NewBaseContainer("id", "root")) 73 s.Add("id2", NewBaseContainer("id2", "root")) 74 75 first := s.First(func(cont *Container) bool { 76 return cont.ID == "id2" 77 }) 78 79 if first == nil { 80 t.Fatal("expected container to not be nil") 81 } 82 if first.ID != "id2" { 83 t.Fatalf("expected id2, got %v", first) 84 } 85 } 86 87 func TestApplyAllContainer(t *testing.T) { 88 s := NewMemoryStore() 89 90 s.Add("id", NewBaseContainer("id", "root")) 91 s.Add("id2", NewBaseContainer("id2", "root")) 92 93 s.ApplyAll(func(cont *Container) { 94 if cont.ID == "id2" { 95 cont.ID = "newID" 96 } 97 }) 98 99 cont := s.Get("id2") 100 if cont == nil { 101 t.Fatal("expected container to not be nil") 102 } 103 if cont.ID != "newID" { 104 t.Fatalf("expected newID, got %v", cont) 105 } 106 }