85 lines
2.5 KiB
Go
85 lines
2.5 KiB
Go
//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/pin"
|
|
"github.com/shcizo/package-updater/internal/protocol"
|
|
)
|
|
|
|
func TestIdentity_CreatedOnceThenReused(t *testing.T) {
|
|
s := openTestStore(t)
|
|
ctx := context.Background()
|
|
k1, err := s.LoadOrCreateIdentity(ctx, pin.GenerateKey)
|
|
require.NoError(t, err)
|
|
k2, err := s.LoadOrCreateIdentity(ctx, pin.GenerateKey)
|
|
require.NoError(t, err)
|
|
assert.True(t, k1.Equal(k2), "a restarted master must keep its key or every agent breaks")
|
|
}
|
|
|
|
func TestMigrationsAreIdempotent(t *testing.T) {
|
|
s := openTestStore(t)
|
|
require.NoError(t, migrate(context.Background(), s.pool))
|
|
}
|
|
|
|
func TestAgents_CreateLookupRevoke(t *testing.T) {
|
|
s := openTestStore(t)
|
|
ctx := context.Background()
|
|
|
|
created, err := s.CreateAgent(ctx, "node-2", "hash-2")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "node-2", created.Name)
|
|
assert.Nil(t, created.RevokedAt)
|
|
|
|
found, err := s.AgentByTokenHash(ctx, "hash-2")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, created.ID, found.ID)
|
|
|
|
_, err = s.AgentByTokenHash(ctx, "nope")
|
|
assert.ErrorIs(t, err, model.ErrNotFound)
|
|
|
|
require.NoError(t, s.RevokeAgent(ctx, created.ID, time.Now()))
|
|
found, err = s.AgentByTokenHash(ctx, "hash-2")
|
|
require.NoError(t, err)
|
|
assert.NotNil(t, found.RevokedAt)
|
|
}
|
|
|
|
func TestAgents_DuplicateNameIsConflict(t *testing.T) {
|
|
s := openTestStore(t)
|
|
ctx := context.Background()
|
|
_, err := s.CreateAgent(ctx, "node-2", "h1")
|
|
require.NoError(t, err)
|
|
_, err = s.CreateAgent(ctx, "node-2", "h2")
|
|
assert.ErrorIs(t, err, model.ErrConflict)
|
|
}
|
|
|
|
func TestAgents_RecordHelloAndMarkSeen(t *testing.T) {
|
|
s := openTestStore(t)
|
|
ctx := context.Background()
|
|
a, err := s.CreateAgent(ctx, "swarm-mgr", "h")
|
|
require.NoError(t, err)
|
|
|
|
at := time.Date(2026, 10, 12, 12, 0, 0, 0, time.UTC)
|
|
require.NoError(t, s.RecordHello(ctx, a.ID, protocol.Hello{
|
|
Hostname: "swarm-mgr", AgentVersion: "v1", SwarmManager: true, SwarmClusterID: "c1",
|
|
}, at))
|
|
require.NoError(t, s.MarkSeen(ctx, a.ID, at.Add(time.Minute)))
|
|
|
|
agents, err := s.ListAgents(ctx)
|
|
require.NoError(t, err)
|
|
require.Len(t, agents, 1)
|
|
assert.Equal(t, "swarm-mgr", agents[0].Hostname)
|
|
assert.Equal(t, "v1", agents[0].AgentVersion)
|
|
assert.True(t, agents[0].SwarmManager)
|
|
require.NotNil(t, agents[0].LastSeenAt)
|
|
assert.True(t, agents[0].LastSeenAt.Equal(at.Add(time.Minute)))
|
|
}
|