github.com/dlintw/docker@v1.5.0-rc4/integration/server_test.go (about)

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