Skip to content

Commit 7d427c3

Browse files
authored
Add ability to skip individual tests (#230)
* Add ability to skip individual tests - Added `runtime.SkipIf` - Re-combined federation test as it can now be individually skipped on Dendrite. From a CI perspective, no changes are required as this uses the existing build tags setup to detect HS impl. * Update onboarding
1 parent b747f38 commit 7d427c3

File tree

6 files changed

+116
-92
lines changed

6 files changed

+116
-92
lines changed

ONBOARDING.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,19 @@ Normally, server logs are only printed when one of the tests fail. To override t
189189

190190
### How do I skip a test?
191191

192-
Use one of `t.Skipf(...)` or `t.SkipNow()`.
192+
To conditionally skip a *single* test based on the homeserver being run, add a single line at the start of the test:
193+
```go
194+
runtime.SkipIf(t, runtime.Dendrite)
195+
```
196+
To conditionally skip an entire *file* based on the homeserver being run, add a [build tag](https://pkg.go.dev/cmd/go#hdr-Build_constraints) at the top of the file which will skip execution of all the tests in this file if Complement is run with this flag:
197+
```go
198+
// +build !dendrite_blacklist
199+
```
200+
You can also do this based on features for MSC tests (which means you must run Complement *with* this tag for these tests *to run*):
201+
```go
202+
// +build msc_2836
203+
```
204+
See [GH Actions](https://github.com/matrix-org/complement/blob/master/.github/workflows/ci.yaml) for an example of how this is used for different homeservers in practice.
193205

194206
### Why do we use `t.Errorf` sometimes and `t.Fatalf` other times?
195207

runtime/hs.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package runtime
2+
3+
import "testing"
4+
5+
const (
6+
Dendrite = "dendrite"
7+
Synapse = "synapse"
8+
)
9+
10+
var Homeserver string
11+
12+
// Skip the test (via t.Skipf) if the homeserver being tested matches `hs`, else return.
13+
//
14+
// The homeserver being tested is detected via the presence of a `*_blacklist` tag e.g:
15+
// go test -tags="dendrite_blacklist"
16+
// This means it is important to always specify this tag when running tests. Failure to do
17+
// so will result in a warning being printed to stdout, and the test will be run. When a new server
18+
// implementation is added, a respective `hs_$name.go` needs to be created in this directory. This
19+
// file pairs together the tag name with a string constant declared in this package
20+
// e.g. dendrite_blacklist == runtime.Dendrite
21+
func SkipIf(t *testing.T, hs string) {
22+
t.Helper()
23+
if Homeserver == hs {
24+
t.Skipf("skipped on %s", hs)
25+
}
26+
if Homeserver == "" {
27+
// they ran Complement without a blacklist so it's impossible to know what HS they are
28+
// running, warn them.
29+
t.Logf(
30+
"WARNING: %s called runtime.SkipIf(%s) but Complement doesn't know which HS is running as it was run without a *_blacklist tag: executing test.",
31+
t.Name(), hs,
32+
)
33+
}
34+
}

runtime/hs_dendrite.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// +build dendrite_blacklist
2+
3+
package runtime
4+
5+
func init() {
6+
Homeserver = Dendrite
7+
}

runtime/hs_synapse.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// +build synapse_blacklist
2+
3+
package runtime
4+
5+
func init() {
6+
Homeserver = Synapse
7+
}

tests/federation_room_join_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/matrix-org/complement/internal/federation"
2121
"github.com/matrix-org/complement/internal/match"
2222
"github.com/matrix-org/complement/internal/must"
23+
"github.com/matrix-org/complement/runtime"
2324
)
2425

2526
// This tests that joining a room with ?server_name= works correctly.
@@ -196,6 +197,60 @@ func TestJoinFederatedRoomWithUnverifiableEvents(t *testing.T) {
196197
alice := deployment.Client(t, "hs1", "@alice:hs1")
197198
alice.JoinRoom(t, roomAlias, nil)
198199
})
200+
t.Run("/send_join response with state with unverifiable auth events shouldn't block room join", func(t *testing.T) {
201+
runtime.SkipIf(t, runtime.Dendrite) // https://github.com/matrix-org/dendrite/issues/2028
202+
room := srv.MustMakeRoom(t, ver, federation.InitialRoomEvents(ver, charlie))
203+
roomAlias := srv.MakeAliasMapping("UnverifiableAuthEvents", room.RoomID)
204+
205+
// create a normal event then modify the signatures
206+
rawEvent := srv.MustCreateEvent(t, room, b.Event{
207+
Sender: charlie,
208+
StateKey: &charlie,
209+
Type: "m.room.member",
210+
Content: map[string]interface{}{
211+
"membership": "join",
212+
"name": "This event has a bad signature",
213+
},
214+
}).JSON()
215+
rawSig, err := json.Marshal(map[string]interface{}{
216+
docker.HostnameRunningComplement: map[string]string{
217+
string(srv.KeyID): "/3z+pJjiJXWhwfqIEzmNksvBHCoXTktK/y0rRuWJXw6i1+ygRG/suDCKhFuuz6gPapRmEMPVILi2mJqHHXPKAg",
218+
},
219+
})
220+
must.NotError(t, "failed to marshal bad signature block", err)
221+
rawEvent, err = sjson.SetRawBytes(rawEvent, "signatures", rawSig)
222+
must.NotError(t, "failed to modify signatures key from event", err)
223+
badlySignedEvent, err := gomatrixserverlib.NewEventFromTrustedJSON(rawEvent, false, ver)
224+
must.NotError(t, "failed to make Event from badly signed event JSON", err)
225+
room.AddEvent(badlySignedEvent)
226+
t.Logf("Created badly signed auth event %s", badlySignedEvent.EventID())
227+
228+
// and now add another event which will use it as an auth event.
229+
goodEvent := srv.MustCreateEvent(t, room, b.Event{
230+
Sender: charlie,
231+
StateKey: &charlie,
232+
Type: "m.room.member",
233+
Content: map[string]interface{}{
234+
"membership": "leave",
235+
},
236+
})
237+
// double-check that the bad event is in its auth events
238+
containsEvent := false
239+
for _, authEventID := range goodEvent.AuthEventIDs() {
240+
if authEventID == badlySignedEvent.EventID() {
241+
containsEvent = true
242+
break
243+
}
244+
}
245+
if !containsEvent {
246+
t.Fatalf("Bad event didn't appear in auth events of state event")
247+
}
248+
room.AddEvent(goodEvent)
249+
t.Logf("Created state event %s", goodEvent.EventID())
250+
251+
alice := deployment.Client(t, "hs1", "@alice:hs1")
252+
alice.JoinRoom(t, roomAlias, nil)
253+
})
199254
}
200255

201256
// This test checks that users cannot circumvent the auth checks via send_join.

tests/federation_room_join_with_unverifiable_auth_events_test.go

Lines changed: 0 additions & 91 deletions
This file was deleted.

0 commit comments

Comments
 (0)