mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-07 14:13:11 -06:00
Remove internal/test and use /test only
Remove a lot of ancient code too.
This commit is contained in:
parent
3078f15d81
commit
23626fdf9c
|
|
@ -20,7 +20,7 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/internal/test"
|
"github.com/matrix-org/dendrite/test"
|
||||||
)
|
)
|
||||||
|
|
||||||
const usage = `Usage: %s
|
const usage = `Usage: %s
|
||||||
|
|
|
||||||
|
|
@ -8,13 +8,25 @@ import (
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/federationapi"
|
"github.com/matrix-org/dendrite/federationapi"
|
||||||
"github.com/matrix-org/dendrite/federationapi/internal"
|
"github.com/matrix-org/dendrite/federationapi/internal"
|
||||||
"github.com/matrix-org/dendrite/internal/test"
|
|
||||||
"github.com/matrix-org/dendrite/setup/base"
|
"github.com/matrix-org/dendrite/setup/base"
|
||||||
"github.com/matrix-org/dendrite/setup/config"
|
"github.com/matrix-org/dendrite/setup/config"
|
||||||
|
"github.com/matrix-org/dendrite/test"
|
||||||
"github.com/matrix-org/gomatrix"
|
"github.com/matrix-org/gomatrix"
|
||||||
"github.com/matrix-org/gomatrixserverlib"
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
/*
|
||||||
|
func TestSyncAPICreateRoomSyncEarly(t *testing.T) {
|
||||||
|
test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
|
||||||
|
testSyncAPICreateRoomSyncEarly(t, dbType)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func testSyncAPICreateRoomSyncEarly(t *testing.T, dbType test.DBType) {
|
||||||
|
user := test.NewUser()
|
||||||
|
room := test.NewRoom(t, user)
|
||||||
|
} */
|
||||||
|
|
||||||
// Tests that event IDs with '/' in them (escaped as %2F) are correctly passed to the right handler and don't 404.
|
// Tests that event IDs with '/' in them (escaped as %2F) are correctly passed to the right handler and don't 404.
|
||||||
// Relevant for v3 rooms and a cause of flakey sytests as the IDs are randomly generated.
|
// Relevant for v3 rooms and a cause of flakey sytests as the IDs are randomly generated.
|
||||||
func TestRoomsV3URLEscapeDoNot404(t *testing.T) {
|
func TestRoomsV3URLEscapeDoNot404(t *testing.T) {
|
||||||
|
|
@ -86,7 +98,7 @@ func TestRoomsV3URLEscapeDoNot404(t *testing.T) {
|
||||||
}
|
}
|
||||||
gerr, ok := err.(gomatrix.HTTPError)
|
gerr, ok := err.(gomatrix.HTTPError)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Errorf("failed to cast response error as gomatrix.HTTPError")
|
t.Errorf("failed to cast response error as gomatrix.HTTPError: %s", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
t.Logf("Error: %+v", gerr)
|
t.Logf("Error: %+v", gerr)
|
||||||
|
|
|
||||||
|
|
@ -8,8 +8,8 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/internal"
|
"github.com/matrix-org/dendrite/internal"
|
||||||
"github.com/matrix-org/dendrite/internal/test"
|
|
||||||
"github.com/matrix-org/dendrite/roomserver/api"
|
"github.com/matrix-org/dendrite/roomserver/api"
|
||||||
|
"github.com/matrix-org/dendrite/test"
|
||||||
"github.com/matrix-org/gomatrixserverlib"
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/internal/test"
|
"github.com/matrix-org/dendrite/test"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestEDUCache(t *testing.T) {
|
func TestEDUCache(t *testing.T) {
|
||||||
|
|
|
||||||
|
|
@ -1,158 +0,0 @@
|
||||||
// Copyright 2017 Vector Creations Ltd
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
package test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"crypto/tls"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/matrix-org/gomatrixserverlib"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Request contains the information necessary to issue a request and test its result
|
|
||||||
type Request struct {
|
|
||||||
Req *http.Request
|
|
||||||
WantedBody string
|
|
||||||
WantedStatusCode int
|
|
||||||
LastErr *LastRequestErr
|
|
||||||
}
|
|
||||||
|
|
||||||
// LastRequestErr is a synchronised error wrapper
|
|
||||||
// Useful for obtaining the last error from a set of requests
|
|
||||||
type LastRequestErr struct {
|
|
||||||
sync.Mutex
|
|
||||||
Err error
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set sets the error
|
|
||||||
func (r *LastRequestErr) Set(err error) {
|
|
||||||
r.Lock()
|
|
||||||
defer r.Unlock()
|
|
||||||
r.Err = err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get gets the error
|
|
||||||
func (r *LastRequestErr) Get() error {
|
|
||||||
r.Lock()
|
|
||||||
defer r.Unlock()
|
|
||||||
return r.Err
|
|
||||||
}
|
|
||||||
|
|
||||||
// CanonicalJSONInput canonicalises a slice of JSON strings
|
|
||||||
// Useful for test input
|
|
||||||
func CanonicalJSONInput(jsonData []string) []string {
|
|
||||||
for i := range jsonData {
|
|
||||||
jsonBytes, err := gomatrixserverlib.CanonicalJSON([]byte(jsonData[i]))
|
|
||||||
if err != nil && err != io.EOF {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
jsonData[i] = string(jsonBytes)
|
|
||||||
}
|
|
||||||
return jsonData
|
|
||||||
}
|
|
||||||
|
|
||||||
// Do issues a request and checks the status code and body of the response
|
|
||||||
func (r *Request) Do() (err error) {
|
|
||||||
client := &http.Client{
|
|
||||||
Timeout: 5 * time.Second,
|
|
||||||
Transport: &http.Transport{
|
|
||||||
TLSClientConfig: &tls.Config{
|
|
||||||
InsecureSkipVerify: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
res, err := client.Do(r.Req)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer (func() { err = res.Body.Close() })()
|
|
||||||
|
|
||||||
if res.StatusCode != r.WantedStatusCode {
|
|
||||||
return fmt.Errorf("incorrect status code. Expected: %d Got: %d", r.WantedStatusCode, res.StatusCode)
|
|
||||||
}
|
|
||||||
|
|
||||||
if r.WantedBody != "" {
|
|
||||||
resBytes, err := ioutil.ReadAll(res.Body)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
jsonBytes, err := gomatrixserverlib.CanonicalJSON(resBytes)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if string(jsonBytes) != r.WantedBody {
|
|
||||||
return fmt.Errorf("returned wrong bytes. Expected:\n%s\n\nGot:\n%s", r.WantedBody, string(jsonBytes))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// DoUntilSuccess blocks and repeats the same request until the response returns the desired status code and body.
|
|
||||||
// It then closes the given channel and returns.
|
|
||||||
func (r *Request) DoUntilSuccess(done chan error) {
|
|
||||||
r.LastErr = &LastRequestErr{}
|
|
||||||
for {
|
|
||||||
if err := r.Do(); err != nil {
|
|
||||||
r.LastErr.Set(err)
|
|
||||||
time.Sleep(1 * time.Second) // don't tightloop
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
close(done)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run repeatedly issues a request until success, error or a timeout is reached
|
|
||||||
func (r *Request) Run(label string, timeout time.Duration, serverCmdChan chan error) {
|
|
||||||
fmt.Printf("==TESTING== %v (timeout: %v)\n", label, timeout)
|
|
||||||
done := make(chan error, 1)
|
|
||||||
|
|
||||||
// We need to wait for the server to:
|
|
||||||
// - have connected to the database
|
|
||||||
// - have created the tables
|
|
||||||
// - be listening on the given port
|
|
||||||
go r.DoUntilSuccess(done)
|
|
||||||
|
|
||||||
// wait for one of:
|
|
||||||
// - the test to pass (done channel is closed)
|
|
||||||
// - the server to exit with an error (error sent on serverCmdChan)
|
|
||||||
// - our test timeout to expire
|
|
||||||
// We don't need to clean up since the main() function handles that in the event we panic
|
|
||||||
select {
|
|
||||||
case <-time.After(timeout):
|
|
||||||
fmt.Printf("==TESTING== %v TIMEOUT\n", label)
|
|
||||||
if reqErr := r.LastErr.Get(); reqErr != nil {
|
|
||||||
fmt.Println("Last /sync request error:")
|
|
||||||
fmt.Println(reqErr)
|
|
||||||
}
|
|
||||||
panic(fmt.Sprintf("%v server timed out", label))
|
|
||||||
case err := <-serverCmdChan:
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("=============================================================================================")
|
|
||||||
fmt.Printf("%v server failed to run. If failing with 'pq: password authentication failed for user' try:", label)
|
|
||||||
fmt.Println(" export PGHOST=/var/run/postgresql")
|
|
||||||
fmt.Println("=============================================================================================")
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
case <-done:
|
|
||||||
fmt.Printf("==TESTING== %v PASSED\n", label)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,76 +0,0 @@
|
||||||
// Copyright 2017 Vector Creations Ltd
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
package test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io"
|
|
||||||
"os/exec"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
// KafkaExecutor executes kafka scripts.
|
|
||||||
type KafkaExecutor struct {
|
|
||||||
// The location of Zookeeper. Typically this is `localhost:2181`.
|
|
||||||
ZookeeperURI string
|
|
||||||
// The directory where Kafka is installed to. Used to locate kafka scripts.
|
|
||||||
KafkaDirectory string
|
|
||||||
// The location of the Kafka logs. Typically this is `localhost:9092`.
|
|
||||||
KafkaURI string
|
|
||||||
// Where stdout and stderr should be written to. Typically this is `os.Stderr`.
|
|
||||||
OutputWriter io.Writer
|
|
||||||
}
|
|
||||||
|
|
||||||
// CreateTopic creates a new kafka topic. This is created with a single partition.
|
|
||||||
func (e *KafkaExecutor) CreateTopic(topic string) error {
|
|
||||||
cmd := exec.Command(
|
|
||||||
filepath.Join(e.KafkaDirectory, "bin", "kafka-topics.sh"),
|
|
||||||
"--create",
|
|
||||||
"--zookeeper", e.ZookeeperURI,
|
|
||||||
"--replication-factor", "1",
|
|
||||||
"--partitions", "1",
|
|
||||||
"--topic", topic,
|
|
||||||
)
|
|
||||||
cmd.Stdout = e.OutputWriter
|
|
||||||
cmd.Stderr = e.OutputWriter
|
|
||||||
return cmd.Run()
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteToTopic writes data to a kafka topic.
|
|
||||||
func (e *KafkaExecutor) WriteToTopic(topic string, data []string) error {
|
|
||||||
cmd := exec.Command(
|
|
||||||
filepath.Join(e.KafkaDirectory, "bin", "kafka-console-producer.sh"),
|
|
||||||
"--broker-list", e.KafkaURI,
|
|
||||||
"--topic", topic,
|
|
||||||
)
|
|
||||||
cmd.Stdout = e.OutputWriter
|
|
||||||
cmd.Stderr = e.OutputWriter
|
|
||||||
cmd.Stdin = strings.NewReader(strings.Join(data, "\n"))
|
|
||||||
return cmd.Run()
|
|
||||||
}
|
|
||||||
|
|
||||||
// DeleteTopic deletes a given kafka topic if it exists.
|
|
||||||
func (e *KafkaExecutor) DeleteTopic(topic string) error {
|
|
||||||
cmd := exec.Command(
|
|
||||||
filepath.Join(e.KafkaDirectory, "bin", "kafka-topics.sh"),
|
|
||||||
"--delete",
|
|
||||||
"--if-exists",
|
|
||||||
"--zookeeper", e.ZookeeperURI,
|
|
||||||
"--topic", topic,
|
|
||||||
)
|
|
||||||
cmd.Stderr = e.OutputWriter
|
|
||||||
cmd.Stdout = e.OutputWriter
|
|
||||||
return cmd.Run()
|
|
||||||
}
|
|
||||||
|
|
@ -1,152 +0,0 @@
|
||||||
// Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
package test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"net"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"os/exec"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/setup/config"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Defaulting allows assignment of string variables with a fallback default value
|
|
||||||
// Useful for use with os.Getenv() for example
|
|
||||||
func Defaulting(value, defaultValue string) string {
|
|
||||||
if value == "" {
|
|
||||||
value = defaultValue
|
|
||||||
}
|
|
||||||
return value
|
|
||||||
}
|
|
||||||
|
|
||||||
// CreateDatabase creates a new database, dropping it first if it exists
|
|
||||||
func CreateDatabase(command string, args []string, database string) error {
|
|
||||||
cmd := exec.Command(command, args...)
|
|
||||||
cmd.Stdin = strings.NewReader(
|
|
||||||
fmt.Sprintf("DROP DATABASE IF EXISTS %s; CREATE DATABASE %s;", database, database),
|
|
||||||
)
|
|
||||||
// Send stdout and stderr to our stderr so that we see error messages from
|
|
||||||
// the psql process
|
|
||||||
cmd.Stdout = os.Stderr
|
|
||||||
cmd.Stderr = os.Stderr
|
|
||||||
return cmd.Run()
|
|
||||||
}
|
|
||||||
|
|
||||||
// CreateBackgroundCommand creates an executable command
|
|
||||||
// The Cmd being executed is returned. A channel is also returned,
|
|
||||||
// which will have any termination errors sent down it, followed immediately by the channel being closed.
|
|
||||||
func CreateBackgroundCommand(command string, args []string) (*exec.Cmd, chan error) {
|
|
||||||
cmd := exec.Command(command, args...)
|
|
||||||
cmd.Stderr = os.Stderr
|
|
||||||
cmd.Stdout = os.Stderr
|
|
||||||
|
|
||||||
if err := cmd.Start(); err != nil {
|
|
||||||
panic("failed to start server: " + err.Error())
|
|
||||||
}
|
|
||||||
cmdChan := make(chan error, 1)
|
|
||||||
go func() {
|
|
||||||
cmdChan <- cmd.Wait()
|
|
||||||
close(cmdChan)
|
|
||||||
}()
|
|
||||||
return cmd, cmdChan
|
|
||||||
}
|
|
||||||
|
|
||||||
// InitDatabase creates the database and config file needed for the server to run
|
|
||||||
func InitDatabase(postgresDatabase, postgresContainerName string, databases []string) {
|
|
||||||
if len(databases) > 0 {
|
|
||||||
var dbCmd string
|
|
||||||
var dbArgs []string
|
|
||||||
if postgresContainerName == "" {
|
|
||||||
dbCmd = "psql"
|
|
||||||
dbArgs = []string{postgresDatabase}
|
|
||||||
} else {
|
|
||||||
dbCmd = "docker"
|
|
||||||
dbArgs = []string{
|
|
||||||
"exec", "-i", postgresContainerName, "psql", "-U", "postgres", postgresDatabase,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for _, database := range databases {
|
|
||||||
if err := CreateDatabase(dbCmd, dbArgs, database); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// StartProxy creates a reverse proxy
|
|
||||||
func StartProxy(bindAddr string, cfg *config.Dendrite) (*exec.Cmd, chan error) {
|
|
||||||
proxyArgs := []string{
|
|
||||||
"--bind-address", bindAddr,
|
|
||||||
"--sync-api-server-url", "http://" + string(cfg.SyncAPI.InternalAPI.Connect),
|
|
||||||
"--client-api-server-url", "http://" + string(cfg.ClientAPI.InternalAPI.Connect),
|
|
||||||
"--media-api-server-url", "http://" + string(cfg.MediaAPI.InternalAPI.Connect),
|
|
||||||
"--tls-cert", "server.crt",
|
|
||||||
"--tls-key", "server.key",
|
|
||||||
}
|
|
||||||
return CreateBackgroundCommand(
|
|
||||||
filepath.Join(filepath.Dir(os.Args[0]), "client-api-proxy"),
|
|
||||||
proxyArgs,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListenAndServe will listen on a random high-numbered port and attach the given router.
|
|
||||||
// Returns the base URL to send requests to. Call `cancel` to shutdown the server, which will block until it has closed.
|
|
||||||
func ListenAndServe(t *testing.T, router http.Handler, useTLS bool) (apiURL string, cancel func()) {
|
|
||||||
listener, err := net.Listen("tcp", ":0")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to listen: %s", err)
|
|
||||||
}
|
|
||||||
port := listener.Addr().(*net.TCPAddr).Port
|
|
||||||
srv := http.Server{}
|
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
wg.Add(1)
|
|
||||||
go func() {
|
|
||||||
defer wg.Done()
|
|
||||||
srv.Handler = router
|
|
||||||
var err error
|
|
||||||
if useTLS {
|
|
||||||
certFile := filepath.Join(os.TempDir(), "dendrite.cert")
|
|
||||||
keyFile := filepath.Join(os.TempDir(), "dendrite.key")
|
|
||||||
err = NewTLSKey(keyFile, certFile)
|
|
||||||
if err != nil {
|
|
||||||
t.Logf("failed to generate tls key/cert: %s", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
err = srv.ServeTLS(listener, certFile, keyFile)
|
|
||||||
} else {
|
|
||||||
err = srv.Serve(listener)
|
|
||||||
}
|
|
||||||
if err != nil && err != http.ErrServerClosed {
|
|
||||||
t.Logf("Listen failed: %s", err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
secure := ""
|
|
||||||
if useTLS {
|
|
||||||
secure = "s"
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("http%s://localhost:%d", secure, port), func() {
|
|
||||||
_ = srv.Shutdown(context.Background())
|
|
||||||
wg.Wait()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -12,7 +12,7 @@ import (
|
||||||
"github.com/matrix-org/dendrite/roomserver/storage"
|
"github.com/matrix-org/dendrite/roomserver/storage"
|
||||||
"github.com/matrix-org/dendrite/setup/base"
|
"github.com/matrix-org/dendrite/setup/base"
|
||||||
"github.com/matrix-org/dendrite/setup/config"
|
"github.com/matrix-org/dendrite/setup/config"
|
||||||
"github.com/matrix-org/dendrite/test"
|
"github.com/matrix-org/dendrite/test/testrig"
|
||||||
"github.com/matrix-org/gomatrixserverlib"
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
"github.com/nats-io/nats.go"
|
"github.com/nats-io/nats.go"
|
||||||
)
|
)
|
||||||
|
|
@ -22,7 +22,7 @@ var jc *nats.Conn
|
||||||
|
|
||||||
func TestMain(m *testing.M) {
|
func TestMain(m *testing.M) {
|
||||||
var b *base.BaseDendrite
|
var b *base.BaseDendrite
|
||||||
b, js, jc = test.Base(nil)
|
b, js, jc = testrig.Base(nil)
|
||||||
code := m.Run()
|
code := m.Run()
|
||||||
b.ShutdownDendrite()
|
b.ShutdownDendrite()
|
||||||
b.WaitForComponentsToFinish()
|
b.WaitForComponentsToFinish()
|
||||||
|
|
|
||||||
|
|
@ -19,8 +19,8 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/internal/test"
|
|
||||||
"github.com/matrix-org/dendrite/roomserver/types"
|
"github.com/matrix-org/dendrite/roomserver/types"
|
||||||
|
"github.com/matrix-org/dendrite/test"
|
||||||
"github.com/matrix-org/gomatrixserverlib"
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ import (
|
||||||
"github.com/matrix-org/dendrite/setup/jetstream"
|
"github.com/matrix-org/dendrite/setup/jetstream"
|
||||||
"github.com/matrix-org/dendrite/syncapi/types"
|
"github.com/matrix-org/dendrite/syncapi/types"
|
||||||
"github.com/matrix-org/dendrite/test"
|
"github.com/matrix-org/dendrite/test"
|
||||||
|
"github.com/matrix-org/dendrite/test/testrig"
|
||||||
userapi "github.com/matrix-org/dendrite/userapi/api"
|
userapi "github.com/matrix-org/dendrite/userapi/api"
|
||||||
"github.com/matrix-org/gomatrixserverlib"
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
"github.com/nats-io/nats.go"
|
"github.com/nats-io/nats.go"
|
||||||
|
|
@ -96,14 +97,14 @@ func testSyncAccessTokens(t *testing.T, dbType test.DBType) {
|
||||||
AccountType: userapi.AccountTypeUser,
|
AccountType: userapi.AccountTypeUser,
|
||||||
}
|
}
|
||||||
|
|
||||||
base, close := test.CreateBaseDendrite(t, dbType)
|
base, close := testrig.CreateBaseDendrite(t, dbType)
|
||||||
defer close()
|
defer close()
|
||||||
|
|
||||||
jsctx, _ := base.NATS.Prepare(base.ProcessContext, &base.Cfg.Global.JetStream)
|
jsctx, _ := base.NATS.Prepare(base.ProcessContext, &base.Cfg.Global.JetStream)
|
||||||
defer jetstream.DeleteAllStreams(jsctx, &base.Cfg.Global.JetStream)
|
defer jetstream.DeleteAllStreams(jsctx, &base.Cfg.Global.JetStream)
|
||||||
msgs := toNATSMsgs(t, base, room.Events())
|
msgs := toNATSMsgs(t, base, room.Events())
|
||||||
AddPublicRoutes(base, &syncUserAPI{accounts: []userapi.Device{alice}}, &syncRoomserverAPI{rooms: []*test.Room{room}}, &syncKeyAPI{})
|
AddPublicRoutes(base, &syncUserAPI{accounts: []userapi.Device{alice}}, &syncRoomserverAPI{rooms: []*test.Room{room}}, &syncKeyAPI{})
|
||||||
test.MustPublishMsgs(t, jsctx, msgs...)
|
testrig.MustPublishMsgs(t, jsctx, msgs...)
|
||||||
|
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
name string
|
name string
|
||||||
|
|
@ -183,7 +184,7 @@ func testSyncAPICreateRoomSyncEarly(t *testing.T, dbType test.DBType) {
|
||||||
AccountType: userapi.AccountTypeUser,
|
AccountType: userapi.AccountTypeUser,
|
||||||
}
|
}
|
||||||
|
|
||||||
base, close := test.CreateBaseDendrite(t, dbType)
|
base, close := testrig.CreateBaseDendrite(t, dbType)
|
||||||
defer close()
|
defer close()
|
||||||
|
|
||||||
jsctx, _ := base.NATS.Prepare(base.ProcessContext, &base.Cfg.Global.JetStream)
|
jsctx, _ := base.NATS.Prepare(base.ProcessContext, &base.Cfg.Global.JetStream)
|
||||||
|
|
@ -198,7 +199,7 @@ func testSyncAPICreateRoomSyncEarly(t *testing.T, dbType test.DBType) {
|
||||||
sinceTokens := make([]string, len(msgs))
|
sinceTokens := make([]string, len(msgs))
|
||||||
AddPublicRoutes(base, &syncUserAPI{accounts: []userapi.Device{alice}}, &syncRoomserverAPI{rooms: []*test.Room{room}}, &syncKeyAPI{})
|
AddPublicRoutes(base, &syncUserAPI{accounts: []userapi.Device{alice}}, &syncRoomserverAPI{rooms: []*test.Room{room}}, &syncKeyAPI{})
|
||||||
for i, msg := range msgs {
|
for i, msg := range msgs {
|
||||||
test.MustPublishMsgs(t, jsctx, msg)
|
testrig.MustPublishMsgs(t, jsctx, msg)
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
base.PublicClientAPIMux.ServeHTTP(w, test.NewRequest(t, "GET", "/_matrix/client/v3/sync", test.WithQueryParams(map[string]string{
|
base.PublicClientAPIMux.ServeHTTP(w, test.NewRequest(t, "GET", "/_matrix/client/v3/sync", test.WithQueryParams(map[string]string{
|
||||||
|
|
@ -262,7 +263,7 @@ func toNATSMsgs(t *testing.T, base *base.BaseDendrite, input []*gomatrixserverli
|
||||||
if ev.StateKey() != nil {
|
if ev.StateKey() != nil {
|
||||||
addsStateIDs = append(addsStateIDs, ev.EventID())
|
addsStateIDs = append(addsStateIDs, ev.EventID())
|
||||||
}
|
}
|
||||||
result[i] = test.NewOutputEventMsg(t, base, ev.RoomID(), api.OutputEvent{
|
result[i] = testrig.NewOutputEventMsg(t, base, ev.RoomID(), api.OutputEvent{
|
||||||
Type: rsapi.OutputTypeNewRoomEvent,
|
Type: rsapi.OutputTypeNewRoomEvent,
|
||||||
NewRoomEvent: &rsapi.OutputNewRoomEvent{
|
NewRoomEvent: &rsapi.OutputNewRoomEvent{
|
||||||
Event: ev,
|
Event: ev,
|
||||||
|
|
|
||||||
47
test/http.go
47
test/http.go
|
|
@ -2,10 +2,15 @@ package test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"path/filepath"
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -43,3 +48,45 @@ func NewRequest(t *testing.T, method, path string, opts ...HTTPRequestOpt) *http
|
||||||
}
|
}
|
||||||
return req
|
return req
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ListenAndServe will listen on a random high-numbered port and attach the given router.
|
||||||
|
// Returns the base URL to send requests to. Call `cancel` to shutdown the server, which will block until it has closed.
|
||||||
|
func ListenAndServe(t *testing.T, router http.Handler, withTLS bool) (apiURL string, cancel func()) {
|
||||||
|
listener, err := net.Listen("tcp", ":0")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to listen: %s", err)
|
||||||
|
}
|
||||||
|
port := listener.Addr().(*net.TCPAddr).Port
|
||||||
|
srv := http.Server{}
|
||||||
|
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
srv.Handler = router
|
||||||
|
var err error
|
||||||
|
if withTLS {
|
||||||
|
certFile := filepath.Join(t.TempDir(), "dendrite.cert")
|
||||||
|
keyFile := filepath.Join(t.TempDir(), "dendrite.key")
|
||||||
|
err = NewTLSKey(keyFile, certFile)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to make TLS key: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = srv.ServeTLS(listener, certFile, keyFile)
|
||||||
|
} else {
|
||||||
|
err = srv.Serve(listener)
|
||||||
|
}
|
||||||
|
if err != nil && err != http.ErrServerClosed {
|
||||||
|
t.Logf("Listen failed: %s", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
s := ""
|
||||||
|
if withTLS {
|
||||||
|
s = "s"
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("http%s://localhost:%d", s, port), func() {
|
||||||
|
_ = srv.Shutdown(context.Background())
|
||||||
|
wg.Wait()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -25,103 +25,19 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"math/big"
|
"math/big"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/setup/config"
|
|
||||||
"github.com/matrix-org/gomatrixserverlib"
|
|
||||||
"gopkg.in/yaml.v2"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// ConfigFile is the name of the config file for a server.
|
|
||||||
ConfigFile = "dendrite.yaml"
|
|
||||||
// ServerKeyFile is the name of the file holding the matrix server private key.
|
// ServerKeyFile is the name of the file holding the matrix server private key.
|
||||||
ServerKeyFile = "server_key.pem"
|
ServerKeyFile = "server_key.pem"
|
||||||
// TLSCertFile is the name of the file holding the TLS certificate used for federation.
|
// TLSCertFile is the name of the file holding the TLS certificate used for federation.
|
||||||
TLSCertFile = "tls_cert.pem"
|
TLSCertFile = "tls_cert.pem"
|
||||||
// TLSKeyFile is the name of the file holding the TLS key used for federation.
|
// TLSKeyFile is the name of the file holding the TLS key used for federation.
|
||||||
TLSKeyFile = "tls_key.pem"
|
TLSKeyFile = "tls_key.pem"
|
||||||
// MediaDir is the name of the directory used to store media.
|
|
||||||
MediaDir = "media"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// MakeConfig makes a config suitable for running integration tests.
|
|
||||||
// Generates new matrix and TLS keys for the server.
|
|
||||||
func MakeConfig(configDir, kafkaURI, database, host string, startPort int) (*config.Dendrite, int, error) {
|
|
||||||
var cfg config.Dendrite
|
|
||||||
cfg.Defaults(true)
|
|
||||||
|
|
||||||
port := startPort
|
|
||||||
assignAddress := func() config.HTTPAddress {
|
|
||||||
result := config.HTTPAddress(fmt.Sprintf("http://%s:%d", host, port))
|
|
||||||
port++
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
serverKeyPath := filepath.Join(configDir, ServerKeyFile)
|
|
||||||
tlsCertPath := filepath.Join(configDir, TLSKeyFile)
|
|
||||||
tlsKeyPath := filepath.Join(configDir, TLSCertFile)
|
|
||||||
mediaBasePath := filepath.Join(configDir, MediaDir)
|
|
||||||
|
|
||||||
if err := NewMatrixKey(serverKeyPath); err != nil {
|
|
||||||
return nil, 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := NewTLSKey(tlsKeyPath, tlsCertPath); err != nil {
|
|
||||||
return nil, 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
cfg.Version = config.Version
|
|
||||||
|
|
||||||
cfg.Global.ServerName = gomatrixserverlib.ServerName(assignAddress())
|
|
||||||
cfg.Global.PrivateKeyPath = config.Path(serverKeyPath)
|
|
||||||
|
|
||||||
cfg.MediaAPI.BasePath = config.Path(mediaBasePath)
|
|
||||||
|
|
||||||
cfg.Global.JetStream.Addresses = []string{kafkaURI}
|
|
||||||
|
|
||||||
// TODO: Use different databases for the different schemas.
|
|
||||||
// Using the same database for every schema currently works because
|
|
||||||
// the table names are globally unique. But we might not want to
|
|
||||||
// rely on that in the future.
|
|
||||||
cfg.AppServiceAPI.Database.ConnectionString = config.DataSource(database)
|
|
||||||
cfg.FederationAPI.Database.ConnectionString = config.DataSource(database)
|
|
||||||
cfg.KeyServer.Database.ConnectionString = config.DataSource(database)
|
|
||||||
cfg.MediaAPI.Database.ConnectionString = config.DataSource(database)
|
|
||||||
cfg.RoomServer.Database.ConnectionString = config.DataSource(database)
|
|
||||||
cfg.SyncAPI.Database.ConnectionString = config.DataSource(database)
|
|
||||||
cfg.UserAPI.AccountDatabase.ConnectionString = config.DataSource(database)
|
|
||||||
|
|
||||||
cfg.AppServiceAPI.InternalAPI.Listen = assignAddress()
|
|
||||||
cfg.FederationAPI.InternalAPI.Listen = assignAddress()
|
|
||||||
cfg.KeyServer.InternalAPI.Listen = assignAddress()
|
|
||||||
cfg.MediaAPI.InternalAPI.Listen = assignAddress()
|
|
||||||
cfg.RoomServer.InternalAPI.Listen = assignAddress()
|
|
||||||
cfg.SyncAPI.InternalAPI.Listen = assignAddress()
|
|
||||||
cfg.UserAPI.InternalAPI.Listen = assignAddress()
|
|
||||||
|
|
||||||
cfg.AppServiceAPI.InternalAPI.Connect = cfg.AppServiceAPI.InternalAPI.Listen
|
|
||||||
cfg.FederationAPI.InternalAPI.Connect = cfg.FederationAPI.InternalAPI.Listen
|
|
||||||
cfg.KeyServer.InternalAPI.Connect = cfg.KeyServer.InternalAPI.Listen
|
|
||||||
cfg.MediaAPI.InternalAPI.Connect = cfg.MediaAPI.InternalAPI.Listen
|
|
||||||
cfg.RoomServer.InternalAPI.Connect = cfg.RoomServer.InternalAPI.Listen
|
|
||||||
cfg.SyncAPI.InternalAPI.Connect = cfg.SyncAPI.InternalAPI.Listen
|
|
||||||
cfg.UserAPI.InternalAPI.Connect = cfg.UserAPI.InternalAPI.Listen
|
|
||||||
|
|
||||||
return &cfg, port, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteConfig writes the config file to the directory.
|
|
||||||
func WriteConfig(cfg *config.Dendrite, configDir string) error {
|
|
||||||
data, err := yaml.Marshal(cfg)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return ioutil.WriteFile(filepath.Join(configDir, ConfigFile), data, 0666)
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMatrixKey generates a new ed25519 matrix server key and writes it to a file.
|
// NewMatrixKey generates a new ed25519 matrix server key and writes it to a file.
|
||||||
func NewMatrixKey(matrixKeyPath string) (err error) {
|
func NewMatrixKey(matrixKeyPath string) (err error) {
|
||||||
var data [35]byte
|
var data [35]byte
|
||||||
|
|
@ -12,7 +12,7 @@
|
||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
package test
|
package testrig
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
@ -24,22 +24,23 @@ import (
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/setup/base"
|
"github.com/matrix-org/dendrite/setup/base"
|
||||||
"github.com/matrix-org/dendrite/setup/config"
|
"github.com/matrix-org/dendrite/setup/config"
|
||||||
|
"github.com/matrix-org/dendrite/test"
|
||||||
"github.com/nats-io/nats.go"
|
"github.com/nats-io/nats.go"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateBaseDendrite(t *testing.T, dbType DBType) (*base.BaseDendrite, func()) {
|
func CreateBaseDendrite(t *testing.T, dbType test.DBType) (*base.BaseDendrite, func()) {
|
||||||
var cfg config.Dendrite
|
var cfg config.Dendrite
|
||||||
cfg.Defaults(false)
|
cfg.Defaults(false)
|
||||||
cfg.Global.JetStream.InMemory = true
|
cfg.Global.JetStream.InMemory = true
|
||||||
|
|
||||||
switch dbType {
|
switch dbType {
|
||||||
case DBTypePostgres:
|
case test.DBTypePostgres:
|
||||||
cfg.Global.Defaults(true) // autogen a signing key
|
cfg.Global.Defaults(true) // autogen a signing key
|
||||||
cfg.MediaAPI.Defaults(true) // autogen a media path
|
cfg.MediaAPI.Defaults(true) // autogen a media path
|
||||||
// use a distinct prefix else concurrent postgres/sqlite runs will clash since NATS will use
|
// use a distinct prefix else concurrent postgres/sqlite runs will clash since NATS will use
|
||||||
// the file system event with InMemory=true :(
|
// the file system event with InMemory=true :(
|
||||||
cfg.Global.JetStream.TopicPrefix = fmt.Sprintf("Test_%d_", dbType)
|
cfg.Global.JetStream.TopicPrefix = fmt.Sprintf("Test_%d_", dbType)
|
||||||
connStr, close := PrepareDBConnectionString(t, dbType)
|
connStr, close := test.PrepareDBConnectionString(t, dbType)
|
||||||
cfg.Global.DatabaseOptions = config.DatabaseOptions{
|
cfg.Global.DatabaseOptions = config.DatabaseOptions{
|
||||||
ConnectionString: config.DataSource(connStr),
|
ConnectionString: config.DataSource(connStr),
|
||||||
MaxOpenConnections: 10,
|
MaxOpenConnections: 10,
|
||||||
|
|
@ -47,7 +48,7 @@ func CreateBaseDendrite(t *testing.T, dbType DBType) (*base.BaseDendrite, func()
|
||||||
ConnMaxLifetimeSeconds: 60,
|
ConnMaxLifetimeSeconds: 60,
|
||||||
}
|
}
|
||||||
return base.NewBaseDendrite(&cfg, "Test", base.DisableMetrics), close
|
return base.NewBaseDendrite(&cfg, "Test", base.DisableMetrics), close
|
||||||
case DBTypeSQLite:
|
case test.DBTypeSQLite:
|
||||||
cfg.Defaults(true) // sets a sqlite db per component
|
cfg.Defaults(true) // sets a sqlite db per component
|
||||||
// use a distinct prefix else concurrent postgres/sqlite runs will clash since NATS will use
|
// use a distinct prefix else concurrent postgres/sqlite runs will clash since NATS will use
|
||||||
// the file system event with InMemory=true :(
|
// the file system event with InMemory=true :(
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package test
|
package testrig
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
@ -24,7 +24,6 @@ import (
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/matrix-org/dendrite/internal/httputil"
|
"github.com/matrix-org/dendrite/internal/httputil"
|
||||||
internalTest "github.com/matrix-org/dendrite/internal/test"
|
|
||||||
"github.com/matrix-org/dendrite/test"
|
"github.com/matrix-org/dendrite/test"
|
||||||
"github.com/matrix-org/dendrite/userapi"
|
"github.com/matrix-org/dendrite/userapi"
|
||||||
"github.com/matrix-org/dendrite/userapi/inthttp"
|
"github.com/matrix-org/dendrite/userapi/inthttp"
|
||||||
|
|
@ -135,7 +134,7 @@ func TestQueryProfile(t *testing.T) {
|
||||||
t.Run("HTTP API", func(t *testing.T) {
|
t.Run("HTTP API", func(t *testing.T) {
|
||||||
router := mux.NewRouter().PathPrefix(httputil.InternalPathPrefix).Subrouter()
|
router := mux.NewRouter().PathPrefix(httputil.InternalPathPrefix).Subrouter()
|
||||||
userapi.AddInternalRoutes(router, userAPI)
|
userapi.AddInternalRoutes(router, userAPI)
|
||||||
apiURL, cancel := internalTest.ListenAndServe(t, router, false)
|
apiURL, cancel := test.ListenAndServe(t, router, false)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
httpAPI, err := inthttp.NewUserAPIClient(apiURL, &http.Client{})
|
httpAPI, err := inthttp.NewUserAPIClient(apiURL, &http.Client{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue