Skip to content

Commit

Permalink
add vlc tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed Jul 10, 2020
1 parent 4c6f929 commit 6dbefec
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 115 deletions.
261 changes: 148 additions & 113 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"net"
"os"
"os/exec"
"strconv"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -47,14 +49,12 @@ var ownDockerIp = func() string {
}()

type container struct {
name string
stdout *bytes.Buffer
name string
}

func newContainer(image string, name string, args []string) (*container, error) {
c := &container{
name: name,
stdout: bytes.NewBuffer(nil),
name: name,
}

exec.Command("docker", "kill", "rtsp-simple-server-test-"+name).Run()
Expand All @@ -65,8 +65,7 @@ func newContainer(image string, name string, args []string) (*container, error)
"rtsp-simple-server-test-" + image}
cmd = append(cmd, args...)
ecmd := exec.Command(cmd[0], cmd[1:]...)

ecmd.Stdout = c.stdout
ecmd.Stdout = nil
ecmd.Stderr = os.Stderr

err := ecmd.Start()
Expand All @@ -85,18 +84,24 @@ func (c *container) close() {
exec.Command("docker", "rm", "rtsp-simple-server-test-"+c.name).Run()
}

func (c *container) wait() {
func (c *container) wait() int {
exec.Command("docker", "wait", "rtsp-simple-server-test-"+c.name).Run()
out, _ := exec.Command("docker", "inspect", "rtsp-simple-server-test-"+c.name,
"--format={{.State.ExitCode}}").Output()
code, _ := strconv.ParseInt(string(out[:len(out)-1]), 10, 64)
return int(code)
}

func TestProtocols(t *testing.T) {
for _, pair := range [][2]string{
{"udp", "udp"},
{"udp", "tcp"},
{"tcp", "udp"},
{"tcp", "tcp"},
for _, conf := range [][3]string{
{"udp", "udp", "ffmpeg"},
{"udp", "tcp", "ffmpeg"},
{"tcp", "udp", "ffmpeg"},
{"tcp", "tcp", "ffmpeg"},
{"tcp", "udp", "vlc"},
{"tcp", "tcp", "vlc"},
} {
t.Run(pair[0]+"_"+pair[1], func(t *testing.T) {
t.Run(strings.Join(conf[:], "_"), func(t *testing.T) {
p, err := newProgram([]string{}, bytes.NewBuffer(nil))
require.NoError(t, err)
defer p.close()
Expand All @@ -111,123 +116,154 @@ func TestProtocols(t *testing.T) {
"-i", "/emptyvideo.ts",
"-c", "copy",
"-f", "rtsp",
"-rtsp_transport", pair[0],
"-rtsp_transport", conf[0],
"rtsp://" + ownDockerIp + ":8554/teststream",
})
require.NoError(t, err)
defer cnt1.close()

time.Sleep(1 * time.Second)

cnt2, err := newContainer("ffmpeg", "dest", []string{
"-hide_banner",
"-loglevel", "panic",
"-rtsp_transport", pair[1],
"-i", "rtsp://" + ownDockerIp + ":8554/teststream",
"-vframes", "1",
"-f", "image2",
"-y", "/dev/null",
})
require.NoError(t, err)
defer cnt2.close()
if conf[2] == "ffmpeg" {
cnt2, err := newContainer("ffmpeg", "dest", []string{
"-hide_banner",
"-loglevel", "panic",
"-rtsp_transport", conf[1],
"-i", "rtsp://" + ownDockerIp + ":8554/teststream",
"-vframes", "1",
"-f", "image2",
"-y", "/dev/null",
})
require.NoError(t, err)
defer cnt2.close()

code := cnt2.wait()
require.Equal(t, 0, code)

} else {
args := []string{}
if conf[1] == "tcp" {
args = append(args, "--rtsp-tcp")
}
args = append(args, "rtsp://"+ownDockerIp+":8554/teststream")

cnt2.wait()
cnt2, err := newContainer("vlc", "dest", args)
require.NoError(t, err)
defer cnt2.close()

require.Equal(t, "all right\n", string(cnt2.stdout.Bytes()))
code := cnt2.wait()
require.Equal(t, 0, code)
}
})
}
}

func TestPublishAuth(t *testing.T) {
stdin := []byte("\n" +
"paths:\n" +
" all:\n" +
" publishUser: testuser\n" +
" publishPass: testpass\n" +
" publishIps: [172.17.0.0/16]\n")
p, err := newProgram([]string{"stdin"}, bytes.NewBuffer(stdin))
require.NoError(t, err)
defer p.close()

time.Sleep(1 * time.Second)

cnt1, err := newContainer("ffmpeg", "source", []string{
"-hide_banner",
"-loglevel", "panic",
"-re",
"-stream_loop", "-1",
"-i", "/emptyvideo.ts",
"-c", "copy",
"-f", "rtsp",
"-rtsp_transport", "udp",
"rtsp://testuser:testpass@" + ownDockerIp + ":8554/teststream",
})
require.NoError(t, err)
defer cnt1.close()

time.Sleep(1 * time.Second)
func TestAuth(t *testing.T) {
t.Run("publish", func(t *testing.T) {
stdin := []byte("\n" +
"paths:\n" +
" all:\n" +
" publishUser: testuser\n" +
" publishPass: testpass\n" +
" publishIps: [172.17.0.0/16]\n")
p, err := newProgram([]string{"stdin"}, bytes.NewBuffer(stdin))
require.NoError(t, err)
defer p.close()

time.Sleep(1 * time.Second)

cnt1, err := newContainer("ffmpeg", "source", []string{
"-hide_banner",
"-loglevel", "panic",
"-re",
"-stream_loop", "-1",
"-i", "/emptyvideo.ts",
"-c", "copy",
"-f", "rtsp",
"-rtsp_transport", "udp",
"rtsp://testuser:testpass@" + ownDockerIp + ":8554/teststream",
})
require.NoError(t, err)
defer cnt1.close()

time.Sleep(1 * time.Second)

cnt2, err := newContainer("ffmpeg", "dest", []string{
"-hide_banner",
"-loglevel", "panic",
"-rtsp_transport", "udp",
"-i", "rtsp://" + ownDockerIp + ":8554/teststream",
"-vframes", "1",
"-f", "image2",
"-y", "/dev/null",
})
require.NoError(t, err)
defer cnt2.close()

cnt2, err := newContainer("ffmpeg", "dest", []string{
"-hide_banner",
"-loglevel", "panic",
"-rtsp_transport", "udp",
"-i", "rtsp://" + ownDockerIp + ":8554/teststream",
"-vframes", "1",
"-f", "image2",
"-y", "/dev/null",
code := cnt2.wait()
require.Equal(t, 0, code)
})
require.NoError(t, err)
defer cnt2.close()

cnt2.wait()

require.Equal(t, "all right\n", string(cnt2.stdout.Bytes()))
}

func TestReadAuth(t *testing.T) {
stdin := []byte("\n" +
"paths:\n" +
" all:\n" +
" readUser: testuser\n" +
" readPass: testpass\n" +
" readIps: [172.17.0.0/16]\n")
p, err := newProgram([]string{"stdin"}, bytes.NewBuffer(stdin))
require.NoError(t, err)
defer p.close()

time.Sleep(1 * time.Second)

cnt1, err := newContainer("ffmpeg", "source", []string{
"-hide_banner",
"-loglevel", "panic",
"-re",
"-stream_loop", "-1",
"-i", "/emptyvideo.ts",
"-c", "copy",
"-f", "rtsp",
"-rtsp_transport", "udp",
"rtsp://" + ownDockerIp + ":8554/teststream",
})
require.NoError(t, err)
defer cnt1.close()
for _, soft := range []string{
"ffmpeg",
"vlc",
} {
t.Run("read_"+soft, func(t *testing.T) {
stdin := []byte("\n" +
"paths:\n" +
" all:\n" +
" readUser: testuser\n" +
" readPass: testpass\n" +
" readIps: [172.17.0.0/16]\n")
p, err := newProgram([]string{"stdin"}, bytes.NewBuffer(stdin))
require.NoError(t, err)
defer p.close()

time.Sleep(1 * time.Second)
time.Sleep(1 * time.Second)

cnt2, err := newContainer("ffmpeg", "dest", []string{
"-hide_banner",
"-loglevel", "panic",
"-rtsp_transport", "udp",
"-i", "rtsp://testuser:testpass@" + ownDockerIp + ":8554/teststream",
"-vframes", "1",
"-f", "image2",
"-y", "/dev/null",
})
require.NoError(t, err)
defer cnt2.close()
cnt1, err := newContainer("ffmpeg", "source", []string{
"-hide_banner",
"-loglevel", "panic",
"-re",
"-stream_loop", "-1",
"-i", "/emptyvideo.ts",
"-c", "copy",
"-f", "rtsp",
"-rtsp_transport", "udp",
"rtsp://" + ownDockerIp + ":8554/teststream",
})
require.NoError(t, err)
defer cnt1.close()

cnt2.wait()
time.Sleep(1 * time.Second)

require.Equal(t, "all right\n", string(cnt2.stdout.Bytes()))
if soft == "ffmpeg" {
cnt2, err := newContainer("ffmpeg", "dest", []string{
"-hide_banner",
"-loglevel", "panic",
"-rtsp_transport", "udp",
"-i", "rtsp://testuser:testpass@" + ownDockerIp + ":8554/teststream",
"-vframes", "1",
"-f", "image2",
"-y", "/dev/null",
})
require.NoError(t, err)
defer cnt2.close()

code := cnt2.wait()
require.Equal(t, 0, code)

} else {
cnt2, err := newContainer("vlc", "dest",
[]string{"rtsp://testuser:testpass@" + ownDockerIp + ":8554/teststream"})
require.NoError(t, err)
defer cnt2.close()

code := cnt2.wait()
require.Equal(t, 0, code)
}
})
}
}

func TestProxy(t *testing.T) {
Expand Down Expand Up @@ -290,9 +326,8 @@ func TestProxy(t *testing.T) {
require.NoError(t, err)
defer cnt2.close()

cnt2.wait()

require.Equal(t, "all right\n", string(cnt2.stdout.Bytes()))
code := cnt2.wait()
require.Equal(t, 0, code)
})
}
}
2 changes: 0 additions & 2 deletions test-images/ffmpeg/start.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/bin/sh -e

ffmpeg $@

echo "all right"
11 changes: 11 additions & 0 deletions test-images/vlc/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM amd64/alpine:3.12

RUN apk add --no-cache \
vlc

RUN adduser -D -H -s /bin/sh -u 9337 user

COPY start.sh /
RUN chmod +x /start.sh

ENTRYPOINT [ "/start.sh" ]
15 changes: 15 additions & 0 deletions test-images/vlc/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh -e

mkdir /out
chown user:user /out

CMD="cvlc --play-and-exit --no-audio --no-video --sout file/ts:/out/stream.ts -vvv $@"
su - user -c "$CMD" 2>&1 &

sleep 10

if [ $(stat -c "%s" /out/stream.ts) -gt 0 ]; then
exit 0
else
exit 1
fi

0 comments on commit 6dbefec

Please sign in to comment.