mediamtx/internal/core/core_test.go

152 lines
2.6 KiB
Go
Raw 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"
2020-05-10 19:32:40 +00:00
"github.com/stretchr/testify/require"
)
2020-12-13 22:43:31 +00:00
func writeTempFile(byts []byte) (string, error) {
2022-09-17 19:19:45 +00:00
tmpf, err := os.CreateTemp(os.TempDir(), "rtsp-")
2020-12-13 22:43:31 +00:00
if err != nil {
return "", err
}
defer tmpf.Close()
_, err = tmpf.Write(byts)
if err != nil {
return "", err
}
return tmpf.Name(), nil
}
func newInstance(conf string) (*Core, bool) {
if conf == "" {
return New([]string{})
}
2020-12-13 22:43:31 +00:00
tmpf, err := writeTempFile([]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",
"encryption: 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() {
medi := testMediaH264
2021-11-03 20:42:29 +00:00
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{medi}})
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() {
medi := testMediaH264
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{medi}})
require.NoError(t, err)
2021-07-24 14:20:04 +00:00
defer conn.Close()
}()
}