forked from mtusi2026/mtusi2026lab6
feat: add bounded observability and verified SLO incident alerts
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
package imageprocessor
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"image"
|
||||
"image/color"
|
||||
"image/png"
|
||||
"testing"
|
||||
|
||||
_ "golang.org/x/image/webp"
|
||||
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/pipeline"
|
||||
)
|
||||
|
||||
// sourcePNG creates a transparent synthetic source with controlled dimensions.
|
||||
func sourcePNG(t *testing.T, width, height int) []byte {
|
||||
t.Helper()
|
||||
pic := image.NewNRGBA(image.Rect(0, 0, width, height))
|
||||
pic.Set(0, 0, color.NRGBA{R: 255, A: 128})
|
||||
var data bytes.Buffer
|
||||
if err := png.Encode(&data, pic); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return data.Bytes()
|
||||
}
|
||||
|
||||
// TestVariantsDimensions verifies real JPEG/WebP output and white transparency composition.
|
||||
func TestVariantsDimensions(t *testing.T) {
|
||||
for _, size := range []struct {
|
||||
width int
|
||||
height int
|
||||
thumbWidth int
|
||||
thumbHeight int
|
||||
}{{320, 200, 256, 160}, {16, 8, 16, 8}, {200, 320, 160, 256}, {1600, 800, 256, 128}} {
|
||||
t.Run(fmt.Sprintf("%dx%d", size.width, size.height), func(t *testing.T) {
|
||||
processor, err := New()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
outputs, err := processor.Process(t.Context(), sourcePNG(t, size.width, size.height))
|
||||
if err != nil || len(outputs) != 3 {
|
||||
t.Fatalf("variants: %v", err)
|
||||
}
|
||||
for _, out := range outputs {
|
||||
decoded, format, err := image.Decode(bytes.NewReader(out.Bytes))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if decoded.Bounds().Dx() != out.Metadata.Width || decoded.Bounds().Dy() != out.Metadata.Height {
|
||||
t.Fatal("metadata dimensions differ")
|
||||
}
|
||||
if out.Metadata.Name == "thumbnail" && (out.Metadata.Width != size.thumbWidth || out.Metadata.Height != size.thumbHeight || format != "jpeg") {
|
||||
t.Fatal("thumbnail contract")
|
||||
}
|
||||
if out.Metadata.Name == "crop" && (out.Metadata.Width != 128 || out.Metadata.Height != 128 || format != "jpeg") {
|
||||
t.Fatal("crop contract")
|
||||
}
|
||||
if out.Metadata.Name == "optimized" && (max(out.Metadata.Width, out.Metadata.Height) > 1280 || format != "webp") {
|
||||
t.Fatal("optimized contract")
|
||||
}
|
||||
r, g, b, _ := decoded.At(decoded.Bounds().Max.X-1, decoded.Bounds().Max.Y-1).RGBA()
|
||||
if min(r, g, b) < 60000 {
|
||||
t.Fatal("alpha not composed on white")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestProcessorInvalidImage verifies malformed bytes are a permanent safe error.
|
||||
func TestProcessorInvalidImage(t *testing.T) {
|
||||
processor := &Processor{webp: "unused"}
|
||||
_, err := processor.Process(t.Context(), []byte("not an image"))
|
||||
if !errors.Is(err, pipeline.ErrInvalidImage) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestProcessorCancellation prevents decode after an expired job deadline.
|
||||
func TestProcessorCancellation(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
cancel()
|
||||
_, err := (&Processor{}).Process(ctx, sourcePNG(t, 10, 10))
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestProcessorEncoderFailure verifies a missing encoder is not a successful variant set.
|
||||
func TestProcessorEncoderFailure(t *testing.T) {
|
||||
_, err := (&Processor{webp: "/missing-encoder"}).Process(t.Context(), sourcePNG(t, 10, 10))
|
||||
if err == nil {
|
||||
t.Fatal("missing encoder accepted")
|
||||
}
|
||||
}
|
||||
|
||||
// TestProcessorRequiresEncoder validates the deployment dependency at startup.
|
||||
func TestProcessorRequiresEncoder(t *testing.T) {
|
||||
t.Setenv("PATH", t.TempDir())
|
||||
if _, err := New(); err == nil {
|
||||
t.Fatal("missing cwebp accepted")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user