Skip to content

Commit

Permalink
update video e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adwski committed May 14, 2024
1 parent c56a7a5 commit cf32615
Show file tree
Hide file tree
Showing 15 changed files with 250 additions and 143 deletions.
29 changes: 0 additions & 29 deletions .github/workflows/unittests.yml

This file was deleted.

4 changes: 0 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@ unittests:
cover:
go tool cover -html profile.cov -o coverage.html

.PHONY: test
test:
go test -v -count=1 ./...

.PHONY: test-all
test-all: docker-infra
go test -v -count=1 -cover -coverpkg=./... -coverprofile=profile.cov --tags e2e ./...
Expand Down
154 changes: 120 additions & 34 deletions e2e/e2e_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
package e2e

import (
"io"
"net/http"
"os"
"strconv"
"strings"
"testing"

Expand All @@ -13,6 +15,7 @@ import (
"github.com/adwski/vidi/internal/api/user/model"
videohttp "github.com/adwski/vidi/internal/api/video/http"
video "github.com/adwski/vidi/internal/api/video/model"
"github.com/adwski/vidi/internal/file"
"github.com/adwski/vidi/internal/mp4"
"github.com/davecgh/go-spew/spew"
"github.com/go-resty/resty/v2"
Expand All @@ -23,7 +26,13 @@ import (
const (
endpointUserLogin = "http://localhost:18081/api/user/login"
endpointUserRegister = "http://localhost:18081/api/user/register"
endpointVideo = "http://localhost:18082/api/video/user/"
endpointVideo = "http://localhost:18082/api/video/"
endpointWatch = "http://localhost:18082/api/watch/"

partSize = 10 * 1024 * 1024
contentTypeVidiMediaPart = "application/x-vidi-mediapart"

testFilePath = "../testfiles/test_seq_h264_high.mp4"
)

func userRegister(t *testing.T, user *model.UserRequest) *http.Cookie {
Expand Down Expand Up @@ -97,7 +106,26 @@ func getCookieWithToken(t *testing.T, cookies []*http.Cookie) *http.Cookie {
return userCookie
}

func videoWatch(t *testing.T, userCookie *http.Cookie, v *videohttp.VideoResponse) *videohttp.WatchResponse {
func videoWatch(t *testing.T, userCookie *http.Cookie, v *videohttp.VideoResponse) []byte {
t.Helper()

var (
errBody common.Response
)
resp, err := resty.New().R().SetHeader("Accept", "application/json").
SetError(&errBody).
SetCookie(userCookie).SetDebug(true).Get(endpointWatch + v.ID)
t.Log("video watch response", resp, err)
require.NoError(t, err)
require.True(t, resp.IsSuccess())
require.Equal(t, http.StatusOK, resp.StatusCode())
require.Empty(t, errBody.Error)
require.NotEmpty(t, resp.Body())

return resp.Body()
}

func videoWatchURL(t *testing.T, userCookie *http.Cookie, v *videohttp.VideoResponse) string {
t.Helper()

var (
Expand All @@ -107,14 +135,16 @@ func videoWatch(t *testing.T, userCookie *http.Cookie, v *videohttp.VideoRespons
resp, err := resty.New().R().SetHeader("Accept", "application/json").
SetError(&errBody).
SetCookie(userCookie).
SetResult(&watchBody).Post(endpointVideo + v.ID + "/watch")
SetResult(&watchBody).
SetQueryParam("mode", "url").
Get(endpointWatch + v.ID)
require.NoError(t, err)
require.True(t, resp.IsSuccess())
require.Equal(t, http.StatusAccepted, resp.StatusCode())
require.Equal(t, http.StatusOK, resp.StatusCode())
require.Empty(t, errBody.Error)
require.NotEmpty(t, watchBody.WatchURL)

return &watchBody
return watchBody.WatchURL
}

func videoWatchFail(t *testing.T, userCookie *http.Cookie, v *videohttp.VideoResponse, code int) {
Expand All @@ -126,31 +156,17 @@ func videoWatchFail(t *testing.T, userCookie *http.Cookie, v *videohttp.VideoRes
resp, err := resty.New().R().SetHeader("Accept", "application/json").
SetError(&errBody).
SetCookie(userCookie).
Post(endpointVideo + v.ID + "/watch")
Get(endpointWatch + v.ID)
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()

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.IsSuccess())
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")
f, errF := os.Open(testFilePath)
require.NoError(t, errF)

resp, err := resty.New().R().
Expand All @@ -164,7 +180,7 @@ func videoUploadFail(t *testing.T, url string) {
func videoUploadFailGet(t *testing.T, url string) {
t.Helper()

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

resp, err := resty.New().R().
Expand Down Expand Up @@ -227,7 +243,6 @@ func videoGetFail(t *testing.T, userCookie *http.Cookie, id string, code int) {
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) []*videohttp.VideoResponse {
Expand All @@ -253,26 +268,83 @@ func videoGetAll(t *testing.T, userCookie *http.Cookie) []*videohttp.VideoRespon
func videoCreate(t *testing.T, userCookie *http.Cookie) *videohttp.VideoResponse {
t.Helper()

// get size
size, err := file.GetSize(testFilePath)
require.NoError(t, err)
require.Greater(t, size, uint64(0))

// make parts with checksums
parts, err := file.MakePartsFromFile(testFilePath, partSize, size)
require.NoError(t, err)
require.Greater(t, len(parts), 0)

var (
videoBody videohttp.VideoResponse
errBody common.Response
respBody videohttp.VideoResponse
reqBody video.CreateRequest
errBody common.Response
)
reqBody.Name = "test"
reqBody.Size = size
for _, part := range parts {
reqBody.Parts = append(reqBody.Parts, &video.Part{
Checksum: part.Checksum,
Num: part.Num,
Size: uint64(part.Size),
})
}
resp, err := resty.New().R().SetHeader("Accept", "application/json").
SetError(&errBody).
SetCookie(userCookie).
SetResult(&videoBody).Post(endpointVideo)
SetBody(&reqBody).
SetResult(&respBody).
Post(endpointVideo)
t.Log("got create video response", resp)
require.NoError(t, err)
require.True(t, resp.IsSuccess())
require.Empty(t, errBody.Error)
require.NotEmpty(t, videoBody.CreatedAt)
require.NotEmpty(t, videoBody.ID)
require.NotEmpty(t, videoBody.UploadInfo.URL)

status, err := video.GetStatusFromName(videoBody.Status)
assert.NotEmpty(t, respBody.CreatedAt)
assert.NotEmpty(t, respBody.ID)
assert.NotEmpty(t, respBody.UploadInfo.URL)
status, err := video.GetStatusFromName(respBody.Status)
require.NoError(t, err)
require.Equal(t, video.StatusCreated, status)

return &videoBody
return &respBody
}

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

// get size
size, err := file.GetSize(testFilePath)
require.NoError(t, err)
require.Greater(t, size, uint64(0))

// make parts with checksums
parts, err := file.MakePartsFromFile(testFilePath, partSize, size)
require.NoError(t, err)
require.Greater(t, len(parts), 0)

// upload parts
f, err := os.Open(testFilePath)
require.NoError(t, err)
defer func() { _ = f.Close() }()

var offset uint
for _, part := range parts {
require.Greater(t, part.Size, uint(0))
b, errB := io.ReadAll(io.LimitReader(f, int64(part.Size)))
require.NoError(t, errB)
resp, rErr := resty.New().R().
SetBody(b).
SetHeader("Content-Length", strconv.FormatUint(uint64(part.Size), 10)).
SetHeader("Content-Type", contentTypeVidiMediaPart).SetDebug(true).
Post(url + "/" + strconv.FormatUint(uint64(part.Num), 10))
t.Log("upload part response", resp, rErr)
require.NoError(t, rErr)
require.True(t, resp.IsSuccess())
offset += part.Size
}
}

func videoCreateFail(t *testing.T) {
Expand All @@ -288,6 +360,20 @@ func videoCreateFail(t *testing.T) {
require.Equal(t, http.StatusUnauthorized, resp.StatusCode())
}

func watchVideoFromMPD(t *testing.T, mpdBody []byte) {
t.Helper()

vMpd, err := mpd.MPDFromBytes(mpdBody)
require.NoError(t, err)
require.NotEmpty(t, vMpd.BaseURL)
url := string(vMpd.BaseURL[0].Value)

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

downloadSegmentsFail(t, url)
}

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

Expand All @@ -314,7 +400,7 @@ func downloadSegmentsFail(t *testing.T, url string) {

r := resty.New()

downloadSegmentFail(t, r, prefixURL+"/not-existent.mp4", http.StatusNotFound)
downloadSegmentFail(t, r, prefixURL+"/not-existent.mp4", http.StatusBadRequest)
downloadSegmentFail(t, r, prefixURLNoSess+"/qweqweqwe/vide1_1.m4s", http.StatusNotFound)
downloadSegmentFail(t, r, prefixURLNoSess, http.StatusBadRequest)
downloadSegmentFail(t, r, prefixURLNoSess+"/qw/", http.StatusBadRequest)
Expand All @@ -336,7 +422,7 @@ func downloadSegments(t *testing.T, url string) {
downloadSegment(t, r, prefixURL+"vide1_3.m4s", "video/iso.segment")
downloadSegment(t, r, prefixURL+"vide1_4.m4s", "video/iso.segment")

downloadSegment(t, r, prefixURL+"soun1_init.mp4", "video/mp4")
downloadSegment(t, r, prefixURL+"soun1_init.mp4", "audio/mp4")
downloadSegment(t, r, prefixURL+"soun1_1.m4s", "video/iso.segment")
downloadSegment(t, r, prefixURL+"soun1_2.m4s", "video/iso.segment")
downloadSegment(t, r, prefixURL+"soun1_3.m4s", "video/iso.segment")
Expand Down
1 change: 1 addition & 0 deletions e2e/uploader.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ log:
server:
http:
address: ":18083"
max_body_size: 5000000
api:
prefix: /upload
redis:
Expand Down
2 changes: 1 addition & 1 deletion e2e/videoapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ server:
address: ":18092"
svc_address: ":18093"
api:
prefix: /api/video
prefix: /api
database:
dsn: postgres://videoapi:videoapi@localhost:5432/videoapi?sslmode=disable
redis:
Expand Down
19 changes: 11 additions & 8 deletions e2e/videoapi_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build e2e && skip
//go:build e2e

package e2e

Expand All @@ -25,7 +25,7 @@ func TestCreateAndDeleteVideo(t *testing.T) {
// Login with existent user
//-------------------------------------------------------------------------------
cookie := userLogin(t, &user.UserRequest{
Username: "testuser",
Username: testUserName,
Password: "testpass",
})
t.Logf("user logged in, token: %v", cookie.Value)
Expand Down Expand Up @@ -58,7 +58,7 @@ func TestCreateAndUploadVideo(t *testing.T) {
// Login with existent user
//-------------------------------------------------------------------------------
cookie := userLogin(t, &user.UserRequest{
Username: "testuser",
Username: testUserName,
Password: "testpass",
})
t.Logf("user logged in, token: %v", cookie.Value)
Expand Down Expand Up @@ -104,7 +104,7 @@ func TestWatchVideo(t *testing.T) {
// Login with existent user
//-------------------------------------------------------------------------------
cookie := userLogin(t, &user.UserRequest{
Username: "testuser",
Username: testUserName,
Password: "testpass",
})
t.Logf("user logged in, token: %v", cookie.Value)
Expand All @@ -118,18 +118,21 @@ func TestWatchVideo(t *testing.T) {
//-------------------------------------------------------------------------------
// Get watch URL
//-------------------------------------------------------------------------------
watchResponse := videoWatch(t, cookie, videosResponse[0])
t.Logf("watch url retrieved: %s", watchResponse.WatchURL)
// with generated url
url := videoWatchURL(t, cookie, videosResponse[0])
watchVideo(t, url)

watchVideo(t, watchResponse.WatchURL)
// with direct api link
mpdBody := videoWatch(t, cookie, videosResponse[0])
watchVideoFromMPD(t, mpdBody)
}

func TestFails(t *testing.T) {
//-------------------------------------------------------------------------------
// Login with existent user
//-------------------------------------------------------------------------------
cookie := userLogin(t, &user.UserRequest{
Username: "testuser",
Username: testUserName,
Password: "testpass",
})
t.Logf("user logged in, token: %v", cookie.Value)
Expand Down
9 changes: 8 additions & 1 deletion internal/api/video/grpc/userside/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,14 @@ func (srv *Server) CreateVideo(ctx context.Context, req *pb.CreateVideoRequest)
vide, err := srv.videoSvc.CreateVideo(ctx, usr, r)
if err != nil {
srv.logger.Error("CreateVideo failed", zap.Error(err))
return nil, status.Error(codes.Internal, "cannot create video")
switch {
case errors.Is(err, model.ErrZeroSize),
errors.Is(err, model.ErrNoParts),
errors.Is(err, model.ErrNoName):
return nil, status.Error(codes.InvalidArgument, err.Error())
default:
return nil, status.Error(codes.Internal, "cannot create video")
}
}
return videoResponse(vide), nil
}
Expand Down
Loading

0 comments on commit cf32615

Please sign in to comment.