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.

282 lines
7.3 KiB

  1. package pipelines
  2. import (
  3. "github.com/geplauder/lithium/storage"
  4. "image"
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. // pipeline deserialization
  11. func TestPipelineDeserialization(t *testing.T) {
  12. t.Run("Image pipeline deserialization is successful", func(t *testing.T) {
  13. const Payload string = `{
  14. "name": "example pipeline",
  15. "type": 0,
  16. "removeMetadata": false,
  17. "steps": [
  18. {
  19. "name": "resize image",
  20. "type": 0
  21. },
  22. {
  23. "name": "compress image",
  24. "type": 1
  25. }
  26. ]
  27. }`
  28. values := DeserializePipelines([][]byte{[]byte(Payload)})
  29. assert.Equal(t, 1, len(values), "Output should contain one element")
  30. assert.Equal(t, "example pipeline", values[0].GetName())
  31. assert.Equal(t, Image, values[0].GetType())
  32. })
  33. t.Run("Video pipelines deserialization is successful", func(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. values := DeserializePipelines([][]byte{[]byte(Payload)})
  50. assert.Equal(t, 1, len(values), "Output should contain one element")
  51. assert.Equal(t, "example pipeline", values[0].GetName())
  52. assert.Equal(t, Video, values[0].GetType())
  53. })
  54. }
  55. // image pipeline steps
  56. func TestExecuteSteps(t *testing.T) {
  57. t.Run("Pipeline executes with no steps", func(t *testing.T) {
  58. const Bucket string = "pipeline_test_01"
  59. const Payload string = `{
  60. "name": "example pipeline",
  61. "type": 1,
  62. "removeMetadata": false,
  63. "steps": []
  64. }`
  65. wd, _ := os.Getwd()
  66. pipe := DeserializePipelines([][]byte{[]byte(Payload)})[0]
  67. storageProvider := storage.GetFileSystemStorageProvider("test", "..")
  68. // copy test file to storage bucket
  69. _, err := storageProvider.StoreExisting(Bucket, "source.jpg", filepath.Join(wd, "../tests/files/900x900.jpg"))
  70. assert.Nil(t, err, "Test file should be readable")
  71. assert.FileExists(t, storageProvider.GetPath(Bucket, "source.jpg"))
  72. // run pipeline steps
  73. dest, err := pipe.Run("source.jpg", Bucket, storageProvider)
  74. assert.Nil(t, err)
  75. assert.FileExists(t, storageProvider.GetPath(Bucket, dest))
  76. // clean up
  77. os.Remove(storageProvider.GetPath(Bucket, "source.jpg"))
  78. os.Remove(storageProvider.GetPath(Bucket, dest))
  79. })
  80. t.Run("Image resizing is successful", func(t *testing.T) {
  81. const Bucket string = "pipeline_test_02"
  82. const Payload string = `{
  83. "name": "example pipeline",
  84. "type": 1,
  85. "removeMetadata": false,
  86. "steps": [
  87. {
  88. "name": "resize image",
  89. "type": 0,
  90. "options": {
  91. "width": 1280,
  92. "height": 720,
  93. "upscale": false
  94. }
  95. }
  96. ]
  97. }`
  98. wd, _ := os.Getwd()
  99. pipe := DeserializePipelines([][]byte{[]byte(Payload)})[0]
  100. storageProvider := storage.GetFileSystemStorageProvider("test", "..")
  101. // copy test file to storage bucket
  102. _, err := storageProvider.StoreExisting(Bucket, "source.jpg", filepath.Join(wd, "../tests/files/900x900.jpg"))
  103. assert.Nil(t, err, "Test file should be readable")
  104. assert.FileExists(t, storageProvider.GetPath(Bucket, "source.jpg"))
  105. // run pipeline steps
  106. dest, err := pipe.Run("source.jpg", Bucket, storageProvider)
  107. assert.Nil(t, err)
  108. assert.FileExists(t, storageProvider.GetPath(Bucket, dest))
  109. // read image config
  110. file, err := storageProvider.OpenFile(Bucket, dest)
  111. assert.Nil(t, err)
  112. imgConf, _, err := image.DecodeConfig(file)
  113. assert.Nil(t, err)
  114. assert.Equal(t, 1280, imgConf.Width)
  115. assert.Equal(t, 720, imgConf.Height)
  116. // clean up
  117. os.Remove(storageProvider.GetPath(Bucket, "source.jpg"))
  118. os.Remove(storageProvider.GetPath(Bucket, dest))
  119. })
  120. t.Run("Image rotation step is successful", func(t *testing.T) {
  121. const Bucket string = "pipeline_test_03"
  122. const Payload string = `{
  123. "name": "example pipeline",
  124. "type": 1,
  125. "removeMetadata": false,
  126. "steps": [
  127. {
  128. "name": "rotate image",
  129. "type": 1,
  130. "options": {
  131. "angle": 90.0
  132. }
  133. }
  134. ]
  135. }`
  136. wd, _ := os.Getwd()
  137. pipe := DeserializePipelines([][]byte{[]byte(Payload)})[0]
  138. storageProvider := storage.GetFileSystemStorageProvider("test", "..")
  139. // copy test file to storage bucket
  140. _, err := storageProvider.StoreExisting(Bucket, "source.jpg", filepath.Join(wd, "../tests/files/800x500.jpg"))
  141. assert.Nil(t, err, "Test file should be readable")
  142. assert.FileExists(t, storageProvider.GetPath(Bucket, "source.jpg"))
  143. // run pipeline steps
  144. dest, err := pipe.Run("source.jpg", Bucket, storageProvider)
  145. assert.Nil(t, err)
  146. assert.FileExists(t, storageProvider.GetPath(Bucket, dest))
  147. // read image config
  148. file, err := storageProvider.OpenFile(Bucket, dest)
  149. assert.Nil(t, err)
  150. imgConf, _, err := image.DecodeConfig(file)
  151. assert.Nil(t, err)
  152. assert.Equal(t, 500, imgConf.Width)
  153. assert.Equal(t, 800, imgConf.Height)
  154. // clean up
  155. os.Remove(storageProvider.GetPath(Bucket, "source.jpg"))
  156. os.Remove(storageProvider.GetPath(Bucket, dest))
  157. })
  158. t.Run("Image grayscale step is successful", func(t *testing.T) {
  159. const Bucket string = "pipeline_test_05"
  160. const Payload string = `{
  161. "name": "example pipeline",
  162. "type": 1,
  163. "removeMetadata": false,
  164. "steps": [
  165. {
  166. "name": "grayscale",
  167. "type": 2
  168. }
  169. ]
  170. }`
  171. wd, _ := os.Getwd()
  172. pipe := DeserializePipelines([][]byte{[]byte(Payload)})[0]
  173. storageProvider := storage.GetFileSystemStorageProvider("test", "..")
  174. // copy test file to storage bucket
  175. _, err := storageProvider.StoreExisting(Bucket, "source.jpg", filepath.Join(wd, "../tests/files/900x900.jpg"))
  176. assert.Nil(t, err, "Test file should be readable")
  177. assert.FileExists(t, storageProvider.GetPath(Bucket, "source.jpg"))
  178. // run pipeline steps
  179. dest, err := pipe.Run("source.jpg", Bucket, storageProvider)
  180. assert.Nil(t, err)
  181. assert.FileExists(t, storageProvider.GetPath(Bucket, dest))
  182. // read image config
  183. file, err := storageProvider.OpenFile(Bucket, dest)
  184. assert.Nil(t, err)
  185. imgConf, _, err := image.DecodeConfig(file)
  186. assert.Nil(t, err)
  187. assert.Equal(t, 900, imgConf.Width)
  188. assert.Equal(t, 900, imgConf.Height)
  189. // clean up
  190. os.Remove(storageProvider.GetPath(Bucket, "source.jpg"))
  191. os.Remove(storageProvider.GetPath(Bucket, dest))
  192. })
  193. }
  194. // output options
  195. func TestEncoding(t *testing.T) {
  196. const Bucket string = "pipeline_test_04"
  197. const Payload string = `{
  198. "name": "example pipeline",
  199. "type": 1,
  200. "removeMetadata": false,
  201. "steps": [
  202. {
  203. "name": "resize image",
  204. "type": 0,
  205. "options": {
  206. "width": 1280,
  207. "height": 720,
  208. "upscale": false
  209. }
  210. }
  211. ],
  212. "output": {
  213. "quality": 50
  214. }
  215. }`
  216. t.Run("Image encoding with jpeg quality is successful", func(t *testing.T) {
  217. wd, _ := os.Getwd()
  218. pipe := DeserializePipelines([][]byte{[]byte(Payload)})[0]
  219. storageProvider := storage.GetFileSystemStorageProvider("test", "..")
  220. // copy test file to storage bucket
  221. _, err := storageProvider.StoreExisting(Bucket, "source.jpg", filepath.Join(wd, "../tests/files/900x900.jpg"))
  222. assert.Nil(t, err, "Test file should be readable")
  223. assert.FileExists(t, storageProvider.GetPath(Bucket, "source.jpg"))
  224. // run pipeline steps
  225. dest, err := pipe.Run("source.jpg", Bucket, storageProvider)
  226. assert.Nil(t, err)
  227. assert.FileExists(t, storageProvider.GetPath(Bucket, dest))
  228. // clean up
  229. os.Remove(storageProvider.GetPath(Bucket, "source.jpg"))
  230. os.Remove(storageProvider.GetPath(Bucket, dest))
  231. })
  232. }