1
0

load_test.go 4.9 KB

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