Skip to content

Commit 35ad9d9

Browse files
committed
Merge remote-tracking branch 'upstream/main'
2 parents 14cc5be + ec49265 commit 35ad9d9

17 files changed

Lines changed: 474 additions & 243 deletions

client/client.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -396,12 +396,18 @@ func (c *CSAPI) GetDefaultRoomVersion(t ct.TestLike) gomatrixserverlib.RoomVersi
396396
return gomatrixserverlib.RoomVersion(defaultVersion.Str)
397397
}
398398

399+
// MustUploadKeys uploads device and/or one time keys to the server, returning the current OTK counts.
400+
// Both device keys and one time keys are optional. Fails the test if the upload fails.
399401
func (c *CSAPI) MustUploadKeys(t ct.TestLike, deviceKeys map[string]interface{}, oneTimeKeys map[string]interface{}) (otkCounts map[string]int) {
400402
t.Helper()
401-
res := c.MustDo(t, "POST", []string{"_matrix", "client", "v3", "keys", "upload"}, WithJSONBody(t, map[string]interface{}{
402-
"device_keys": deviceKeys,
403-
"one_time_keys": oneTimeKeys,
404-
}))
403+
reqBody := make(map[string]interface{})
404+
if deviceKeys != nil {
405+
reqBody["device_keys"] = deviceKeys
406+
}
407+
if oneTimeKeys != nil {
408+
reqBody["one_time_keys"] = oneTimeKeys
409+
}
410+
res := c.MustDo(t, "POST", []string{"_matrix", "client", "v3", "keys", "upload"}, WithJSONBody(t, reqBody))
405411
bodyBytes := ParseJSON(t, res)
406412
s := struct {
407413
OTKCounts map[string]int `json:"one_time_key_counts"`
@@ -492,6 +498,20 @@ func (c *CSAPI) MustGenerateOneTimeKeys(t ct.TestLike, otkCount uint) (deviceKey
492498
return deviceKeys, oneTimeKeys
493499
}
494500

501+
// MustSetDisplayName sets the global display name for this account or fails the test.
502+
func (c *CSAPI) MustSetDisplayName(t ct.TestLike, displayname string) {
503+
c.MustDo(t, "PUT", []string{"_matrix", "client", "v3", "profile", c.UserID, "displayname"}, WithJSONBody(t, map[string]any{
504+
"displayname": displayname,
505+
}))
506+
}
507+
508+
// MustGetDisplayName returns the global display name for this user or fails the test.
509+
func (c *CSAPI) MustGetDisplayName(t ct.TestLike, userID string) string {
510+
res := c.MustDo(t, "GET", []string{"_matrix", "client", "v3", "profile", userID, "displayname"})
511+
body := ParseJSON(t, res)
512+
return GetJSONFieldStr(t, body, "displayname")
513+
}
514+
495515
// WithRawBody sets the HTTP request body to `body`
496516
func WithRawBody(body []byte) RequestOpt {
497517
return func(req *http.Request) {
@@ -572,10 +592,11 @@ func (c *CSAPI) MustDo(t ct.TestLike, method string, paths []string, opts ...Req
572592
// })
573593
func (c *CSAPI) Do(t ct.TestLike, method string, paths []string, opts ...RequestOpt) *http.Response {
574594
t.Helper()
595+
escapedPaths := make([]string, len(paths))
575596
for i := range paths {
576-
paths[i] = url.PathEscape(paths[i])
597+
escapedPaths[i] = url.PathEscape(paths[i])
577598
}
578-
reqURL := c.BaseURL + "/" + strings.Join(paths, "/")
599+
reqURL := c.BaseURL + "/" + strings.Join(escapedPaths, "/")
579600
req, err := http.NewRequest(method, reqURL, nil)
580601
if err != nil {
581602
ct.Fatalf(t, "CSAPI.Do failed to create http.NewRequest: %s", err)

go.mod

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ require (
99
github.com/docker/go-connections v0.4.0
1010
github.com/gorilla/mux v1.8.0
1111
github.com/matrix-org/gomatrix v0.0.0-20220926102614-ceba4d9f7530
12-
github.com/matrix-org/gomatrixserverlib v0.0.0-20230921171121-0466775328c7
12+
github.com/matrix-org/gomatrixserverlib v0.0.0-20250119093516-0a1b2bafb5cf
1313
github.com/matrix-org/util v0.0.0-20221111132719-399730281e66
1414
github.com/sirupsen/logrus v1.9.3
15-
github.com/tidwall/gjson v1.16.0
15+
github.com/tidwall/gjson v1.18.0
1616
github.com/tidwall/sjson v1.2.5
17-
golang.org/x/crypto v0.27.0
17+
golang.org/x/crypto v0.32.0
1818
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
1919
gonum.org/v1/plot v0.11.0
2020
)
@@ -50,9 +50,9 @@ require (
5050
go.opentelemetry.io/otel/sdk v1.30.0 // indirect
5151
go.opentelemetry.io/otel/trace v1.30.0 // indirect
5252
golang.org/x/image v0.18.0 // indirect
53-
golang.org/x/net v0.29.0 // indirect
54-
golang.org/x/sys v0.25.0 // indirect
55-
golang.org/x/text v0.18.0 // indirect
53+
golang.org/x/net v0.33.0 // indirect
54+
golang.org/x/sys v0.29.0 // indirect
55+
golang.org/x/text v0.21.0 // indirect
5656
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
5757
gotest.tools/v3 v3.0.3 // indirect
5858
)

go.sum

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI
6969
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
7070
github.com/matrix-org/gomatrix v0.0.0-20220926102614-ceba4d9f7530 h1:kHKxCOLcHH8r4Fzarl4+Y3K5hjothkVW5z7T1dUM11U=
7171
github.com/matrix-org/gomatrix v0.0.0-20220926102614-ceba4d9f7530/go.mod h1:/gBX06Kw0exX1HrwmoBibFA98yBk/jxKpGVeyQbff+s=
72-
github.com/matrix-org/gomatrixserverlib v0.0.0-20230921171121-0466775328c7 h1:NhPNNFLHwdDb/upeicBh1GkxX/sFinEp5HF1WBqPtiY=
73-
github.com/matrix-org/gomatrixserverlib v0.0.0-20230921171121-0466775328c7/go.mod h1:H9V9N3Uqn1bBJqYJNGK1noqtgJTaCEhtTdcH/mp50uU=
72+
github.com/matrix-org/gomatrixserverlib v0.0.0-20250119093516-0a1b2bafb5cf h1:NcRPAlNWXSMrYBOw9oBEX7z5uQxIKA1m/eo51DYQ7KM=
73+
github.com/matrix-org/gomatrixserverlib v0.0.0-20250119093516-0a1b2bafb5cf/go.mod h1:lcYW5K+XQ1MSNUhFJAWXx3oeErkl4f3BohYDboc9vJw=
7474
github.com/matrix-org/util v0.0.0-20221111132719-399730281e66 h1:6z4KxomXSIGWqhHcfzExgkH3Z3UkIXry4ibJS4Aqz2Y=
7575
github.com/matrix-org/util v0.0.0-20221111132719-399730281e66/go.mod h1:iBI1foelCqA09JJgPV0FYz4qA5dUXYOxMi57FxKBdd4=
7676
github.com/miekg/dns v1.1.50 h1:DQUfb9uc6smULcREF09Uc+/Gd46YWqJd5DbpPE9xkcA=
@@ -105,8 +105,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
105105
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
106106
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
107107
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
108-
github.com/tidwall/gjson v1.16.0 h1:SyXa+dsSPpUlcwEDuKuEBJEz5vzTvOea+9rjyYodQFg=
109-
github.com/tidwall/gjson v1.16.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
108+
github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY=
109+
github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
110110
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
111111
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
112112
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
@@ -135,8 +135,8 @@ go.opentelemetry.io/proto/otlp v1.3.1/go.mod h1:0X1WI4de4ZsLrrJNLAQbFeLCm3T7yBkR
135135
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
136136
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
137137
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
138-
golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=
139-
golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
138+
golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
139+
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
140140
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
141141
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
142142
golang.org/x/image v0.0.0-20190910094157-69e4b8554b2a/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
@@ -157,13 +157,13 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
157157
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
158158
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
159159
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
160-
golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo=
161-
golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0=
160+
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
161+
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
162162
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
163163
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
164164
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
165-
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
166-
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
165+
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
166+
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
167167
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
168168
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
169169
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -173,13 +173,13 @@ golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7w
173173
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
174174
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
175175
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
176-
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
177-
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
176+
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
177+
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
178178
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
179179
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
180180
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
181-
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
182-
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
181+
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
182+
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
183183
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac h1:7zkz7BUtwNFFqcowJ+RIgu2MaV/MapERkDIy+mwPyjs=
184184
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
185185
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

internal/docker/builder.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,48 @@ func printLogs(docker *client.Client, containerID, contextStr string) {
494494
log.Printf("============== %s : END LOGS ==============\n\n\n", contextStr)
495495
}
496496

497+
func printPortBindingsOfAllComplementContainers(docker *client.Client, contextStr string) {
498+
ctx := context.Background()
499+
500+
containers, err := docker.ContainerList(ctx, container.ListOptions{
501+
All: true,
502+
Filters: label(
503+
complementLabel,
504+
),
505+
})
506+
if err != nil {
507+
log.Printf("%s : Failed to list containers while trying to `printPortBindingsOfAllComplementContainers`: %s\n", contextStr, err)
508+
return
509+
}
510+
511+
log.Printf("============== %s : START ALL COMPLEMENT DOCKER PORT BINDINGS ==============\n", contextStr)
512+
513+
for _, container := range containers {
514+
log.Printf("Container: %s: %s", container.ID, container.Names)
515+
516+
inspectRes, err := docker.ContainerInspect(ctx, container.ID)
517+
if err != nil {
518+
log.Printf("%s : Failed to inspect container (%s) while trying to `printPortBindingsOfAllComplementContainers`: %s\n", contextStr, container.ID, err)
519+
return
520+
}
521+
522+
// Print an example so it's easier to understand the output
523+
log.Printf(" (host) -> (container)\n")
524+
// Then print the actual port bindings
525+
for containerPort, portBindings := range inspectRes.NetworkSettings.Ports {
526+
hostPortBindingStrings := make([]string, len(portBindings))
527+
for portBindingIndex, portBinding := range portBindings {
528+
hostPortBindingStrings[portBindingIndex] = fmt.Sprintf("%s:%s", portBinding.HostIP, portBinding.HostPort)
529+
}
530+
531+
log.Printf(" %s -> %s\n", strings.Join(hostPortBindingStrings, ", "), containerPort)
532+
}
533+
534+
}
535+
536+
log.Printf("=============== %s : END ALL COMPLEMENT DOCKER PORT BINDINGS ===============\n\n\n", contextStr)
537+
}
538+
497539
func endpoints(p nat.PortMap, csPort, ssPort int) (baseURL, fedBaseURL string, err error) {
498540
csapiPort := fmt.Sprintf("%d/tcp", csPort)
499541
csapiPortInfo, ok := p[nat.Port(csapiPort)]

internal/docker/deployer.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ func (d *Deployer) CreateDirtyServer(hsName string) (*HomeserverDeployment, erro
9191
baseImageURI = uri
9292
}
9393

94+
containerName := fmt.Sprintf("complement_%s_dirty_%s", d.config.PackageNamespace, hsName)
9495
hsDeployment, err := deployImage(
95-
d.Docker, baseImageURI, fmt.Sprintf("complement_%s_dirty_%s", d.config.PackageNamespace, hsName),
96+
d.Docker, baseImageURI, containerName,
9697
d.config.PackageNamespace, "", hsName, nil, "dirty",
9798
networkName, d.config,
9899
)
@@ -101,6 +102,11 @@ func (d *Deployer) CreateDirtyServer(hsName string) (*HomeserverDeployment, erro
101102
// print logs to help debug
102103
printLogs(d.Docker, hsDeployment.ContainerID, "dirty")
103104
}
105+
106+
// Give some context for what the port bindings look like the time of the failure.
107+
// This gives better context for when `bind: address already in use` errors happen.
108+
printPortBindingsOfAllComplementContainers(d.Docker, "While dirty deploying "+containerName)
109+
104110
return nil, fmt.Errorf("CreateDirtyServer: Failed to deploy image %v : %w", baseImageURI, err)
105111
}
106112
return hsDeployment, nil
@@ -164,15 +170,21 @@ func (d *Deployer) Deploy(ctx context.Context, blueprintName string) (*Deploymen
164170
asIDToRegistrationMap := asIDToRegistrationFromLabels(img.Labels)
165171

166172
// TODO: Make CSAPI port configurable
173+
containerName := fmt.Sprintf("complement_%s_%s_%s_%d", d.config.PackageNamespace, d.DeployNamespace, contextStr, counter)
167174
deployment, err := deployImage(
168-
d.Docker, img.ID, fmt.Sprintf("complement_%s_%s_%s_%d", d.config.PackageNamespace, d.DeployNamespace, contextStr, counter),
175+
d.Docker, img.ID, containerName,
169176
d.config.PackageNamespace, blueprintName, hsName, asIDToRegistrationMap, contextStr, networkName, d.config,
170177
)
171178
if err != nil {
172179
if deployment != nil && deployment.ContainerID != "" {
173180
// print logs to help debug
174181
printLogs(d.Docker, deployment.ContainerID, contextStr)
175182
}
183+
184+
// Give some context for what the port bindings look like the time of the failure.
185+
// This gives better context for when `bind: address already in use` errors happen.
186+
printPortBindingsOfAllComplementContainers(d.Docker, "While deploying "+containerName)
187+
176188
return fmt.Errorf("Deploy: Failed to deploy image %+v : %w", img, err)
177189
}
178190
mu.Lock()

test_main.go

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,33 @@ import (
99
"github.com/matrix-org/complement/ct"
1010
)
1111

12-
var testPackage *TestPackage
12+
var (
13+
testPackage *TestPackage
14+
customDeployer func(numServers int) Deployment
15+
)
16+
17+
type complementOpts struct {
18+
cleanup func()
19+
customDeployment func(numServers int) Deployment
20+
}
21+
type opt func(*complementOpts)
22+
23+
// WithCleanup adds a cleanup function which is called prior to terminating the test suite.
24+
// It is called BEFORE Complement containers are destroyed.
25+
// This function should be used for per-suite cleanup operations e.g tearing down containers, killing
26+
// child processes, etc.
27+
func WithCleanup(fn func()) opt {
28+
return func(co *complementOpts) {
29+
co.cleanup = fn
30+
}
31+
}
32+
33+
// WithDeployment adds a custom mechanism to deploy homeservers.
34+
func WithDeployment(fn func(numServers int) Deployment) opt {
35+
return func(co *complementOpts) {
36+
co.customDeployment = fn
37+
}
38+
}
1339

1440
// TestMain is the main entry point for Complement.
1541
//
@@ -19,23 +45,26 @@ var testPackage *TestPackage
1945
// The 'namespace' should be unique for this test package, among all test packages which may run in parallel, to avoid
2046
// docker containers stepping on each other. For MSCs, use the MSC name. For versioned releases, use the version number
2147
// along with any sub-directory name.
22-
func TestMain(m *testing.M, namespace string) {
23-
TestMainWithCleanup(m, namespace, nil)
24-
}
48+
//
49+
// Functional options can be used to control how Complement processes deployments.
50+
func TestMain(m *testing.M, namespace string, customOpts ...opt) {
51+
opts := &complementOpts{}
52+
for _, o := range customOpts {
53+
o(opts)
54+
}
55+
if opts.customDeployment != nil {
56+
customDeployer = opts.customDeployment
57+
}
2558

26-
// TestMainWithCleanup is TestMain but with a cleanup function prior to terminating the test suite.
27-
// This function should be used for per-suite cleanup operations e.g tearing down containers, killing
28-
// child processes, etc.
29-
func TestMainWithCleanup(m *testing.M, namespace string, cleanup func()) {
3059
var err error
3160
testPackage, err = NewTestPackage(namespace)
3261
if err != nil {
3362
fmt.Printf("Error: %s", err)
3463
os.Exit(1)
3564
}
3665
exitCode := m.Run()
37-
if cleanup != nil {
38-
cleanup()
66+
if opts.cleanup != nil {
67+
opts.cleanup()
3968
}
4069
testPackage.Cleanup()
4170
os.Exit(exitCode)
@@ -61,5 +90,8 @@ func Deploy(t ct.TestLike, numServers int) Deployment {
6190
if testPackage == nil {
6291
ct.Fatalf(t, "Deploy: testPackage not set, did you forget to call complement.TestMain?")
6392
}
93+
if customDeployer != nil {
94+
return customDeployer(numServers)
95+
}
6496
return testPackage.Deploy(t, numServers)
6597
}

tests/csapi/apidoc_profile_displayname_test.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import (
44
"testing"
55

66
"github.com/matrix-org/complement"
7-
"github.com/matrix-org/complement/client"
87
"github.com/matrix-org/complement/helpers"
9-
"github.com/matrix-org/complement/match"
108
"github.com/matrix-org/complement/must"
119
)
1210

@@ -18,19 +16,11 @@ func TestProfileDisplayName(t *testing.T) {
1816
displayName := "my_display_name"
1917
// sytest: PUT /profile/:user_id/displayname sets my name
2018
t.Run("PUT /profile/:user_id/displayname sets my name", func(t *testing.T) {
21-
reqBody := client.WithJSONBody(t, map[string]interface{}{
22-
"displayname": displayName,
23-
})
24-
_ = authedClient.MustDo(t, "PUT", []string{"_matrix", "client", "v3", "profile", authedClient.UserID, "displayname"}, reqBody)
19+
authedClient.MustSetDisplayName(t, displayName)
2520
})
2621
// sytest: GET /profile/:user_id/displayname publicly accessible
2722
t.Run("GET /profile/:user_id/displayname publicly accessible", func(t *testing.T) {
28-
res := unauthedClient.Do(t, "GET", []string{"_matrix", "client", "v3", "profile", authedClient.UserID, "displayname"})
29-
must.MatchResponse(t, res, match.HTTPResponse{
30-
StatusCode: 200,
31-
JSON: []match.JSON{
32-
match.JSONKeyEqual("displayname", displayName),
33-
},
34-
})
23+
gotDisplayName := unauthedClient.MustGetDisplayName(t, authedClient.UserID)
24+
must.Equal(t, gotDisplayName, displayName, "display name mismatch")
3525
})
3626
}

tests/csapi/keychanges_test.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ func TestKeyChangesLocal(t *testing.T) {
2828
unauthedClient := deployment.UnauthenticatedClient(t, "hs1")
2929

3030
t.Run("New login should create a device_lists.changed entry", func(t *testing.T) {
31-
mustUploadKeys(t, bob)
31+
bobDeviceKeys, bobOTKs := bob.MustGenerateOneTimeKeys(t, 1)
32+
bob.MustUploadKeys(t, bobDeviceKeys, bobOTKs)
3233

3334
roomID := alice.MustCreateRoom(t, map[string]interface{}{"preset": "public_chat"})
3435
bob.MustJoinRoom(t, roomID, []string{})
@@ -48,7 +49,8 @@ func TestKeyChangesLocal(t *testing.T) {
4849
unauthedClient.AccessToken = must.GetJSONFieldStr(t, loginResp, "access_token")
4950
unauthedClient.DeviceID = must.GetJSONFieldStr(t, loginResp, "device_id")
5051
unauthedClient.UserID = must.GetJSONFieldStr(t, loginResp, "user_id")
51-
mustUploadKeys(t, unauthedClient)
52+
unauthedKeys, unauthedOTKs := unauthedClient.MustGenerateOneTimeKeys(t, 1)
53+
unauthedClient.MustUploadKeys(t, unauthedKeys, unauthedOTKs)
5254

5355
// Alice should now see a device list changed entry for Bob
5456
nextBatch := alice.MustSyncUntil(t, client.SyncReq{Since: nextBatch1}, func(userID string, syncResp gjson.Result) error {
@@ -98,13 +100,3 @@ func TestKeyChangesLocal(t *testing.T) {
98100
}
99101
})
100102
}
101-
102-
func mustUploadKeys(t *testing.T, user *client.CSAPI) {
103-
t.Helper()
104-
deviceKeys, oneTimeKeys := user.MustGenerateOneTimeKeys(t, 5)
105-
reqBody := client.WithJSONBody(t, map[string]interface{}{
106-
"device_keys": deviceKeys,
107-
"one_time_keys": oneTimeKeys,
108-
})
109-
user.MustDo(t, "POST", []string{"_matrix", "client", "v3", "keys", "upload"}, reqBody)
110-
}

0 commit comments

Comments
 (0)