basic integration test
This commit is contained in:
@@ -3,6 +3,7 @@ package client
|
|||||||
import (
|
import (
|
||||||
"container/ring"
|
"container/ring"
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -254,6 +255,20 @@ func (c *RTCClient) Run() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *RTCClient) WaitUntilConnected() error {
|
||||||
|
ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return errors.New("could not connect after timeout")
|
||||||
|
case <-time.After(10 * time.Millisecond):
|
||||||
|
if c.iceConnected {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (c *RTCClient) ReadResponse() (*livekit.SignalResponse, error) {
|
func (c *RTCClient) ReadResponse() (*livekit.SignalResponse, error) {
|
||||||
for {
|
for {
|
||||||
// handle special messages and pass on the rest
|
// handle special messages and pass on the rest
|
||||||
|
|||||||
@@ -314,8 +314,6 @@ github.com/pion/interceptor v0.0.9 h1:fk5hTdyLO3KURQsf/+RjMpEm4NE3yeTY9Kh97b5Bvw
|
|||||||
github.com/pion/interceptor v0.0.9/go.mod h1:dHgEP5dtxOTf21MObuBAjJeAayPxLUAZjerGH8Xr07c=
|
github.com/pion/interceptor v0.0.9/go.mod h1:dHgEP5dtxOTf21MObuBAjJeAayPxLUAZjerGH8Xr07c=
|
||||||
github.com/pion/ion-log v1.0.0 h1:2lJLImCmfCWCR38hLWsjQfBWe6NFz/htbqiYHwvOP/Q=
|
github.com/pion/ion-log v1.0.0 h1:2lJLImCmfCWCR38hLWsjQfBWe6NFz/htbqiYHwvOP/Q=
|
||||||
github.com/pion/ion-log v1.0.0/go.mod h1:jwcla9KoB9bB/4FxYDSRJPcPYSLp5XiUUMnOLaqwl4E=
|
github.com/pion/ion-log v1.0.0/go.mod h1:jwcla9KoB9bB/4FxYDSRJPcPYSLp5XiUUMnOLaqwl4E=
|
||||||
github.com/pion/ion-sfu v1.7.2 h1:jW59IxIQtcGotK/BYlTqOF1Vsp/UqM4BoRD6MMnYw1Y=
|
|
||||||
github.com/pion/ion-sfu v1.7.2/go.mod h1:61HfTCWVx6rTpYCc7kmvnTToEpyBoMwDhOyvMiUtNhk=
|
|
||||||
github.com/pion/ion-sfu v1.7.7 h1:qbBOsUJrU8ZlFUC8Gmz8P3FBfZ5rzr6X/FErt87dioA=
|
github.com/pion/ion-sfu v1.7.7 h1:qbBOsUJrU8ZlFUC8Gmz8P3FBfZ5rzr6X/FErt87dioA=
|
||||||
github.com/pion/ion-sfu v1.7.7/go.mod h1:D6Qnd7GHbYiRI1ye5a1IHst6cbbATawCb8fDs0oLxEI=
|
github.com/pion/ion-sfu v1.7.7/go.mod h1:D6Qnd7GHbYiRI1ye5a1IHst6cbbATawCb8fDs0oLxEI=
|
||||||
github.com/pion/logging v0.2.2 h1:M9+AIj/+pxNsDfAT64+MAVgJO0rsyLnoJKCqf//DoeY=
|
github.com/pion/logging v0.2.2 h1:M9+AIj/+pxNsDfAT64+MAVgJO0rsyLnoJKCqf//DoeY=
|
||||||
|
|||||||
+11
-8
@@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/twitchtv/twirp"
|
"github.com/twitchtv/twirp"
|
||||||
|
|
||||||
"github.com/livekit/livekit-server/pkg/auth"
|
"github.com/livekit/livekit-server/pkg/auth"
|
||||||
|
"github.com/livekit/livekit-server/pkg/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -40,8 +41,7 @@ func (m *APIKeyAuthMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request,
|
|||||||
|
|
||||||
if authHeader != "" {
|
if authHeader != "" {
|
||||||
if !strings.HasPrefix(authHeader, bearerPrefix) {
|
if !strings.HasPrefix(authHeader, bearerPrefix) {
|
||||||
w.WriteHeader(http.StatusUnauthorized)
|
handleError(w, http.StatusUnauthorized, "invalid authorization header. Must start with "+bearerPrefix)
|
||||||
w.Write([]byte("invalid authorization header. Must start with " + bearerPrefix))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,22 +54,19 @@ func (m *APIKeyAuthMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request,
|
|||||||
if authToken != "" {
|
if authToken != "" {
|
||||||
v, err := auth.ParseAPIToken(authToken)
|
v, err := auth.ParseAPIToken(authToken)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusUnauthorized)
|
handleError(w, http.StatusUnauthorized, "invalid authorization token")
|
||||||
w.Write([]byte("invalid authorization token"))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
secret := m.provider.GetSecret(v.APIKey())
|
secret := m.provider.GetSecret(v.APIKey())
|
||||||
if secret == "" {
|
if secret == "" {
|
||||||
w.WriteHeader(http.StatusUnauthorized)
|
handleError(w, http.StatusUnauthorized, "invalid API key")
|
||||||
w.Write([]byte("invalid API key"))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
grants, err := v.Verify(secret)
|
grants, err := v.Verify(secret)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusUnauthorized)
|
handleError(w, http.StatusUnauthorized, "invalid token: "+err.Error())
|
||||||
w.Write([]byte("invalid token: " + err.Error()))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,3 +127,9 @@ func EnsureCreatePermission(ctx context.Context) error {
|
|||||||
func twirpAuthError(err error) error {
|
func twirpAuthError(err error) error {
|
||||||
return twirp.NewError(twirp.Unauthenticated, err.Error())
|
return twirp.NewError(twirp.Unauthenticated, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handleError(w http.ResponseWriter, status int, msg string) {
|
||||||
|
logger.GetLogger().Debugw("error handling request", "error", msg, "status", status)
|
||||||
|
w.WriteHeader(status)
|
||||||
|
w.Write([]byte(msg))
|
||||||
|
}
|
||||||
|
|||||||
+7
-7
@@ -46,7 +46,7 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
claims := GetGrants(r.Context())
|
claims := GetGrants(r.Context())
|
||||||
// require a claim
|
// require a claim
|
||||||
if claims == nil || claims.Video == nil {
|
if claims == nil || claims.Video == nil {
|
||||||
writeJSONError(w, http.StatusUnauthorized, rtc.ErrPermissionDenied.Error())
|
handleError(w, http.StatusUnauthorized, rtc.ErrPermissionDenied.Error())
|
||||||
}
|
}
|
||||||
pName = claims.Identity
|
pName = claims.Identity
|
||||||
}
|
}
|
||||||
@@ -54,14 +54,14 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
onlyName, err := EnsureJoinPermission(r.Context())
|
onlyName, err := EnsureJoinPermission(r.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeJSONError(w, http.StatusUnauthorized, err.Error())
|
handleError(w, http.StatusUnauthorized, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
room, err := s.manager.GetRoomWithConstraint(roomId, onlyName)
|
room, err := s.manager.GetRoomWithConstraint(roomId, onlyName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO: return errors/status correctly
|
// TODO: return errors/status correctly
|
||||||
writeJSONError(w, http.StatusNotFound, err.Error())
|
handleError(w, http.StatusNotFound, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,7 +71,7 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
logger.GetLogger().Warnw("could not upgrade to WS",
|
logger.GetLogger().Warnw("could not upgrade to WS",
|
||||||
"err", err,
|
"err", err,
|
||||||
)
|
)
|
||||||
writeJSONError(w, http.StatusInternalServerError, err.Error())
|
handleError(w, http.StatusInternalServerError, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
conn.SetCloseHandler(func(code int, text string) error {
|
conn.SetCloseHandler(func(code int, text string) error {
|
||||||
@@ -82,12 +82,12 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
pc, err := rtc.NewPeerConnection(s.manager.Config())
|
pc, err := rtc.NewPeerConnection(s.manager.Config())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeJSONError(w, http.StatusInternalServerError, "could not create peerConnection", err.Error())
|
handleError(w, http.StatusInternalServerError, "could not create peerConnection: "+err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
participant, err := rtc.NewParticipant(pc, signalConn, pName, s.manager.Config().Receiver)
|
participant, err := rtc.NewParticipant(pc, signalConn, pName, s.manager.Config().Receiver)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeJSONError(w, http.StatusInternalServerError, "could not create participant", err.Error())
|
handleError(w, http.StatusInternalServerError, "could not create participant: "+err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,7 +98,7 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
)
|
)
|
||||||
|
|
||||||
if err := room.Join(participant); err != nil {
|
if err := room.Join(participant); err != nil {
|
||||||
writeJSONError(w, http.StatusInternalServerError, "could not join room", err.Error())
|
handleError(w, http.StatusInternalServerError, "could not join room: "+err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,22 +3,32 @@ package test
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/livekit/livekit-server/cmd/cli/client"
|
"github.com/livekit/livekit-server/cmd/cli/client"
|
||||||
"github.com/livekit/livekit-server/pkg/auth"
|
"github.com/livekit/livekit-server/pkg/auth"
|
||||||
"github.com/livekit/livekit-server/pkg/config"
|
"github.com/livekit/livekit-server/pkg/config"
|
||||||
|
"github.com/livekit/livekit-server/pkg/logger"
|
||||||
"github.com/livekit/livekit-server/pkg/service"
|
"github.com/livekit/livekit-server/pkg/service"
|
||||||
|
"github.com/livekit/livekit-server/proto/livekit"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
testApiKey = "apikey"
|
testApiKey = "apikey"
|
||||||
testApiSecret = "apiSecret"
|
testApiSecret = "apiSecret"
|
||||||
|
testRoom = "mytestroom"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
serverConfig *config.Config
|
||||||
|
roomClient livekit.RoomService
|
||||||
)
|
)
|
||||||
|
|
||||||
func waitForServerToStart(s *service.LivekitServer) {
|
func waitForServerToStart(s *service.LivekitServer) {
|
||||||
// wait till ready
|
// wait till ready
|
||||||
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
|
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
@@ -31,20 +41,51 @@ func waitForServerToStart(s *service.LivekitServer) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func withTimeout(t *testing.T, f func() bool) {
|
||||||
|
ctx, _ := context.WithTimeout(context.Background(), time.Second)
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
t.Fatal("timed out")
|
||||||
|
case <-time.After(10 * time.Millisecond):
|
||||||
|
if f() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func createServer() *service.LivekitServer {
|
func createServer() *service.LivekitServer {
|
||||||
conf, err := config.NewConfig("")
|
var err error
|
||||||
|
serverConfig, err = config.NewConfig("")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Sprintf("could not create config: %v", err))
|
panic(fmt.Sprintf("could not create config: %v", err))
|
||||||
}
|
}
|
||||||
s, err := service.InitializeServer(conf, &StaticKeyProvider{})
|
s, err := service.InitializeServer(serverConfig, &StaticKeyProvider{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Sprintf("could not create server: %v", err))
|
panic(fmt.Sprintf("could not create server: %v", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
roomClient = livekit.NewRoomServiceJSONClient(fmt.Sprintf("http://localhost:%d", serverConfig.APIPort), &http.Client{})
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func createClient(room, name string) *client.RTCClient {
|
// creates a client and runs against server
|
||||||
return nil
|
func createClient(name string) *client.RTCClient {
|
||||||
|
token := joinToken(testRoom, name)
|
||||||
|
ws, err := client.NewWebSocketConn(fmt.Sprintf("ws://localhost:%d", serverConfig.RTCPort), token)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
c, err := client.NewRTCClient(ws)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
go c.Run()
|
||||||
|
|
||||||
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
func joinToken(room, name string) string {
|
func joinToken(room, name string) string {
|
||||||
@@ -78,6 +119,7 @@ func (p *StaticKeyProvider) NumKeys() int {
|
|||||||
|
|
||||||
func (p *StaticKeyProvider) GetSecret(key string) string {
|
func (p *StaticKeyProvider) GetSecret(key string) string {
|
||||||
if key == testApiKey {
|
if key == testApiKey {
|
||||||
|
logger.GetLogger().Debugf("returning secret: %s", testApiSecret)
|
||||||
return testApiSecret
|
return testApiSecret
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
|
|||||||
@@ -1,26 +1,58 @@
|
|||||||
package test
|
package test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/twitchtv/twirp"
|
||||||
|
|
||||||
|
"github.com/livekit/livekit-server/pkg/logger"
|
||||||
|
"github.com/livekit/livekit-server/pkg/service"
|
||||||
|
"github.com/livekit/livekit-server/proto/livekit"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestScenarioDefault(t *testing.T) {
|
func TestClientCouldConnect(t *testing.T) {
|
||||||
assert.True(t, true)
|
c1 := createClient("c1")
|
||||||
|
assert.NoError(t, c1.WaitUntilConnected())
|
||||||
|
c2 := createClient("c2")
|
||||||
|
assert.NoError(t, c2.WaitUntilConnected())
|
||||||
|
|
||||||
|
// ensure they both see each other
|
||||||
|
withTimeout(t, func() bool {
|
||||||
|
return len(c1.RemoteParticipants()) == 1 && len(c2.RemoteParticipants()) == 1
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMain(m *testing.M) {
|
func TestMain(m *testing.M) {
|
||||||
|
logger.InitDevelopment()
|
||||||
s := createServer()
|
s := createServer()
|
||||||
|
service.AuthRequired = true
|
||||||
go func() {
|
go func() {
|
||||||
s.Start()
|
s.Start()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
waitForServerToStart(s)
|
waitForServerToStart(s)
|
||||||
|
|
||||||
|
// create test room
|
||||||
|
token := createRoomToken()
|
||||||
|
header := make(http.Header)
|
||||||
|
logger.GetLogger().Debugw("auth token", "token", token)
|
||||||
|
header.Set("Authorization", "Bearer "+token)
|
||||||
|
tctx, err := twirp.WithHTTPRequestHeaders(context.Background(), header)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
_, err = roomClient.CreateRoom(tctx, &livekit.CreateRoomRequest{Name: testRoom})
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
code := m.Run()
|
code := m.Run()
|
||||||
|
|
||||||
|
roomClient.DeleteRoom(tctx, &livekit.DeleteRoomRequest{Room: testRoom})
|
||||||
s.Stop()
|
s.Stop()
|
||||||
os.Exit(code)
|
os.Exit(code)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user