github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/container/view_test.go (about)

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