load_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package integration
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "image/png"
  7. "io"
  8. "net/http"
  9. "os"
  10. "path"
  11. "path/filepath"
  12. "strings"
  13. "testing"
  14. "github.com/corona10/goimagehash"
  15. "github.com/imgproxy/imgproxy/v3"
  16. "github.com/imgproxy/imgproxy/v3/config"
  17. "github.com/imgproxy/imgproxy/v3/imagetype"
  18. "github.com/imgproxy/imgproxy/v3/testutil"
  19. "github.com/imgproxy/imgproxy/v3/vips"
  20. "github.com/stretchr/testify/suite"
  21. )
  22. const (
  23. similarityThreshold = 5 // Distance between images to be considered similar
  24. )
  25. type LoadTestSuite struct {
  26. suite.Suite
  27. ctx context.Context
  28. cancel context.CancelFunc
  29. testData *testutil.TestDataProvider
  30. testImagesPath string
  31. }
  32. // SetupSuite starts imgproxy instance server
  33. func (s *LoadTestSuite) SetupSuite() {
  34. s.testData = testutil.NewTestDataProvider(s.T())
  35. s.testImagesPath = s.testData.Path("test-images")
  36. s.ctx, s.cancel = context.WithCancel(s.T().Context())
  37. s.startImgproxy(s.ctx)
  38. }
  39. // TearDownSuite stops imgproxy instance server
  40. func (s *LoadTestSuite) TearDownSuite() {
  41. s.cancel()
  42. }
  43. // testLoadFolder fetches images iterates over images in the specified folder,
  44. // runs imgproxy on each image, and compares the result with the reference image
  45. // which is expected to be in the `integration` folder with the same name
  46. // but with `.png` extension.
  47. func (s *LoadTestSuite) testLoadFolder(folder string) {
  48. walkPath := path.Join(s.testImagesPath, folder)
  49. // Iterate over the files in the source folder
  50. err := filepath.Walk(walkPath, func(path string, info os.FileInfo, err error) error {
  51. s.Require().NoError(err)
  52. // Skip directories
  53. if info.IsDir() {
  54. return nil
  55. }
  56. // get the base name of the file (8-bpp.png)
  57. basePath := filepath.Base(path)
  58. // Replace the extension with .png
  59. referencePath := strings.TrimSuffix(basePath, filepath.Ext(basePath)) + ".png"
  60. // Construct the full path to the reference image (integration/ folder)
  61. referencePath = filepath.Join(s.testImagesPath, "integration", folder, referencePath)
  62. // Construct the source URL for imgproxy (no processing)
  63. sourceUrl := fmt.Sprintf("insecure/plain/local:///%s/%s@png", folder, basePath)
  64. imgproxyImageBytes := s.fetchImage(sourceUrl)
  65. imgproxyImage, err := png.Decode(bytes.NewReader(imgproxyImageBytes))
  66. s.Require().NoError(err, "Failed to decode PNG image from imgproxy for %s", basePath)
  67. referenceFile, err := os.Open(referencePath)
  68. s.Require().NoError(err)
  69. defer referenceFile.Close()
  70. referenceImage, err := png.Decode(referenceFile)
  71. s.Require().NoError(err, "Failed to decode PNG reference image for %s", referencePath)
  72. hash1, err := goimagehash.DifferenceHash(imgproxyImage)
  73. s.Require().NoError(err)
  74. hash2, err := goimagehash.DifferenceHash(referenceImage)
  75. s.Require().NoError(err)
  76. distance, err := hash1.Distance(hash2)
  77. s.Require().NoError(err)
  78. s.Require().LessOrEqual(distance, similarityThreshold,
  79. "Image %s differs from reference image %s by %d, which is greater than the allowed threshold of %d",
  80. basePath, referencePath, distance, similarityThreshold)
  81. return nil
  82. })
  83. s.Require().NoError(err)
  84. }
  85. // fetchImage fetches an image from the imgproxy server
  86. func (s *LoadTestSuite) fetchImage(path string) []byte {
  87. url := fmt.Sprintf("http://%s:%d/%s", bindHost, bindPort, path)
  88. resp, err := http.Get(url)
  89. s.Require().NoError(err, "Failed to fetch image from %s", url)
  90. defer resp.Body.Close()
  91. s.Require().Equal(http.StatusOK, resp.StatusCode, "Expected status code 200 OK, got %d, url: %s", resp.StatusCode, url)
  92. bytes, err := io.ReadAll(resp.Body)
  93. s.Require().NoError(err, "Failed to read response body from %s", url)
  94. return bytes
  95. }
  96. func (s *LoadTestSuite) startImgproxy(ctx context.Context) *imgproxy.Imgproxy {
  97. c, err := imgproxy.LoadConfigFromEnv(nil)
  98. s.Require().NoError(err)
  99. c.Server.Bind = ":" + fmt.Sprintf("%d", bindPort)
  100. c.Transport.Local.Root = s.testImagesPath
  101. c.Server.LogMemStats = true
  102. config.MaxAnimationFrames = 999
  103. config.DevelopmentErrorsMode = true
  104. i, err := imgproxy.New(ctx, c)
  105. s.Require().NoError(err)
  106. go func() {
  107. err = i.StartServer(ctx)
  108. if err != nil {
  109. s.T().Errorf("Imgproxy server exited with error: %v", err)
  110. }
  111. }()
  112. return i
  113. }
  114. // TestLoadSaveToPng ensures that our load pipeline works,
  115. // including standard and custom loaders. For each source image
  116. // in the folder, it does the passthrough request through imgproxy:
  117. // no processing, just convert format of the source file to png.
  118. // Then, it compares the result with the reference image.
  119. func (s *LoadTestSuite) TestLoadSaveToPng() {
  120. testCases := []struct {
  121. name string
  122. imageType imagetype.Type
  123. folderName string
  124. }{
  125. {"GIF", imagetype.GIF, "gif"},
  126. {"JPEG", imagetype.JPEG, "jpg"},
  127. {"HEIC", imagetype.HEIC, "heif"},
  128. {"JXL", imagetype.JXL, "jxl"},
  129. {"SVG", imagetype.SVG, "svg"},
  130. {"TIFF", imagetype.TIFF, "tiff"},
  131. {"WEBP", imagetype.WEBP, "webp"},
  132. {"BMP", imagetype.BMP, "bmp"},
  133. {"ICO", imagetype.ICO, "ico"},
  134. }
  135. for _, tc := range testCases {
  136. s.T().Run(tc.name, func(t *testing.T) {
  137. if vips.SupportsLoad(tc.imageType) {
  138. s.testLoadFolder(tc.folderName)
  139. } else {
  140. t.Skipf("%s format not supported by VIPS", tc.name)
  141. }
  142. })
  143. }
  144. }
  145. func TestIntegration(t *testing.T) {
  146. suite.Run(t, new(LoadTestSuite))
  147. }