Skip to content

Commit 3c6b66d

Browse files
authored
Gracefully stop containers (#631)
* Gracefully stop containers * Revert "Gracefully stop containers" This reverts commit 291f67c. * Add a ContainerKillFunc which can be used to overwrite the way a container is stopped after running a test
1 parent 3865b81 commit 3c6b66d

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

internal/docker/deployer.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232

3333
"github.com/docker/docker/client"
3434
"github.com/docker/go-connections/nat"
35+
complementRuntime "github.com/matrix-org/complement/runtime"
3536

3637
"github.com/docker/docker/api/types"
3738
"github.com/docker/docker/api/types/container"
@@ -161,7 +162,7 @@ func (d *Deployer) Destroy(dep *Deployment, printServerLogs bool, testName strin
161162

162163
printLogs(d.Docker, hsDep.ContainerID, hsDep.ContainerID)
163164
} else {
164-
err := d.Docker.ContainerKill(context.Background(), hsDep.ContainerID, "KILL")
165+
err := complementRuntime.ContainerKillFunc(d.Docker, hsDep.ContainerID)
165166
if err != nil {
166167
log.Printf("Destroy: Failed to destroy container %s : %s\n", hsDep.ContainerID, err)
167168
}

runtime/hs.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package runtime
22

3-
import "testing"
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/docker/docker/client"
8+
)
49

510
const (
611
Dendrite = "dendrite"
@@ -10,10 +15,18 @@ const (
1015

1116
var Homeserver string
1217

18+
// ContainerKillFunc is used to destroy a container, it can be overwritten by Homeserver implementations
19+
// to e.g. gracefully stop a container.
20+
var ContainerKillFunc = func(client *client.Client, containerID string) error {
21+
return client.ContainerKill(context.Background(), containerID, "KILL")
22+
}
23+
1324
// Skip the test (via t.Skipf) if the homeserver being tested matches one of the homeservers, else return.
1425
//
1526
// The homeserver being tested is detected via the presence of a `*_blacklist` tag e.g:
16-
// go test -tags="dendrite_blacklist"
27+
//
28+
// go test -tags="dendrite_blacklist"
29+
//
1730
// This means it is important to always specify this tag when running tests. Failure to do
1831
// so will result in a warning being printed to stdout, and the test will be run. When a new server
1932
// implementation is added, a respective `hs_$name.go` needs to be created in this directory. This

runtime/hs_dendrite.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
1+
//go:build dendrite_blacklist
12
// +build dendrite_blacklist
23

34
package runtime
45

6+
import (
7+
"context"
8+
"time"
9+
10+
"github.com/docker/docker/client"
11+
)
12+
513
func init() {
614
Homeserver = Dendrite
15+
// For Dendrite, we want to always stop the container gracefully, as this is needed to
16+
// extract e.g. coverage reports.
17+
ContainerKillFunc = func(client *client.Client, containerID string) error {
18+
timeout := 1 * time.Second
19+
return client.ContainerStop(context.Background(), containerID, &timeout)
20+
}
721
}

0 commit comments

Comments
 (0)