mediamtx/internal/core/core_test.go

134 lines
2.4 KiB
Go
Raw Permalink Normal View History

package core
2020-05-10 19:32:40 +00:00
import (
"os"
"path/filepath"
2020-05-10 19:32:40 +00:00
"testing"
"time"
2023-08-26 16:54:28 +00:00
"github.com/bluenviron/gortsplib/v4"
"github.com/bluenviron/gortsplib/v4/pkg/description"
"github.com/bluenviron/mediamtx/internal/test"
2020-05-10 19:32:40 +00:00
"github.com/stretchr/testify/require"
)
func newInstance(conf string) (*Core, bool) {
if conf == "" {
return New([]string{})
}
tmpf, err := test.CreateTempFile([]byte(conf))
if err != nil {
return nil, false
}
2020-12-13 22:43:31 +00:00
defer os.Remove(tmpf)
return New([]string{tmpf})
}
func TestCoreErrors(t *testing.T) {
for _, ca := range []struct {
name string
conf string
}{
{
"logger",
"logDestinations: [file]\n" +
"logFile: /nonexisting/nonexist\n",
},
{
"metrics",
"metrics: yes\n" +
"metricsAddress: invalid\n",
},
{
"pprof",
"pprof: yes\n" +
"pprofAddress: invalid\n",
},
{
"playback",
"playback: yes\n" +
"playbackAddress: invalid\n",
},
{
"rtsp",
"rtspAddress: invalid\n",
},
{
"rtsps",
"rtspEncryption: strict\n" +
"rtspAddress: invalid\n",
},
{
"rtmp",
"rtmpAddress: invalid\n",
},
{
"rtmps",
"rtmpEncryption: strict\n" +
"rtmpAddress: invalid\n",
},
{
"hls",
"hlsAddress: invalid\n",
},
{
"webrtc",
"webrtcAddress: invalid\n",
},
{
"srt",
"srtAddress: invalid\n",
},
{
"api",
"api: yes\n" +
"apiAddress: invalid\n",
},
} {
t.Run(ca.name, func(t *testing.T) {
_, ok := newInstance(ca.conf)
require.Equal(t, false, ok)
})
}
}
func TestCoreHotReloading(t *testing.T) {
confPath := filepath.Join(os.TempDir(), "rtsp-conf")
2022-09-17 19:19:45 +00:00
err := os.WriteFile(confPath, []byte("paths:\n"+
" test1:\n"+
2021-11-03 20:42:29 +00:00
" publishUser: myuser\n"+
" publishPass: mypass\n"),
2021-05-23 16:43:49 +00:00
0o644)
require.NoError(t, err)
defer os.Remove(confPath)
p, ok := New([]string{confPath})
require.Equal(t, true, ok)
defer p.Close()
func() {
2021-11-12 21:29:56 +00:00
c := gortsplib.Client{}
2023-08-26 16:54:28 +00:00
err = c.StartRecording("rtsp://localhost:8554/test1",
&description.Session{Medias: []*description.Media{test.UniqueMediaH264()}})
require.EqualError(t, err, "bad status code: 401 (Unauthorized)")
}()
2022-09-17 19:19:45 +00:00
err = os.WriteFile(confPath, []byte("paths:\n"+
2021-11-03 20:42:29 +00:00
" test1:\n"),
2021-05-23 16:43:49 +00:00
0o644)
require.NoError(t, err)
time.Sleep(1 * time.Second)
func() {
2021-11-12 21:29:56 +00:00
conn := gortsplib.Client{}
2023-08-26 16:54:28 +00:00
err = conn.StartRecording("rtsp://localhost:8554/test1",
&description.Session{Medias: []*description.Media{test.UniqueMediaH264()}})
require.NoError(t, err)
2021-07-24 14:20:04 +00:00
defer conn.Close()
}()
}