209 lines
4.6 KiB
Go
209 lines
4.6 KiB
Go
package store
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func newTestStore(t *testing.T) *Store {
|
|
t.Helper()
|
|
s, err := New(":memory:")
|
|
if err != nil {
|
|
t.Fatalf("failed to open in-memory store: %v", err)
|
|
}
|
|
t.Cleanup(func() { s.Close() })
|
|
return s
|
|
}
|
|
|
|
// ── Users ─────────────────────────────────────────────────────────────────────
|
|
|
|
func TestUpsertAndGetUserHash(t *testing.T) {
|
|
s := newTestStore(t)
|
|
|
|
if err := s.UpsertUser("alice", "hash123"); err != nil {
|
|
t.Fatalf("UpsertUser: %v", err)
|
|
}
|
|
|
|
h, err := s.GetUserHash("alice")
|
|
if err != nil {
|
|
t.Fatalf("GetUserHash: %v", err)
|
|
}
|
|
if h != "hash123" {
|
|
t.Errorf("expected hash123, got %q", h)
|
|
}
|
|
}
|
|
|
|
func TestGetUserHash_NotFound(t *testing.T) {
|
|
s := newTestStore(t)
|
|
|
|
_, err := s.GetUserHash("nobody")
|
|
if err == nil {
|
|
t.Fatal("expected error for missing user, got nil")
|
|
}
|
|
}
|
|
|
|
func TestUpsertUser_Update(t *testing.T) {
|
|
s := newTestStore(t)
|
|
|
|
if err := s.UpsertUser("alice", "first"); err != nil {
|
|
t.Fatalf("UpsertUser: %v", err)
|
|
}
|
|
if err := s.UpsertUser("alice", "second"); err != nil {
|
|
t.Fatalf("UpsertUser update: %v", err)
|
|
}
|
|
|
|
h, err := s.GetUserHash("alice")
|
|
if err != nil {
|
|
t.Fatalf("GetUserHash: %v", err)
|
|
}
|
|
if h != "second" {
|
|
t.Errorf("expected second, got %q", h)
|
|
}
|
|
}
|
|
|
|
func TestUserExists(t *testing.T) {
|
|
s := newTestStore(t)
|
|
|
|
ok, err := s.UserExists("alice")
|
|
if err != nil {
|
|
t.Fatalf("UserExists: %v", err)
|
|
}
|
|
if ok {
|
|
t.Error("expected false for non-existent user")
|
|
}
|
|
|
|
_ = s.UpsertUser("alice", "hash")
|
|
|
|
ok, err = s.UserExists("alice")
|
|
if err != nil {
|
|
t.Fatalf("UserExists: %v", err)
|
|
}
|
|
if !ok {
|
|
t.Error("expected true after insert")
|
|
}
|
|
}
|
|
|
|
// ── Agents ────────────────────────────────────────────────────────────────────
|
|
|
|
func TestCreateAgentToken(t *testing.T) {
|
|
s := newTestStore(t)
|
|
|
|
if err := s.CreateAgentToken("id1", "tok1", "host1"); err != nil {
|
|
t.Fatalf("CreateAgentToken: %v", err)
|
|
}
|
|
|
|
a, err := s.AgentByToken("tok1")
|
|
if err != nil {
|
|
t.Fatalf("AgentByToken: %v", err)
|
|
}
|
|
if a.ID != "id1" || a.Hostname != "host1" {
|
|
t.Errorf("unexpected agent: %+v", a)
|
|
}
|
|
}
|
|
|
|
func TestAgentByToken_NotFound(t *testing.T) {
|
|
s := newTestStore(t)
|
|
|
|
_, err := s.AgentByToken("doesnotexist")
|
|
if err == nil {
|
|
t.Fatal("expected error for unknown token")
|
|
}
|
|
}
|
|
|
|
func TestUpsertAgent(t *testing.T) {
|
|
s := newTestStore(t)
|
|
|
|
a := &Agent{
|
|
ID: "agent1",
|
|
Token: "tok1",
|
|
Hostname: "myhost",
|
|
Alias: "myalias",
|
|
IPAddress: "10.0.0.1",
|
|
Arch: "amd64",
|
|
OS: "linux",
|
|
Online: true,
|
|
}
|
|
if err := s.UpsertAgent(a); err != nil {
|
|
t.Fatalf("UpsertAgent: %v", err)
|
|
}
|
|
|
|
got, err := s.GetAgent("agent1")
|
|
if err != nil {
|
|
t.Fatalf("GetAgent: %v", err)
|
|
}
|
|
if got.Hostname != "myhost" || got.Alias != "myalias" {
|
|
t.Errorf("unexpected agent: %+v", got)
|
|
}
|
|
}
|
|
|
|
func TestListAgents(t *testing.T) {
|
|
s := newTestStore(t)
|
|
|
|
_ = s.CreateAgentToken("a1", "t1", "host-b")
|
|
_ = s.CreateAgentToken("a2", "t2", "host-a")
|
|
|
|
agents, err := s.ListAgents()
|
|
if err != nil {
|
|
t.Fatalf("ListAgents: %v", err)
|
|
}
|
|
if len(agents) != 2 {
|
|
t.Fatalf("expected 2 agents, got %d", len(agents))
|
|
}
|
|
// ORDER BY hostname: host-a < host-b
|
|
if agents[0].Hostname != "host-a" || agents[1].Hostname != "host-b" {
|
|
t.Errorf("unexpected order: %v %v", agents[0].Hostname, agents[1].Hostname)
|
|
}
|
|
}
|
|
|
|
func TestSetAgentOffline(t *testing.T) {
|
|
s := newTestStore(t)
|
|
|
|
_ = s.UpsertAgent(&Agent{
|
|
ID: "a1",
|
|
Token: "t1",
|
|
Hostname: "h1",
|
|
Online: true,
|
|
})
|
|
|
|
if err := s.SetAgentOffline("a1"); err != nil {
|
|
t.Fatalf("SetAgentOffline: %v", err)
|
|
}
|
|
|
|
a, err := s.GetAgent("a1")
|
|
if err != nil {
|
|
t.Fatalf("GetAgent: %v", err)
|
|
}
|
|
if a.Online {
|
|
t.Error("expected Online=false after SetAgentOffline")
|
|
}
|
|
}
|
|
|
|
func TestUpdateAgentAlias(t *testing.T) {
|
|
s := newTestStore(t)
|
|
|
|
_ = s.CreateAgentToken("a1", "t1", "host1")
|
|
|
|
if err := s.UpdateAgentAlias("a1", "newalias"); err != nil {
|
|
t.Fatalf("UpdateAgentAlias: %v", err)
|
|
}
|
|
|
|
a, err := s.GetAgent("a1")
|
|
if err != nil {
|
|
t.Fatalf("GetAgent: %v", err)
|
|
}
|
|
if a.Alias != "newalias" {
|
|
t.Errorf("expected alias 'newalias', got %q", a.Alias)
|
|
}
|
|
}
|
|
|
|
func TestCreateAgentToken_IdempotentIgnore(t *testing.T) {
|
|
s := newTestStore(t)
|
|
|
|
// INSERT OR IGNORE — second call should not error
|
|
if err := s.CreateAgentToken("id1", "tok1", "h1"); err != nil {
|
|
t.Fatalf("first call: %v", err)
|
|
}
|
|
if err := s.CreateAgentToken("id1", "tok1", "h1"); err != nil {
|
|
t.Fatalf("second call (should be idempotent): %v", err)
|
|
}
|
|
}
|