github.com/erriapo/docker@v1.6.0-rc2/daemon/daemon_test.go (about)

     1  package daemon
     2  
     3  import (
     4  	"github.com/docker/docker/pkg/graphdb"
     5  	"github.com/docker/docker/pkg/truncindex"
     6  	"os"
     7  	"path"
     8  	"testing"
     9  )
    10  
    11  //
    12  // https://github.com/docker/docker/issues/8069
    13  //
    14  
    15  func TestGet(t *testing.T) {
    16  	c1 := &Container{
    17  		ID:   "5a4ff6a163ad4533d22d69a2b8960bf7fafdcba06e72d2febdba229008b0bf57",
    18  		Name: "tender_bardeen",
    19  	}
    20  	c2 := &Container{
    21  		ID:   "3cdbd1aa394fd68559fd1441d6eff2ab7c1e6363582c82febfaa8045df3bd8de",
    22  		Name: "drunk_hawking",
    23  	}
    24  	c3 := &Container{
    25  		ID:   "3cdbd1aa394fd68559fd1441d6eff2abfafdcba06e72d2febdba229008b0bf57",
    26  		Name: "3cdbd1aa",
    27  	}
    28  	c4 := &Container{
    29  		ID:   "75fb0b800922abdbef2d27e60abcdfaf7fb0698b2a96d22d3354da361a6ff4a5",
    30  		Name: "5a4ff6a163ad4533d22d69a2b8960bf7fafdcba06e72d2febdba229008b0bf57",
    31  	}
    32  	c5 := &Container{
    33  		ID:   "d22d69a2b8960bf7fafdcba06e72d2febdba960bf7fafdcba06e72d2f9008b060b",
    34  		Name: "d22d69a2b896",
    35  	}
    36  
    37  	store := &contStore{
    38  		s: map[string]*Container{
    39  			c1.ID: c1,
    40  			c2.ID: c2,
    41  			c3.ID: c3,
    42  			c4.ID: c4,
    43  			c5.ID: c5,
    44  		},
    45  	}
    46  
    47  	index := truncindex.NewTruncIndex([]string{})
    48  	index.Add(c1.ID)
    49  	index.Add(c2.ID)
    50  	index.Add(c3.ID)
    51  	index.Add(c4.ID)
    52  	index.Add(c5.ID)
    53  
    54  	daemonTestDbPath := path.Join(os.TempDir(), "daemon_test.db")
    55  	graph, err := graphdb.NewSqliteConn(daemonTestDbPath)
    56  	if err != nil {
    57  		t.Fatalf("Failed to create daemon test sqlite database at %s", daemonTestDbPath)
    58  	}
    59  	graph.Set(c1.Name, c1.ID)
    60  	graph.Set(c2.Name, c2.ID)
    61  	graph.Set(c3.Name, c3.ID)
    62  	graph.Set(c4.Name, c4.ID)
    63  	graph.Set(c5.Name, c5.ID)
    64  
    65  	daemon := &Daemon{
    66  		containers:     store,
    67  		idIndex:        index,
    68  		containerGraph: graph,
    69  	}
    70  
    71  	if container, _ := daemon.Get("3cdbd1aa394fd68559fd1441d6eff2ab7c1e6363582c82febfaa8045df3bd8de"); container != c2 {
    72  		t.Fatal("Should explicitly match full container IDs")
    73  	}
    74  
    75  	if container, _ := daemon.Get("75fb0b8009"); container != c4 {
    76  		t.Fatal("Should match a partial ID")
    77  	}
    78  
    79  	if container, _ := daemon.Get("drunk_hawking"); container != c2 {
    80  		t.Fatal("Should match a full name")
    81  	}
    82  
    83  	// c3.Name is a partial match for both c3.ID and c2.ID
    84  	if c, _ := daemon.Get("3cdbd1aa"); c != c3 {
    85  		t.Fatal("Should match a full name even though it collides with another container's ID")
    86  	}
    87  
    88  	if container, _ := daemon.Get("d22d69a2b896"); container != c5 {
    89  		t.Fatal("Should match a container where the provided prefix is an exact match to the it's name, and is also a prefix for it's ID")
    90  	}
    91  
    92  	if _, err := daemon.Get("3cdbd1"); err == nil {
    93  		t.Fatal("Should return an error when provided a prefix that partially matches multiple container ID's")
    94  	}
    95  
    96  	if _, err := daemon.Get("nothing"); err == nil {
    97  		t.Fatal("Should return an error when provided a prefix that is neither a name or a partial match to an ID")
    98  	}
    99  
   100  	os.Remove(daemonTestDbPath)
   101  }