sonda/internal/images/job_test.go
tgiachi a880b87a9e chore: move the module path to git.kmalloc.org/squid/sonda
Migrating hosting from Codeberg to git.kmalloc.org. Renames the
module path in go.mod, every internal import, and the OCI image
source label in the Dockerfile. Build and full test suite verified
green after the rename.
2026-11-15 17:30:29 +01:00

304 lines
11 KiB
Go

package images
import (
"context"
"errors"
"image/color"
"os"
"path/filepath"
"sync"
"sync/atomic"
"testing"
"time"
"git.kmalloc.org/squid/sonda/internal/jobs"
)
type fakeLister struct {
mu sync.Mutex
ids []int64
refresh []bool
}
func (f *fakeLister) AlbumsToBuild(_ context.Context, after int64, limit int, refresh bool, _ time.Time) ([]int64, error) {
f.mu.Lock()
defer f.mu.Unlock()
f.refresh = append(f.refresh, refresh)
var out []int64
for _, id := range f.ids {
if id > after && len(out) < limit {
out = append(out, id)
}
}
return out, nil
}
func (f *fakeLister) CountAlbumsToBuild(context.Context, bool, time.Time) (int, error) {
f.mu.Lock()
defer f.mu.Unlock()
return len(f.ids), nil
}
type jobRig struct {
*remoteRig
lister *fakeLister
job *jobs.Job
}
// newJobRig has albums 1 to 6, all in the same folder with no picture, and an archive that
// has a cover for each.
func newJobRig(t *testing.T) *jobRig {
t.Helper()
r := &jobRig{remoteRig: newRemoteRig(t), lister: &fakeLister{}}
for id := int64(1); id <= 6; id++ {
r.src.files[id] = []File{{Root: r.root, RelPath: "Album One/01.mp3"}}
r.lister.ids = append(r.lister.ids, id)
}
r.job = NewCoversJob(r.svc, r.lister)
return r
}
func waitDone(t *testing.T, j *jobs.Job) jobs.Status {
t.Helper()
deadline := time.Now().Add(20 * time.Second)
for {
if st := j.Status(); !st.Running {
return st
}
if time.Now().After(deadline) {
t.Fatal("the job did not finish")
}
time.Sleep(5 * time.Millisecond)
}
}
func TestTheJobBuildsTheCoverOfEveryAlbumAndSaysWhatItFound(t *testing.T) {
r := newJobRig(t)
// Album 3 has a picture in its folder; the others are served by the archive, except 6.
r.src.files[3] = []File{{Root: r.root, RelPath: "Album One/01.mp3"}}
r.write(t, "Album One/cover.jpg", jpegBytes(t, 300, 300, color.White))
calls := int32(0)
if err := r.job.Start(context.Background(), jobs.Options{}); err != nil {
t.Fatal(err)
}
st := waitDone(t, r.job)
_ = calls
if st.Last == nil || st.Last.Checked != 6 || st.LastError != "" {
t.Fatalf("status = %+v", st)
}
// Every album shares the folder with a cover.jpg, so the files serve all six.
if st.Last.Outcomes[OutcomeFromFiles] != 6 || st.Last.Outcomes[OutcomeFromArchive] != 0 || st.Last.Outcomes[OutcomeNone] != 0 || st.Last.Failed != 0 {
t.Errorf("summary = %+v", *st.Last)
}
for id := 1; id <= 6; id++ {
if rec := r.store.recs[key("album", int64(id))]; rec.Status != StatusOK {
t.Errorf("album %d: record %+v", id, rec)
}
}
if st.StartedAt.IsZero() || st.LastFinishedAt.IsZero() || st.Running {
t.Errorf("times or running wrong: %+v", st)
}
}
func TestTheJobFallsBackToTheArchiveAndCountsWhatHasNoCover(t *testing.T) {
r := newJobRig(t)
r.remote.set(Found{Data: jpegBytes(t, 600, 600, color.White)}, nil)
if err := r.job.Start(context.Background(), jobs.Options{}); err != nil {
t.Fatal(err)
}
st := waitDone(t, r.job)
if st.Last.Outcomes[OutcomeFromArchive] != 6 || st.Last.Outcomes[OutcomeFromFiles] != 0 {
t.Errorf("summary = %+v, want six from the archive", *st.Last)
}
r2 := newJobRig(t)
r2.remote.set(Found{}, ErrNoImage)
_ = r2.job.Start(context.Background(), jobs.Options{})
if st := waitDone(t, r2.job); st.Last.Outcomes[OutcomeNone] != 6 {
t.Errorf("summary = %+v, want six without a cover", *st.Last)
}
}
func TestAlbumsThatAreAlreadyDoneAreNotBuiltAgainUnlessRefreshIsAsked(t *testing.T) {
r := newJobRig(t)
r.remote.set(Found{Data: jpegBytes(t, 600, 600, color.White)}, nil)
_ = r.job.Start(context.Background(), jobs.Options{})
waitDone(t, r.job)
if n := atomic.LoadInt32(&r.remote.calls); n != 6 {
t.Fatalf("the archive was asked %d times, want 6", n)
}
// Run again: the lister (the database, in production) says nothing is left; the job asks for the
// albums that still need work, and a refresh asks for all of them.
_ = r.job.Start(context.Background(), jobs.Options{})
waitDone(t, r.job)
if n := atomic.LoadInt32(&r.remote.calls); n != 6 {
t.Errorf("a second run asked the archive again for covers it has: %d calls", n)
}
_ = r.job.Start(context.Background(), jobs.Options{Refresh: true})
st := waitDone(t, r.job)
if n := atomic.LoadInt32(&r.remote.calls); n != 12 {
t.Errorf("a refresh asked the archive %d times in all, want 12", n)
}
if st.Last.Checked != 6 {
t.Errorf("summary = %+v", *st.Last)
}
last := r.lister.refresh[len(r.lister.refresh)-1]
if !last {
t.Error("the refresh option was not given to the list of albums")
}
}
func TestTheJobGivesUpWhenTheArchiveKeepsFailing(t *testing.T) {
r := newJobRig(t)
r.remote.set(Found{}, ErrSourceUnavailable)
_ = r.job.Start(context.Background(), jobs.Options{})
st := waitDone(t, r.job)
if st.Last == nil || st.Last.Failed != jobs.MaxConsecutiveFailures || st.LastError == "" || st.Last.Checked != jobs.MaxConsecutiveFailures {
t.Errorf("status = %+v, want it to give up after %d failures in a row with a reason", st, jobs.MaxConsecutiveFailures)
}
if n := atomic.LoadInt32(&r.remote.calls); n != int32(jobs.MaxConsecutiveFailures) {
t.Errorf("the archive was asked %d times after it kept failing", n)
}
}
func TestAnOutageThatPassesDoesNotStopTheJob(t *testing.T) {
r := newJobRig(t)
r.remote.set(Found{}, ErrSourceUnavailable)
// It fails on the first album and works on the rest.
var seen int32
r.svc.SetAlbumRemote(remoteFunc(func(ctx context.Context, id int64) (Found, error) {
if atomic.AddInt32(&seen, 1) == 1 {
return Found{}, ErrSourceUnavailable
}
return Found{Data: jpegBytes(t, 400, 400, color.White)}, nil
}))
_ = r.job.Start(context.Background(), jobs.Options{})
st := waitDone(t, r.job)
if st.Last.Checked != 6 || st.Last.Failed != 1 || st.Last.Outcomes[OutcomeFromArchive] != 5 || st.LastError != "" {
t.Errorf("status = %+v", st)
}
}
type remoteFunc func(ctx context.Context, id int64) (Found, error)
func (f remoteFunc) AlbumImage(ctx context.Context, id int64) (Found, error) { return f(ctx, id) }
func TestAnAlbumThatWentIsSkippedNotCountedAsAFailure(t *testing.T) {
r := newJobRig(t)
delete(r.src.files, 2) // the album has nothing to play any more
_ = r.job.Start(context.Background(), jobs.Options{})
st := waitDone(t, r.job)
if st.Last.Skipped != 1 || st.Last.Failed != 0 || st.Last.Checked != 6 {
t.Errorf("summary = %+v", *st.Last)
}
}
// ---- what a refresh must never do ----
func TestARefreshWithTheArchiveOffKeepsTheCoversThatCameFromIt(t *testing.T) {
r := newJobRig(t)
r.remote.set(Found{Data: jpegBytes(t, 600, 600, color.RGBA{0, 90, 0, 255})}, nil)
_ = r.job.Start(context.Background(), jobs.Options{})
waitDone(t, r.job)
before := r.store.recs[key("album", 1)]
if before.Source != SourceCoverArt {
t.Fatalf("record = %+v", before)
}
// The admin switches the provider off (not purged) and refreshes.
r.remote.set(Found{}, ErrProviderDisabled)
r.now = r.now.Add(time.Hour)
_ = r.job.Start(context.Background(), jobs.Options{Refresh: true})
st := waitDone(t, r.job)
after := r.store.recs[key("album", 1)]
if after.Status != StatusOK || after.Source != SourceCoverArt || after.ETag != before.ETag {
t.Errorf("record = %+v, want the archive cover kept", after)
}
if _, err := os.Stat(filepath.Join(r.data, "images", "album-remote", "1-large.jpg")); err != nil {
t.Errorf("the file of an archive cover went when the provider was switched off: %v", err)
}
if st.Last.Failed != 0 {
t.Errorf("summary = %+v", *st.Last)
}
// An album that had none stays with none, of course.
if got := r.store.recs[key("album", 2)]; got.Status != StatusOK {
t.Errorf("album 2 = %+v", got)
}
}
func TestACoverFromTheArchiveWhoseProviderIsOffIsNotSearchedAgainAtEveryRequest(t *testing.T) {
r := newRemoteRig(t)
r.rig.read(t, 1, Large) // from the archive
r.remote.set(Found{}, ErrProviderDisabled)
r.now = r.now.Add(CoverTTL + time.Hour)
r.rig.read(t, 1, Large) // kept, and its date renewed
reads := atomic.LoadInt32(&r.reads)
for i := 0; i < 3; i++ {
r.rig.read(t, 1, Large)
}
if got := atomic.LoadInt32(&r.reads); got != reads {
t.Errorf("the files were searched again %d times within the new period", got-reads)
}
}
func TestAnUnreachableLibraryIsAFailureNotNoPicture(t *testing.T) {
r := newRemoteRig(t)
r.write(t, "Album One/cover.jpg", jpegBytes(t, 300, 300, color.RGBA{200, 0, 0, 255}))
first := r.rig.read(t, 1, Large)
// The mount goes away: nothing under the root can be opened.
if err := os.RemoveAll(r.root); err != nil {
t.Fatal(err)
}
r.store.clear("album", 1) // a rescan, or a refresh, wants to look again
_, err := r.svc.AlbumCover(context.Background(), 1, Large)
if !errors.Is(err, ErrFilesUnreadable) {
t.Fatalf("err = %v, want ErrFilesUnreadable", err)
}
if rec := r.store.recs[key("album", 1)]; rec.Status == StatusNone {
t.Errorf("record = %+v: an unreadable library must not become 'no cover'", rec)
}
if n := atomic.LoadInt32(&r.remote.calls); n != 0 {
t.Errorf("the archive was asked %d times about files that could not even be read", n)
}
if entries, _ := os.ReadDir(filepath.Join(r.data, "images", "album")); len(entries) != 2 {
t.Errorf("%d files left, want the two sizes still there", len(entries))
}
_ = first
}
func TestARefreshOfAnUnreachableLibraryGivesUpInsteadOfWipingEverything(t *testing.T) {
r := newJobRig(t)
r.write(t, "Album One/cover.jpg", jpegBytes(t, 300, 300, color.White))
_ = r.job.Start(context.Background(), jobs.Options{})
waitDone(t, r.job)
if err := os.RemoveAll(r.root); err != nil {
t.Fatal(err)
}
_ = r.job.Start(context.Background(), jobs.Options{Refresh: true})
st := waitDone(t, r.job)
if st.Last.Failed != jobs.MaxConsecutiveFailures || st.LastError == "" || st.Last.Outcomes[OutcomeNone] != 0 {
t.Errorf("status = %+v, want it to give up with failures and not count 'none'", st)
}
for id := int64(1); id <= 6; id++ {
if rec := r.store.recs[key("album", id)]; rec.Status != StatusOK {
t.Errorf("album %d: %+v, want its cover kept", id, rec)
}
}
}
func TestADeadProviderIsSeenEvenWhenOldCoversCouldBeServed(t *testing.T) {
r := newJobRig(t)
r.remote.set(Found{Data: jpegBytes(t, 600, 600, color.White)}, nil)
_ = r.job.Start(context.Background(), jobs.Options{})
waitDone(t, r.job)
// Every archive cover is old now, and the archive is down.
r.now = r.now.Add(CoverTTL + time.Hour)
r.remote.set(Found{}, ErrSourceUnavailable)
atomic.StoreInt32(&r.remote.calls, 0)
_ = r.job.Start(context.Background(), jobs.Options{})
st := waitDone(t, r.job)
if st.Last.Failed != jobs.MaxConsecutiveFailures || st.LastError == "" || st.Last.Outcomes[OutcomeFromArchive] != 0 {
t.Errorf("status = %+v, want it to notice the outage and give up", st)
}
if n := atomic.LoadInt32(&r.remote.calls); n != int32(jobs.MaxConsecutiveFailures) {
t.Errorf("the archive was asked %d times", n)
}
}