Files
headscale/hscontrol/db/policy_test.go
T
Kristoffer Dalby 524ccc46aa db: keep only the current policy
Every write inserted a row and nothing deleted one, while every reader
takes only the newest. Writers store and install under one lock, so an
uncommitted insert cannot outlive the newer row that pruned the rest.
2026-10-28 14:37:08 +00:00

32 lines
745 B
Go

package db
import (
"strconv"
"testing"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestSetPolicyKeepsOnlyCurrent checks repeated writes replace the stored
// policy instead of piling up rows.
func TestSetPolicyKeepsOnlyCurrent(t *testing.T) {
db := dbForTest(t)
for i := range 5 {
_, err := db.SetPolicy(
`{"hosts": {"h": "100.64.0.` + strconv.Itoa(i) + `/32"}}`,
)
require.NoError(t, err)
}
var rows int64
require.NoError(t, db.DB.Unscoped().Model(&types.Policy{}).Count(&rows).Error)
assert.Equal(t, int64(1), rows)
p, err := db.GetPolicy()
require.NoError(t, err)
assert.JSONEq(t, `{"hosts": {"h": "100.64.0.4/32"}}`, p.Data)
}