Skip to content

Commit f8f2b2a

Browse files
authored
Add Stop/StartServer functions to Deployment (#692)
* Add Stop/StartServer functions to Deployment This will be used to test connectivity issues over federation. * Add pause/unpause to keep ports stable * Better docs --------- Co-authored-by: Kegan Dougal <=>
1 parent 971b5e1 commit f8f2b2a

File tree

3 files changed

+108
-11
lines changed

3 files changed

+108
-11
lines changed

internal/docker/deployer.go

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -250,33 +250,65 @@ func (d *Deployer) executePostScript(hsDep *HomeserverDeployment, testName strin
250250
return cmd.CombinedOutput()
251251
}
252252

253-
// Restart a homeserver deployment.
254-
func (d *Deployer) Restart(hsDep *HomeserverDeployment, cfg *config.Complement) error {
253+
func (d *Deployer) PauseServer(hsDep *HomeserverDeployment) error {
254+
ctx := context.Background()
255+
err := d.Docker.ContainerPause(ctx, hsDep.ContainerID)
256+
if err != nil {
257+
return fmt.Errorf("failed to pause container %s: %s", hsDep.ContainerID, err)
258+
}
259+
return nil
260+
}
261+
262+
func (d *Deployer) UnpauseServer(hsDep *HomeserverDeployment) error {
255263
ctx := context.Background()
256-
secs := int(cfg.SpawnHSTimeout.Seconds())
264+
err := d.Docker.ContainerUnpause(ctx, hsDep.ContainerID)
265+
if err != nil {
266+
return fmt.Errorf("failed to unpause container %s: %s", hsDep.ContainerID, err)
267+
}
268+
return nil
269+
}
270+
271+
func (d *Deployer) StopServer(hsDep *HomeserverDeployment) error {
272+
ctx := context.Background()
273+
secs := int(d.config.SpawnHSTimeout.Seconds())
257274
err := d.Docker.ContainerStop(ctx, hsDep.ContainerID, container.StopOptions{
258275
Timeout: &secs,
259276
})
260277
if err != nil {
261-
return fmt.Errorf("Restart: Failed to stop container %s: %s", hsDep.ContainerID, err)
278+
return fmt.Errorf("failed to stop container %s: %s", hsDep.ContainerID, err)
262279
}
280+
return nil
281+
}
263282

264-
err = d.Docker.ContainerStart(ctx, hsDep.ContainerID, types.ContainerStartOptions{})
283+
// Restart a homeserver deployment.
284+
func (d *Deployer) Restart(hsDep *HomeserverDeployment) error {
285+
if err := d.StopServer(hsDep); err != nil {
286+
return fmt.Errorf("Restart: %s", err)
287+
}
288+
if err := d.StartServer(hsDep); err != nil {
289+
return fmt.Errorf("Restart: %s", err)
290+
}
291+
return nil
292+
}
293+
294+
func (d *Deployer) StartServer(hsDep *HomeserverDeployment) error {
295+
ctx := context.Background()
296+
err := d.Docker.ContainerStart(ctx, hsDep.ContainerID, types.ContainerStartOptions{})
265297
if err != nil {
266-
return fmt.Errorf("Restart: Failed to start container %s: %s", hsDep.ContainerID, err)
298+
return fmt.Errorf("failed to start container %s: %s", hsDep.ContainerID, err)
267299
}
268300

269301
// Wait for the container to be ready.
270302
baseURL, fedBaseURL, err := waitForPorts(ctx, d.Docker, hsDep.ContainerID)
271303
if err != nil {
272-
return fmt.Errorf("Restart: Failed to get ports for container %s: %s", hsDep.ContainerID, err)
304+
return fmt.Errorf("failed to get ports for container %s: %s", hsDep.ContainerID, err)
273305
}
274306
hsDep.SetEndpoints(baseURL, fedBaseURL)
275307

276-
stopTime := time.Now().Add(cfg.SpawnHSTimeout)
308+
stopTime := time.Now().Add(d.config.SpawnHSTimeout)
277309
_, err = waitForContainer(ctx, d.Docker, hsDep, stopTime)
278310
if err != nil {
279-
return fmt.Errorf("Restart: Failed to restart container %s: %s", hsDep.ContainerID, err)
311+
return fmt.Errorf("failed to wait for container %s: %s", hsDep.ContainerID, err)
280312
}
281313

282314
return nil

internal/docker/deployment.go

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ func (d *Deployment) AppServiceUser(t *testing.T, hsName, appServiceUserID strin
229229
func (d *Deployment) Restart(t *testing.T) error {
230230
t.Helper()
231231
for _, hsDep := range d.HS {
232-
err := d.Deployer.Restart(hsDep, d.Config)
232+
err := d.Deployer.Restart(hsDep)
233233
if err != nil {
234234
t.Errorf("Deployment.Restart: %s", err)
235235
return err
@@ -238,3 +238,47 @@ func (d *Deployment) Restart(t *testing.T) error {
238238

239239
return nil
240240
}
241+
242+
func (d *Deployment) StartServer(t *testing.T, hsName string) {
243+
t.Helper()
244+
hsDep := d.HS[hsName]
245+
if hsDep == nil {
246+
t.Fatalf("StartServer: %s does not exist in this deployment", hsName)
247+
}
248+
if err := d.Deployer.StartServer(hsDep); err != nil {
249+
t.Fatalf("StartServer: %s", err)
250+
}
251+
}
252+
253+
func (d *Deployment) StopServer(t *testing.T, hsName string) {
254+
t.Helper()
255+
hsDep := d.HS[hsName]
256+
if hsDep == nil {
257+
t.Fatalf("StopServer: %s does not exist in this deployment", hsName)
258+
}
259+
if err := d.Deployer.StopServer(hsDep); err != nil {
260+
t.Fatalf("StopServer: %s", err)
261+
}
262+
}
263+
264+
func (d *Deployment) PauseServer(t *testing.T, hsName string) {
265+
t.Helper()
266+
hsDep := d.HS[hsName]
267+
if hsDep == nil {
268+
t.Fatalf("PauseServer: %s does not exist in this deployment", hsName)
269+
}
270+
if err := d.Deployer.PauseServer(hsDep); err != nil {
271+
t.Fatalf("PauseServer: %s", err)
272+
}
273+
}
274+
275+
func (d *Deployment) UnpauseServer(t *testing.T, hsName string) {
276+
t.Helper()
277+
hsDep := d.HS[hsName]
278+
if hsDep == nil {
279+
t.Fatalf("UnpauseServer: %s does not exist in this deployment", hsName)
280+
}
281+
if err := d.Deployer.UnpauseServer(hsDep); err != nil {
282+
t.Fatalf("UnpauseServer: %s", err)
283+
}
284+
}

test_package.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,29 @@ type Deployment interface {
3030
// AppServiceUser returns a client for the given app service user ID. The HS in question must have an appservice
3131
// hooked up to it already. TODO: REMOVE
3232
AppServiceUser(t *testing.T, hsName, appServiceUserID string) *client.CSAPI
33-
// Restart a deployment.
33+
// Restart a deployment. Restarts all homeservers in this deployment.
34+
// This function is designed to be used to make assertions that servers are persisting information to disk.
3435
Restart(t *testing.T) error
36+
// Stop the container running this HS. Fails the test if this is not possible.
37+
// This function is designed to be used to make assertions when federated servers are unreachable.
38+
// Do not use this function if you need the HS CSAPI URL to be stable, prefer PauseServer if you need this.
39+
StopServer(t *testing.T, hsName string)
40+
// Start the container running this HS. The HS must exist in this deployment already and it must be stopped already.
41+
// Fails the test if this isn't true, or there was a problem.
42+
// This function is designed to be used to start a server previously stopped via StopServer.
43+
// Do not use this function if you need the HS CSAPI URL to be stable, prefer UnpauseServer if you need this.
44+
StartServer(t *testing.T, hsName string)
45+
// Pause a running homeserver. The HS will be suspended, preserving data in memory.
46+
// Prefer this function over StopServer if you need to keep the port allocations stable across your test.
47+
// This function is designed to be used to make assertions when federated servers are unreachable.
48+
// Fails the test if there is a problem pausing.
49+
// See https://docs.docker.com/engine/reference/commandline/pause/
50+
PauseServer(t *testing.T, hsName string)
51+
// Unpause a running homeserver. The HS will be suspended, preserving data in memory.
52+
// Fails the test if there is a problem unpausing.
53+
// This function is designed to be used to make assertions when federated servers are unreachable.
54+
// see https://docs.docker.com/engine/reference/commandline/unpause/
55+
UnpauseServer(t *testing.T, hsName string)
3556
// Destroy the entire deployment. Destroys all running containers. If `printServerLogs` is true,
3657
// will print container logs before killing the container.
3758
Destroy(t *testing.T)

0 commit comments

Comments
 (0)