Files
package-updater/internal/master/store/services_integration_test.go
T
shcizo 7e4b927293 fix(master): keep rollbacks of auto services until a new digest (#9)
Adds services.last_auto_digest so the poller starts one automatic update per
registry digest, and re-reads the service before starting it. Also records a
bare image's running version as tag latest, only records well-formed
digests, and shows a digest-less version unless a digest row of its tag was
first seen at or after it.
2026-10-13 08:28:50 +02:00

216 lines
7.2 KiB
Go

//go:build integration
package store
import (
"context"
"strconv"
"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"
)
var (
t0 = time.Date(2026, 10, 12, 12, 0, 0, 0, time.UTC)
slusk = protocol.Service{Kind: protocol.KindCompose, ComposeProject: "slusk", ComposeService: "web", WorkingDir: "/srv/slusk", Image: "gitea.shcizo.se/shcizo/slusk:1.4.2", Digest: digD1, Status: "running"}
adhoc = protocol.Service{Kind: protocol.KindContainer, ContainerName: "adhoc", Image: "redis:alpine", Status: "stopped"}
gitea = protocol.Service{Kind: protocol.KindSwarm, SwarmService: "gitea_gitea", Image: "gitea/gitea:1.27.3@sha256:87a6", Digest: digGitea, Status: "running"}
worker = protocol.Hello{Hostname: "node-2"}
manager = protocol.Hello{Hostname: "swarm-mgr", SwarmManager: true, SwarmClusterID: "c1"}
)
func names(svcs []model.Service) []string {
var out []string
for _, s := range svcs {
out = append(out, s.DisplayName())
}
return out
}
func newAgent(t *testing.T, s *Store, name string) int64 {
t.Helper()
a, err := s.CreateAgent(context.Background(), name, "hash-"+name)
require.NoError(t, err)
return a.ID
}
func TestApplySnapshot_InsertsAsManualWithAgentName(t *testing.T) {
s := openTestStore(t)
ctx := context.Background()
id := newAgent(t, s, "node-2")
require.NoError(t, s.ApplySnapshot(ctx, id, worker, []protocol.Service{slusk}, t0))
got, err := s.ListServices(ctx)
require.NoError(t, err)
require.Len(t, got, 1)
assert.Equal(t, "manual", got[0].UpdateMode)
assert.Equal(t, "node-2", got[0].AgentName)
assert.Equal(t, "gitea.shcizo.se/shcizo/slusk", got[0].ImageRepo)
assert.Equal(t, "gitea.shcizo.se/shcizo/slusk:1.4.2", got[0].ActualRef)
assert.Equal(t, digD1, got[0].ActualDigest)
assert.Equal(t, "compose|"+itoa(id)+"|slusk|web", got[0].LocationKey)
}
func TestApplySnapshot_RemovedWhileOfflineThenBack(t *testing.T) {
s := openTestStore(t)
ctx := context.Background()
id := newAgent(t, s, "node-2")
require.NoError(t, s.ApplySnapshot(ctx, id, worker, []protocol.Service{slusk, adhoc}, t0))
before, err := s.ListServices(ctx)
require.NoError(t, err)
require.NoError(t, s.ApplySnapshot(ctx, id, worker, []protocol.Service{slusk}, t0.Add(time.Hour)))
got, err := s.ListServices(ctx)
require.NoError(t, err)
assert.Equal(t, []string{"slusk/web"}, names(got))
require.NoError(t, s.ApplySnapshot(ctx, id, worker, []protocol.Service{slusk, adhoc}, t0.Add(2*time.Hour)))
after, err := s.ListServices(ctx)
require.NoError(t, err)
require.Len(t, after, 2)
for i := range before {
assert.Equal(t, before[i].ID, after[i].ID, "a returning service keeps its row")
assert.True(t, before[i].FirstSeenAt.Equal(after[i].FirstSeenAt))
}
}
func TestApplySnapshot_TwoManagersShareSwarmRowsAndKeepOwnCompose(t *testing.T) {
s := openTestStore(t)
ctx := context.Background()
a := newAgent(t, s, "swarm-mgr")
b := newAgent(t, s, "swarm-mgr-2")
require.NoError(t, s.ApplySnapshot(ctx, a, manager, []protocol.Service{gitea, slusk}, t0))
require.NoError(t, s.ApplySnapshot(ctx, b, manager, []protocol.Service{gitea}, t0))
got, err := s.ListServices(ctx)
require.NoError(t, err)
assert.ElementsMatch(t, []string{"slusk/web", "gitea_gitea"}, names(got),
"one swarm row, and b's snapshot must not remove a's compose service")
}
func TestApplySnapshot_IgnoresSwarmFromWorker(t *testing.T) {
s := openTestStore(t)
ctx := context.Background()
id := newAgent(t, s, "node-2")
require.NoError(t, s.ApplySnapshot(ctx, id, worker, []protocol.Service{gitea}, t0))
got, err := s.ListServices(ctx)
require.NoError(t, err)
assert.Empty(t, got)
}
func TestApplySnapshot_IgnoresSwarmFromManagerWithoutClusterID(t *testing.T) {
s := openTestStore(t)
ctx := context.Background()
a := newAgent(t, s, "mgr-a")
b := newAgent(t, s, "mgr-b")
noCluster := protocol.Hello{Hostname: "mgr", SwarmManager: true}
require.NoError(t, s.ApplySnapshot(ctx, a, manager, []protocol.Service{gitea}, t0))
require.NoError(t, s.ApplySnapshot(ctx, b, noCluster, []protocol.Service{gitea}, t0))
got, err := s.ListServices(ctx)
require.NoError(t, err)
require.Len(t, got, 1, "the cluster-less manager adds no swarm row")
assert.Equal(t, "swarm|c1|gitea_gitea", got[0].LocationKey)
require.NoError(t, s.ApplySnapshot(ctx, b, noCluster, nil, t0.Add(time.Hour)))
got, err = s.ListServices(ctx)
require.NoError(t, err)
assert.Len(t, got, 1, "the cluster-less manager removes no swarm rows")
}
func TestUpsertAndRemoveService(t *testing.T) {
s := openTestStore(t)
ctx := context.Background()
id := newAgent(t, s, "swarm-mgr")
require.NoError(t, s.UpsertService(ctx, id, "c1", gitea, t0))
changed := gitea
changed.Status = "degraded"
require.NoError(t, s.UpsertService(ctx, id, "c1", changed, t0.Add(time.Minute)))
got, err := s.ListServices(ctx)
require.NoError(t, err)
require.Len(t, got, 1)
assert.Equal(t, "degraded", got[0].Status)
assert.Equal(t, "swarm|c1|gitea_gitea", got[0].LocationKey)
require.NoError(t, s.RemoveService(ctx, id, "c1", gitea.Key(), t0.Add(2*time.Minute)))
got, err = s.ListServices(ctx)
require.NoError(t, err)
assert.Empty(t, got)
}
func TestListServices_SwarmLast(t *testing.T) {
s := openTestStore(t)
ctx := context.Background()
id := newAgent(t, s, "swarm-mgr")
require.NoError(t, s.ApplySnapshot(ctx, id, manager, []protocol.Service{gitea, slusk}, t0))
got, err := s.ListServices(ctx)
require.NoError(t, err)
assert.Equal(t, []string{"slusk/web", "gitea_gitea"}, names(got))
}
func itoa(n int64) string { return strconv.FormatInt(n, 10) }
func TestGetService_IncludesRemoved(t *testing.T) {
s := openTestStore(t)
ctx := context.Background()
id := newAgent(t, s, "node-2")
require.NoError(t, s.ApplySnapshot(ctx, id, worker, []protocol.Service{slusk}, t0))
svcs, err := s.ListServices(ctx)
require.NoError(t, err)
require.Len(t, svcs, 1)
require.NoError(t, s.RemoveService(ctx, id, "", slusk.Key(), t0))
got, err := s.GetService(ctx, svcs[0].ID)
require.NoError(t, err)
assert.NotNil(t, got.RemovedAt)
assert.Equal(t, "node-2", got.AgentName)
_, err = s.GetService(ctx, 99999)
assert.ErrorIs(t, err, model.ErrNotFound)
}
func TestApplySnapshot_StoresConfigFiles(t *testing.T) {
s := openTestStore(t)
ctx := context.Background()
id := newAgent(t, s, "node-2")
withFiles := slusk
withFiles.ConfigFiles = "/srv/slusk/compose.yaml,/srv/slusk/compose.override.yaml"
require.NoError(t, s.ApplySnapshot(ctx, id, worker, []protocol.Service{withFiles}, t0))
got, err := s.ListServices(ctx)
require.NoError(t, err)
require.Len(t, got, 1)
assert.Equal(t, withFiles.ConfigFiles, got[0].ConfigFiles)
}
func TestApplySnapshot_StoresBuilt(t *testing.T) {
s := openTestStore(t)
ctx := context.Background()
id := newAgent(t, s, "node-2")
built := slusk
built.Built = true
require.NoError(t, s.ApplySnapshot(ctx, id, worker, []protocol.Service{built}, t0))
got, err := s.ListServices(ctx)
require.NoError(t, err)
require.Len(t, got, 1)
assert.True(t, got[0].Built)
// A later report without the flag clears it.
require.NoError(t, s.UpsertService(ctx, id, "", slusk, t0.Add(time.Minute)))
got, err = s.ListServices(ctx)
require.NoError(t, err)
assert.False(t, got[0].Built)
}