Browse code

Merge pull request #12450 from brahmaroutu/integration_test3_12255

Port test from integration tests - test low memory on create

Phil Estes authored on 2015/04/21 06:27:17
Showing 4 changed files
... ...
@@ -1226,6 +1226,10 @@ func checkKernel() error {
1226 1226
 func (daemon *Daemon) verifyHostConfig(hostConfig *runconfig.HostConfig) ([]string, error) {
1227 1227
 	var warnings []string
1228 1228
 
1229
+	if hostConfig == nil {
1230
+		return warnings, nil
1231
+	}
1232
+
1229 1233
 	if hostConfig.LxcConf.Len() > 0 && !strings.Contains(daemon.ExecutionDriver().Name(), "lxc") {
1230 1234
 		return warnings, fmt.Errorf("Cannot use --lxc-conf with execdriver: %s", daemon.ExecutionDriver().Name())
1231 1235
 	}
... ...
@@ -20,6 +20,10 @@ func (daemon *Daemon) ContainerStart(name string, hostConfig *runconfig.HostConf
20 20
 		return fmt.Errorf("Container already started")
21 21
 	}
22 22
 
23
+	if _, err = daemon.verifyHostConfig(hostConfig); err != nil {
24
+		return err
25
+	}
26
+
23 27
 	// This is kept for backward compatibility - hostconfig should be passed when
24 28
 	// creating a container, not during start.
25 29
 	if hostConfig != nil {
... ...
@@ -817,3 +817,54 @@ func TestContainerApiPostCreateNull(t *testing.T) {
817 817
 
818 818
 	logDone("containers REST API - Create Null")
819 819
 }
820
+
821
+func TestCreateWithTooLowMemoryLimit(t *testing.T) {
822
+	defer deleteAllContainers()
823
+	config := `{
824
+		"Image":     "busybox",
825
+		"Cmd":       "ls",
826
+		"OpenStdin": true,
827
+		"CpuShares": 100,
828
+		"Memory":    524287
829
+	}`
830
+
831
+	_, body, err := sockRequestRaw("POST", "/containers/create", strings.NewReader(config), "application/json")
832
+	b, err2 := readBody(body)
833
+	if err2 != nil {
834
+		t.Fatal(err2)
835
+	}
836
+
837
+	if err == nil || !strings.Contains(string(b), "Minimum memory limit allowed is 4MB") {
838
+		t.Errorf("Memory limit is smaller than the allowed limit. Container creation should've failed!")
839
+	}
840
+
841
+	logDone("container REST API - create can't set too low memory limit")
842
+}
843
+
844
+func TestStartWithTooLowMemoryLimit(t *testing.T) {
845
+	defer deleteAllContainers()
846
+
847
+	out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "create", "busybox"))
848
+	if err != nil {
849
+		t.Fatal(err, out)
850
+	}
851
+
852
+	containerID := strings.TrimSpace(out)
853
+
854
+	config := `{
855
+                "CpuShares": 100,
856
+                "Memory":    524287
857
+        }`
858
+
859
+	_, body, err := sockRequestRaw("POST", "/containers/"+containerID+"/start", strings.NewReader(config), "application/json")
860
+	b, err2 := readBody(body)
861
+	if err2 != nil {
862
+		t.Fatal(err2)
863
+	}
864
+
865
+	if err == nil || !strings.Contains(string(b), "Minimum memory limit allowed is 4MB") {
866
+		t.Errorf("Memory limit is smaller than the allowed limit. Container creation should've failed!")
867
+	}
868
+
869
+	logDone("container REST API - start can't set too low memory limit")
870
+}
... ...
@@ -3496,15 +3496,3 @@ func TestRunPidHostWithChildIsKillable(t *testing.T) {
3496 3496
 	}
3497 3497
 	logDone("run - can kill container with pid-host and some childs of pid 1")
3498 3498
 }
3499
-
3500
-func TestRunWithTooSmallMemoryLimit(t *testing.T) {
3501
-	defer deleteAllContainers()
3502
-	// this memory limit is 1 byte less than the min, which is 4MB
3503
-	// https://github.com/docker/docker/blob/v1.5.0/daemon/create.go#L22
3504
-	out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-m", "4194303", "busybox"))
3505
-	if err == nil || !strings.Contains(out, "Minimum memory limit allowed is 4MB") {
3506
-		t.Fatalf("expected run to fail when using too low a memory limit: %q", out)
3507
-	}
3508
-
3509
-	logDone("run - can't set too low memory limit")
3510
-}