1
0

load_test.go 4.8 KB

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