Skip to content

Custom Deployment quality of life improvements (pass t, config) #778

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

Merged
merged 8 commits into from
May 16, 2025
5 changes: 4 additions & 1 deletion internal/docker/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ type Builder struct {
}

func NewBuilder(cfg *config.Complement) (*Builder, error) {
cli, err := client.NewEnvClient()
cli, err := client.NewClientWithOpts(
client.FromEnv,
client.WithAPIVersionNegotiation(),
)
if err != nil {
return nil, err
}
Expand Down
5 changes: 4 additions & 1 deletion internal/docker/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ type Deployer struct {
}

func NewDeployer(deployNamespace string, cfg *config.Complement) (*Deployer, error) {
cli, err := client.NewEnvClient()
cli, err := client.NewClientWithOpts(
client.FromEnv,
client.WithAPIVersionNegotiation(),
)
if err != nil {
return nil, err
}
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.
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)
}
Loading