|
|
@ -1,12 +1,19 @@ |
|
|
|
package pipelines |
|
|
|
|
|
|
|
import ( |
|
|
|
"github.com/geplauder/lithium/storage" |
|
|
|
"image" |
|
|
|
"os" |
|
|
|
"path/filepath" |
|
|
|
"testing" |
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert" |
|
|
|
) |
|
|
|
|
|
|
|
func TestImagePipelineDeserialization(t *testing.T) { |
|
|
|
// pipeline deserialization
|
|
|
|
|
|
|
|
func TestPipelineDeserialization(t *testing.T) { |
|
|
|
t.Run("Image pipeline deserialization is successful", func(t *testing.T) { |
|
|
|
const Payload string = `{ |
|
|
|
"name": "example pipeline", |
|
|
|
"type": 0, |
|
|
@ -23,16 +30,14 @@ func TestImagePipelineDeserialization(t *testing.T) { |
|
|
|
] |
|
|
|
}` |
|
|
|
|
|
|
|
t.Run("Image pipeline deserialization is successful", func(t *testing.T) { |
|
|
|
values := DeserializePipelines([][]byte{[]byte(Payload)}) |
|
|
|
|
|
|
|
assert.Equal(t, 1, len(values), "Output should contain one element") |
|
|
|
assert.Equal(t, "example pipeline", values[0].GetName()) |
|
|
|
assert.Equal(t, Image, values[0].GetType()) |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
func TestVideoPipelineDeserialization(t *testing.T) { |
|
|
|
t.Run("Video pipelines deserialization is successful", func(t *testing.T) { |
|
|
|
const Payload string = `{ |
|
|
|
"name": "example pipeline", |
|
|
|
"type": 1, |
|
|
@ -49,7 +54,6 @@ func TestVideoPipelineDeserialization(t *testing.T) { |
|
|
|
] |
|
|
|
}` |
|
|
|
|
|
|
|
t.Run("Video pipelines deserialization is successful", func(t *testing.T) { |
|
|
|
values := DeserializePipelines([][]byte{[]byte(Payload)}) |
|
|
|
|
|
|
|
assert.Equal(t, 1, len(values), "Output should contain one element") |
|
|
@ -57,3 +61,304 @@ func TestVideoPipelineDeserialization(t *testing.T) { |
|
|
|
assert.Equal(t, Video, values[0].GetType()) |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
// image pipeline steps
|
|
|
|
|
|
|
|
func TestExecuteSteps(t *testing.T) { |
|
|
|
t.Run("Pipeline executes with no steps", func(t *testing.T) { |
|
|
|
const Bucket string = "pipeline_test_01" |
|
|
|
const Payload string = `{ |
|
|
|
"name": "example pipeline", |
|
|
|
"type": 1, |
|
|
|
"removeMetadata": false, |
|
|
|
"steps": [] |
|
|
|
}` |
|
|
|
|
|
|
|
wd, _ := os.Getwd() |
|
|
|
pipe := DeserializePipelines([][]byte{[]byte(Payload)})[0] |
|
|
|
|
|
|
|
storageProvider := storage.GetFileSystemStorageProvider("test", "..") |
|
|
|
|
|
|
|
// copy test file to storage bucket
|
|
|
|
_, err := storageProvider.StoreExisting(Bucket, "source.jpg", filepath.Join(wd, "../tests/files/900x900.jpg")) |
|
|
|
assert.Nil(t, err, "Test file should be readable") |
|
|
|
assert.FileExists(t, storageProvider.GetPath(Bucket, "source.jpg")) |
|
|
|
|
|
|
|
// run pipeline steps
|
|
|
|
dest, err := pipe.Run("source.jpg", Bucket, storageProvider) |
|
|
|
assert.Nil(t, err) |
|
|
|
assert.FileExists(t, storageProvider.GetPath(Bucket, dest)) |
|
|
|
|
|
|
|
// clean up
|
|
|
|
os.Remove(storageProvider.GetPath(Bucket, "source.jpg")) |
|
|
|
os.Remove(storageProvider.GetPath(Bucket, dest)) |
|
|
|
}) |
|
|
|
|
|
|
|
t.Run("Image resizing is successful", func(t *testing.T) { |
|
|
|
const Bucket string = "pipeline_test_02" |
|
|
|
const Payload string = `{ |
|
|
|
"name": "example pipeline", |
|
|
|
"type": 1, |
|
|
|
"removeMetadata": false, |
|
|
|
"steps": [ |
|
|
|
{ |
|
|
|
"name": "resize image", |
|
|
|
"type": 0, |
|
|
|
"options": { |
|
|
|
"width": 1280, |
|
|
|
"height": 720, |
|
|
|
"upscale": false |
|
|
|
} |
|
|
|
} |
|
|
|
] |
|
|
|
}` |
|
|
|
|
|
|
|
wd, _ := os.Getwd() |
|
|
|
pipe := DeserializePipelines([][]byte{[]byte(Payload)})[0] |
|
|
|
|
|
|
|
storageProvider := storage.GetFileSystemStorageProvider("test", "..") |
|
|
|
|
|
|
|
// copy test file to storage bucket
|
|
|
|
_, err := storageProvider.StoreExisting(Bucket, "source.jpg", filepath.Join(wd, "../tests/files/900x900.jpg")) |
|
|
|
assert.Nil(t, err, "Test file should be readable") |
|
|
|
assert.FileExists(t, storageProvider.GetPath(Bucket, "source.jpg")) |
|
|
|
|
|
|
|
// run pipeline steps
|
|
|
|
dest, err := pipe.Run("source.jpg", Bucket, storageProvider) |
|
|
|
assert.Nil(t, err) |
|
|
|
assert.FileExists(t, storageProvider.GetPath(Bucket, dest)) |
|
|
|
|
|
|
|
// read image config
|
|
|
|
file, err := storageProvider.OpenFile(Bucket, dest) |
|
|
|
assert.Nil(t, err) |
|
|
|
|
|
|
|
imgConf, _, err := image.DecodeConfig(file) |
|
|
|
assert.Nil(t, err) |
|
|
|
|
|
|
|
assert.Equal(t, 1280, imgConf.Width) |
|
|
|
assert.Equal(t, 720, imgConf.Height) |
|
|
|
|
|
|
|
// clean up
|
|
|
|
os.Remove(storageProvider.GetPath(Bucket, "source.jpg")) |
|
|
|
os.Remove(storageProvider.GetPath(Bucket, dest)) |
|
|
|
}) |
|
|
|
|
|
|
|
t.Run("Image rotation step is successful", func(t *testing.T) { |
|
|
|
const Bucket string = "pipeline_test_03" |
|
|
|
const Payload string = `{ |
|
|
|
"name": "example pipeline", |
|
|
|
"type": 1, |
|
|
|
"removeMetadata": false, |
|
|
|
"steps": [ |
|
|
|
{ |
|
|
|
"name": "rotate image", |
|
|
|
"type": 1, |
|
|
|
"options": { |
|
|
|
"angle": 90.0 |
|
|
|
} |
|
|
|
} |
|
|
|
] |
|
|
|
}` |
|
|
|
|
|
|
|
wd, _ := os.Getwd() |
|
|
|
pipe := DeserializePipelines([][]byte{[]byte(Payload)})[0] |
|
|
|
|
|
|
|
storageProvider := storage.GetFileSystemStorageProvider("test", "..") |
|
|
|
|
|
|
|
// copy test file to storage bucket
|
|
|
|
_, err := storageProvider.StoreExisting(Bucket, "source.jpg", filepath.Join(wd, "../tests/files/800x500.jpg")) |
|
|
|
assert.Nil(t, err, "Test file should be readable") |
|
|
|
assert.FileExists(t, storageProvider.GetPath(Bucket, "source.jpg")) |
|
|
|
|
|
|
|
// run pipeline steps
|
|
|
|
dest, err := pipe.Run("source.jpg", Bucket, storageProvider) |
|
|
|
assert.Nil(t, err) |
|
|
|
assert.FileExists(t, storageProvider.GetPath(Bucket, dest)) |
|
|
|
|
|
|
|
// read image config
|
|
|
|
file, err := storageProvider.OpenFile(Bucket, dest) |
|
|
|
assert.Nil(t, err) |
|
|
|
|
|
|
|
imgConf, _, err := image.DecodeConfig(file) |
|
|
|
assert.Nil(t, err) |
|
|
|
|
|
|
|
assert.Equal(t, 500, imgConf.Width) |
|
|
|
assert.Equal(t, 800, imgConf.Height) |
|
|
|
|
|
|
|
// clean up
|
|
|
|
os.Remove(storageProvider.GetPath(Bucket, "source.jpg")) |
|
|
|
os.Remove(storageProvider.GetPath(Bucket, dest)) |
|
|
|
}) |
|
|
|
|
|
|
|
t.Run("Image flip step is successful", func(t *testing.T) { |
|
|
|
const Bucket string = "pipeline_test_06" |
|
|
|
const Payload string = `{ |
|
|
|
"name": "example pipeline", |
|
|
|
"type": 1, |
|
|
|
"removeMetadata": false, |
|
|
|
"steps": [ |
|
|
|
{ |
|
|
|
"name": "flip image", |
|
|
|
"type": 2, |
|
|
|
"options": { |
|
|
|
"direction": "h" |
|
|
|
} |
|
|
|
} |
|
|
|
] |
|
|
|
}` |
|
|
|
|
|
|
|
wd, _ := os.Getwd() |
|
|
|
pipe := DeserializePipelines([][]byte{[]byte(Payload)})[0] |
|
|
|
|
|
|
|
storageProvider := storage.GetFileSystemStorageProvider("test", "..") |
|
|
|
|
|
|
|
// copy test file to storage bucket
|
|
|
|
_, err := storageProvider.StoreExisting(Bucket, "source.jpg", filepath.Join(wd, "../tests/files/800x500.jpg")) |
|
|
|
assert.Nil(t, err, "Test file should be readable") |
|
|
|
assert.FileExists(t, storageProvider.GetPath(Bucket, "source.jpg")) |
|
|
|
|
|
|
|
// run pipeline steps
|
|
|
|
dest, err := pipe.Run("source.jpg", Bucket, storageProvider) |
|
|
|
assert.Nil(t, err) |
|
|
|
assert.FileExists(t, storageProvider.GetPath(Bucket, dest)) |
|
|
|
|
|
|
|
// read image config
|
|
|
|
file, err := storageProvider.OpenFile(Bucket, dest) |
|
|
|
assert.Nil(t, err) |
|
|
|
|
|
|
|
imgConf, _, err := image.DecodeConfig(file) |
|
|
|
assert.Nil(t, err) |
|
|
|
|
|
|
|
assert.Equal(t, 800, imgConf.Width) |
|
|
|
assert.Equal(t, 500, imgConf.Height) |
|
|
|
|
|
|
|
// clean up
|
|
|
|
os.Remove(storageProvider.GetPath(Bucket, "source.jpg")) |
|
|
|
os.Remove(storageProvider.GetPath(Bucket, dest)) |
|
|
|
}) |
|
|
|
|
|
|
|
t.Run("Image flip step direction validation is successful", func(t *testing.T) { |
|
|
|
const Bucket string = "pipeline_test_06" |
|
|
|
const Payload string = `{ |
|
|
|
"name": "example pipeline", |
|
|
|
"type": 1, |
|
|
|
"removeMetadata": false, |
|
|
|
"steps": [ |
|
|
|
{ |
|
|
|
"name": "flip image", |
|
|
|
"type": 2, |
|
|
|
"options": { |
|
|
|
"direction": "f" |
|
|
|
} |
|
|
|
} |
|
|
|
] |
|
|
|
}` |
|
|
|
|
|
|
|
wd, _ := os.Getwd() |
|
|
|
pipe := DeserializePipelines([][]byte{[]byte(Payload)})[0] |
|
|
|
|
|
|
|
storageProvider := storage.GetFileSystemStorageProvider("test", "..") |
|
|
|
|
|
|
|
// copy test file to storage bucket
|
|
|
|
_, err := storageProvider.StoreExisting(Bucket, "source.jpg", filepath.Join(wd, "../tests/files/800x500.jpg")) |
|
|
|
assert.Nil(t, err, "Test file should be readable") |
|
|
|
assert.FileExists(t, storageProvider.GetPath(Bucket, "source.jpg")) |
|
|
|
|
|
|
|
// run pipeline steps
|
|
|
|
_, err = pipe.Run("source.jpg", Bucket, storageProvider) |
|
|
|
assert.EqualError(t, err, "invalid flip direction: f") |
|
|
|
|
|
|
|
// clean up
|
|
|
|
os.Remove(storageProvider.GetPath(Bucket, "source.jpg")) |
|
|
|
}) |
|
|
|
|
|
|
|
t.Run("Image grayscale step is successful", func(t *testing.T) { |
|
|
|
const Bucket string = "pipeline_test_05" |
|
|
|
const Payload string = `{ |
|
|
|
"name": "example pipeline", |
|
|
|
"type": 1, |
|
|
|
"removeMetadata": false, |
|
|
|
"steps": [ |
|
|
|
{ |
|
|
|
"name": "grayscale", |
|
|
|
"type": 3 |
|
|
|
} |
|
|
|
] |
|
|
|
}` |
|
|
|
|
|
|
|
wd, _ := os.Getwd() |
|
|
|
pipe := DeserializePipelines([][]byte{[]byte(Payload)})[0] |
|
|
|
|
|
|
|
storageProvider := storage.GetFileSystemStorageProvider("test", "..") |
|
|
|
|
|
|
|
// copy test file to storage bucket
|
|
|
|
_, err := storageProvider.StoreExisting(Bucket, "source.jpg", filepath.Join(wd, "../tests/files/900x900.jpg")) |
|
|
|
assert.Nil(t, err, "Test file should be readable") |
|
|
|
assert.FileExists(t, storageProvider.GetPath(Bucket, "source.jpg")) |
|
|
|
|
|
|
|
// run pipeline steps
|
|
|
|
dest, err := pipe.Run("source.jpg", Bucket, storageProvider) |
|
|
|
assert.Nil(t, err) |
|
|
|
assert.FileExists(t, storageProvider.GetPath(Bucket, dest)) |
|
|
|
|
|
|
|
// read image config
|
|
|
|
file, err := storageProvider.OpenFile(Bucket, dest) |
|
|
|
assert.Nil(t, err) |
|
|
|
|
|
|
|
imgConf, _, err := image.DecodeConfig(file) |
|
|
|
assert.Nil(t, err) |
|
|
|
|
|
|
|
assert.Equal(t, 900, imgConf.Width) |
|
|
|
assert.Equal(t, 900, imgConf.Height) |
|
|
|
|
|
|
|
// clean up
|
|
|
|
os.Remove(storageProvider.GetPath(Bucket, "source.jpg")) |
|
|
|
os.Remove(storageProvider.GetPath(Bucket, dest)) |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
// output options
|
|
|
|
|
|
|
|
func TestEncoding(t *testing.T) { |
|
|
|
const Bucket string = "pipeline_test_04" |
|
|
|
const Payload string = `{ |
|
|
|
"name": "example pipeline", |
|
|
|
"type": 1, |
|
|
|
"removeMetadata": false, |
|
|
|
"steps": [ |
|
|
|
{ |
|
|
|
"name": "resize image", |
|
|
|
"type": 0, |
|
|
|
"options": { |
|
|
|
"width": 1280, |
|
|
|
"height": 720, |
|
|
|
"upscale": false |
|
|
|
} |
|
|
|
} |
|
|
|
], |
|
|
|
"output": { |
|
|
|
"quality": 50 |
|
|
|
} |
|
|
|
}` |
|
|
|
|
|
|
|
t.Run("Image encoding with jpeg quality is successful", func(t *testing.T) { |
|
|
|
wd, _ := os.Getwd() |
|
|
|
pipe := DeserializePipelines([][]byte{[]byte(Payload)})[0] |
|
|
|
|
|
|
|
storageProvider := storage.GetFileSystemStorageProvider("test", "..") |
|
|
|
|
|
|
|
// copy test file to storage bucket
|
|
|
|
_, err := storageProvider.StoreExisting(Bucket, "source.jpg", filepath.Join(wd, "../tests/files/900x900.jpg")) |
|
|
|
assert.Nil(t, err, "Test file should be readable") |
|
|
|
assert.FileExists(t, storageProvider.GetPath(Bucket, "source.jpg")) |
|
|
|
|
|
|
|
// run pipeline steps
|
|
|
|
dest, err := pipe.Run("source.jpg", Bucket, storageProvider) |
|
|
|
assert.Nil(t, err) |
|
|
|
assert.FileExists(t, storageProvider.GetPath(Bucket, dest)) |
|
|
|
|
|
|
|
// clean up
|
|
|
|
os.Remove(storageProvider.GetPath(Bucket, "source.jpg")) |
|
|
|
os.Remove(storageProvider.GetPath(Bucket, dest)) |
|
|
|
}) |
|
|
|
} |