Skip to content
10 changes: 10 additions & 0 deletions internal/docker/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ func (hsDep *HomeserverDeployment) SetEndpoints(baseURL string, fedBaseURL strin
}
Comment thread
MadLittleMods marked this conversation as resolved.
}

func (d *Deployment) GetFullyQualifiedHomeserverName(t ct.TestLike, hsName string) string {
_, ok := d.HS[hsName]
if !ok {
ct.Fatalf(t, "Deployment.GetFullyQualifiedHomeserverName - HS name '%s' not found", hsName)
}
// We have network aliases for each Docker container that will resolve the `hsName` to
// the container.
return hsName
}

Comment thread
MadLittleMods marked this conversation as resolved.
Outdated
// DestroyAtCleanup destroys the entire deployment. It should be called at cleanup time for dirty
// deployments only. Handles configuration options for things which should run at container destroy
// time, like post-run scripts and printing logs.
Expand Down
22 changes: 15 additions & 7 deletions test_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,40 @@ import (
"testing"

"github.com/matrix-org/complement/b"
"github.com/matrix-org/complement/config"
"github.com/matrix-org/complement/ct"
)

var (
testPackage *TestPackage
customDeployer func(numServers int) Deployment
customDeployer func(t ct.TestLike, numServers int, config *config.Complement) Deployment
)

type complementOpts struct {
cleanup func()
customDeployment func(numServers int) Deployment
// Args:
// - We pass in the Complement config (`testPackage.Config`) so the deployer can inspect
// `DebugLoggingEnabled`, `SpawnHSTimeout`, `PackageNamespace` etc.
cleanup func(config *config.Complement)
// Args:
// - We pass in `t` as there needs to be a way to handle an error in the custom deployer.
// - We pass in the Complement config (`testPackage.Config`) so the deployer can inspect
// `DebugLoggingEnabled`, `SpawnHSTimeout`, `PackageNamespace`, etc.
Comment thread
kegsay marked this conversation as resolved.
customDeployment func(t ct.TestLike, numServers int, config *config.Complement) Deployment
}
type opt func(*complementOpts)

// WithCleanup adds a cleanup function which is called prior to terminating the test suite.
// It is called BEFORE Complement containers are destroyed.
// This function should be used for per-suite cleanup operations e.g tearing down containers, killing
// child processes, etc.
func WithCleanup(fn func()) opt {
func WithCleanup(fn func(config *config.Complement)) opt {
return func(co *complementOpts) {
co.cleanup = fn
}
}

// WithDeployment adds a custom mechanism to deploy homeservers.
func WithDeployment(fn func(numServers int) Deployment) opt {
func WithDeployment(fn func(t ct.TestLike, numServers int, config *config.Complement) Deployment) opt {
return func(co *complementOpts) {
co.customDeployment = fn
}
Expand Down Expand Up @@ -64,7 +72,7 @@ func TestMain(m *testing.M, namespace string, customOpts ...opt) {
}
exitCode := m.Run()
if opts.cleanup != nil {
opts.cleanup()
opts.cleanup(testPackage.Config)
}
testPackage.Cleanup()
os.Exit(exitCode)
Expand All @@ -91,7 +99,7 @@ func Deploy(t ct.TestLike, numServers int) Deployment {
ct.Fatalf(t, "Deploy: testPackage not set, did you forget to call complement.TestMain?")
}
if customDeployer != nil {
return customDeployer(numServers)
return customDeployer(t, numServers, testPackage.Config)
}
return testPackage.Deploy(t, numServers)
}
7 changes: 7 additions & 0 deletions test_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ import (

// Deployment provides a way for tests to interact with a set of homeservers.
type Deployment interface {
// Returns the resolvable server name (host) of a homeserver given its short alias
// (e.g., "hs1", "hs2").
//
// In the case of the standard Docker deployment, this will be the same `hs1`, `hs2`
// but may be different for other custom deployments (ex.
// `shardDeployment1.GetFullyQualifiedHomeserverName(t, "hs1")` -> `hs1.shard1:8081`).
GetFullyQualifiedHomeserverName(t ct.TestLike, hsName string) string
Copy link
Copy Markdown
Collaborator Author

@MadLittleMods MadLittleMods May 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kegsay Just to double-check with the Complement taste-maker, do these changes make sense to you?

Are you okay with this breaking change to the complement.Deployment interface?

Are you okay with the breaking changes to complement.WithDeployment(...) / complement.WithCleanup(...)?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy with adding more params to With... options.

I'm unhappy with adding GetFullyQualifiedHomeserverName. See https://github.com/matrix-org/complement/pull/778/files#r2091384008

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review @kegsay!

The GetFullyQualifiedHomeserverName changes have been split off to #780

// UnauthenticatedClient returns a blank CSAPI client.
UnauthenticatedClient(t ct.TestLike, serverName string) *client.CSAPI
// Register a new user on the given server.
Expand Down
Loading