test: ffmpeg processor tests

This commit is contained in:
Adrian Shum 2022-09-16 14:21:56 +08:00 committed by GitHub
parent bb48a1e717
commit d9c697a0b2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 165 additions and 15 deletions

View file

@ -1,6 +1,7 @@
# imagorvideo
[![Test Status](https://github.com/cshum/imagorvideo/workflows/test/badge.svg)](https://github.com/cshum/imagorvideo/actions/workflows/test.yml)
[![Coverage Status](https://img.shields.io/coveralls/github/cshum/imagorvideo)](https://coveralls.io/github/cshum/imagorvideo?branch=master)
[![Docker Hub](https://img.shields.io/badge/docker-shumc/imagorvideo-blue.svg)](https://hub.docker.com/r/shumc/imagorvideo/)
[![GitHub Container Registry](https://ghcr-badge.herokuapp.com/cshum/imagorvideo/latest_tag?trim=major&label=ghcr.io&ignore=next,master&color=%23007ec6)](https://github.com/cshum/imagorvideo/pkgs/container/imagorvideo)

17
config_test.go Normal file
View file

@ -0,0 +1,17 @@
package imagorvideo
import (
"github.com/cshum/imagor"
"github.com/cshum/imagor/config"
"github.com/stretchr/testify/assert"
"testing"
)
func TestConfig(t *testing.T) {
srv := config.CreateServer([]string{
"-ffmpeg-fallback-image", "https://foo.com/bar.jpg",
}, Config)
app := srv.App.(*imagor.Imagor)
processor := app.Processors[0].(*Processor)
assert.Equal(t, "https://foo.com/bar.jpg", processor.FallbackImage)
}

15
ffmpeg/errors_test.go Normal file
View file

@ -0,0 +1,15 @@
package ffmpeg
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestErrors(t *testing.T) {
assert.Equal(t, "ffmpeg: cannot allocate memory", ErrNoMem.Error())
assert.Equal(t, "ffmpeg: end of file", ErrEOF.Error())
assert.Equal(t, "ffmpeg: unknown error occurred", ErrUnknown.Error())
assert.Equal(t, "ffmpeg: decoder not found", ErrDecoderNotFound.Error())
assert.Equal(t, "ffmpeg: invalid data found when processing input", ErrInvalidData.Error())
assert.Equal(t, "ffmpeg: video or cover art size exceeds maximum allowed dimensions", ErrTooBig.Error())
}

View file

@ -26,7 +26,7 @@ var files = []string{
var baseDir = "../testdata/"
func TestAVContextMeta(t *testing.T) {
func TestAVContext(t *testing.T) {
vips.Startup(nil)
SetFFmpegLogLevel(AVLogDebug)
logger := zap.NewExample()
@ -43,7 +43,7 @@ func TestAVContextMeta(t *testing.T) {
}
})
require.NoError(t, os.MkdirAll(baseDir+"golden/meta", 0755))
require.NoError(t, os.MkdirAll(baseDir+"golden/result", 0755))
require.NoError(t, os.MkdirAll(baseDir+"golden/export", 0755))
t.Parallel()
for _, filename := range files {
t.Run(filename, func(t *testing.T) {
@ -87,12 +87,3 @@ func TestAVContextMeta(t *testing.T) {
})
}
}
func TestErrors(t *testing.T) {
assert.Equal(t, "ffmpeg: cannot allocate memory", ErrNoMem.Error())
assert.Equal(t, "ffmpeg: end of file", ErrEOF.Error())
assert.Equal(t, "ffmpeg: unknown error occurred", ErrUnknown.Error())
assert.Equal(t, "ffmpeg: decoder not found", ErrDecoderNotFound.Error())
assert.Equal(t, "ffmpeg: invalid data found when processing input", ErrInvalidData.Error())
assert.Equal(t, "ffmpeg: video or cover art size exceeds maximum allowed dimensions", ErrTooBig.Error())
}

View file

@ -57,9 +57,6 @@ func (p *Processor) Process(ctx context.Context, in *imagor.Blob, params imagorp
if err == nil || out != nil {
return
}
if _, ok := err.(imagor.ErrForward); ok {
return
}
// fallback image on error
out = imagor.NewBlobFromBytes(transPixel)
if p.FallbackImage != "" {
@ -85,8 +82,15 @@ func (p *Processor) Process(ctx context.Context, in *imagor.Blob, params imagorp
if reader, size, err = in.NewReader(); err != nil {
return
}
if size <= 0 {
// force read full file if size unknown
_ = reader.Close()
reader = nil
}
default:
if reader, size, err = in.NewReadSeeker(); err != nil {
}
if reader == nil {
if reader, size, err = in.NewReadSeeker(); err != nil || size <= 0 {
// write to temp file if read seeker not available
if reader, _, err = in.NewReader(); err != nil {
return

121
processor_test.go Normal file
View file

@ -0,0 +1,121 @@
package imagorvideo
import (
"context"
"fmt"
"github.com/cshum/imagor"
"github.com/cshum/imagor/imagorpath"
"github.com/cshum/imagor/storage/filestorage"
"github.com/cshum/imagor/vips"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"reflect"
"runtime"
"testing"
)
var testDataDir string
func init() {
_, b, _, _ := runtime.Caller(0)
testDataDir = filepath.Join(filepath.Dir(b), "./testdata")
}
type test struct {
name string
path string
}
func TestProcessor(t *testing.T) {
v := vips.NewProcessor(vips.WithDebug(true))
require.NoError(t, v.Startup(context.Background()))
t.Cleanup(func() {
require.NoError(t, v.Shutdown(context.Background()))
})
var resultDir = filepath.Join(testDataDir, "golden/result")
doGoldenTests(t, resultDir, []test{
{name: "mkv", path: "fit-in/100x100/everybody-betray-me.mkv"},
{name: "mkv meta", path: "meta/everybody-betray-me.mkv"},
{name: "mp4", path: "fit-in/100x100/schizo_0.mp4"},
{name: "mp4 90", path: "fit-in/100x100/schizo_90.mp4"},
{name: "mp4 180", path: "fit-in/100x100/schizo_180.mp4"},
{name: "mp4 270", path: "fit-in/100x100/schizo_270.mp4"},
{name: "image", path: "fit-in/100x100/demo.png"},
}, WithDebug(true))
}
func doGoldenTests(t *testing.T, resultDir string, tests []test, opts ...Option) {
resStorage := filestorage.New(resultDir,
filestorage.WithSaveErrIfExists(true))
loader := filestorage.New(testDataDir)
loaders := []imagor.Loader{
loader,
loaderFunc(func(r *http.Request, image string) (blob *imagor.Blob, err error) {
image, _ = loader.Path(image)
return imagor.NewBlob(func() (reader io.ReadCloser, size int64, err error) {
// force read full file by 0 size
reader, err = os.Open(image)
return
}), nil
}),
}
for i, loader := range loaders {
app := imagor.New(
imagor.WithLoaders(loader),
imagor.WithUnsafe(true),
imagor.WithDebug(true),
imagor.WithLogger(zap.NewExample()),
imagor.WithProcessors(NewProcessor(opts...), vips.NewProcessor()),
)
require.NoError(t, app.Startup(context.Background()))
t.Cleanup(func() {
assert.NoError(t, app.Shutdown(context.Background()))
})
for _, tt := range tests {
t.Run(fmt.Sprintf("%s-%d", tt.name, i+1), func(t *testing.T) {
w := httptest.NewRecorder()
ctx, cancel := context.WithCancel(context.Background())
req := httptest.NewRequest(
http.MethodGet, fmt.Sprintf("/unsafe/%s", tt.path), nil).WithContext(ctx)
app.ServeHTTP(w, req)
cancel()
assert.Equal(t, 200, w.Code)
b := imagor.NewBlobFromBytes(w.Body.Bytes())
_ = resStorage.Put(context.Background(), tt.path, b)
path := filepath.Join(resultDir, imagorpath.Normalize(tt.path, nil))
bc := imagor.NewBlobFromFile(path)
buf, err := bc.ReadAll()
require.NoError(t, err)
if reflect.DeepEqual(buf, w.Body.Bytes()) {
return
}
img1, err := vips.LoadImageFromFile(path, nil)
require.NoError(t, err)
img2, err := vips.LoadImageFromBuffer(w.Body.Bytes(), nil)
require.NoError(t, err)
require.Equal(t, img1.Width(), img2.Width(), "width mismatch")
require.Equal(t, img1.Height(), img2.Height(), "height mismatch")
buf1, err := img1.ExportWebp(nil)
require.NoError(t, err)
buf2, err := img2.ExportWebp(nil)
require.NoError(t, err)
require.True(t, reflect.DeepEqual(buf1, buf2), "image mismatch")
})
}
}
}
type loaderFunc func(r *http.Request, image string) (blob *imagor.Blob, err error)
func (f loaderFunc) Get(r *http.Request, image string) (*imagor.Blob, error) {
return f(r, image)
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

View file

@ -0,0 +1 @@
{"orientation":1,"duration":7407000000,"width":640,"height":480,"has_video":true,"has_audio":true,"has_alpha":false}