Commit fea67f36 authored by Jürgen Enge's avatar Jürgen Enge
Browse files

multiple tests with main function

parent 8bc5a7b9
Pipeline #8601 failed with stages
in 26 seconds
......@@ -5,6 +5,9 @@ Memoriav Media Streaming Server
## Installation
go get gitlab.switch.ch/memoriav/memobase-2020/streaming-server
go build gitlab.switch.ch/memoriav/memobase-2020/streaming-server/cmd/server
## Test
go test gitlab.switch.ch/memoriav/memobase-2020/streaming-server/cmd/server
## Prerequsites
......@@ -17,7 +20,7 @@ MariaDB/MySQL Server with table structure of [table.sql](tables.sql)
go get gitlab.switch.ch/memoriav/memobase-2020/streaming-server
go build gitlab.switch.ch/memoriav/memobase-2020/streaming-server/cmd/tokenService
## Usage
http://localhost:83/memostream/sig-01
......
......@@ -8,31 +8,33 @@ import (
"gitlab.switch.ch/memoriav/memobase-2020/services/streaming-server/pkg/memostream"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"testing"
)
var db *sql.DB
var prep *sqlmock.ExpectedPrepare
func TestServer(t *testing.T) {
func TestMain( m *testing.M) {
workingDirectory, err := os.Getwd()
if err != nil {
t.Fatalf("error getting working directory: %v", err)
log.Fatalf("error getting working directory: %v", err)
}
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("an error '%v' was not expected when opening a stub database connection", err)
log.Fatalf("an error '%v' was not expected when opening a stub database connection", err)
}
defer db.Close()
prep := mock.ExpectPrepare("SELECT uri, access, proto AS protocol, `status` FROM test.entities WHERE sig = ?")
prep = mock.ExpectPrepare("SELECT uri, access, proto AS protocol, `status` FROM test.entities WHERE sig = ?")
resolverDB, err := memostream.NewResolverDBMysql(db, "SELECT uri, access, proto AS protocol, `status` FROM test.entities WHERE sig = ?")
if err != nil {
t.Fatalf("cannot initialize resolverdb: %v", err)
log.Fatalf("cannot initialize resolverdb: %v", err)
}
defer resolverDB.Close()
......@@ -68,19 +70,26 @@ func TestServer(t *testing.T) {
fmt.Sprintf("%s/../../web/static", workingDirectory),
)
if err != nil {
t.Fatalf("error initializing server: %v", err)
log.Fatalf("error initializing server: %v", err)
}
var shutdown = false
go func() {
if err := srv.ListenAndServe("", ""); err != nil {
if !shutdown {
t.Errorf("server died: %+v", err)
log.Errorf("server died: %+v", err)
}
}
}()
exitCode := m.Run()
shutdown = true
srv.Shutdown(context.Background())
os.Exit(exitCode)
}
func TestProxy(t *testing.T) {
t.Logf("checking cache query for http://localhost:9876/memo/sig-001 - proxy")
prep.
ExpectQuery().
......@@ -102,23 +111,23 @@ func TestServer(t *testing.T) {
t.Errorf("error checking proxy - invalid size %v", num)
}
resp.Body.Close()
}
func TestFile(t *testing.T) {
t.Logf("checking cache query for http://localhost:9876/memo/sig-002 - file")
prep.
ExpectQuery().
WithArgs("sig-002").
WillReturnRows(sqlmock.NewRows([]string{"uri", "access", "protocol", "status"}).
AddRow("file://x/memoriav_logo_400x400.jpg", "public", "file", "ok"))
resp, err = http.Get("http://localhost:9876/memo/sig-002")
resp, err := http.Get("http://localhost:9876/memo/sig-002")
if err != nil {
t.Errorf("error checking file: %v", err)
}
if resp.StatusCode != 200 {
t.Errorf("error checking file - invalid status: %v %v", resp.StatusCode, resp.Status)
}
num, err = io.Copy(ioutil.Discard, resp.Body)
num, err := io.Copy(ioutil.Discard, resp.Body)
if err != nil {
t.Errorf("error checking file - cannot read data: %v", err)
}
......@@ -126,21 +135,23 @@ func TestServer(t *testing.T) {
t.Errorf("error checking file - invalid size %v", num)
}
resp.Body.Close()
}
func TestRedirect(t *testing.T) {
t.Logf("checking cache query for http://localhost:9876/memo/sig-003 - redirect")
prep.
ExpectQuery().
WithArgs("sig-003").
WillReturnRows(sqlmock.NewRows([]string{"uri", "access", "protocol", "status"}).
AddRow("http://localhost:9876/static/memoriav_logo_400x400.jpg", "public", "redirect", "ok"))
resp, err = http.Get("http://localhost:9876/memo/sig-003")
resp, err := http.Get("http://localhost:9876/memo/sig-003")
if err != nil {
t.Errorf("error checking redirect: %v", err)
}
if resp.StatusCode != 200 {
t.Errorf("error checking redirect - invalid status: %v %v", resp.StatusCode, resp.Status)
}
num, err = io.Copy(ioutil.Discard, resp.Body)
num, err := io.Copy(ioutil.Discard, resp.Body)
if err != nil {
t.Errorf("error checking redirect - cannot read data: %v", err)
}
......@@ -148,10 +159,6 @@ func TestServer(t *testing.T) {
t.Errorf("error checking redirect - invalid size %v", num)
}
resp.Body.Close()
shutdown = true
srv.Shutdown(context.Background())
}
......@@ -12,4 +12,5 @@ require (
github.com/gorilla/handlers v1.4.2
github.com/gorilla/mux v1.7.4
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7
gitlab.switch.ch/memoriav/memobase-2020/streaming-server v0.0.0-20200427161844-717b50bc664a // indirect
)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment