run tests without --network=host

This commit is contained in:
aler9 2020-06-08 21:36:42 +02:00
parent f3436faec3
commit d68c18de2d
2 changed files with 67 additions and 31 deletions

View File

@ -10,6 +10,7 @@ help:
@echo "" @echo ""
@echo " mod-tidy run go mod tidy" @echo " mod-tidy run go mod tidy"
@echo " format format source files" @echo " format format source files"
@echo " test run available tests"
@echo " run ARGS=args run app" @echo " run ARGS=args run app"
@echo " release build release assets" @echo " release build release assets"
@echo " travis-setup setup travis CI" @echo " travis-setup setup travis CI"
@ -29,6 +30,29 @@ format:
docker run --rm -it -v $(PWD):/s $(BASE_IMAGE) \ docker run --rm -it -v $(PWD):/s $(BASE_IMAGE) \
sh -c "cd /s && find . -type f -name '*.go' | xargs gofmt -l -w -s" sh -c "cd /s && find . -type f -name '*.go' | xargs gofmt -l -w -s"
define DOCKERFILE_TEST
FROM $(BASE_IMAGE)
RUN apk add --no-cache make docker-cli git
WORKDIR /s
COPY go.mod go.sum ./
RUN go mod download
COPY . ./
endef
export DOCKERFILE_TEST
test:
echo "$$DOCKERFILE_TEST" | docker build -q . -f - -t temp
docker run --rm -it \
-v /var/run/docker.sock:/var/run/docker.sock:ro \
temp \
make test-nodocker
test-nodocker:
$(foreach IMG,$(shell echo test-images/*/ | xargs -n1 basename), \
docker build -q test-images/$(IMG) -t rtsp-simple-server-test-$(IMG)$(NL))
$(eval export CGO_ENABLED = 0)
go test -v .
define DOCKERFILE_RUN define DOCKERFILE_RUN
FROM $(BASE_IMAGE) FROM $(BASE_IMAGE)
RUN apk add --no-cache git RUN apk add --no-cache git
@ -49,30 +73,6 @@ run:
temp \ temp \
/out $(ARGS) /out $(ARGS)
define DOCKERFILE_TEST
FROM $(BASE_IMAGE)
RUN apk add --no-cache make docker-cli git
WORKDIR /s
COPY go.mod go.sum ./
RUN go mod download
COPY . ./
endef
export DOCKERFILE_TEST
test:
echo "$$DOCKERFILE_TEST" | docker build -q . -f - -t temp
docker run --rm -it \
--network=host \
-v /var/run/docker.sock:/var/run/docker.sock:ro \
temp \
make test-nodocker
test-nodocker:
$(foreach IMG,$(shell echo test-images/*/ | xargs -n1 basename), \
docker build -q test-images/$(IMG) -t rtsp-simple-server-test-$(IMG)$(NL))
$(eval export CGO_ENABLED = 0)
go test -v .
define DOCKERFILE_RELEASE define DOCKERFILE_RELEASE
FROM $(BASE_IMAGE) FROM $(BASE_IMAGE)
RUN apk add --no-cache zip make git tar RUN apk add --no-cache zip make git tar

View File

@ -2,6 +2,7 @@ package main
import ( import (
"bytes" "bytes"
"net"
"os" "os"
"os/exec" "os/exec"
"testing" "testing"
@ -10,6 +11,41 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
var ownDockerIp = func() string {
out, err := exec.Command("docker", "network", "inspect", "bridge",
"-f", "{{range .IPAM.Config}}{{.Subnet}}{{end}}").Output()
if err != nil {
panic(err)
}
_, ipnet, err := net.ParseCIDR(string(out[:len(out)-1]))
if err != nil {
panic(err)
}
ifaces, err := net.Interfaces()
if err != nil {
panic(err)
}
for _, i := range ifaces {
addrs, err := i.Addrs()
if err != nil {
continue
}
for _, addr := range addrs {
if v, ok := addr.(*net.IPNet); ok {
if ipnet.Contains(v.IP) {
return v.IP.String()
}
}
}
}
panic("IP not found")
}()
type container struct { type container struct {
name string name string
stdout *bytes.Buffer stdout *bytes.Buffer
@ -24,7 +60,7 @@ func newContainer(image string, name string, args []string) (*container, error)
exec.Command("docker", "kill", "rtsp-simple-server-test-"+name).Run() exec.Command("docker", "kill", "rtsp-simple-server-test-"+name).Run()
exec.Command("docker", "wait", "rtsp-simple-server-test-"+name).Run() exec.Command("docker", "wait", "rtsp-simple-server-test-"+name).Run()
cmd := []string{"docker", "run", "--network=host", cmd := []string{"docker", "run",
"--name=rtsp-simple-server-test-" + name, "--name=rtsp-simple-server-test-" + name,
"rtsp-simple-server-test-" + image} "rtsp-simple-server-test-" + image}
cmd = append(cmd, args...) cmd = append(cmd, args...)
@ -76,7 +112,7 @@ func TestProtocols(t *testing.T) {
"-c", "copy", "-c", "copy",
"-f", "rtsp", "-f", "rtsp",
"-rtsp_transport", pair[0], "-rtsp_transport", pair[0],
"rtsp://localhost:8554/teststream", "rtsp://" + ownDockerIp + ":8554/teststream",
}) })
require.NoError(t, err) require.NoError(t, err)
defer cnt1.close() defer cnt1.close()
@ -87,7 +123,7 @@ func TestProtocols(t *testing.T) {
"-hide_banner", "-hide_banner",
"-loglevel", "panic", "-loglevel", "panic",
"-rtsp_transport", pair[1], "-rtsp_transport", pair[1],
"-i", "rtsp://localhost:8554/teststream", "-i", "rtsp://" + ownDockerIp + ":8554/teststream",
"-vframes", "1", "-vframes", "1",
"-f", "image2", "-f", "image2",
"-y", "/dev/null", "-y", "/dev/null",
@ -121,7 +157,7 @@ func TestPublishAuth(t *testing.T) {
"-c", "copy", "-c", "copy",
"-f", "rtsp", "-f", "rtsp",
"-rtsp_transport", "udp", "-rtsp_transport", "udp",
"rtsp://testuser:testpass@localhost:8554/teststream", "rtsp://testuser:testpass@" + ownDockerIp + ":8554/teststream",
}) })
require.NoError(t, err) require.NoError(t, err)
defer cnt1.close() defer cnt1.close()
@ -132,7 +168,7 @@ func TestPublishAuth(t *testing.T) {
"-hide_banner", "-hide_banner",
"-loglevel", "panic", "-loglevel", "panic",
"-rtsp_transport", "udp", "-rtsp_transport", "udp",
"-i", "rtsp://localhost:8554/teststream", "-i", "rtsp://" + ownDockerIp + ":8554/teststream",
"-vframes", "1", "-vframes", "1",
"-f", "image2", "-f", "image2",
"-y", "/dev/null", "-y", "/dev/null",
@ -164,7 +200,7 @@ func TestReadAuth(t *testing.T) {
"-c", "copy", "-c", "copy",
"-f", "rtsp", "-f", "rtsp",
"-rtsp_transport", "udp", "-rtsp_transport", "udp",
"rtsp://localhost:8554/teststream", "rtsp://" + ownDockerIp + ":8554/teststream",
}) })
require.NoError(t, err) require.NoError(t, err)
defer cnt1.close() defer cnt1.close()
@ -175,7 +211,7 @@ func TestReadAuth(t *testing.T) {
"-hide_banner", "-hide_banner",
"-loglevel", "panic", "-loglevel", "panic",
"-rtsp_transport", "udp", "-rtsp_transport", "udp",
"-i", "rtsp://testuser:testpass@localhost:8554/teststream", "-i", "rtsp://testuser:testpass@" + ownDockerIp + ":8554/teststream",
"-vframes", "1", "-vframes", "1",
"-f", "image2", "-f", "image2",
"-y", "/dev/null", "-y", "/dev/null",