Micro-service for file storage and processing written in Go
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

217 lines
5.5 KiB

  1. package pipelines
  2. import (
  3. "github.com/geplauder/lithium/storage"
  4. "os"
  5. "path/filepath"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. // pipeline deserialization
  10. func TestImagePipelineDeserialization(t *testing.T) {
  11. const Payload string = `{
  12. "name": "example pipeline",
  13. "type": 0,
  14. "removeMetadata": false,
  15. "steps": [
  16. {
  17. "name": "resize image",
  18. "type": 0
  19. },
  20. {
  21. "name": "compress image",
  22. "type": 1
  23. }
  24. ]
  25. }`
  26. t.Run("Image pipeline deserialization is successful", func(t *testing.T) {
  27. values := DeserializePipelines([][]byte{[]byte(Payload)})
  28. assert.Equal(t, 1, len(values), "Output should contain one element")
  29. assert.Equal(t, "example pipeline", values[0].GetName())
  30. assert.Equal(t, Image, values[0].GetType())
  31. })
  32. }
  33. func TestVideoPipelineDeserialization(t *testing.T) {
  34. const Payload string = `{
  35. "name": "example pipeline",
  36. "type": 1,
  37. "removeMetadata": false,
  38. "steps": [
  39. {
  40. "name": "resize image",
  41. "type": 0
  42. },
  43. {
  44. "name": "compress image",
  45. "type": 1
  46. }
  47. ]
  48. }`
  49. t.Run("Video pipelines deserialization is successful", func(t *testing.T) {
  50. values := DeserializePipelines([][]byte{[]byte(Payload)})
  51. assert.Equal(t, 1, len(values), "Output should contain one element")
  52. assert.Equal(t, "example pipeline", values[0].GetName())
  53. assert.Equal(t, Video, values[0].GetType())
  54. })
  55. }
  56. // image pipeline steps
  57. func TestNoSteps(t *testing.T) {
  58. const Payload string = `{
  59. "name": "example pipeline",
  60. "type": 1,
  61. "removeMetadata": false,
  62. "steps": []
  63. }`
  64. const Bucket string = "pipeline_test_04"
  65. t.Run("Pipeline execution is successful", func(t *testing.T) {
  66. wd, _ := os.Getwd()
  67. pipe := DeserializePipelines([][]byte{[]byte(Payload)})[0]
  68. storageProvider := storage.GetFileSystemStorageProvider("test", "..")
  69. // copy test file to storage bucket
  70. _, err := storageProvider.StoreExisting(Bucket, "source.jpg", filepath.Join(wd, "../tests/files/900x900.jpg"))
  71. assert.Nil(t, err, "Test file should be readable")
  72. assert.FileExists(t, storageProvider.GetPath(Bucket, "source.jpg"))
  73. // run pipeline steps
  74. dest, err := pipe.Run("source.jpg", Bucket, storageProvider)
  75. assert.Nil(t, err)
  76. assert.FileExists(t, storageProvider.GetPath(Bucket, dest))
  77. // clean up
  78. os.Remove(storageProvider.GetPath(Bucket, "source.jpg"))
  79. os.Remove(storageProvider.GetPath(Bucket, dest))
  80. })
  81. }
  82. func TestResizeImage(t *testing.T) {
  83. const Payload string = `{
  84. "name": "example pipeline",
  85. "type": 1,
  86. "removeMetadata": false,
  87. "steps": [
  88. {
  89. "name": "resize image",
  90. "type": 0,
  91. "options": {
  92. "width": 1280,
  93. "height": 720,
  94. "upscale": false
  95. }
  96. }
  97. ]
  98. }`
  99. const Bucket string = "pipeline_test_01"
  100. t.Run("Image resizing is successful", func(t *testing.T) {
  101. wd, _ := os.Getwd()
  102. pipe := DeserializePipelines([][]byte{[]byte(Payload)})[0]
  103. storageProvider := storage.GetFileSystemStorageProvider("test", "..")
  104. _, err := storageProvider.StoreExisting(Bucket, "source.jpg", filepath.Join(wd, "../tests/files/900x900.jpg"))
  105. assert.Nil(t, err, "Test file should be readable")
  106. assert.FileExists(t, storageProvider.GetPath(Bucket, "source.jpg"))
  107. dest, err := pipe.Run("source.jpg", Bucket, storageProvider)
  108. assert.Nil(t, err)
  109. assert.FileExists(t, storageProvider.GetPath(Bucket, dest))
  110. // clean up
  111. os.Remove(storageProvider.GetPath(Bucket, "source.jpg"))
  112. os.Remove(storageProvider.GetPath(Bucket, dest))
  113. })
  114. }
  115. func TestRotateImage(t *testing.T) {
  116. const Payload string = `{
  117. "name": "example pipeline",
  118. "type": 1,
  119. "removeMetadata": false,
  120. "steps": [
  121. {
  122. "name": "rotate image",
  123. "type": 1,
  124. "options": {
  125. "angle": 90.0
  126. }
  127. }
  128. ]
  129. }`
  130. const Bucket string = "pipeline_test_03"
  131. t.Run("Image rotating is successful", func(t *testing.T) {
  132. wd, _ := os.Getwd()
  133. pipe := DeserializePipelines([][]byte{[]byte(Payload)})[0]
  134. storageProvider := storage.GetFileSystemStorageProvider("test", "..")
  135. _, err := storageProvider.StoreExisting(Bucket, "source.jpg", filepath.Join(wd, "../tests/files/900x900.jpg"))
  136. assert.Nil(t, err, "Test file should be readable")
  137. assert.FileExists(t, storageProvider.GetPath(Bucket, "source.jpg"))
  138. dest, err := pipe.Run("source.jpg", Bucket, storageProvider)
  139. assert.Nil(t, err)
  140. assert.FileExists(t, storageProvider.GetPath(Bucket, dest))
  141. // clean up
  142. os.Remove(storageProvider.GetPath(Bucket, "source.jpg"))
  143. os.Remove(storageProvider.GetPath(Bucket, dest))
  144. })
  145. }
  146. // output options
  147. func TestEncodeImageWithJpegQuality(t *testing.T) {
  148. const Payload string = `{
  149. "name": "example pipeline",
  150. "type": 1,
  151. "removeMetadata": false,
  152. "steps": [
  153. {
  154. "name": "resize image",
  155. "type": 0,
  156. "options": {
  157. "width": 1280,
  158. "height": 720,
  159. "upscale": false
  160. }
  161. }
  162. ],
  163. "output": {
  164. "quality": 50
  165. }
  166. }`
  167. const Bucket string = "pipeline_test_02"
  168. t.Run("Image encoding with jpeg quality is successful", func(t *testing.T) {
  169. wd, _ := os.Getwd()
  170. pipe := DeserializePipelines([][]byte{[]byte(Payload)})[0]
  171. storageProvider := storage.GetFileSystemStorageProvider("test", "..")
  172. _, err := storageProvider.StoreExisting(Bucket, "source.jpg", filepath.Join(wd, "../tests/files/900x900.jpg"))
  173. assert.Nil(t, err, "Test file should be readable")
  174. assert.FileExists(t, storageProvider.GetPath(Bucket, "source.jpg"))
  175. dest, err := pipe.Run("source.jpg", Bucket, storageProvider)
  176. assert.Nil(t, err)
  177. assert.FileExists(t, storageProvider.GetPath(Bucket, dest))
  178. // clean up
  179. os.Remove(storageProvider.GetPath(Bucket, "source.jpg"))
  180. os.Remove(storageProvider.GetPath(Bucket, dest))
  181. })
  182. }