github.com/jthurman42/docker@v1.6.0-rc1/integration/server_test.go (about)

     1  package docker
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/docker/docker/builder"
     9  	"github.com/docker/docker/engine"
    10  )
    11  
    12  func TestCreateNumberHostname(t *testing.T) {
    13  	eng := NewTestEngine(t)
    14  	defer mkDaemonFromEngine(eng, t).Nuke()
    15  
    16  	config, _, _, err := parseRun([]string{"-h", "web.0", unitTestImageID, "echo test"})
    17  	if err != nil {
    18  		t.Fatal(err)
    19  	}
    20  
    21  	createTestContainer(eng, config, t)
    22  }
    23  
    24  func TestCommit(t *testing.T) {
    25  	eng := NewTestEngine(t)
    26  	b := &builder.BuilderJob{Engine: eng}
    27  	b.Install()
    28  	defer mkDaemonFromEngine(eng, t).Nuke()
    29  
    30  	config, _, _, err := parseRun([]string{unitTestImageID, "/bin/cat"})
    31  	if err != nil {
    32  		t.Fatal(err)
    33  	}
    34  
    35  	id := createTestContainer(eng, config, t)
    36  
    37  	job := eng.Job("commit", id)
    38  	job.Setenv("repo", "testrepo")
    39  	job.Setenv("tag", "testtag")
    40  	job.SetenvJson("config", config)
    41  	if err := job.Run(); err != nil {
    42  		t.Fatal(err)
    43  	}
    44  }
    45  
    46  func TestMergeConfigOnCommit(t *testing.T) {
    47  	eng := NewTestEngine(t)
    48  	b := &builder.BuilderJob{Engine: eng}
    49  	b.Install()
    50  	runtime := mkDaemonFromEngine(eng, t)
    51  	defer runtime.Nuke()
    52  
    53  	container1, _, _ := mkContainer(runtime, []string{"-e", "FOO=bar", unitTestImageID, "echo test > /tmp/foo"}, t)
    54  	defer runtime.Rm(container1)
    55  
    56  	config, _, _, err := parseRun([]string{container1.ID, "cat /tmp/foo"})
    57  	if err != nil {
    58  		t.Error(err)
    59  	}
    60  
    61  	job := eng.Job("commit", container1.ID)
    62  	job.Setenv("repo", "testrepo")
    63  	job.Setenv("tag", "testtag")
    64  	job.SetenvJson("config", config)
    65  	var outputBuffer = bytes.NewBuffer(nil)
    66  	job.Stdout.Add(outputBuffer)
    67  	if err := job.Run(); err != nil {
    68  		t.Error(err)
    69  	}
    70  
    71  	container2, _, _ := mkContainer(runtime, []string{engine.Tail(outputBuffer, 1)}, t)
    72  	defer runtime.Rm(container2)
    73  
    74  	job = eng.Job("container_inspect", container1.Name)
    75  	baseContainer, _ := job.Stdout.AddEnv()
    76  	if err := job.Run(); err != nil {
    77  		t.Error(err)
    78  	}
    79  
    80  	job = eng.Job("container_inspect", container2.Name)
    81  	commitContainer, _ := job.Stdout.AddEnv()
    82  	if err := job.Run(); err != nil {
    83  		t.Error(err)
    84  	}
    85  
    86  	baseConfig := baseContainer.GetSubEnv("Config")
    87  	commitConfig := commitContainer.GetSubEnv("Config")
    88  
    89  	if commitConfig.Get("Env") != baseConfig.Get("Env") {
    90  		t.Fatalf("Env config in committed container should be %v, was %v",
    91  			baseConfig.Get("Env"), commitConfig.Get("Env"))
    92  	}
    93  
    94  	if baseConfig.Get("Cmd") != "[\"echo test \\u003e /tmp/foo\"]" {
    95  		t.Fatalf("Cmd in base container should be [\"echo test \\u003e /tmp/foo\"], was %s",
    96  			baseConfig.Get("Cmd"))
    97  	}
    98  
    99  	if commitConfig.Get("Cmd") != "[\"cat /tmp/foo\"]" {
   100  		t.Fatalf("Cmd in committed container should be [\"cat /tmp/foo\"], was %s",
   101  			commitConfig.Get("Cmd"))
   102  	}
   103  }
   104  
   105  func TestRestartKillWait(t *testing.T) {
   106  	eng := NewTestEngine(t)
   107  	runtime := mkDaemonFromEngine(eng, t)
   108  	defer runtime.Nuke()
   109  
   110  	config, hostConfig, _, err := parseRun([]string{"-i", unitTestImageID, "/bin/cat"})
   111  	if err != nil {
   112  		t.Fatal(err)
   113  	}
   114  
   115  	id := createTestContainer(eng, config, t)
   116  
   117  	job := eng.Job("containers")
   118  	job.SetenvBool("all", true)
   119  	outs, err := job.Stdout.AddListTable()
   120  	if err != nil {
   121  		t.Fatal(err)
   122  	}
   123  	if err := job.Run(); err != nil {
   124  		t.Fatal(err)
   125  	}
   126  
   127  	if len(outs.Data) != 1 {
   128  		t.Errorf("Expected 1 container, %v found", len(outs.Data))
   129  	}
   130  
   131  	job = eng.Job("start", id)
   132  	if err := job.ImportEnv(hostConfig); err != nil {
   133  		t.Fatal(err)
   134  	}
   135  	if err := job.Run(); err != nil {
   136  		t.Fatal(err)
   137  	}
   138  	job = eng.Job("kill", id)
   139  	if err := job.Run(); err != nil {
   140  		t.Fatal(err)
   141  	}
   142  
   143  	eng = newTestEngine(t, false, runtime.Config().Root)
   144  
   145  	job = eng.Job("containers")
   146  	job.SetenvBool("all", true)
   147  	outs, err = job.Stdout.AddListTable()
   148  	if err != nil {
   149  		t.Fatal(err)
   150  	}
   151  	if err := job.Run(); err != nil {
   152  		t.Fatal(err)
   153  	}
   154  
   155  	if len(outs.Data) != 1 {
   156  		t.Errorf("Expected 1 container, %v found", len(outs.Data))
   157  	}
   158  
   159  	setTimeout(t, "Waiting on stopped container timedout", 5*time.Second, func() {
   160  		job = eng.Job("wait", outs.Data[0].Get("Id"))
   161  		if err := job.Run(); err != nil {
   162  			t.Fatal(err)
   163  		}
   164  	})
   165  }
   166  
   167  func TestCreateStartRestartStopStartKillRm(t *testing.T) {
   168  	eng := NewTestEngine(t)
   169  	defer mkDaemonFromEngine(eng, t).Nuke()
   170  
   171  	config, hostConfig, _, err := parseRun([]string{"-i", unitTestImageID, "/bin/cat"})
   172  	if err != nil {
   173  		t.Fatal(err)
   174  	}
   175  
   176  	id := createTestContainer(eng, config, t)
   177  
   178  	job := eng.Job("containers")
   179  	job.SetenvBool("all", true)
   180  	outs, err := job.Stdout.AddListTable()
   181  	if err != nil {
   182  		t.Fatal(err)
   183  	}
   184  	if err := job.Run(); err != nil {
   185  		t.Fatal(err)
   186  	}
   187  
   188  	if len(outs.Data) != 1 {
   189  		t.Errorf("Expected 1 container, %v found", len(outs.Data))
   190  	}
   191  
   192  	job = eng.Job("start", id)
   193  	if err := job.ImportEnv(hostConfig); err != nil {
   194  		t.Fatal(err)
   195  	}
   196  	if err := job.Run(); err != nil {
   197  		t.Fatal(err)
   198  	}
   199  
   200  	job = eng.Job("restart", id)
   201  	job.SetenvInt("t", 2)
   202  	if err := job.Run(); err != nil {
   203  		t.Fatal(err)
   204  	}
   205  
   206  	job = eng.Job("stop", id)
   207  	job.SetenvInt("t", 2)
   208  	if err := job.Run(); err != nil {
   209  		t.Fatal(err)
   210  	}
   211  
   212  	job = eng.Job("start", id)
   213  	if err := job.ImportEnv(hostConfig); err != nil {
   214  		t.Fatal(err)
   215  	}
   216  	if err := job.Run(); err != nil {
   217  		t.Fatal(err)
   218  	}
   219  
   220  	if err := eng.Job("kill", id).Run(); err != nil {
   221  		t.Fatal(err)
   222  	}
   223  
   224  	// FIXME: this failed once with a race condition ("Unable to remove filesystem for xxx: directory not empty")
   225  	job = eng.Job("rm", id)
   226  	job.SetenvBool("removeVolume", true)
   227  	if err := job.Run(); err != nil {
   228  		t.Fatal(err)
   229  	}
   230  
   231  	job = eng.Job("containers")
   232  	job.SetenvBool("all", true)
   233  	outs, err = job.Stdout.AddListTable()
   234  	if err != nil {
   235  		t.Fatal(err)
   236  	}
   237  	if err := job.Run(); err != nil {
   238  		t.Fatal(err)
   239  	}
   240  
   241  	if len(outs.Data) != 0 {
   242  		t.Errorf("Expected 0 container, %v found", len(outs.Data))
   243  	}
   244  }
   245  
   246  func TestRunWithTooLowMemoryLimit(t *testing.T) {
   247  	eng := NewTestEngine(t)
   248  	defer mkDaemonFromEngine(eng, t).Nuke()
   249  
   250  	// Try to create a container with a memory limit of 1 byte less than the minimum allowed limit.
   251  	job := eng.Job("create")
   252  	job.Setenv("Image", unitTestImageID)
   253  	job.Setenv("Memory", "524287")
   254  	job.Setenv("CpuShares", "1000")
   255  	job.SetenvList("Cmd", []string{"/bin/cat"})
   256  	if err := job.Run(); err == nil {
   257  		t.Errorf("Memory limit is smaller than the allowed limit. Container creation should've failed!")
   258  	}
   259  }
   260  
   261  func TestImagesFilter(t *testing.T) {
   262  	eng := NewTestEngine(t)
   263  	defer nuke(mkDaemonFromEngine(eng, t))
   264  
   265  	if err := eng.Job("tag", unitTestImageName, "utest", "tag1").Run(); err != nil {
   266  		t.Fatal(err)
   267  	}
   268  
   269  	if err := eng.Job("tag", unitTestImageName, "utest/docker", "tag2").Run(); err != nil {
   270  		t.Fatal(err)
   271  	}
   272  
   273  	if err := eng.Job("tag", unitTestImageName, "utest:5000/docker", "tag3").Run(); err != nil {
   274  		t.Fatal(err)
   275  	}
   276  
   277  	images := getImages(eng, t, false, "utest*/*")
   278  
   279  	if len(images.Data[0].GetList("RepoTags")) != 2 {
   280  		t.Fatal("incorrect number of matches returned")
   281  	}
   282  
   283  	images = getImages(eng, t, false, "utest")
   284  
   285  	if len(images.Data[0].GetList("RepoTags")) != 1 {
   286  		t.Fatal("incorrect number of matches returned")
   287  	}
   288  
   289  	images = getImages(eng, t, false, "utest*")
   290  
   291  	if len(images.Data[0].GetList("RepoTags")) != 1 {
   292  		t.Fatal("incorrect number of matches returned")
   293  	}
   294  
   295  	images = getImages(eng, t, false, "*5000*/*")
   296  
   297  	if len(images.Data[0].GetList("RepoTags")) != 1 {
   298  		t.Fatal("incorrect number of matches returned")
   299  	}
   300  }