mirror of
https://github.com/datarhei/core.git
synced 2025-10-05 16:07:07 +08:00
Fix tests such that ffmpeg binary is not required
This commit is contained in:
8
Dockerfile.test
Normal file
8
Dockerfile.test
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
FROM golang:1.18.3-alpine3.15
|
||||||
|
|
||||||
|
RUN apk add alpine-sdk
|
||||||
|
|
||||||
|
COPY . /dist/core
|
||||||
|
|
||||||
|
RUN cd /dist/core && \
|
||||||
|
go test -coverprofile=coverage.out -covermode=atomic -v ./...
|
@@ -6,23 +6,29 @@ import (
|
|||||||
|
|
||||||
"github.com/datarhei/core/v16/http/api"
|
"github.com/datarhei/core/v16/http/api"
|
||||||
"github.com/datarhei/core/v16/http/mock"
|
"github.com/datarhei/core/v16/http/mock"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getDummyAboutRouter() *echo.Echo {
|
func getDummyAboutRouter() (*echo.Echo, error) {
|
||||||
router := mock.DummyEcho()
|
router := mock.DummyEcho()
|
||||||
|
|
||||||
rs := mock.DummyRestreamer()
|
rs, err := mock.DummyRestreamer("../../mock")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
handler := NewAbout(rs, []string{})
|
handler := NewAbout(rs, []string{})
|
||||||
|
|
||||||
router.Add("GET", "/", handler.About)
|
router.Add("GET", "/", handler.About)
|
||||||
|
|
||||||
return router
|
return router, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAbout(t *testing.T) {
|
func TestAbout(t *testing.T) {
|
||||||
router := getDummyAboutRouter()
|
router, err := getDummyAboutRouter()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
response := mock.Request(t, http.StatusOK, router, "GET", "/", nil)
|
response := mock.Request(t, http.StatusOK, router, "GET", "/", nil)
|
||||||
|
|
||||||
|
@@ -49,6 +49,7 @@ func TestConfigSet(t *testing.T) {
|
|||||||
var data bytes.Buffer
|
var data bytes.Buffer
|
||||||
|
|
||||||
cfg := config.New()
|
cfg := config.New()
|
||||||
|
cfg.FFmpeg.Binary = "true"
|
||||||
cfg.DB.Dir = "."
|
cfg.DB.Dir = "."
|
||||||
cfg.Storage.Disk.Dir = "."
|
cfg.Storage.Disk.Dir = "."
|
||||||
cfg.Storage.MimeTypes = ""
|
cfg.Storage.MimeTypes = ""
|
||||||
|
@@ -6,6 +6,7 @@ import (
|
|||||||
|
|
||||||
"github.com/datarhei/core/v16/http/api"
|
"github.com/datarhei/core/v16/http/api"
|
||||||
"github.com/datarhei/core/v16/http/mock"
|
"github.com/datarhei/core/v16/http/mock"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
)
|
)
|
||||||
@@ -16,18 +17,24 @@ type Response struct {
|
|||||||
Data interface{}
|
Data interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDummyRestreamHandler() *RestreamHandler {
|
func getDummyRestreamHandler() (*RestreamHandler, error) {
|
||||||
rs := mock.DummyRestreamer()
|
rs, err := mock.DummyRestreamer("../../mock")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
handler := NewRestream(rs)
|
handler := NewRestream(rs)
|
||||||
|
|
||||||
return handler
|
return handler, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDummyRestreamRouter() *echo.Echo {
|
func getDummyRestreamRouter() (*echo.Echo, error) {
|
||||||
router := mock.DummyEcho()
|
router := mock.DummyEcho()
|
||||||
|
|
||||||
restream := getDummyRestreamHandler()
|
restream, err := getDummyRestreamHandler()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
router.GET("/", restream.GetAll)
|
router.GET("/", restream.GetAll)
|
||||||
router.POST("/", restream.Add)
|
router.POST("/", restream.Add)
|
||||||
@@ -37,11 +44,12 @@ func getDummyRestreamRouter() *echo.Echo {
|
|||||||
router.DELETE("/:id", restream.Delete)
|
router.DELETE("/:id", restream.Delete)
|
||||||
router.PUT("/:id/command", restream.Command)
|
router.PUT("/:id/command", restream.Command)
|
||||||
|
|
||||||
return router
|
return router, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAddProcessMissingField(t *testing.T) {
|
func TestAddProcessMissingField(t *testing.T) {
|
||||||
router := getDummyRestreamRouter()
|
router, err := getDummyRestreamRouter()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
data := mock.Read(t, "./fixtures/addProcessMissingField.json")
|
data := mock.Read(t, "./fixtures/addProcessMissingField.json")
|
||||||
|
|
||||||
@@ -49,7 +57,8 @@ func TestAddProcessMissingField(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestAddProcessInvalidType(t *testing.T) {
|
func TestAddProcessInvalidType(t *testing.T) {
|
||||||
router := getDummyRestreamRouter()
|
router, err := getDummyRestreamRouter()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
data := mock.Read(t, "./fixtures/addProcessInvalidType.json")
|
data := mock.Read(t, "./fixtures/addProcessInvalidType.json")
|
||||||
|
|
||||||
@@ -57,7 +66,8 @@ func TestAddProcessInvalidType(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestAddProcess(t *testing.T) {
|
func TestAddProcess(t *testing.T) {
|
||||||
router := getDummyRestreamRouter()
|
router, err := getDummyRestreamRouter()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
data := mock.Read(t, "./fixtures/addProcess.json")
|
data := mock.Read(t, "./fixtures/addProcess.json")
|
||||||
|
|
||||||
@@ -67,13 +77,15 @@ func TestAddProcess(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRemoveUnknownProcess(t *testing.T) {
|
func TestRemoveUnknownProcess(t *testing.T) {
|
||||||
router := getDummyRestreamRouter()
|
router, err := getDummyRestreamRouter()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
mock.Request(t, http.StatusNotFound, router, "DELETE", "/foobar", nil)
|
mock.Request(t, http.StatusNotFound, router, "DELETE", "/foobar", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRemoveProcess(t *testing.T) {
|
func TestRemoveProcess(t *testing.T) {
|
||||||
router := getDummyRestreamRouter()
|
router, err := getDummyRestreamRouter()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
data := mock.Read(t, "./fixtures/removeProcess.json")
|
data := mock.Read(t, "./fixtures/removeProcess.json")
|
||||||
|
|
||||||
@@ -82,7 +94,8 @@ func TestRemoveProcess(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessInfo(t *testing.T) {
|
func TestProcessInfo(t *testing.T) {
|
||||||
router := getDummyRestreamRouter()
|
router, err := getDummyRestreamRouter()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
data := mock.Read(t, "./fixtures/addProcess.json")
|
data := mock.Read(t, "./fixtures/addProcess.json")
|
||||||
|
|
||||||
@@ -93,13 +106,15 @@ func TestProcessInfo(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessReportNotFound(t *testing.T) {
|
func TestProcessReportNotFound(t *testing.T) {
|
||||||
router := getDummyRestreamRouter()
|
router, err := getDummyRestreamRouter()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
mock.Request(t, http.StatusNotFound, router, "GET", "/test/report", nil)
|
mock.Request(t, http.StatusNotFound, router, "GET", "/test/report", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessReport(t *testing.T) {
|
func TestProcessReport(t *testing.T) {
|
||||||
router := getDummyRestreamRouter()
|
router, err := getDummyRestreamRouter()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
data := mock.Read(t, "./fixtures/addProcess.json")
|
data := mock.Read(t, "./fixtures/addProcess.json")
|
||||||
|
|
||||||
@@ -110,14 +125,16 @@ func TestProcessReport(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessCommandNotFound(t *testing.T) {
|
func TestProcessCommandNotFound(t *testing.T) {
|
||||||
router := getDummyRestreamRouter()
|
router, err := getDummyRestreamRouter()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
command := mock.Read(t, "./fixtures/commandStart.json")
|
command := mock.Read(t, "./fixtures/commandStart.json")
|
||||||
mock.Request(t, http.StatusBadRequest, router, "PUT", "/test/command", command)
|
mock.Request(t, http.StatusBadRequest, router, "PUT", "/test/command", command)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessCommandInvalid(t *testing.T) {
|
func TestProcessCommandInvalid(t *testing.T) {
|
||||||
router := getDummyRestreamRouter()
|
router, err := getDummyRestreamRouter()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
data := mock.Read(t, "./fixtures/addProcess.json")
|
data := mock.Read(t, "./fixtures/addProcess.json")
|
||||||
|
|
||||||
@@ -128,7 +145,8 @@ func TestProcessCommandInvalid(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessCommand(t *testing.T) {
|
func TestProcessCommand(t *testing.T) {
|
||||||
router := getDummyRestreamRouter()
|
router, err := getDummyRestreamRouter()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
data := mock.Read(t, "./fixtures/addProcess.json")
|
data := mock.Read(t, "./fixtures/addProcess.json")
|
||||||
|
|
||||||
|
@@ -3,10 +3,12 @@ package mock
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -14,6 +16,7 @@ import (
|
|||||||
"github.com/datarhei/core/v16/http/api"
|
"github.com/datarhei/core/v16/http/api"
|
||||||
"github.com/datarhei/core/v16/http/errorhandler"
|
"github.com/datarhei/core/v16/http/errorhandler"
|
||||||
"github.com/datarhei/core/v16/http/validator"
|
"github.com/datarhei/core/v16/http/validator"
|
||||||
|
"github.com/datarhei/core/v16/internal/testhelper"
|
||||||
"github.com/datarhei/core/v16/restream"
|
"github.com/datarhei/core/v16/restream"
|
||||||
"github.com/datarhei/core/v16/restream/store"
|
"github.com/datarhei/core/v16/restream/store"
|
||||||
|
|
||||||
@@ -23,19 +26,30 @@ import (
|
|||||||
"github.com/xeipuuv/gojsonschema"
|
"github.com/xeipuuv/gojsonschema"
|
||||||
)
|
)
|
||||||
|
|
||||||
func DummyRestreamer() restream.Restreamer {
|
func DummyRestreamer(pathPrefix string) (restream.Restreamer, error) {
|
||||||
|
binary, err := testhelper.BuildBinary("ffmpeg", filepath.Join(pathPrefix, "../../internal/testhelper"))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to build helper program: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
store := store.NewDummyStore(store.DummyConfig{})
|
store := store.NewDummyStore(store.DummyConfig{})
|
||||||
|
|
||||||
ffmpeg, _ := ffmpeg.New(ffmpeg.Config{
|
ffmpeg, err := ffmpeg.New(ffmpeg.Config{
|
||||||
Binary: "ffmpeg",
|
Binary: binary,
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
rs, _ := restream.New(restream.Config{
|
rs, err := restream.New(restream.Config{
|
||||||
Store: store,
|
Store: store,
|
||||||
FFmpeg: ffmpeg,
|
FFmpeg: ffmpeg,
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
return rs
|
return rs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func DummyEcho() *echo.Echo {
|
func DummyEcho() *echo.Echo {
|
||||||
|
3
internal/.gitignore
vendored
Normal file
3
internal/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
testhelper/ignoresigint/ignoresigint
|
||||||
|
testhelper/sigint/sigint
|
||||||
|
testhelper/ffmpeg/ffmpeg
|
103
internal/testhelper/ffmpeg/ffmpeg.go
Normal file
103
internal/testhelper/ffmpeg/ffmpeg.go
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
header := `ffmpeg version 4.0.2 Copyright (c) 2000-2018 the FFmpeg developers
|
||||||
|
built with Apple LLVM version 9.1.0 (clang-902.0.39.2)
|
||||||
|
configuration: --prefix=/usr/local/Cellar/ffmpeg/4.0.2 --enable-shared --enable-pthreads --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-gpl --enable-libmp3lame --enable-libx264 --enable-libx265 --enable-libxvid --enable-opencl --enable-videotoolbox --disable-lzma
|
||||||
|
libavutil 56. 14.100 / 56. 14.100
|
||||||
|
libavcodec 58. 18.100 / 58. 18.100
|
||||||
|
libavformat 58. 12.100 / 58. 12.100
|
||||||
|
libavdevice 58. 3.100 / 58. 3.100
|
||||||
|
libavfilter 7. 16.100 / 7. 16.100
|
||||||
|
libavresample 4. 0. 0 / 4. 0. 0
|
||||||
|
libswscale 5. 1.100 / 5. 1.100
|
||||||
|
libswresample 3. 1.100 / 3. 1.100
|
||||||
|
libpostproc 55. 1.100 / 55. 1.100`
|
||||||
|
|
||||||
|
prelude := `Input #0, lavfi, from 'testsrc=size=1280x720:rate=25':
|
||||||
|
Duration: N/A, start: 0.000000, bitrate: N/A
|
||||||
|
Stream #0:0: Video: rawvideo (RGB[24] / 0x18424752), rgb24, 1280x720 [SAR 1:1 DAR 16:9], 25 tbr, 25 tbn, 25 tbc
|
||||||
|
Input #1, lavfi, from 'anullsrc=r=44100:cl=stereo':
|
||||||
|
Duration: N/A, start: 0.000000, bitrate: 705 kb/s
|
||||||
|
Stream #1:0: Audio: pcm_u8, 44100 Hz, stereo, u8, 705 kb/s
|
||||||
|
Stream #1:1(eng): Audio: aac (LC), 48000 Hz, stereo, fltp (default)
|
||||||
|
Stream mapping:
|
||||||
|
Stream #0:0 -> #0:0 (rawvideo (native) -> h264 (libx264))
|
||||||
|
Stream #1:0 -> #0:1 (pcm_u8 (native) -> aac (native))
|
||||||
|
Press [q] to stop, [?] for help
|
||||||
|
[libx264 @ 0x7fa96a800600] using SAR=1/1
|
||||||
|
[libx264 @ 0x7fa96a800600] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
|
||||||
|
[libx264 @ 0x7fa96a800600] profile Constrained Baseline, level 3.1
|
||||||
|
[libx264 @ 0x7fa96a800600] 264 - core 152 r2854 e9a5903 - H.264/MPEG-4 AVC codec - Copyleft 2003-2017 - http://www.videolan.org/x264.html - options: cabac=0 ref=1 deblock=0:0:0 analyse=0:0 me=dia subme=0 psy=1 psy_rd=1.00:0.00 mixed_ref=0 me_range=16 chroma_me=1 trellis=0 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=0 threads=6 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=0 keyint=50 keyint_min=5 scenecut=0 intra_refresh=0 rc=crf mbtree=0 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=0
|
||||||
|
[hls @ 0x7fa969803a00] Opening './data/testsrc5375.ts.tmp' for writing
|
||||||
|
Output #0, hls, to './data/testsrc.m3u8':
|
||||||
|
Metadata:
|
||||||
|
encoder : Lavf58.12.100
|
||||||
|
Stream #0:0: Video: h264 (libx264), yuv420p(progressive), 1280x720 [SAR 1:1 DAR 16:9], q=-1--1, 25 fps, 90k tbn, 25 tbc
|
||||||
|
Metadata:
|
||||||
|
encoder : Lavc58.18.100 libx264
|
||||||
|
Side data:
|
||||||
|
cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
|
||||||
|
Stream #0:1: Audio: aac (LC), 44100 Hz, stereo, fltp, 64 kb/s
|
||||||
|
Metadata:
|
||||||
|
encoder : Lavc58.18.100 aac
|
||||||
|
[hls @ 0x7fa969803a00] Opening './data/testsrc5376.ts.tmp' for writing=0.872x
|
||||||
|
[hls @ 0x7fa969803a00] Opening './data/testsrc.m3u8.tmp' for writing
|
||||||
|
[hls @ 0x7fa969803a00] Opening './data/testsrc.m3u8.tmp' for writing`
|
||||||
|
|
||||||
|
fmt.Fprintf(os.Stderr, "%s\n", header)
|
||||||
|
|
||||||
|
if len(os.Args) <= 1 {
|
||||||
|
os.Exit(2)
|
||||||
|
}
|
||||||
|
|
||||||
|
lastArg := os.Args[len(os.Args)-1]
|
||||||
|
|
||||||
|
if lastArg == "-version" {
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(lastArg) > 1 && lastArg[0] == '-' {
|
||||||
|
os.Exit(2)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Fprintf(os.Stderr, "%s\n", prelude)
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
|
||||||
|
go func(ctx context.Context) {
|
||||||
|
ticker := time.NewTicker(time.Second)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
frame := uint64(0)
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case <-ticker.C:
|
||||||
|
frame += 25
|
||||||
|
fmt.Fprintf(os.Stderr, "frame=%5d fps= 25 q=-1.0 Lsize=N/A time=00:00:02.32 bitrate=N/A speed=1.0x \r", frame)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}(ctx)
|
||||||
|
|
||||||
|
// Wait for interrupt signal to gracefully shutdown the app
|
||||||
|
quit := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(quit, os.Interrupt)
|
||||||
|
<-quit
|
||||||
|
|
||||||
|
cancel()
|
||||||
|
|
||||||
|
fmt.Fprintf(os.Stderr, "\nExiting normally, received signal 2.\n")
|
||||||
|
|
||||||
|
os.Exit(255)
|
||||||
|
}
|
15
internal/testhelper/sigint/sigint.go
Normal file
15
internal/testhelper/sigint/sigint.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// Wait for interrupt signal to gracefully shutdown the app
|
||||||
|
quit := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(quit, os.Interrupt)
|
||||||
|
<-quit
|
||||||
|
|
||||||
|
os.Exit(255)
|
||||||
|
}
|
21
internal/testhelper/testhelper.go
Normal file
21
internal/testhelper/testhelper.go
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package testhelper
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
func BuildBinary(name, pathprefix string) (string, error) {
|
||||||
|
dir := filepath.Join(pathprefix, name)
|
||||||
|
aout := filepath.Join(dir, name)
|
||||||
|
|
||||||
|
fmt.Printf("aout: %s\n", aout)
|
||||||
|
|
||||||
|
err := exec.Command("go", "build", "-o", aout, dir).Run()
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("build command: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return aout, nil
|
||||||
|
}
|
1
process/helper/.gitignore
vendored
1
process/helper/.gitignore
vendored
@@ -1 +0,0 @@
|
|||||||
ignoresigint
|
|
@@ -1,11 +1,11 @@
|
|||||||
package process
|
package process
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os/exec"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/datarhei/core/v16/internal/testhelper"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestProcess(t *testing.T) {
|
func TestProcess(t *testing.T) {
|
||||||
@@ -18,21 +18,21 @@ func TestProcess(t *testing.T) {
|
|||||||
StaleTimeout: 0,
|
StaleTimeout: 0,
|
||||||
})
|
})
|
||||||
|
|
||||||
assert.Equal(t, "finished", p.Status().State)
|
require.Equal(t, "finished", p.Status().State)
|
||||||
|
|
||||||
p.Start()
|
p.Start()
|
||||||
|
|
||||||
assert.Equal(t, "running", p.Status().State)
|
require.Equal(t, "running", p.Status().State)
|
||||||
|
|
||||||
time.Sleep(5 * time.Second)
|
time.Sleep(5 * time.Second)
|
||||||
|
|
||||||
assert.Equal(t, "running", p.Status().State)
|
require.Equal(t, "running", p.Status().State)
|
||||||
|
|
||||||
p.Stop()
|
p.Stop()
|
||||||
|
|
||||||
time.Sleep(2 * time.Second)
|
time.Sleep(2 * time.Second)
|
||||||
|
|
||||||
assert.Equal(t, "killed", p.Status().State)
|
require.Equal(t, "killed", p.Status().State)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReconnectProcess(t *testing.T) {
|
func TestReconnectProcess(t *testing.T) {
|
||||||
@@ -50,11 +50,11 @@ func TestReconnectProcess(t *testing.T) {
|
|||||||
|
|
||||||
time.Sleep(3 * time.Second)
|
time.Sleep(3 * time.Second)
|
||||||
|
|
||||||
assert.Equal(t, "finished", p.Status().State)
|
require.Equal(t, "finished", p.Status().State)
|
||||||
|
|
||||||
p.Stop()
|
p.Stop()
|
||||||
|
|
||||||
assert.Equal(t, "finished", p.Status().State)
|
require.Equal(t, "finished", p.Status().State)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStaleProcess(t *testing.T) {
|
func TestStaleProcess(t *testing.T) {
|
||||||
@@ -71,11 +71,11 @@ func TestStaleProcess(t *testing.T) {
|
|||||||
|
|
||||||
time.Sleep(5 * time.Second)
|
time.Sleep(5 * time.Second)
|
||||||
|
|
||||||
assert.Equal(t, "killed", p.Status().State)
|
require.Equal(t, "killed", p.Status().State)
|
||||||
|
|
||||||
p.Stop()
|
p.Stop()
|
||||||
|
|
||||||
assert.Equal(t, "killed", p.Status().State)
|
require.Equal(t, "killed", p.Status().State)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStaleReconnectProcess(t *testing.T) {
|
func TestStaleReconnectProcess(t *testing.T) {
|
||||||
@@ -92,11 +92,11 @@ func TestStaleReconnectProcess(t *testing.T) {
|
|||||||
|
|
||||||
time.Sleep(10 * time.Second)
|
time.Sleep(10 * time.Second)
|
||||||
|
|
||||||
assert.Equal(t, "killed", p.Status().State)
|
require.Equal(t, "killed", p.Status().State)
|
||||||
|
|
||||||
p.Stop()
|
p.Stop()
|
||||||
|
|
||||||
assert.Equal(t, "killed", p.Status().State)
|
require.Equal(t, "killed", p.Status().State)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNonExistingProcess(t *testing.T) {
|
func TestNonExistingProcess(t *testing.T) {
|
||||||
@@ -114,11 +114,11 @@ func TestNonExistingProcess(t *testing.T) {
|
|||||||
|
|
||||||
time.Sleep(3 * time.Second)
|
time.Sleep(3 * time.Second)
|
||||||
|
|
||||||
assert.Equal(t, "failed", p.Status().State)
|
require.Equal(t, "failed", p.Status().State)
|
||||||
|
|
||||||
p.Stop()
|
p.Stop()
|
||||||
|
|
||||||
assert.Equal(t, "failed", p.Status().State)
|
require.Equal(t, "failed", p.Status().State)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNonExistingReconnectProcess(t *testing.T) {
|
func TestNonExistingReconnectProcess(t *testing.T) {
|
||||||
@@ -136,18 +136,18 @@ func TestNonExistingReconnectProcess(t *testing.T) {
|
|||||||
|
|
||||||
time.Sleep(5 * time.Second)
|
time.Sleep(5 * time.Second)
|
||||||
|
|
||||||
assert.Equal(t, "failed", p.Status().State)
|
require.Equal(t, "failed", p.Status().State)
|
||||||
|
|
||||||
p.Stop()
|
p.Stop()
|
||||||
|
|
||||||
assert.Equal(t, "failed", p.Status().State)
|
require.Equal(t, "failed", p.Status().State)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessFailed(t *testing.T) {
|
func TestProcessFailed(t *testing.T) {
|
||||||
p, _ := New(Config{
|
p, _ := New(Config{
|
||||||
Binary: "ffmpeg",
|
Binary: "sleep",
|
||||||
Args: []string{
|
Args: []string{
|
||||||
"-i",
|
"hello",
|
||||||
},
|
},
|
||||||
Reconnect: false,
|
Reconnect: false,
|
||||||
StaleTimeout: 0,
|
StaleTimeout: 0,
|
||||||
@@ -159,24 +159,22 @@ func TestProcessFailed(t *testing.T) {
|
|||||||
|
|
||||||
p.Stop()
|
p.Stop()
|
||||||
|
|
||||||
assert.Equal(t, "failed", p.Status().State)
|
require.Equal(t, "failed", p.Status().State)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFFmpegKill(t *testing.T) {
|
func TestFFmpegKill(t *testing.T) {
|
||||||
|
binary, err := testhelper.BuildBinary("sigint", "../internal/testhelper")
|
||||||
|
require.NoError(t, err, "Failed to build helper program")
|
||||||
|
|
||||||
p, _ := New(Config{
|
p, _ := New(Config{
|
||||||
Binary: "ffmpeg",
|
Binary: binary,
|
||||||
Args: []string{
|
Args: []string{},
|
||||||
"-f", "lavfi",
|
|
||||||
"-i", "testsrc2",
|
|
||||||
"-codec", "copy",
|
|
||||||
"-f", "null",
|
|
||||||
"-",
|
|
||||||
},
|
|
||||||
Reconnect: false,
|
Reconnect: false,
|
||||||
StaleTimeout: 0,
|
StaleTimeout: 0,
|
||||||
})
|
})
|
||||||
|
|
||||||
p.Start()
|
err = p.Start()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
time.Sleep(5 * time.Second)
|
time.Sleep(5 * time.Second)
|
||||||
|
|
||||||
@@ -184,23 +182,22 @@ func TestFFmpegKill(t *testing.T) {
|
|||||||
|
|
||||||
time.Sleep(3 * time.Second)
|
time.Sleep(3 * time.Second)
|
||||||
|
|
||||||
assert.Equal(t, "finished", p.Status().State)
|
require.Equal(t, "finished", p.Status().State)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessForceKill(t *testing.T) {
|
func TestProcessForceKill(t *testing.T) {
|
||||||
if err := exec.Command("go", "build", "-o", "./helper/ignoresigint", "./helper").Run(); err != nil {
|
binary, err := testhelper.BuildBinary("ignoresigint", "../internal/testhelper")
|
||||||
t.Errorf("Failed to build helper program: %s", err)
|
require.NoError(t, err, "Failed to build helper program")
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
p, _ := New(Config{
|
p, _ := New(Config{
|
||||||
Binary: "./helper/ignoresigint",
|
Binary: binary,
|
||||||
Args: []string{},
|
Args: []string{},
|
||||||
Reconnect: false,
|
Reconnect: false,
|
||||||
StaleTimeout: 0,
|
StaleTimeout: 0,
|
||||||
})
|
})
|
||||||
|
|
||||||
p.Start()
|
err = p.Start()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
time.Sleep(3 * time.Second)
|
time.Sleep(3 * time.Second)
|
||||||
|
|
||||||
@@ -208,9 +205,9 @@ func TestProcessForceKill(t *testing.T) {
|
|||||||
|
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
|
|
||||||
assert.Equal(t, "finishing", p.Status().State)
|
require.Equal(t, "finishing", p.Status().State)
|
||||||
|
|
||||||
time.Sleep(5 * time.Second)
|
time.Sleep(5 * time.Second)
|
||||||
|
|
||||||
assert.Equal(t, "killed", p.Status().State)
|
require.Equal(t, "killed", p.Status().State)
|
||||||
}
|
}
|
||||||
|
@@ -1,26 +1,40 @@
|
|||||||
package restream
|
package restream
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/datarhei/core/v16/ffmpeg"
|
"github.com/datarhei/core/v16/ffmpeg"
|
||||||
|
"github.com/datarhei/core/v16/internal/testhelper"
|
||||||
"github.com/datarhei/core/v16/net"
|
"github.com/datarhei/core/v16/net"
|
||||||
"github.com/datarhei/core/v16/restream/app"
|
"github.com/datarhei/core/v16/restream/app"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getDummyRestreamer(portrange net.Portranger) Restreamer {
|
func getDummyRestreamer(portrange net.Portranger) (Restreamer, error) {
|
||||||
ffmpeg, _ := ffmpeg.New(ffmpeg.Config{
|
binary, err := testhelper.BuildBinary("ffmpeg", "../internal/testhelper")
|
||||||
Binary: "ffmpeg",
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to build helper program: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ffmpeg, err := ffmpeg.New(ffmpeg.Config{
|
||||||
|
Binary: binary,
|
||||||
Portrange: portrange,
|
Portrange: portrange,
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
rs, _ := New(Config{
|
rs, err := New(Config{
|
||||||
FFmpeg: ffmpeg,
|
FFmpeg: ffmpeg,
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
return rs
|
return rs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDummyProcess() *app.Config {
|
func getDummyProcess() *app.Config {
|
||||||
@@ -61,10 +75,11 @@ func getDummyProcess() *app.Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestAddProcess(t *testing.T) {
|
func TestAddProcess(t *testing.T) {
|
||||||
var err error = nil
|
rs, err := getDummyRestreamer(nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
rs := getDummyRestreamer(nil)
|
|
||||||
process := getDummyProcess()
|
process := getDummyProcess()
|
||||||
|
require.NotNil(t, process)
|
||||||
|
|
||||||
_, err = rs.GetProcess(process.ID)
|
_, err = rs.GetProcess(process.ID)
|
||||||
require.NotEqual(t, nil, err, "Unset process found (%s)", process.ID)
|
require.NotEqual(t, nil, err, "Unset process found (%s)", process.ID)
|
||||||
@@ -80,7 +95,9 @@ func TestAddProcess(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestAutostartProcess(t *testing.T) {
|
func TestAutostartProcess(t *testing.T) {
|
||||||
rs := getDummyRestreamer(nil)
|
rs, err := getDummyRestreamer(nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
process := getDummyProcess()
|
process := getDummyProcess()
|
||||||
process.Autostart = true
|
process.Autostart = true
|
||||||
|
|
||||||
@@ -93,9 +110,8 @@ func TestAutostartProcess(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestAddInvalidProcess(t *testing.T) {
|
func TestAddInvalidProcess(t *testing.T) {
|
||||||
var err error = nil
|
rs, err := getDummyRestreamer(nil)
|
||||||
|
require.NoError(t, err)
|
||||||
rs := getDummyRestreamer(nil)
|
|
||||||
|
|
||||||
// Invalid process ID
|
// Invalid process ID
|
||||||
process := getDummyProcess()
|
process := getDummyProcess()
|
||||||
@@ -162,9 +178,9 @@ func TestAddInvalidProcess(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRemoveProcess(t *testing.T) {
|
func TestRemoveProcess(t *testing.T) {
|
||||||
var err error = nil
|
rs, err := getDummyRestreamer(nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
rs := getDummyRestreamer(nil)
|
|
||||||
process := getDummyProcess()
|
process := getDummyProcess()
|
||||||
|
|
||||||
err = rs.AddProcess(process)
|
err = rs.AddProcess(process)
|
||||||
@@ -178,9 +194,9 @@ func TestRemoveProcess(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGetProcess(t *testing.T) {
|
func TestGetProcess(t *testing.T) {
|
||||||
var err error = nil
|
rs, err := getDummyRestreamer(nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
rs := getDummyRestreamer(nil)
|
|
||||||
process := getDummyProcess()
|
process := getDummyProcess()
|
||||||
|
|
||||||
rs.AddProcess(process)
|
rs.AddProcess(process)
|
||||||
@@ -194,9 +210,9 @@ func TestGetProcess(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestStartProcess(t *testing.T) {
|
func TestStartProcess(t *testing.T) {
|
||||||
var err error = nil
|
rs, err := getDummyRestreamer(nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
rs := getDummyRestreamer(nil)
|
|
||||||
process := getDummyProcess()
|
process := getDummyProcess()
|
||||||
|
|
||||||
rs.AddProcess(process)
|
rs.AddProcess(process)
|
||||||
@@ -220,9 +236,9 @@ func TestStartProcess(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestStopProcess(t *testing.T) {
|
func TestStopProcess(t *testing.T) {
|
||||||
var err error = nil
|
rs, err := getDummyRestreamer(nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
rs := getDummyRestreamer(nil)
|
|
||||||
process := getDummyProcess()
|
process := getDummyProcess()
|
||||||
|
|
||||||
rs.AddProcess(process)
|
rs.AddProcess(process)
|
||||||
@@ -245,9 +261,9 @@ func TestStopProcess(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRestartProcess(t *testing.T) {
|
func TestRestartProcess(t *testing.T) {
|
||||||
var err error = nil
|
rs, err := getDummyRestreamer(nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
rs := getDummyRestreamer(nil)
|
|
||||||
process := getDummyProcess()
|
process := getDummyProcess()
|
||||||
|
|
||||||
rs.AddProcess(process)
|
rs.AddProcess(process)
|
||||||
@@ -270,9 +286,9 @@ func TestRestartProcess(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestReloadProcess(t *testing.T) {
|
func TestReloadProcess(t *testing.T) {
|
||||||
var err error = nil
|
rs, err := getDummyRestreamer(nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
rs := getDummyRestreamer(nil)
|
|
||||||
process := getDummyProcess()
|
process := getDummyProcess()
|
||||||
|
|
||||||
rs.AddProcess(process)
|
rs.AddProcess(process)
|
||||||
@@ -301,7 +317,9 @@ func TestReloadProcess(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessData(t *testing.T) {
|
func TestProcessData(t *testing.T) {
|
||||||
rs := getDummyRestreamer(nil)
|
rs, err := getDummyRestreamer(nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
process := getDummyProcess()
|
process := getDummyProcess()
|
||||||
|
|
||||||
rs.AddProcess(process)
|
rs.AddProcess(process)
|
||||||
@@ -320,9 +338,9 @@ func TestProcessData(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestLog(t *testing.T) {
|
func TestLog(t *testing.T) {
|
||||||
var err error = nil
|
rs, err := getDummyRestreamer(nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
rs := getDummyRestreamer(nil)
|
|
||||||
process := getDummyProcess()
|
process := getDummyProcess()
|
||||||
|
|
||||||
rs.AddProcess(process)
|
rs.AddProcess(process)
|
||||||
@@ -353,9 +371,9 @@ func TestLog(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestPlayoutNoRange(t *testing.T) {
|
func TestPlayoutNoRange(t *testing.T) {
|
||||||
var err error = nil
|
rs, err := getDummyRestreamer(nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
rs := getDummyRestreamer(nil)
|
|
||||||
process := getDummyProcess()
|
process := getDummyProcess()
|
||||||
|
|
||||||
process.Input[0].Address = "playout:" + process.Input[0].Address
|
process.Input[0].Address = "playout:" + process.Input[0].Address
|
||||||
@@ -373,11 +391,12 @@ func TestPlayoutNoRange(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestPlayoutRange(t *testing.T) {
|
func TestPlayoutRange(t *testing.T) {
|
||||||
var err error = nil
|
portrange, err := net.NewPortrange(3000, 3001)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
portrange, _ := net.NewPortrange(3000, 3001)
|
rs, err := getDummyRestreamer(portrange)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
rs := getDummyRestreamer(portrange)
|
|
||||||
process := getDummyProcess()
|
process := getDummyProcess()
|
||||||
|
|
||||||
process.Input[0].Address = "playout:" + process.Input[0].Address
|
process.Input[0].Address = "playout:" + process.Input[0].Address
|
||||||
@@ -396,9 +415,9 @@ func TestPlayoutRange(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestAddressReference(t *testing.T) {
|
func TestAddressReference(t *testing.T) {
|
||||||
var err error = nil
|
rs, err := getDummyRestreamer(nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
rs := getDummyRestreamer(nil)
|
|
||||||
process1 := getDummyProcess()
|
process1 := getDummyProcess()
|
||||||
process2 := getDummyProcess()
|
process2 := getDummyProcess()
|
||||||
|
|
||||||
@@ -428,7 +447,10 @@ func TestAddressReference(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestOutputAddressValidation(t *testing.T) {
|
func TestOutputAddressValidation(t *testing.T) {
|
||||||
rs := getDummyRestreamer(nil).(*restream)
|
rsi, err := getDummyRestreamer(nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
rs := rsi.(*restream)
|
||||||
|
|
||||||
type res struct {
|
type res struct {
|
||||||
path string
|
path string
|
||||||
|
Reference in New Issue
Block a user