forked from mtusi2026/mtusi2026lab5
127 lines
3.4 KiB
Go
127 lines
3.4 KiB
Go
//go:build pipeline
|
|
|
|
package studenttests_test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/gob"
|
|
"image"
|
|
"image/color"
|
|
"image/jpeg"
|
|
"image/png"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"golang.org/x/image/webp"
|
|
)
|
|
|
|
func TestPipelineImageVersions(t *testing.T) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
|
defer cancel()
|
|
|
|
binary := filepath.Join(t.TempDir(), "worker")
|
|
build := exec.CommandContext(ctx, "go", "build", "-tags=nodynamic", "-o", binary, "../cmd/worker")
|
|
if out, err := build.CombinedOutput(); err != nil {
|
|
t.Fatalf("build worker: %v\n%s", err, out)
|
|
}
|
|
|
|
src := image.NewNRGBA(image.Rect(0, 0, 320, 200))
|
|
for y := 0; y < 200; y++ {
|
|
for x := 0; x < 320; x++ {
|
|
src.SetNRGBA(x, y, color.NRGBA{
|
|
R: uint8(x*17 + y*3),
|
|
G: uint8(x*5 + y*11),
|
|
B: uint8(x * y), A: 255,
|
|
})
|
|
}
|
|
}
|
|
|
|
var source bytes.Buffer
|
|
if err := png.Encode(&source, src); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
sourcePath := filepath.Join(t.TempDir(), "source.png")
|
|
if err := os.WriteFile(sourcePath, source.Bytes(), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Decode the child process protocol without exporting worker internals.
|
|
type variant struct {
|
|
Name, ContentType string
|
|
Data []byte
|
|
Width, Height int
|
|
}
|
|
process := func(version string) []variant {
|
|
t.Helper()
|
|
args := []string{"-process-image", "40000000"}
|
|
if version != "" {
|
|
args = append(args, "-pipeline-version", version)
|
|
}
|
|
cmd := exec.CommandContext(ctx, binary, args...)
|
|
input, err := os.Open(sourcePath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer input.Close()
|
|
cmd.Stdin = input
|
|
var stderr bytes.Buffer
|
|
cmd.Stderr = &stderr
|
|
out, err := cmd.Output()
|
|
if err != nil {
|
|
t.Fatalf("process v%s: %v: %s", version, err, stderr.String())
|
|
}
|
|
|
|
var variants []variant
|
|
if err := gob.NewDecoder(bytes.NewReader(out)).Decode(&variants); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(variants) != 3 {
|
|
t.Fatalf("expected three variants, got %d", len(variants))
|
|
}
|
|
for i, name := range []string{"thumbnail", "crop", "optimized"} {
|
|
v := variants[i]
|
|
var cfg image.Config
|
|
var err error
|
|
width, height, contentType := 256, 160, "image/jpeg"
|
|
if name == "optimized" {
|
|
width, height, contentType = 320, 200, "image/webp"
|
|
cfg, err = webp.DecodeConfig(bytes.NewReader(v.Data))
|
|
} else {
|
|
if name == "crop" {
|
|
width, height = 128, 128
|
|
}
|
|
cfg, err = jpeg.DecodeConfig(bytes.NewReader(v.Data))
|
|
}
|
|
if err != nil || v.Name != name || v.ContentType != contentType ||
|
|
v.Width != width || v.Height != height || cfg.Width != width || cfg.Height != height {
|
|
t.Fatalf("invalid v%s %s: metadata=%dx%d, decoded=%dx%d, error=%v",
|
|
version, name, v.Width, v.Height, cfg.Width, cfg.Height, err)
|
|
}
|
|
}
|
|
return variants
|
|
}
|
|
|
|
v1, v2 := process("1"), process("2")
|
|
defaultVersion := process("")
|
|
for i := range v1 {
|
|
if !bytes.Equal(v1[i].Data, defaultVersion[i].Data) {
|
|
t.Fatal("default processing differs from v1")
|
|
}
|
|
wantEqual := v1[i].Name != "optimized"
|
|
if bytes.Equal(v1[i].Data, v2[i].Data) != wantEqual {
|
|
t.Fatalf("expected only optimized bytes to differ: %s", v1[i].Name)
|
|
}
|
|
}
|
|
|
|
cmd := exec.CommandContext(ctx, binary, "-process-image", "40000000", "-pipeline-version", "3")
|
|
cmd.Stdin = bytes.NewReader(source.Bytes())
|
|
if out, err := cmd.Output(); err == nil || len(out) != 0 {
|
|
t.Fatal("unsupported version must fail without a result")
|
|
}
|
|
}
|