Browse code

fix unless-stopped unexpected behavior

fix https://github.com/moby/moby/issues/35304.

Signed-off-by: dengguangxing <dengguangxing@huawei.com>

Deng Guangxing authored on 2017/11/01 15:15:02
Showing 3 changed files
... ...
@@ -87,6 +87,7 @@ func (daemon *Daemon) killWithSignal(container *containerpkg.Container, sig int)
87 87
 
88 88
 	if !daemon.IsShuttingDown() {
89 89
 		container.HasBeenManuallyStopped = true
90
+		container.CheckpointTo(daemon.containersReplica)
90 91
 	}
91 92
 
92 93
 	// if the container is currently restarting we do not need to send the signal
... ...
@@ -158,6 +158,7 @@ func (daemon *Daemon) containerStart(container *container.Container, checkpoint
158 158
 
159 159
 	if resetRestartManager {
160 160
 		container.ResetRestartManager(true)
161
+		container.HasBeenManuallyStopped = false
161 162
 	}
162 163
 
163 164
 	if daemon.saveApparmorConfig(container); err != nil {
... ...
@@ -194,7 +195,6 @@ func (daemon *Daemon) containerStart(container *container.Container, checkpoint
194 194
 	}
195 195
 
196 196
 	container.SetRunning(pid, true)
197
-	container.HasBeenManuallyStopped = false
198 197
 	container.HasBeenStartedBefore = true
199 198
 	daemon.setStateCounter(container)
200 199
 
... ...
@@ -124,6 +124,9 @@ func (s *DockerDaemonSuite) TestDaemonRestartUnlessStopped(c *check.C) {
124 124
 	out, err = s.d.Cmd("run", "-d", "--name", "top2", "--restart", "unless-stopped", "busybox:latest", "top")
125 125
 	c.Assert(err, check.IsNil, check.Commentf("run top2: %v", out))
126 126
 
127
+	out, err = s.d.Cmd("run", "-d", "--name", "exit", "--restart", "unless-stopped", "busybox:latest", "false")
128
+	c.Assert(err, check.IsNil, check.Commentf("run exit: %v", out))
129
+
127 130
 	testRun := func(m map[string]bool, prefix string) {
128 131
 		var format string
129 132
 		for name, shouldRun := range m {
... ...
@@ -139,7 +142,10 @@ func (s *DockerDaemonSuite) TestDaemonRestartUnlessStopped(c *check.C) {
139 139
 	}
140 140
 
141 141
 	// both running
142
-	testRun(map[string]bool{"top1": true, "top2": true}, "")
142
+	testRun(map[string]bool{"top1": true, "top2": true, "exit": true}, "")
143
+
144
+	out, err = s.d.Cmd("stop", "exit")
145
+	c.Assert(err, check.IsNil, check.Commentf(out))
143 146
 
144 147
 	out, err = s.d.Cmd("stop", "top1")
145 148
 	c.Assert(err, check.IsNil, check.Commentf("%s", out))
... ...
@@ -148,20 +154,23 @@ func (s *DockerDaemonSuite) TestDaemonRestartUnlessStopped(c *check.C) {
148 148
 	c.Assert(err, check.IsNil, check.Commentf("%s", out))
149 149
 
150 150
 	// both stopped
151
-	testRun(map[string]bool{"top1": false, "top2": false}, "")
151
+	testRun(map[string]bool{"top1": false, "top2": false, "exit": false}, "")
152 152
 
153 153
 	s.d.Restart(c)
154 154
 
155 155
 	// restart=always running
156
-	testRun(map[string]bool{"top1": true, "top2": false}, "After daemon restart: ")
156
+	testRun(map[string]bool{"top1": true, "top2": false, "exit": false}, "After daemon restart: ")
157 157
 
158 158
 	out, err = s.d.Cmd("start", "top2")
159 159
 	c.Assert(err, check.IsNil, check.Commentf("start top2: %v", out))
160 160
 
161
+	out, err = s.d.Cmd("start", "exit")
162
+	c.Assert(err, check.IsNil, check.Commentf("start exit: %v", out))
163
+
161 164
 	s.d.Restart(c)
162 165
 
163 166
 	// both running
164
-	testRun(map[string]bool{"top1": true, "top2": true}, "After second daemon restart: ")
167
+	testRun(map[string]bool{"top1": true, "top2": true, "exit": true}, "After second daemon restart: ")
165 168
 
166 169
 }
167 170