fix panic error when docker stats a stopped container
| ... | ... |
@@ -315,6 +315,9 @@ func (d *driver) Clean(id string) error {
|
| 315 | 315 |
|
| 316 | 316 |
func (d *driver) Stats(id string) (*execdriver.ResourceStats, error) {
|
| 317 | 317 |
c := d.activeContainers[id] |
| 318 |
+ if c == nil {
|
|
| 319 |
+ return nil, execdriver.ErrNotRunning |
|
| 320 |
+ } |
|
| 318 | 321 |
now := time.Now() |
| 319 | 322 |
stats, err := c.Stats() |
| 320 | 323 |
if err != nil {
|
| ... | ... |
@@ -307,7 +307,7 @@ func TestGetContainerStats(t *testing.T) {
|
| 307 | 307 |
t.Fatal("stream was not closed after container was removed")
|
| 308 | 308 |
case sr := <-bc: |
| 309 | 309 |
if sr.err != nil {
|
| 310 |
- t.Fatal(err) |
|
| 310 |
+ t.Fatal(sr.err) |
|
| 311 | 311 |
} |
| 312 | 312 |
|
| 313 | 313 |
dec := json.NewDecoder(bytes.NewBuffer(sr.body)) |
| ... | ... |
@@ -320,6 +320,32 @@ func TestGetContainerStats(t *testing.T) {
|
| 320 | 320 |
logDone("container REST API - check GET containers/stats")
|
| 321 | 321 |
} |
| 322 | 322 |
|
| 323 |
+func TestGetStoppedContainerStats(t *testing.T) {
|
|
| 324 |
+ defer deleteAllContainers() |
|
| 325 |
+ var ( |
|
| 326 |
+ name = "statscontainer" |
|
| 327 |
+ runCmd = exec.Command(dockerBinary, "create", "--name", name, "busybox", "top") |
|
| 328 |
+ ) |
|
| 329 |
+ out, _, err := runCommandWithOutput(runCmd) |
|
| 330 |
+ if err != nil {
|
|
| 331 |
+ t.Fatalf("Error on container creation: %v, output: %q", err, out)
|
|
| 332 |
+ } |
|
| 333 |
+ |
|
| 334 |
+ go func() {
|
|
| 335 |
+ // We'll never get return for GET stats from sockRequest as of now, |
|
| 336 |
+ // just send request and see if panic or error would happen on daemon side. |
|
| 337 |
+ _, err := sockRequest("GET", "/containers/"+name+"/stats", nil)
|
|
| 338 |
+ if err != nil {
|
|
| 339 |
+ t.Fatal(err) |
|
| 340 |
+ } |
|
| 341 |
+ }() |
|
| 342 |
+ |
|
| 343 |
+ // allow some time to send request and let daemon deal with it |
|
| 344 |
+ time.Sleep(1 * time.Second) |
|
| 345 |
+ |
|
| 346 |
+ logDone("container REST API - check GET stopped containers/stats")
|
|
| 347 |
+} |
|
| 348 |
+ |
|
| 323 | 349 |
func TestBuildApiDockerfilePath(t *testing.T) {
|
| 324 | 350 |
// Test to make sure we stop people from trying to leave the |
| 325 | 351 |
// build context when specifying the path to the dockerfile |