Browse code

Add tests for API container delete

Signed-off-by: Antonio Murdaca <me@runcom.ninja>

Antonio Murdaca authored on 2015/05/03 21:54:55
Showing 2 changed files
... ...
@@ -6,6 +6,7 @@ import (
6 6
 	"encoding/json"
7 7
 	"io"
8 8
 	"net/http"
9
+	"os"
9 10
 	"os/exec"
10 11
 	"strings"
11 12
 	"time"
... ...
@@ -1040,3 +1041,112 @@ func (s *DockerSuite) TestContainerApiCopyContainerNotFound(c *check.C) {
1040 1040
 	c.Assert(err, check.IsNil)
1041 1041
 	c.Assert(status, check.Equals, http.StatusNotFound)
1042 1042
 }
1043
+
1044
+func (s *DockerSuite) TestContainerApiDelete(c *check.C) {
1045
+	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
1046
+	out, _, err := runCommandWithOutput(runCmd)
1047
+	c.Assert(err, check.IsNil)
1048
+
1049
+	id := strings.TrimSpace(out)
1050
+	c.Assert(waitRun(id), check.IsNil)
1051
+
1052
+	stopCmd := exec.Command(dockerBinary, "stop", id)
1053
+	_, err = runCommand(stopCmd)
1054
+	c.Assert(err, check.IsNil)
1055
+
1056
+	status, _, err := sockRequest("DELETE", "/containers/"+id, nil)
1057
+	c.Assert(err, check.IsNil)
1058
+	c.Assert(status, check.Equals, http.StatusNoContent)
1059
+}
1060
+
1061
+func (s *DockerSuite) TestContainerApiDeleteNotExist(c *check.C) {
1062
+	status, body, err := sockRequest("DELETE", "/containers/doesnotexist", nil)
1063
+	c.Assert(err, check.IsNil)
1064
+	c.Assert(status, check.Equals, http.StatusNotFound)
1065
+	c.Assert(string(body), check.Matches, "no such id: doesnotexist\n")
1066
+}
1067
+
1068
+func (s *DockerSuite) TestContainerApiDeleteForce(c *check.C) {
1069
+	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
1070
+	out, _, err := runCommandWithOutput(runCmd)
1071
+	c.Assert(err, check.IsNil)
1072
+
1073
+	id := strings.TrimSpace(out)
1074
+	c.Assert(waitRun(id), check.IsNil)
1075
+
1076
+	status, _, err := sockRequest("DELETE", "/containers/"+id+"?force=1", nil)
1077
+	c.Assert(err, check.IsNil)
1078
+	c.Assert(status, check.Equals, http.StatusNoContent)
1079
+}
1080
+
1081
+func (s *DockerSuite) TestContainerApiDeleteRemoveLinks(c *check.C) {
1082
+	runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "tlink1", "busybox", "top")
1083
+	out, _, err := runCommandWithOutput(runCmd)
1084
+	c.Assert(err, check.IsNil)
1085
+
1086
+	id := strings.TrimSpace(out)
1087
+	c.Assert(waitRun(id), check.IsNil)
1088
+
1089
+	runCmd = exec.Command(dockerBinary, "run", "--link", "tlink1:tlink1", "--name", "tlink2", "-d", "busybox", "top")
1090
+	out, _, err = runCommandWithOutput(runCmd)
1091
+	c.Assert(err, check.IsNil)
1092
+
1093
+	id2 := strings.TrimSpace(out)
1094
+	c.Assert(waitRun(id2), check.IsNil)
1095
+
1096
+	links, err := inspectFieldJSON(id2, "HostConfig.Links")
1097
+	c.Assert(err, check.IsNil)
1098
+
1099
+	if links != "[\"/tlink1:/tlink2/tlink1\"]" {
1100
+		c.Fatal("expected to have links between containers")
1101
+	}
1102
+
1103
+	status, _, err := sockRequest("DELETE", "/containers/tlink2/tlink1?link=1", nil)
1104
+	c.Assert(err, check.IsNil)
1105
+	c.Assert(status, check.Equals, http.StatusNoContent)
1106
+
1107
+	linksPostRm, err := inspectFieldJSON(id2, "HostConfig.Links")
1108
+	c.Assert(err, check.IsNil)
1109
+
1110
+	if linksPostRm != "null" {
1111
+		c.Fatal("call to api deleteContainer links should have removed the specified links")
1112
+	}
1113
+}
1114
+
1115
+func (s *DockerSuite) TestContainerApiDeleteConflict(c *check.C) {
1116
+	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
1117
+	out, _, err := runCommandWithOutput(runCmd)
1118
+	c.Assert(err, check.IsNil)
1119
+
1120
+	id := strings.TrimSpace(out)
1121
+	c.Assert(waitRun(id), check.IsNil)
1122
+
1123
+	status, _, err := sockRequest("DELETE", "/containers/"+id, nil)
1124
+	c.Assert(status, check.Equals, http.StatusConflict)
1125
+	c.Assert(err, check.IsNil)
1126
+}
1127
+
1128
+func (s *DockerSuite) TestContainerApiDeleteRemoveVolume(c *check.C) {
1129
+	testRequires(c, SameHostDaemon)
1130
+
1131
+	runCmd := exec.Command(dockerBinary, "run", "-d", "-v", "/testvolume", "busybox", "top")
1132
+	out, _, err := runCommandWithOutput(runCmd)
1133
+	c.Assert(err, check.IsNil)
1134
+
1135
+	id := strings.TrimSpace(out)
1136
+	c.Assert(waitRun(id), check.IsNil)
1137
+
1138
+	vol, err := inspectFieldMap(id, "Volumes", "/testvolume")
1139
+	c.Assert(err, check.IsNil)
1140
+
1141
+	_, err = os.Stat(vol)
1142
+	c.Assert(err, check.IsNil)
1143
+
1144
+	status, _, err := sockRequest("DELETE", "/containers/"+id+"?v=1&force=1", nil)
1145
+	c.Assert(status, check.Equals, http.StatusNoContent)
1146
+	c.Assert(err, check.IsNil)
1147
+
1148
+	if _, err := os.Stat(vol); !os.IsNotExist(err) {
1149
+		c.Fatalf("expected to get ErrNotExist error, got %v", err)
1150
+	}
1151
+}
... ...
@@ -1,7 +1,6 @@
1 1
 package main
2 2
 
3 3
 import (
4
-	"net/http"
5 4
 	"os"
6 5
 	"os/exec"
7 6
 	"strings"
... ...
@@ -54,16 +53,6 @@ func (s *DockerSuite) TestRmRunningContainer(c *check.C) {
54 54
 
55 55
 }
56 56
 
57
-func (s *DockerSuite) TestRmRunningContainerCheckError409(c *check.C) {
58
-
59
-	createRunningContainer(c, "foo")
60
-
61
-	endpoint := "/containers/foo"
62
-	status, _, err := sockRequest("DELETE", endpoint, nil)
63
-	c.Assert(status, check.Equals, http.StatusConflict)
64
-	c.Assert(err, check.IsNil)
65
-}
66
-
67 57
 func (s *DockerSuite) TestRmForceRemoveRunningContainer(c *check.C) {
68 58
 
69 59
 	createRunningContainer(c, "foo")