2022-01-03 19:59:59 +00:00
|
|
|
package media_test
|
|
|
|
|
|
|
|
import (
|
2022-01-04 05:51:25 +00:00
|
|
|
"bytes"
|
2022-01-03 19:59:59 +00:00
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"io"
|
2022-01-04 05:51:25 +00:00
|
|
|
"strings"
|
2022-01-03 19:59:59 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.netflux.io/rob/clipper/config"
|
|
|
|
"git.netflux.io/rob/clipper/generated/mocks"
|
|
|
|
"git.netflux.io/rob/clipper/generated/store"
|
|
|
|
"git.netflux.io/rob/clipper/media"
|
|
|
|
"github.com/google/uuid"
|
2022-01-04 05:51:25 +00:00
|
|
|
"github.com/kkdai/youtube/v2"
|
2022-01-03 19:59:59 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2022-01-04 05:51:25 +00:00
|
|
|
"github.com/stretchr/testify/mock"
|
2022-01-03 19:59:59 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2022-01-04 05:51:25 +00:00
|
|
|
func TestGetVideoFromYoutube(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2022-01-05 18:49:21 +00:00
|
|
|
wp := media.NewTestWorkerPool()
|
2022-01-04 05:51:25 +00:00
|
|
|
logger := zap.NewNop().Sugar()
|
|
|
|
|
|
|
|
const (
|
|
|
|
itag = 234
|
|
|
|
videoID = "video001"
|
|
|
|
videoMimeType = "video/mp4"
|
|
|
|
videoContentLength = int64(100_000)
|
|
|
|
)
|
|
|
|
mediaSetID := uuid.New()
|
|
|
|
mediaSet := store.MediaSet{
|
|
|
|
ID: mediaSetID,
|
|
|
|
YoutubeID: videoID,
|
|
|
|
VideoYoutubeItag: int32(itag),
|
|
|
|
}
|
|
|
|
|
|
|
|
video := &youtube.Video{
|
|
|
|
ID: videoID,
|
|
|
|
Formats: []youtube.Format{{ItagNo: itag, FPS: 30, AudioChannels: 0, MimeType: videoMimeType, ContentLength: videoContentLength}},
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("NOK,ErrorFetchingVideo", func(t *testing.T) {
|
|
|
|
var mockStore mocks.Store
|
|
|
|
mockStore.On("GetMediaSet", ctx, mediaSetID).Return(mediaSet, nil)
|
|
|
|
|
|
|
|
var youtubeClient mocks.YoutubeClient
|
|
|
|
youtubeClient.On("GetVideoContext", ctx, videoID).Return(nil, errors.New("nope"))
|
|
|
|
|
2022-01-05 18:49:21 +00:00
|
|
|
service := media.NewMediaSetService(&mockStore, &youtubeClient, nil, nil, wp, config.Config{}, logger)
|
2022-01-04 05:51:25 +00:00
|
|
|
_, err := service.GetVideo(ctx, mediaSetID)
|
|
|
|
assert.EqualError(t, err, "error fetching video: nope")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("NOK,ErrorFetchingStream", func(t *testing.T) {
|
|
|
|
var mockStore mocks.Store
|
|
|
|
mockStore.On("GetMediaSet", ctx, mediaSetID).Return(mediaSet, nil)
|
|
|
|
|
|
|
|
var youtubeClient mocks.YoutubeClient
|
|
|
|
youtubeClient.On("GetVideoContext", ctx, videoID).Return(video, nil)
|
|
|
|
youtubeClient.On("GetStreamContext", ctx, video, &video.Formats[0]).Return(nil, int64(0), errors.New("network failure"))
|
|
|
|
|
2022-01-05 18:49:21 +00:00
|
|
|
service := media.NewMediaSetService(&mockStore, &youtubeClient, nil, nil, wp, config.Config{}, logger)
|
2022-01-04 05:51:25 +00:00
|
|
|
_, err := service.GetVideo(ctx, mediaSetID)
|
|
|
|
assert.EqualError(t, err, "error fetching stream: network failure")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("NOK,ErrorPuttingObjectInFileStore", func(t *testing.T) {
|
|
|
|
var mockStore mocks.Store
|
|
|
|
mockStore.On("GetMediaSet", ctx, mediaSetID).Return(mediaSet, nil)
|
|
|
|
|
|
|
|
encodedContent := "a video stream"
|
|
|
|
reader := io.NopCloser(strings.NewReader(encodedContent))
|
|
|
|
|
|
|
|
var youtubeClient mocks.YoutubeClient
|
|
|
|
youtubeClient.On("GetVideoContext", ctx, videoID).Return(video, nil)
|
|
|
|
youtubeClient.On("GetStreamContext", ctx, video, &video.Formats[0]).Return(reader, int64(len(encodedContent)), nil)
|
|
|
|
|
|
|
|
var fileStore mocks.FileStore
|
|
|
|
fileStore.On("PutObject", ctx, mock.Anything, mock.Anything, videoMimeType).Return(int64(0), errors.New("error storing object"))
|
|
|
|
|
2022-01-05 18:49:21 +00:00
|
|
|
service := media.NewMediaSetService(&mockStore, &youtubeClient, &fileStore, nil, wp, config.Config{}, logger)
|
2022-01-04 05:51:25 +00:00
|
|
|
stream, err := service.GetVideo(ctx, mediaSetID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = stream.Next()
|
|
|
|
assert.EqualError(t, err, "error waiting for progress: error uploading to file store: error storing object")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("NOK,ErrorClosingStream", func(t *testing.T) {
|
|
|
|
var mockStore mocks.Store
|
|
|
|
mockStore.On("GetMediaSet", ctx, mediaSetID).Return(mediaSet, nil)
|
|
|
|
|
|
|
|
encodedContent := "a video stream"
|
|
|
|
reader := errorCloser{Reader: strings.NewReader(encodedContent)}
|
|
|
|
|
|
|
|
var youtubeClient mocks.YoutubeClient
|
|
|
|
youtubeClient.On("GetVideoContext", ctx, videoID).Return(video, nil)
|
|
|
|
youtubeClient.On("GetStreamContext", ctx, video, &video.Formats[0]).Return(reader, int64(len(encodedContent)), nil)
|
|
|
|
|
|
|
|
var fileStore mocks.FileStore
|
|
|
|
fileStore.On("PutObject", ctx, mock.Anything, mock.Anything, videoMimeType).Return(int64(len(encodedContent)), nil)
|
|
|
|
|
2022-01-05 18:49:21 +00:00
|
|
|
service := media.NewMediaSetService(&mockStore, &youtubeClient, &fileStore, nil, wp, config.Config{}, logger)
|
2022-01-04 05:51:25 +00:00
|
|
|
stream, err := service.GetVideo(ctx, mediaSetID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = stream.Next()
|
|
|
|
assert.EqualError(t, err, "error waiting for progress: error closing video stream: close error")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("NOK,ErrorGettingObjectURL", func(t *testing.T) {
|
|
|
|
var mockStore mocks.Store
|
|
|
|
mockStore.On("GetMediaSet", ctx, mediaSetID).Return(mediaSet, nil)
|
|
|
|
|
|
|
|
encodedContent := "a video stream"
|
|
|
|
reader := io.NopCloser(strings.NewReader(encodedContent))
|
|
|
|
|
|
|
|
var youtubeClient mocks.YoutubeClient
|
|
|
|
youtubeClient.On("GetVideoContext", ctx, videoID).Return(video, nil)
|
|
|
|
youtubeClient.On("GetStreamContext", ctx, video, &video.Formats[0]).Return(reader, int64(len(encodedContent)), nil)
|
|
|
|
|
|
|
|
var fileStore mocks.FileStore
|
|
|
|
fileStore.On("PutObject", ctx, mock.Anything, mock.Anything, videoMimeType).Return(int64(len(encodedContent)), nil)
|
|
|
|
fileStore.On("GetURL", ctx, mock.Anything).Return("", errors.New("URL error"))
|
|
|
|
|
2022-01-05 18:49:21 +00:00
|
|
|
service := media.NewMediaSetService(&mockStore, &youtubeClient, &fileStore, nil, wp, config.Config{}, logger)
|
2022-01-04 05:51:25 +00:00
|
|
|
stream, err := service.GetVideo(ctx, mediaSetID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = stream.Next()
|
|
|
|
assert.EqualError(t, err, "error waiting for progress: error getting object URL: URL error")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("NOK,ErrorUpdatingStore", func(t *testing.T) {
|
|
|
|
var mockStore mocks.Store
|
|
|
|
mockStore.On("GetMediaSet", ctx, mediaSetID).Return(mediaSet, nil)
|
|
|
|
mockStore.On("SetVideoUploaded", ctx, mock.Anything).Return(mediaSet, errors.New("boom"))
|
|
|
|
|
|
|
|
encodedContent := "a video stream"
|
|
|
|
reader := io.NopCloser(strings.NewReader(encodedContent))
|
|
|
|
|
|
|
|
var youtubeClient mocks.YoutubeClient
|
|
|
|
youtubeClient.On("GetVideoContext", ctx, videoID).Return(video, nil)
|
|
|
|
youtubeClient.On("GetStreamContext", ctx, video, &video.Formats[0]).Return(reader, int64(len(encodedContent)), nil)
|
|
|
|
|
|
|
|
var fileStore mocks.FileStore
|
|
|
|
fileStore.On("PutObject", ctx, mock.Anything, mock.Anything, videoMimeType).Return(int64(len(encodedContent)), nil)
|
|
|
|
fileStore.On("GetURL", ctx, mock.Anything).Return("a url", nil)
|
|
|
|
|
2022-01-05 18:49:21 +00:00
|
|
|
service := media.NewMediaSetService(&mockStore, &youtubeClient, &fileStore, nil, wp, config.Config{}, logger)
|
2022-01-04 05:51:25 +00:00
|
|
|
stream, err := service.GetVideo(ctx, mediaSetID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = stream.Next()
|
|
|
|
assert.EqualError(t, err, "error waiting for progress: error saving to store: boom")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("OK", func(t *testing.T) {
|
|
|
|
var mockStore mocks.Store
|
|
|
|
mockStore.On("GetMediaSet", ctx, mediaSetID).Return(mediaSet, nil)
|
|
|
|
mockStore.On("SetVideoUploaded", ctx, mock.Anything).Return(mediaSet, nil)
|
|
|
|
defer mockStore.AssertExpectations(t)
|
|
|
|
|
|
|
|
encodedContent := make([]byte, videoContentLength)
|
|
|
|
reader := io.NopCloser(bytes.NewReader(encodedContent))
|
|
|
|
|
|
|
|
var youtubeClient mocks.YoutubeClient
|
|
|
|
youtubeClient.On("GetVideoContext", ctx, videoID).Return(video, nil)
|
|
|
|
youtubeClient.On("GetStreamContext", ctx, video, &video.Formats[0]).Return(reader, videoContentLength, nil)
|
|
|
|
defer youtubeClient.AssertExpectations(t)
|
|
|
|
|
|
|
|
var fileStore mocks.FileStore
|
|
|
|
fileStore.On("PutObject", ctx, mock.Anything, mock.Anything, videoMimeType).
|
|
|
|
Run(func(args mock.Arguments) {
|
|
|
|
n, err := io.Copy(io.Discard, args[2].(io.Reader))
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, videoContentLength, n)
|
|
|
|
}).
|
|
|
|
Return(videoContentLength, nil)
|
|
|
|
fileStore.On("GetURL", ctx, mock.Anything).Return("a url", nil)
|
|
|
|
defer fileStore.AssertExpectations(t)
|
|
|
|
|
2022-01-05 18:49:21 +00:00
|
|
|
service := media.NewMediaSetService(&mockStore, &youtubeClient, &fileStore, nil, wp, config.Config{}, logger)
|
2022-01-04 05:51:25 +00:00
|
|
|
stream, err := service.GetVideo(ctx, mediaSetID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var (
|
|
|
|
lastPercentComplete float32
|
|
|
|
lastURL string
|
|
|
|
)
|
|
|
|
|
|
|
|
for {
|
|
|
|
progress, err := stream.Next()
|
|
|
|
if err != io.EOF {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.GreaterOrEqual(t, progress.PercentComplete, lastPercentComplete)
|
|
|
|
lastPercentComplete = progress.PercentComplete
|
|
|
|
lastURL = progress.URL
|
|
|
|
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, float32(100), lastPercentComplete)
|
|
|
|
assert.Equal(t, "a url", lastURL)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type errorCloser struct {
|
|
|
|
io.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c errorCloser) Close() error { return errors.New("close error") }
|
|
|
|
|
2022-01-03 19:59:59 +00:00
|
|
|
func TestGetVideoFromFileStore(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2022-01-05 18:49:21 +00:00
|
|
|
wp := media.NewTestWorkerPool()
|
2022-01-03 19:59:59 +00:00
|
|
|
logger := zap.NewNop().Sugar()
|
|
|
|
|
2022-01-04 05:51:25 +00:00
|
|
|
videoID := "video002"
|
2022-01-03 19:59:59 +00:00
|
|
|
mediaSetID := uuid.New()
|
|
|
|
mediaSet := store.MediaSet{
|
|
|
|
ID: mediaSetID,
|
|
|
|
YoutubeID: videoID,
|
|
|
|
VideoS3UploadedAt: sql.NullTime{Time: time.Now(), Valid: true},
|
|
|
|
VideoS3Key: sql.NullString{String: "videos/myvideo", Valid: true},
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("NOK,ErrorFetchingMediaSet", func(t *testing.T) {
|
|
|
|
var mockStore mocks.Store
|
|
|
|
mockStore.On("GetMediaSet", ctx, mediaSetID).Return(store.MediaSet{}, errors.New("database fail"))
|
|
|
|
|
2022-01-05 18:49:21 +00:00
|
|
|
service := media.NewMediaSetService(&mockStore, nil, nil, nil, wp, config.Config{}, logger)
|
2022-01-03 19:59:59 +00:00
|
|
|
_, err := service.GetVideo(ctx, mediaSetID)
|
|
|
|
require.EqualError(t, err, "error getting media set: database fail")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("NOK,ErrorGettingObjectURL", func(t *testing.T) {
|
|
|
|
var mockStore mocks.Store
|
|
|
|
mockStore.On("GetMediaSet", ctx, mediaSetID).Return(mediaSet, nil)
|
|
|
|
|
|
|
|
var fileStore mocks.FileStore
|
|
|
|
fileStore.On("GetURL", ctx, "videos/myvideo").Return("", errors.New("key missing"))
|
|
|
|
|
2022-01-05 18:49:21 +00:00
|
|
|
service := media.NewMediaSetService(&mockStore, nil, &fileStore, nil, wp, config.Config{}, logger)
|
2022-01-03 19:59:59 +00:00
|
|
|
_, err := service.GetVideo(ctx, mediaSetID)
|
|
|
|
require.EqualError(t, err, "error generating presigned URL: key missing")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("OK", func(t *testing.T) {
|
|
|
|
var mockStore mocks.Store
|
|
|
|
mockStore.On("GetMediaSet", ctx, mediaSetID).Return(mediaSet, nil)
|
|
|
|
|
|
|
|
const url = "https://www.example.com/audio"
|
|
|
|
var fileStore mocks.FileStore
|
|
|
|
fileStore.On("GetURL", ctx, "videos/myvideo").Return(url, nil)
|
|
|
|
|
2022-01-05 18:49:21 +00:00
|
|
|
service := media.NewMediaSetService(&mockStore, nil, &fileStore, nil, wp, config.Config{}, logger)
|
2022-01-03 19:59:59 +00:00
|
|
|
stream, err := service.GetVideo(ctx, mediaSetID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
progress, err := stream.Next()
|
|
|
|
assert.Equal(t, float32(100), progress.PercentComplete)
|
|
|
|
assert.Equal(t, url, progress.URL)
|
|
|
|
assert.Equal(t, io.EOF, err)
|
|
|
|
})
|
|
|
|
}
|