-
Notifications
You must be signed in to change notification settings - Fork 712
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add docker lifecycle controls, containers in states other that runnin…
…g, and a filter for those containers. Also add integration test for container controls.
- Loading branch information
Showing
13 changed files
with
530 additions
and
235 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#! /bin/bash | ||
|
||
. ./config.sh | ||
|
||
start_suite "Test container controls" | ||
|
||
weave_on $HOST1 launch | ||
scope_on $HOST1 launch | ||
|
||
CID=$(weave_on $HOST1 run -dti --name alpine alpine /bin/sh) | ||
|
||
wait_for_containers $HOST1 60 alpine | ||
|
||
assert "docker_on $HOST1 inspect --format='{{.State.Running}}' alpine" "true" | ||
PROBEID=$(docker_on $HOST1 logs weavescope 2>&1 | grep "probe starting" | sed -n 's/^.*ID \([0-9a-f]*\)$/\1/p') | ||
HOSTID=$(echo $HOST1 | cut -d"." -f1) | ||
assert_raises "curl -f -X POST 'http://$HOST1:4040/api/control/$PROBEID/$HOSTID;$CID/docker_stop_container'" | ||
|
||
sleep 5 | ||
assert "docker_on $HOST1 inspect --format='{{.State.Running}}' alpine" "false" | ||
|
||
scope_end_suite |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package docker | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/weaveworks/scope/probe/controls" | ||
"github.com/weaveworks/scope/report" | ||
"github.com/weaveworks/scope/xfer" | ||
) | ||
|
||
// Control IDs used by the docker intergation. | ||
const ( | ||
StopContainer = "docker_stop_container" | ||
StartContainer = "docker_start_container" | ||
RestartContainer = "docker_restart_container" | ||
PauseContainer = "docker_pause_container" | ||
UnpauseContainer = "docker_unpause_container" | ||
|
||
waitTime = 10 | ||
) | ||
|
||
func (r *registry) stopContainer(req xfer.Request) xfer.Response { | ||
log.Printf("Stopping container %s", req.NodeID) | ||
|
||
_, containerID, ok := report.ParseContainerNodeID(req.NodeID) | ||
if !ok { | ||
return xfer.ResponseErrorf("Invalid ID: %s", req.NodeID) | ||
} | ||
|
||
return xfer.ResponseError(r.client.StopContainer(containerID, waitTime)) | ||
} | ||
|
||
func (r *registry) startContainer(req xfer.Request) xfer.Response { | ||
log.Printf("Starting container %s", req.NodeID) | ||
|
||
_, containerID, ok := report.ParseContainerNodeID(req.NodeID) | ||
if !ok { | ||
return xfer.ResponseErrorf("Invalid ID: %s", req.NodeID) | ||
} | ||
|
||
return xfer.ResponseError(r.client.StartContainer(containerID, nil)) | ||
} | ||
|
||
func (r *registry) restartContainer(req xfer.Request) xfer.Response { | ||
log.Printf("Restarting container %s", req.NodeID) | ||
|
||
_, containerID, ok := report.ParseContainerNodeID(req.NodeID) | ||
if !ok { | ||
return xfer.ResponseErrorf("Invalid ID: %s", req.NodeID) | ||
} | ||
|
||
return xfer.ResponseError(r.client.RestartContainer(containerID, waitTime)) | ||
} | ||
|
||
func (r *registry) pauseContainer(req xfer.Request) xfer.Response { | ||
log.Printf("Pausing container %s", req.NodeID) | ||
|
||
_, containerID, ok := report.ParseContainerNodeID(req.NodeID) | ||
if !ok { | ||
return xfer.ResponseErrorf("Invalid ID: %s", req.NodeID) | ||
} | ||
|
||
return xfer.ResponseError(r.client.PauseContainer(containerID)) | ||
} | ||
|
||
func (r *registry) unpauseContainer(req xfer.Request) xfer.Response { | ||
log.Printf("Unpausing container %s", req.NodeID) | ||
|
||
_, containerID, ok := report.ParseContainerNodeID(req.NodeID) | ||
if !ok { | ||
return xfer.ResponseErrorf("Invalid ID: %s", req.NodeID) | ||
} | ||
|
||
return xfer.ResponseError(r.client.UnpauseContainer(containerID)) | ||
} | ||
|
||
func (r *registry) registerControls() { | ||
controls.Register(StopContainer, r.stopContainer) | ||
controls.Register(StartContainer, r.startContainer) | ||
controls.Register(RestartContainer, r.restartContainer) | ||
controls.Register(PauseContainer, r.pauseContainer) | ||
controls.Register(UnpauseContainer, r.unpauseContainer) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package docker_test | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
"time" | ||
|
||
"github.com/weaveworks/scope/probe/controls" | ||
"github.com/weaveworks/scope/probe/docker" | ||
"github.com/weaveworks/scope/report" | ||
"github.com/weaveworks/scope/xfer" | ||
) | ||
|
||
func TestControls(t *testing.T) { | ||
mdc := newMockClient() | ||
setupStubs(mdc, func() { | ||
registry, _ := docker.NewRegistry(10 * time.Second) | ||
defer registry.Stop() | ||
|
||
for _, tc := range []struct{ command, result string }{ | ||
{docker.StopContainer, "stopped"}, | ||
{docker.StartContainer, "started"}, | ||
{docker.RestartContainer, "restarted"}, | ||
{docker.PauseContainer, "paused"}, | ||
{docker.UnpauseContainer, "unpaused"}, | ||
} { | ||
result := controls.HandleControlRequest(xfer.Request{ | ||
Control: tc.command, | ||
NodeID: report.MakeContainerNodeID("", "a1b2c3d4e5"), | ||
}) | ||
if !reflect.DeepEqual(result, xfer.Response{ | ||
Error: tc.result, | ||
}) { | ||
t.Error(result) | ||
} | ||
} | ||
}) | ||
} |
Oops, something went wrong.