Skip to content

Commit 6b63eff

Browse files
Custom Deployment quality of life improvements (pass t, config) (#778)
Spawning from a real use-case with a custom `Deployment`/`Deployer` (Element-internal). I think this is the first working example of a custom deployment so it makes sense that we ran into a couple oversights. This PR introduces some quality of life for working with a custom `Deployment`. For reference, custom deployments were introduced in #750. - Pass `t` to allow the custom deployment creation callback (`complement.WithDeployment(callback)`) to handle errors gracefully - Also pass in `complement.Config` so the custom Deployment can align behavior to what's configured
1 parent d2e04c9 commit 6b63eff

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

internal/docker/builder.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ type Builder struct {
4747
}
4848

4949
func NewBuilder(cfg *config.Complement) (*Builder, error) {
50-
cli, err := client.NewEnvClient()
50+
cli, err := client.NewClientWithOpts(
51+
client.FromEnv,
52+
client.WithAPIVersionNegotiation(),
53+
)
5154
if err != nil {
5255
return nil, err
5356
}

internal/docker/deployer.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ type Deployer struct {
5656
}
5757

5858
func NewDeployer(deployNamespace string, cfg *config.Complement) (*Deployer, error) {
59-
cli, err := client.NewEnvClient()
59+
cli, err := client.NewClientWithOpts(
60+
client.FromEnv,
61+
client.WithAPIVersionNegotiation(),
62+
)
6063
if err != nil {
6164
return nil, err
6265
}

test_main.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,40 @@ import (
66
"testing"
77

88
"github.com/matrix-org/complement/b"
9+
"github.com/matrix-org/complement/config"
910
"github.com/matrix-org/complement/ct"
1011
)
1112

1213
var (
1314
testPackage *TestPackage
14-
customDeployer func(numServers int) Deployment
15+
customDeployer func(t ct.TestLike, numServers int, config *config.Complement) Deployment
1516
)
1617

1718
type complementOpts struct {
18-
cleanup func()
19-
customDeployment func(numServers int) Deployment
19+
// Args:
20+
// - We pass in the Complement config (`testPackage.Config`) so the deployer can inspect
21+
// `DebugLoggingEnabled`, `SpawnHSTimeout`, `PackageNamespace` etc.
22+
cleanup func(config *config.Complement)
23+
// Args:
24+
// - We pass in `t` as there needs to be a way to handle an error in the custom deployer.
25+
// - We pass in the Complement config (`testPackage.Config`) so the deployer can inspect
26+
// `DebugLoggingEnabled`, `SpawnHSTimeout`, `PackageNamespace`, etc.
27+
customDeployment func(t ct.TestLike, numServers int, config *config.Complement) Deployment
2028
}
2129
type opt func(*complementOpts)
2230

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

3341
// WithDeployment adds a custom mechanism to deploy homeservers.
34-
func WithDeployment(fn func(numServers int) Deployment) opt {
42+
func WithDeployment(fn func(t ct.TestLike, numServers int, config *config.Complement) Deployment) opt {
3543
return func(co *complementOpts) {
3644
co.customDeployment = fn
3745
}
@@ -64,7 +72,7 @@ func TestMain(m *testing.M, namespace string, customOpts ...opt) {
6472
}
6573
exitCode := m.Run()
6674
if opts.cleanup != nil {
67-
opts.cleanup()
75+
opts.cleanup(testPackage.Config)
6876
}
6977
testPackage.Cleanup()
7078
os.Exit(exitCode)
@@ -91,7 +99,7 @@ func Deploy(t ct.TestLike, numServers int) Deployment {
9199
ct.Fatalf(t, "Deploy: testPackage not set, did you forget to call complement.TestMain?")
92100
}
93101
if customDeployer != nil {
94-
return customDeployer(numServers)
102+
return customDeployer(t, numServers, testPackage.Config)
95103
}
96104
return testPackage.Deploy(t, numServers)
97105
}

0 commit comments

Comments
 (0)