feat(store): available ref, update mode and services-by-repo for the webhook (#8)

This commit is contained in:
2026-10-12 20:40:04 +02:00
parent 417ef315c2
commit cfc6ea6713
6 changed files with 240 additions and 15 deletions
@@ -0,0 +1,149 @@
//go:build integration
package store
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/shcizo/package-updater/internal/master/model"
"github.com/shcizo/package-updater/internal/protocol"
)
func TestServicesByRepo(t *testing.T) {
s := openTestStore(t)
ctx := context.Background()
id := newAgent(t, s, "node-2")
other := protocol.Service{Kind: protocol.KindCompose, ComposeProject: "o", ComposeService: "o", Image: "gitea.shcizo.se/shcizo/Slusk:1", Status: "running"}
gone := protocol.Service{Kind: protocol.KindCompose, ComposeProject: "g", ComposeService: "g", Image: "gitea.shcizo.se/shcizo/slusk:0.9", Status: "running"}
require.NoError(t, s.ApplySnapshot(ctx, id, worker, []protocol.Service{slusk, other, gone, adhoc}, t0))
require.NoError(t, s.RemoveService(ctx, id, "", gone.Key(), t0))
got, err := s.ServicesByRepo(ctx, "gitea.shcizo.se/shcizo/slusk")
require.NoError(t, err)
assert.Equal(t, []string{"slusk/web"}, names(got), "removed rows and rows of other (case-sensitive) repos are excluded")
assert.Equal(t, "node-2", got[0].AgentName)
none, err := s.ServicesByRepo(ctx, "nope")
require.NoError(t, err)
assert.Empty(t, none)
}
func TestSetAvailable_RoundTrip(t *testing.T) {
s := openTestStore(t)
ctx := context.Background()
_, svc := jobFixture(t, s, "a")
require.NoError(t, s.SetAvailable(ctx, svc, model.Available{Ref: "r:2", Source: model.SourceWebhook, At: t0}))
got, err := s.GetService(ctx, svc)
require.NoError(t, err)
assert.Equal(t, "r:2", got.AvailableRef)
assert.Equal(t, model.SourceWebhook, got.AvailableSource)
require.NotNil(t, got.AvailableAt)
assert.True(t, t0.Equal(*got.AvailableAt))
}
func TestSetUpdateMode(t *testing.T) {
s := openTestStore(t)
ctx := context.Background()
agent, svc := jobFixture(t, s, "a")
require.NoError(t, s.SetUpdateMode(ctx, svc, model.ModeAuto))
got, err := s.GetService(ctx, svc)
require.NoError(t, err)
assert.Equal(t, model.ModeAuto, got.UpdateMode)
require.NoError(t, s.SetUpdateMode(ctx, svc, model.ModeManual))
got, _ = s.GetService(ctx, svc)
assert.Equal(t, model.ModeManual, got.UpdateMode)
assert.ErrorIs(t, s.SetUpdateMode(ctx, 99999, model.ModeAuto), model.ErrNotFound)
require.NoError(t, s.RemoveService(ctx, agent, "", slusk.Key(), t0))
assert.ErrorIs(t, s.SetUpdateMode(ctx, svc, model.ModeAuto), model.ErrNotFound)
}
func TestUpsertKeepsUpdateMode(t *testing.T) {
s := openTestStore(t)
ctx := context.Background()
agent, svc := jobFixture(t, s, "a")
got, err := s.GetService(ctx, svc)
require.NoError(t, err)
assert.Equal(t, model.ModeManual, got.UpdateMode, "a new service starts manual")
require.NoError(t, s.SetUpdateMode(ctx, svc, model.ModeAuto))
changed := slusk
changed.Image = "gitea.shcizo.se/shcizo/slusk:1.5.0"
require.NoError(t, s.UpsertService(ctx, agent, "", changed, t0.Add(time.Minute)))
require.NoError(t, s.ApplySnapshot(ctx, agent, worker, []protocol.Service{changed}, t0.Add(2*time.Minute)))
got, err = s.GetService(ctx, svc)
require.NoError(t, err)
assert.Equal(t, model.ModeAuto, got.UpdateMode)
}
func TestFinishJob_ClearsAvailableOnlyWhenJobIsNewer(t *testing.T) {
succeed := model.JobFinish{Status: model.JobSucceeded, Ref: "r:2@sha256:d", StartedAt: t0, FinishedAt: t0}
tests := []struct {
name string
availableAt time.Time // job is created at t0
finish model.JobFinish
wantCleared bool
}{
{"suggestion before the job", t0.Add(-time.Minute), succeed, true},
{"suggestion at the same instant", t0, succeed, true},
{"suggestion after the job was created", t0.Add(time.Minute), succeed, false},
{"failed job keeps the suggestion", t0.Add(-time.Minute),
model.JobFinish{Status: model.JobFailed, Error: "boom", StartedAt: t0, FinishedAt: t0}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := openTestStore(t)
ctx := context.Background()
agent, svc := jobFixture(t, s, "a")
require.NoError(t, s.SetAvailable(ctx, svc, model.Available{Ref: "r:2", Source: model.SourceWebhook, At: tt.availableAt}))
j, err := s.CreateJob(ctx, newJob(svc, agent))
require.NoError(t, err)
ok, err := s.FinishJob(ctx, agent, j.ID, tt.finish)
require.NoError(t, err)
require.True(t, ok)
got, err := s.GetService(ctx, svc)
require.NoError(t, err)
if tt.wantCleared {
assert.Empty(t, got.AvailableRef)
assert.Empty(t, got.AvailableSource)
assert.Nil(t, got.AvailableAt)
} else {
assert.Equal(t, "r:2", got.AvailableRef)
assert.Equal(t, model.SourceWebhook, got.AvailableSource)
assert.NotNil(t, got.AvailableAt)
}
})
}
}
func TestFinishJob_WithoutAvailableLeavesItEmpty(t *testing.T) {
s := openTestStore(t)
ctx := context.Background()
agent, svc := jobFixture(t, s, "a")
j, err := s.CreateJob(ctx, newJob(svc, agent))
require.NoError(t, err)
ok, err := s.FinishJob(ctx, agent, j.ID, model.JobFinish{Status: model.JobSucceeded, Ref: "r:1@sha256:d", StartedAt: t0, FinishedAt: t0})
require.NoError(t, err)
require.True(t, ok)
got, err := s.GetService(ctx, svc)
require.NoError(t, err)
assert.Empty(t, got.AvailableRef)
assert.Nil(t, got.AvailableAt)
assert.Equal(t, "r:1@sha256:d", got.DesiredRef)
}