-
Notifications
You must be signed in to change notification settings - Fork 52
/
deployment.go
84 lines (75 loc) · 2.89 KB
/
deployment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package docker
import (
"testing"
"time"
"github.com/matrix-org/complement/internal/client"
)
// Deployment is the complete instantiation of a Blueprint, with running containers
// for each homeserver in the Blueprint.
type Deployment struct {
// The Deployer which was responsible for this deployment
Deployer *Deployer
// The name of the deployed blueprint
BlueprintName string
// A map of HS name to a HomeserverDeployment
HS map[string]HomeserverDeployment
}
// HomeserverDeployment represents a running homeserver in a container.
type HomeserverDeployment struct {
BaseURL string // e.g http://localhost:38646
FedBaseURL string // e.g https://localhost:48373
ContainerID string // e.g 10de45efba
AccessTokens map[string]string // e.g { "@alice:hs1": "myAcc3ssT0ken" }
ApplicationServices map[string]string // e.g { "my-as-id": "id: xxx\nas_token: xxx ..."} }
}
// Destroy the entire deployment. Destroys all running containers. If `printServerLogs` is true,
// will print container logs before killing the container.
func (d *Deployment) Destroy(t *testing.T) {
t.Helper()
d.Deployer.Destroy(d, d.Deployer.config.AlwaysPrintServerLogs || t.Failed())
}
// Client returns a CSAPI client targeting the given hsName, using the access token for the given userID.
// Fails the test if the hsName is not found. Returns an unauthenticated client if userID is "", fails the test
// if the userID is otherwise not found.
func (d *Deployment) Client(t *testing.T, hsName, userID string) *client.CSAPI {
t.Helper()
dep, ok := d.HS[hsName]
if !ok {
t.Fatalf("Deployment.Client - HS name '%s' not found", hsName)
return nil
}
token := dep.AccessTokens[userID]
if token == "" && userID != "" {
t.Fatalf("Deployment.Client - HS name '%s' - user ID '%s' not found", hsName, userID)
return nil
}
return &client.CSAPI{
UserID: userID,
AccessToken: token,
BaseURL: dep.BaseURL,
Client: client.NewLoggedClient(t, hsName, nil),
SyncUntilTimeout: 5 * time.Second,
Debug: d.Deployer.debugLogging,
}
}
// RegisterUser within a homeserver and return an authenticatedClient, Fails the test if the hsName is not found.
func (d *Deployment) RegisterUser(t *testing.T, hsName, localpart, password string) *client.CSAPI {
t.Helper()
dep, ok := d.HS[hsName]
if !ok {
t.Fatalf("Deployment.Client - HS name '%s' not found", hsName)
return nil
}
client := &client.CSAPI{
BaseURL: dep.BaseURL,
Client: client.NewLoggedClient(t, hsName, nil),
SyncUntilTimeout: 5 * time.Second,
Debug: d.Deployer.debugLogging,
}
userID, accessToken := client.RegisterUser(t, localpart, password)
// remember the token so subsequent calls to deployment.Client return the user
dep.AccessTokens[userID] = accessToken
client.UserID = userID
client.AccessToken = accessToken
return client
}