Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Stop/StartServer functions to Deployment #692

Merged
merged 3 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions internal/docker/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,33 +250,47 @@ func (d *Deployer) executePostScript(hsDep *HomeserverDeployment, testName strin
return cmd.CombinedOutput()
}

// Restart a homeserver deployment.
func (d *Deployer) Restart(hsDep *HomeserverDeployment, cfg *config.Complement) error {
func (d *Deployer) StopServer(hsDep *HomeserverDeployment) error {
ctx := context.Background()
secs := int(cfg.SpawnHSTimeout.Seconds())
secs := int(d.config.SpawnHSTimeout.Seconds())
err := d.Docker.ContainerStop(ctx, hsDep.ContainerID, container.StopOptions{
Timeout: &secs,
})
if err != nil {
return fmt.Errorf("Restart: Failed to stop container %s: %s", hsDep.ContainerID, err)
return fmt.Errorf("failed to stop container %s: %s", hsDep.ContainerID, err)
}
return nil
}

err = d.Docker.ContainerStart(ctx, hsDep.ContainerID, types.ContainerStartOptions{})
// Restart a homeserver deployment.
func (d *Deployer) Restart(hsDep *HomeserverDeployment) error {
if err := d.StopServer(hsDep); err != nil {
return fmt.Errorf("Restart: %s", err)
}
if err := d.StartServer(hsDep); err != nil {
return fmt.Errorf("Restart: %s", err)
}
return nil
}

func (d *Deployer) StartServer(hsDep *HomeserverDeployment) error {
ctx := context.Background()
err := d.Docker.ContainerStart(ctx, hsDep.ContainerID, types.ContainerStartOptions{})
if err != nil {
return fmt.Errorf("Restart: Failed to start container %s: %s", hsDep.ContainerID, err)
return fmt.Errorf("failed to start container %s: %s", hsDep.ContainerID, err)
}

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

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

return nil
Expand Down
24 changes: 23 additions & 1 deletion internal/docker/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func (d *Deployment) AppServiceUser(t *testing.T, hsName, appServiceUserID strin
func (d *Deployment) Restart(t *testing.T) error {
t.Helper()
for _, hsDep := range d.HS {
err := d.Deployer.Restart(hsDep, d.Config)
err := d.Deployer.Restart(hsDep)
if err != nil {
t.Errorf("Deployment.Restart: %s", err)
return err
Expand All @@ -238,3 +238,25 @@ func (d *Deployment) Restart(t *testing.T) error {

return nil
}

func (d *Deployment) StartServer(t *testing.T, hsName string) {
t.Helper()
hsDep := d.HS[hsName]
if hsDep == nil {
t.Fatalf("StartServer: %s does not exist in this deployment", hsName)
}
if err := d.Deployer.StartServer(hsDep); err != nil {
t.Fatalf("StartServer: %s", err)
}
}

func (d *Deployment) StopServer(t *testing.T, hsName string) {
t.Helper()
hsDep := d.HS[hsName]
if hsDep == nil {
t.Fatalf("StopServer: %s does not exist in this deployment", hsName)
}
if err := d.Deployer.StopServer(hsDep); err != nil {
t.Fatalf("StopServer: %s", err)
}
}
10 changes: 9 additions & 1 deletion test_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,16 @@ type Deployment interface {
// AppServiceUser returns a client for the given app service user ID. The HS in question must have an appservice
// hooked up to it already. TODO: REMOVE
AppServiceUser(t *testing.T, hsName, appServiceUserID string) *client.CSAPI
// Restart a deployment.
// Restart a deployment. Restarts all homeservers in this deployment.
// This function is designed to be used to make assertions that servers are persisting information to disk.
Restart(t *testing.T) error
// Stop the container running this HS. Fails the test if this is not possible.
// This function is designed to be used to make assertions when federated servers are unreachable.
StopServer(t *testing.T, hsName string)
// Start the container running this HS. The HS must exist in this deployment already and it must be stopped already.
// Fails the test if this isn't true, or there was a problem.
// This function is designed to be used to make assertions when federated servers are unreachable.
kegsay marked this conversation as resolved.
Show resolved Hide resolved
StartServer(t *testing.T, hsName string)
// Destroy the entire deployment. Destroys all running containers. If `printServerLogs` is true,
// will print container logs before killing the container.
Destroy(t *testing.T)
Expand Down
Loading