Skip to content

Commit

Permalink
add more tests and guthub tests workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
adwski committed Jan 31, 2024
1 parent f905db6 commit 01f431b
Show file tree
Hide file tree
Showing 43 changed files with 1,941 additions and 83 deletions.
60 changes: 60 additions & 0 deletions .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: e2e tests

on:
pull_request:
push:
branches:
- main

jobs:

e2etests:
runs-on: ubuntu-latest

services:
postgres:
image: postgres:16.1
env:
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd pg_isready
--health-interval 5s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432

redis:
image: redis:7
ports:
- 6379:6379

minio:
image: minio/minio:edge-cicd
options: --health-cmd "curl -s http://localhost:9000/minio/health/live"
env:
MINIO_ROOT_USER: admin
MINIO_ROOT_PASSWORD: password
ports:
- 9000:9000

steps:
- uses: actions/checkout@v4

- name: setup db
env:
PGPASSWORD: postgres
run: |
psql -h localhost -p 5432 -U postgres -f ./docker/compose/initdb/0001_init.sql
- uses: actions/setup-go@v5
with:
go-version-file: go.mod

- name: install deps
run: go mod download

- name: run tests
run: |
go test -v -count=1 -cover -coverpkg=./... -coverprofile=profile.cov --tags e2e ./...
go tool cover -func profile.cov
11 changes: 11 additions & 0 deletions .mockery.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
quiet: False
disable-version-string: True
with-expecter: True
mockname: "Mock{{.InterfaceName}}"
filename: "mock_{{.InterfaceName|lower}}_test.go"
outpkg: "{{.PackageName}}"
dir: "{{.InterfaceDir}}"
packages:
github.com/adwski/vidi/internal/api/video:
interfaces:
Store: {}
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
.PHONY: mock
mock:
find . -type f -name "mock_*" -exec rm -rf {} +
mockery

.PHONY: docker-dev
docker-dev:
cd docker/compose ;\
Expand Down
2 changes: 1 addition & 1 deletion docker/compose/docker-compose.infra.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ services:
- "vidi-db:/var/lib/postgresql/data"
- "./initdb:/docker-entrypoint-initdb.d:ro"
ports:
- "5400:5432"
- "5432:5432"
networks:
- vidi

Expand Down
114 changes: 113 additions & 1 deletion e2e/e2e_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"strings"
"testing"

"github.com/davecgh/go-spew/spew"

"github.com/Eyevinn/dash-mpd/mpd"
common "github.com/adwski/vidi/internal/api/model"
"github.com/adwski/vidi/internal/api/user/model"
Expand All @@ -35,6 +37,16 @@ func userRegister(t *testing.T, user *model.UserRequest) *http.Cookie {
return getCookieWithToken(t, resp.Cookies())
}

func userRegisterFail(t *testing.T, user any, code int) {
t.Helper()

resp, body := makeCommonRequest(t, endpointUserRegister, user)
require.True(t, resp.IsError())
require.NotEmpty(t, body.Error)
require.Empty(t, body.Message)
require.Equal(t, code, resp.StatusCode())
}

func userLogin(t *testing.T, user *model.UserRequest) *http.Cookie {
t.Helper()

Expand All @@ -45,13 +57,14 @@ func userLogin(t *testing.T, user *model.UserRequest) *http.Cookie {
return getCookieWithToken(t, resp.Cookies())
}

func userLoginFail(t *testing.T, user *model.UserRequest) {
func userLoginFail(t *testing.T, user any, code int) {
t.Helper()

resp, body := makeCommonRequest(t, endpointUserLogin, user)
require.Truef(t, resp.IsError(), "user should not exist")
require.Empty(t, body.Message)
require.NotEmpty(t, body.Error)
require.Equal(t, code, resp.StatusCode())
}

func makeCommonRequest(t *testing.T, url string, reqBody interface{}) (*resty.Response, *common.Response) {
Expand Down Expand Up @@ -105,6 +118,22 @@ func videoWatch(t *testing.T, userCookie *http.Cookie, v *video.Response) *video
return &watchBody
}

func videoWatchFail(t *testing.T, userCookie *http.Cookie, v *video.Response, code int) {
t.Helper()

var (
errBody common.Response
)
resp, err := resty.New().R().SetHeader("Accept", "application/json").
SetError(&errBody).
SetCookie(userCookie).
Post(endpointVideo + v.ID + "/watch")
require.NoError(t, err)
require.True(t, resp.IsError())
require.Equal(t, code, resp.StatusCode())
require.NotEmpty(t, errBody.Error)
}

func videoUpload(t *testing.T, url string) {
t.Helper()

Expand All @@ -119,6 +148,34 @@ func videoUpload(t *testing.T, url string) {
require.Equal(t, http.StatusNoContent, resp.StatusCode())
}

func videoUploadFail(t *testing.T, url string) {
t.Helper()

f, errF := os.Open("../testFiles/test_seq_h264_high.mp4")
require.NoError(t, errF)

resp, err := resty.New().R().
SetHeader("Content-Type", "video/mp4").
SetBody(f).Post(url)
require.NoError(t, err)
require.True(t, resp.IsError())
require.Equal(t, http.StatusBadRequest, resp.StatusCode())
}

func videoUploadFailGet(t *testing.T, url string) {
t.Helper()

f, errF := os.Open("../testFiles/test_seq_h264_high.mp4")
require.NoError(t, errF)

resp, err := resty.New().R().
SetHeader("Content-Type", "video/mp4").
SetBody(f).Get(url)
require.NoError(t, err)
require.True(t, resp.IsError())
require.Equal(t, http.StatusBadRequest, resp.StatusCode())
}

func videoDelete(t *testing.T, userCookie *http.Cookie, id string) {
t.Helper()

Expand Down Expand Up @@ -157,6 +214,23 @@ func videoGet(t *testing.T, userCookie *http.Cookie, id string) *video.Response
return &videoBody
}

func videoGetFail(t *testing.T, userCookie *http.Cookie, id string, code int) {
t.Helper()

var (
videoBody video.Response
errBody common.Response
)
resp, err := resty.New().R().SetHeader("Accept", "application/json").
SetError(&errBody).
SetCookie(userCookie).
SetResult(&videoBody).Get(endpointVideo + id)
require.NoError(t, err)
require.True(t, resp.IsError())
require.Equal(t, code, resp.StatusCode())
// require.NotEmpty(t, errBody.Error)
}

func videoGetAll(t *testing.T, userCookie *http.Cookie) []*video.Response {
t.Helper()

Expand Down Expand Up @@ -228,6 +302,26 @@ func watchVideo(t *testing.T, url string) {

checkStaticMPD(t, vMpd)
downloadSegments(t, url)

downloadSegmentsFail(t, url)
}

func downloadSegmentsFail(t *testing.T, url string) {
t.Helper()

prefixURL := strings.TrimSuffix(url, "/manifest.mpd")

prefixURLNoSess := prefixURL[:strings.LastIndexByte(prefixURL, '/')]

r := resty.New()

downloadSegmentFail(t, r, prefixURL+"/not-existent.mp4", http.StatusNotFound)
downloadSegmentFail(t, r, prefixURLNoSess+"/qweqweqwe/vide1_1.m4s", http.StatusNotFound)
downloadSegmentFail(t, r, prefixURLNoSess, http.StatusBadRequest)
downloadSegmentFail(t, r, prefixURLNoSess+"/qw/", http.StatusBadRequest)
downloadSegmentFail(t, r, prefixURL+"/vide1_init.wrong", http.StatusBadRequest)
downloadSegmentFailPost(t, r, prefixURL+"/vide1_init.mp4", http.StatusBadRequest)
spew.Dump(prefixURLNoSess)
}

func downloadSegments(t *testing.T, url string) {
Expand Down Expand Up @@ -263,6 +357,24 @@ func downloadSegment(t *testing.T, r *resty.Client, url, contentType string) {
assert.Equal(t, contentType, resp.Header().Get("Content-Type"))
}

func downloadSegmentFail(t *testing.T, r *resty.Client, url string, code int) {
t.Helper()

resp, err := r.R().Get(url)
require.NoError(t, err)
require.True(t, resp.IsError())
require.Equal(t, code, resp.StatusCode())
}

func downloadSegmentFailPost(t *testing.T, r *resty.Client, url string, code int) {
t.Helper()

resp, err := r.R().Post(url)
require.NoError(t, err)
require.True(t, resp.IsError())
require.Equal(t, code, resp.StatusCode())
}

func checkStaticMPD(t *testing.T, vMpd *mpd.MPD) {
t.Helper()

Expand Down
9 changes: 5 additions & 4 deletions e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ package e2e

import (
"context"
"github.com/adwski/vidi/internal/app/processor"
"github.com/adwski/vidi/internal/app/streamer"
"github.com/adwski/vidi/internal/app/uploader"
"github.com/adwski/vidi/internal/app/video"
"os"
"sync"
"testing"
"time"

"github.com/adwski/vidi/internal/app/processor"
"github.com/adwski/vidi/internal/app/streamer"
"github.com/adwski/vidi/internal/app/uploader"
"github.com/adwski/vidi/internal/app/video"

"github.com/adwski/vidi/internal/app/user"
)

Expand Down
3 changes: 3 additions & 0 deletions e2e/streamer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ s3:
access_key: admin
secret_key: password
bucket: vidi
cors:
enable: true
allow_origin: "*"
2 changes: 1 addition & 1 deletion e2e/userapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ server:
api:
prefix: /api/user
database:
dsn: postgres://userapi:userapi@localhost:5400/userapi?sslmode=disable
dsn: postgres://userapi:userapi@localhost:5432/userapi?sslmode=disable
31 changes: 29 additions & 2 deletions e2e/userapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package e2e

import (
"net/http"
"testing"

"github.com/adwski/vidi/internal/api/user/model"
Expand All @@ -16,7 +17,7 @@ func TestUserRegistration(t *testing.T) {
userLoginFail(t, &model.UserRequest{
Username: "testuser",
Password: "testpass",
})
}, http.StatusUnauthorized)

//-------------------------------------------------------------------------------
// Register user
Expand All @@ -26,6 +27,19 @@ func TestUserRegistration(t *testing.T) {
Password: "testpass",
})
t.Logf("user is registered, token: %v", cookie.Value)

//-------------------------------------------------------------------------------
// Register existing user
//-------------------------------------------------------------------------------
userRegisterFail(t, &model.UserRequest{
Username: "testuser",
Password: "testpass",
}, http.StatusConflict)

//-------------------------------------------------------------------------------
// Register with invalid data
//-------------------------------------------------------------------------------
userRegisterFail(t, "", http.StatusBadRequest)
}

func TestUserLogin(t *testing.T) {
Expand All @@ -44,5 +58,18 @@ func TestUserLogin(t *testing.T) {
userLoginFail(t, &model.UserRequest{
Username: "testuser2",
Password: "testpass2",
})
}, http.StatusUnauthorized)

//-------------------------------------------------------------------------------
// Login with wrong password
//-------------------------------------------------------------------------------
userLoginFail(t, &model.UserRequest{
Username: "testuser",
Password: "testpass2",
}, http.StatusUnauthorized)

//-------------------------------------------------------------------------------
// Login with invalid params
//-------------------------------------------------------------------------------
userLoginFail(t, "", http.StatusBadRequest)
}
2 changes: 1 addition & 1 deletion e2e/videoapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ server:
api:
prefix: /api/video
database:
dsn: postgres://videoapi:videoapi@localhost:5400/videoapi?sslmode=disable
dsn: postgres://videoapi:videoapi@localhost:5432/videoapi?sslmode=disable
redis:
dsn: redis://localhost:6379/0
media:
Expand Down
Loading

0 comments on commit 01f431b

Please sign in to comment.