github.com/lazyboychen7/engine@v17.12.1-ce-rc2+incompatible/container/view_test.go (about)

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