2022-05-27 13:33:41 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
memdb "github.com/hashicorp/go-memdb"
|
2022-08-15 15:19:53 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2022-05-27 13:33:41 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2022-06-01 15:48:26 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
2022-05-27 13:33:41 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func TestStateStore_GetVariable(t *testing.T) {
|
2022-06-14 17:28:10 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
testState := testStateStore(t)
|
|
|
|
ws := memdb.NewWatchSet()
|
2022-08-26 18:03:56 +00:00
|
|
|
sve, err := testState.GetVariable(ws, "default", "not/a/path")
|
2022-06-14 17:28:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, sve)
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func TestStateStore_UpsertVariables(t *testing.T) {
|
2022-05-27 13:33:41 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
testState := testStateStore(t)
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
svs := []*structs.VariableEncrypted{
|
|
|
|
mock.VariableEncrypted(),
|
|
|
|
mock.VariableEncrypted(),
|
2022-08-15 15:19:53 +00:00
|
|
|
}
|
|
|
|
svs[0].Path = "aaaaa"
|
|
|
|
svs[1].Path = "bbbbb"
|
|
|
|
|
2022-05-27 13:33:41 +00:00
|
|
|
insertIndex := uint64(20)
|
2022-07-15 15:08:53 +00:00
|
|
|
|
2022-08-02 13:32:09 +00:00
|
|
|
var expectedQuotaSize int
|
2022-07-15 15:08:53 +00:00
|
|
|
for _, v := range svs {
|
2022-08-02 13:32:09 +00:00
|
|
|
expectedQuotaSize += len(v.Data)
|
2022-07-15 15:08:53 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// Ensure new variables are inserted as expected with their
|
2022-07-15 15:08:53 +00:00
|
|
|
// correct indexes, along with an update to the index table.
|
2022-06-27 17:17:22 +00:00
|
|
|
t.Run("1 create new variables", func(t *testing.T) {
|
2022-08-26 18:03:56 +00:00
|
|
|
// Perform the initial upsert of variables.
|
2022-08-15 15:19:53 +00:00
|
|
|
for _, sv := range svs {
|
|
|
|
insertIndex++
|
2022-08-26 18:03:56 +00:00
|
|
|
resp := testState.VarSet(insertIndex, &structs.VarApplyStateRequest{
|
|
|
|
Op: structs.VarOpSet,
|
2022-08-15 15:19:53 +00:00
|
|
|
Var: sv,
|
|
|
|
})
|
|
|
|
require.NoError(t, resp.Error)
|
|
|
|
}
|
2022-05-27 13:33:41 +00:00
|
|
|
|
|
|
|
// Check that the index for the table was modified as expected.
|
2022-08-26 18:03:56 +00:00
|
|
|
initialIndex, err := testState.Index(TableVariables)
|
2022-05-27 13:33:41 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, insertIndex, initialIndex)
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// List all the variables in the table
|
|
|
|
iter, err := testState.Variables(ws)
|
2022-05-27 13:33:41 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
got := []*structs.VariableEncrypted{}
|
2022-05-27 13:33:41 +00:00
|
|
|
for raw := iter.Next(); raw != nil; raw = iter.Next() {
|
2022-08-26 18:03:56 +00:00
|
|
|
sv := raw.(*structs.VariableEncrypted)
|
|
|
|
var svCopy structs.VariableEncrypted
|
2022-08-15 15:19:53 +00:00
|
|
|
svCopy = sv.Copy()
|
|
|
|
got = append(got, &svCopy)
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
2022-08-26 18:03:56 +00:00
|
|
|
require.Len(t, got, 2, "incorrect number of variables found")
|
2022-08-15 15:19:53 +00:00
|
|
|
|
|
|
|
// Ensure the create and modify indexes are populated correctly.
|
|
|
|
require.Equal(t, uint64(21), got[0].CreateIndex, "%s: incorrect create index", got[0].Path)
|
|
|
|
require.Equal(t, uint64(21), got[0].ModifyIndex, "%s: incorrect modify index", got[0].Path)
|
|
|
|
require.Equal(t, uint64(22), got[1].CreateIndex, "%s: incorrect create index", got[1].Path)
|
|
|
|
require.Equal(t, uint64(22), got[1].ModifyIndex, "%s: incorrect modify index", got[1].Path)
|
2022-06-27 17:17:22 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
quotaUsed, err := testState.VariablesQuotaByNamespace(ws, structs.DefaultNamespace)
|
2022-06-27 17:17:22 +00:00
|
|
|
require.NoError(t, err)
|
2022-08-02 13:32:09 +00:00
|
|
|
require.Equal(t, int64(expectedQuotaSize), quotaUsed.Size)
|
2022-08-15 15:19:53 +00:00
|
|
|
|
|
|
|
// update the mocks so the test element has the correct create/modify
|
|
|
|
// indexes and times now that we have validated them
|
|
|
|
svs = got
|
2022-05-27 13:33:41 +00:00
|
|
|
})
|
2022-06-27 17:17:22 +00:00
|
|
|
|
2022-06-14 17:28:10 +00:00
|
|
|
t.Run("1a fetch variable", func(t *testing.T) {
|
2022-08-26 18:03:56 +00:00
|
|
|
sve, err := testState.GetVariable(ws, svs[0].Namespace, svs[0].Path)
|
2022-06-14 17:28:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, sve)
|
|
|
|
})
|
2022-07-15 15:08:53 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// Upsert the exact same variables without any modification. In this
|
2022-08-15 15:19:53 +00:00
|
|
|
// case, the index table should not be updated, indicating no write actually
|
|
|
|
// happened due to equality checking.
|
2022-05-27 13:33:41 +00:00
|
|
|
t.Run("2 upsert same", func(t *testing.T) {
|
|
|
|
reInsertIndex := uint64(30)
|
2022-08-15 15:19:53 +00:00
|
|
|
|
|
|
|
for _, sv := range svs {
|
2022-08-26 18:03:56 +00:00
|
|
|
svReq := &structs.VarApplyStateRequest{
|
|
|
|
Op: structs.VarOpSet,
|
2022-08-15 15:19:53 +00:00
|
|
|
Var: sv,
|
|
|
|
}
|
|
|
|
reInsertIndex++
|
2022-08-26 18:03:56 +00:00
|
|
|
resp := testState.VarSet(reInsertIndex, svReq)
|
2022-08-15 15:19:53 +00:00
|
|
|
require.NoError(t, resp.Error)
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
reInsertActualIndex, err := testState.Index(TableVariables)
|
2022-05-27 13:33:41 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, insertIndex, reInsertActualIndex, "index should not have changed")
|
2022-07-15 15:08:53 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
quotaUsed, err := testState.VariablesQuotaByNamespace(ws, structs.DefaultNamespace)
|
2022-07-15 15:08:53 +00:00
|
|
|
require.NoError(t, err)
|
2022-08-02 13:32:09 +00:00
|
|
|
require.Equal(t, int64(expectedQuotaSize), quotaUsed.Size)
|
2022-05-27 13:33:41 +00:00
|
|
|
})
|
2022-07-15 15:08:53 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// Modify a single one of the previously inserted variables and
|
2022-08-15 15:19:53 +00:00
|
|
|
// performs an upsert. This ensures the index table is modified correctly
|
2022-08-26 18:03:56 +00:00
|
|
|
// and that each variable is updated, or not, as expected.
|
2022-05-27 13:33:41 +00:00
|
|
|
t.Run("3 modify one", func(t *testing.T) {
|
|
|
|
sv1Update := svs[0].Copy()
|
2022-06-14 17:28:10 +00:00
|
|
|
sv1Update.KeyID = "sv1-update"
|
2022-07-15 15:08:53 +00:00
|
|
|
|
|
|
|
buf := make([]byte, 1+len(sv1Update.Data))
|
|
|
|
copy(buf, sv1Update.Data)
|
|
|
|
buf[len(buf)-1] = 'x'
|
|
|
|
sv1Update.Data = buf
|
|
|
|
|
2022-05-27 13:33:41 +00:00
|
|
|
update1Index := uint64(40)
|
2022-08-15 15:19:53 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
resp := testState.VarSet(update1Index, &structs.VarApplyStateRequest{
|
|
|
|
Op: structs.VarOpSet,
|
2022-08-15 15:19:53 +00:00
|
|
|
Var: &sv1Update,
|
|
|
|
})
|
|
|
|
require.NoError(t, resp.Error)
|
2022-05-27 13:33:41 +00:00
|
|
|
|
|
|
|
// Check that the index for the table was modified as expected.
|
2022-08-26 18:03:56 +00:00
|
|
|
updateActualIndex, err := testState.Index(TableVariables)
|
2022-05-27 13:33:41 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, update1Index, updateActualIndex, "index should have changed")
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// Get the variables from the table.
|
|
|
|
iter, err := testState.Variables(ws)
|
2022-05-27 13:33:41 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
got := []*structs.VariableEncrypted{}
|
2022-08-15 15:19:53 +00:00
|
|
|
|
|
|
|
// Iterate all the stored variables and assert indexes have been updated as expected
|
2022-05-27 13:33:41 +00:00
|
|
|
for raw := iter.Next(); raw != nil; raw = iter.Next() {
|
2022-08-26 18:03:56 +00:00
|
|
|
sv := raw.(*structs.VariableEncrypted)
|
|
|
|
var svCopy structs.VariableEncrypted
|
2022-08-15 15:19:53 +00:00
|
|
|
svCopy = sv.Copy()
|
|
|
|
got = append(got, &svCopy)
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
2022-08-15 15:19:53 +00:00
|
|
|
require.Len(t, got, 2)
|
|
|
|
require.Equal(t, update1Index, got[0].ModifyIndex)
|
|
|
|
require.Equal(t, insertIndex, got[1].ModifyIndex)
|
|
|
|
|
|
|
|
// update the mocks so the test element has the correct create/modify
|
|
|
|
// indexes and times now that we have validated them
|
|
|
|
svs = got
|
2022-07-15 15:08:53 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
quotaUsed, err := testState.VariablesQuotaByNamespace(ws, structs.DefaultNamespace)
|
2022-07-15 15:08:53 +00:00
|
|
|
require.NoError(t, err)
|
2022-08-02 13:32:09 +00:00
|
|
|
require.Equal(t, int64(expectedQuotaSize+1), quotaUsed.Size)
|
2022-05-27 13:33:41 +00:00
|
|
|
})
|
2022-07-15 15:08:53 +00:00
|
|
|
|
|
|
|
// Modify the second variable but send an upsert request that
|
|
|
|
// includes this and the already modified variable.
|
2022-05-27 13:33:41 +00:00
|
|
|
t.Run("4 upsert other", func(t *testing.T) {
|
|
|
|
update2Index := uint64(50)
|
|
|
|
sv2 := svs[1].Copy()
|
2022-06-14 17:28:10 +00:00
|
|
|
sv2.KeyID = "sv2-update"
|
2022-05-27 13:33:41 +00:00
|
|
|
sv2.ModifyIndex = update2Index
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
resp := testState.VarSet(update2Index, &structs.VarApplyStateRequest{
|
|
|
|
Op: structs.VarOpSet,
|
2022-08-15 15:19:53 +00:00
|
|
|
Var: &sv2,
|
|
|
|
})
|
|
|
|
require.NoError(t, resp.Error)
|
2022-05-27 13:33:41 +00:00
|
|
|
|
|
|
|
// Check that the index for the table was modified as expected.
|
2022-08-26 18:03:56 +00:00
|
|
|
update2ActualIndex, err := testState.Index(TableVariables)
|
2022-05-27 13:33:41 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, update2Index, update2ActualIndex, "index should have changed")
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// Get the variables from the table.
|
|
|
|
iter, err := testState.Variables(ws)
|
2022-05-27 13:33:41 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
got := []structs.VariableEncrypted{}
|
2022-08-15 15:19:53 +00:00
|
|
|
|
|
|
|
// Iterate all the stored variables and assert indexes have been updated as expected
|
2022-05-27 13:33:41 +00:00
|
|
|
for raw := iter.Next(); raw != nil; raw = iter.Next() {
|
2022-08-26 18:03:56 +00:00
|
|
|
sv := raw.(*structs.VariableEncrypted)
|
2022-08-15 15:19:53 +00:00
|
|
|
got = append(got, sv.Copy())
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
2022-08-15 15:19:53 +00:00
|
|
|
require.Len(t, got, 2)
|
|
|
|
require.Equal(t, svs[0].ModifyIndex, got[0].ModifyIndex)
|
|
|
|
require.Equal(t, update2Index, got[1].ModifyIndex)
|
|
|
|
|
2022-10-10 14:28:46 +00:00
|
|
|
require.True(t, svs[0].Equal(got[0]))
|
|
|
|
require.True(t, sv2.Equal(got[1]))
|
2022-07-15 15:08:53 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
quotaUsed, err := testState.VariablesQuotaByNamespace(ws, structs.DefaultNamespace)
|
2022-07-15 15:08:53 +00:00
|
|
|
require.NoError(t, err)
|
2022-08-02 13:32:09 +00:00
|
|
|
require.Equal(t, int64(expectedQuotaSize+1), quotaUsed.Size)
|
2022-07-15 15:08:53 +00:00
|
|
|
|
2022-05-27 13:33:41 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func TestStateStore_DeleteVariable(t *testing.T) {
|
2022-05-27 13:33:41 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
testState := testStateStore(t)
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// Generate some test variables that we will use and modify throughout.
|
|
|
|
svs := []*structs.VariableEncrypted{
|
|
|
|
mock.VariableEncrypted(),
|
|
|
|
mock.VariableEncrypted(),
|
2022-08-15 15:19:53 +00:00
|
|
|
}
|
|
|
|
svs[0].Path = "aaaaa"
|
|
|
|
svs[1].Path = "bbbbb"
|
|
|
|
|
2022-05-27 13:33:41 +00:00
|
|
|
initialIndex := uint64(10)
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
t.Run("1 delete a variable that does not exist", func(t *testing.T) {
|
2022-08-15 15:19:53 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
resp := testState.VarDelete(initialIndex, &structs.VarApplyStateRequest{
|
|
|
|
Op: structs.VarOpDelete,
|
2022-08-15 15:19:53 +00:00
|
|
|
Var: svs[0],
|
|
|
|
})
|
2022-08-26 18:03:56 +00:00
|
|
|
require.NoError(t, resp.Error, "deleting non-existing var is not an error")
|
2022-05-27 13:33:41 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
actualInitialIndex, err := testState.Index(TableVariables)
|
2022-07-15 15:08:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, uint64(0), actualInitialIndex, "index should not have changed")
|
2022-05-27 13:33:41 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
quotaUsed, err := testState.VariablesQuotaByNamespace(nil, structs.DefaultNamespace)
|
2022-07-15 15:08:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, quotaUsed)
|
|
|
|
})
|
2022-05-27 13:33:41 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// Upsert two variables, deletes one, then ensure the
|
2022-07-15 15:08:53 +00:00
|
|
|
// remaining is left as expected.
|
|
|
|
t.Run("2 upsert variable and delete", func(t *testing.T) {
|
2022-05-27 13:33:41 +00:00
|
|
|
|
2022-08-02 13:32:09 +00:00
|
|
|
ns := mock.Namespace()
|
|
|
|
ns.Name = svs[0].Namespace
|
|
|
|
require.NoError(t, testState.UpsertNamespaces(initialIndex, []*structs.Namespace{ns}))
|
|
|
|
|
2022-08-15 15:19:53 +00:00
|
|
|
for _, sv := range svs {
|
2022-08-26 18:03:56 +00:00
|
|
|
svReq := &structs.VarApplyStateRequest{
|
|
|
|
Op: structs.VarOpSet,
|
2022-08-15 15:19:53 +00:00
|
|
|
Var: sv,
|
|
|
|
}
|
|
|
|
initialIndex++
|
2022-08-26 18:03:56 +00:00
|
|
|
resp := testState.VarSet(initialIndex, svReq)
|
2022-08-15 15:19:53 +00:00
|
|
|
require.NoError(t, resp.Error)
|
|
|
|
}
|
2022-05-27 13:33:41 +00:00
|
|
|
|
2022-07-15 15:08:53 +00:00
|
|
|
// Perform the delete.
|
|
|
|
delete1Index := uint64(20)
|
2022-08-15 15:19:53 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
resp := testState.VarDelete(delete1Index, &structs.VarApplyStateRequest{
|
|
|
|
Op: structs.VarOpDelete,
|
2022-08-15 15:19:53 +00:00
|
|
|
Var: svs[0],
|
|
|
|
})
|
|
|
|
require.NoError(t, resp.Error)
|
2022-05-27 13:33:41 +00:00
|
|
|
|
2022-07-15 15:08:53 +00:00
|
|
|
// Check that the index for the table was modified as expected.
|
2022-08-26 18:03:56 +00:00
|
|
|
actualDelete1Index, err := testState.Index(TableVariables)
|
2022-07-15 15:08:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, delete1Index, actualDelete1Index, "index should have changed")
|
2022-05-27 13:33:41 +00:00
|
|
|
|
2022-07-15 15:08:53 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
2022-05-27 13:33:41 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// Get the variables from the table.
|
|
|
|
iter, err := testState.Variables(ws)
|
2022-07-15 15:08:53 +00:00
|
|
|
require.NoError(t, err)
|
2022-05-27 13:33:41 +00:00
|
|
|
|
2022-07-15 15:08:53 +00:00
|
|
|
var delete1Count int
|
2022-08-02 13:32:09 +00:00
|
|
|
var expectedQuotaSize int
|
2022-05-27 13:33:41 +00:00
|
|
|
|
2022-07-15 15:08:53 +00:00
|
|
|
// Iterate all the stored variables and assert we have the expected
|
|
|
|
// number.
|
|
|
|
for raw := iter.Next(); raw != nil; raw = iter.Next() {
|
|
|
|
delete1Count++
|
2022-08-26 18:03:56 +00:00
|
|
|
v := raw.(*structs.VariableEncrypted)
|
2022-08-02 13:32:09 +00:00
|
|
|
expectedQuotaSize += len(v.Data)
|
2022-07-15 15:08:53 +00:00
|
|
|
}
|
|
|
|
require.Equal(t, 1, delete1Count, "unexpected number of variables in table")
|
2022-08-26 18:03:56 +00:00
|
|
|
quotaUsed, err := testState.VariablesQuotaByNamespace(ws, structs.DefaultNamespace)
|
2022-07-15 15:08:53 +00:00
|
|
|
require.NoError(t, err)
|
2022-08-02 13:32:09 +00:00
|
|
|
require.Equal(t, int64(expectedQuotaSize), quotaUsed.Size)
|
2022-07-15 15:08:53 +00:00
|
|
|
})
|
2022-05-27 13:33:41 +00:00
|
|
|
|
2022-07-15 15:08:53 +00:00
|
|
|
t.Run("3 delete remaining variable", func(t *testing.T) {
|
|
|
|
delete2Index := uint64(30)
|
2022-08-15 15:19:53 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
resp := testState.VarDelete(delete2Index, &structs.VarApplyStateRequest{
|
|
|
|
Op: structs.VarOpDelete,
|
2022-08-15 15:19:53 +00:00
|
|
|
Var: svs[1],
|
|
|
|
})
|
|
|
|
require.NoError(t, resp.Error)
|
2022-05-27 13:33:41 +00:00
|
|
|
|
2022-07-15 15:08:53 +00:00
|
|
|
// Check that the index for the table was modified as expected.
|
2022-08-26 18:03:56 +00:00
|
|
|
actualDelete2Index, err := testState.Index(TableVariables)
|
2022-07-15 15:08:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, delete2Index, actualDelete2Index, "index should have changed")
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// Get the variables from the table.
|
2022-07-15 15:08:53 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
2022-08-26 18:03:56 +00:00
|
|
|
iter, err := testState.Variables(ws)
|
2022-07-15 15:08:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var delete2Count int
|
|
|
|
|
|
|
|
// Ensure the table is empty.
|
|
|
|
for raw := iter.Next(); raw != nil; raw = iter.Next() {
|
|
|
|
delete2Count++
|
|
|
|
}
|
|
|
|
require.Equal(t, 0, delete2Count, "unexpected number of variables in table")
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
quotaUsed, err := testState.VariablesQuotaByNamespace(ws, structs.DefaultNamespace)
|
2022-07-15 15:08:53 +00:00
|
|
|
require.NoError(t, err)
|
2022-08-02 13:32:09 +00:00
|
|
|
require.Equal(t, int64(0), quotaUsed.Size)
|
2022-07-15 15:08:53 +00:00
|
|
|
})
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
2022-07-15 15:08:53 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func TestStateStore_GetVariables(t *testing.T) {
|
2022-05-27 13:33:41 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
testState := testStateStore(t)
|
|
|
|
|
2022-08-02 13:32:09 +00:00
|
|
|
ns := mock.Namespace()
|
|
|
|
ns.Name = "~*magical*~"
|
|
|
|
initialIndex := uint64(10)
|
|
|
|
require.NoError(t, testState.UpsertNamespaces(initialIndex, []*structs.Namespace{ns}))
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// Generate some test variables in different namespaces and upsert them.
|
|
|
|
svs := []*structs.VariableEncrypted{
|
|
|
|
mock.VariableEncrypted(),
|
|
|
|
mock.VariableEncrypted(),
|
2022-08-15 15:19:53 +00:00
|
|
|
}
|
|
|
|
svs[0].Path = "aaaaa"
|
2022-05-27 13:33:41 +00:00
|
|
|
svs[0].Namespace = "~*magical*~"
|
2022-08-15 15:19:53 +00:00
|
|
|
svs[1].Path = "bbbbb"
|
|
|
|
|
|
|
|
for _, sv := range svs {
|
2022-08-26 18:03:56 +00:00
|
|
|
svReq := &structs.VarApplyStateRequest{
|
|
|
|
Op: structs.VarOpSet,
|
2022-08-15 15:19:53 +00:00
|
|
|
Var: sv,
|
|
|
|
}
|
|
|
|
initialIndex++
|
2022-08-26 18:03:56 +00:00
|
|
|
resp := testState.VarSet(initialIndex, svReq)
|
2022-08-15 15:19:53 +00:00
|
|
|
require.NoError(t, resp.Error)
|
|
|
|
}
|
2022-05-27 13:33:41 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// Look up variables using the namespace of the first mock variable.
|
2022-05-27 13:33:41 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
2022-08-26 18:03:56 +00:00
|
|
|
iter, err := testState.GetVariablesByNamespace(ws, svs[0].Namespace)
|
2022-05-27 13:33:41 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var count1 int
|
|
|
|
|
|
|
|
for raw := iter.Next(); raw != nil; raw = iter.Next() {
|
2022-08-26 18:03:56 +00:00
|
|
|
sv := raw.(*structs.VariableEncrypted)
|
2022-05-27 13:33:41 +00:00
|
|
|
require.Equal(t, svs[0].Namespace, sv.Namespace)
|
2022-08-15 15:19:53 +00:00
|
|
|
require.Equal(t, uint64(11), sv.CreateIndex, "%s incorrect create index", sv.Path)
|
|
|
|
require.Equal(t, uint64(11), sv.ModifyIndex, "%s incorrect modify index", sv.Path)
|
|
|
|
count1++
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
2022-08-15 15:19:53 +00:00
|
|
|
|
2022-05-27 13:33:41 +00:00
|
|
|
require.Equal(t, 1, count1)
|
|
|
|
|
|
|
|
// Look up variables using the namespace of the second mock variable.
|
2022-08-26 18:03:56 +00:00
|
|
|
iter, err = testState.GetVariablesByNamespace(ws, svs[1].Namespace)
|
2022-05-27 13:33:41 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var count2 int
|
|
|
|
|
|
|
|
for raw := iter.Next(); raw != nil; raw = iter.Next() {
|
|
|
|
count2++
|
2022-08-26 18:03:56 +00:00
|
|
|
sv := raw.(*structs.VariableEncrypted)
|
2022-08-15 15:19:53 +00:00
|
|
|
require.Equal(t, initialIndex, sv.CreateIndex, "%s incorrect create index", sv.Path)
|
|
|
|
require.Equal(t, initialIndex, sv.ModifyIndex, "%s incorrect modify index", sv.Path)
|
2022-05-27 13:33:41 +00:00
|
|
|
require.Equal(t, svs[1].Namespace, sv.Namespace)
|
|
|
|
}
|
|
|
|
require.Equal(t, 1, count2)
|
|
|
|
|
|
|
|
// Look up variables using a namespace that shouldn't contain any
|
2022-07-15 15:08:53 +00:00
|
|
|
// variables.
|
2022-08-26 18:03:56 +00:00
|
|
|
iter, err = testState.GetVariablesByNamespace(ws, "pony-club")
|
2022-05-27 13:33:41 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var count3 int
|
|
|
|
|
|
|
|
for raw := iter.Next(); raw != nil; raw = iter.Next() {
|
|
|
|
count3++
|
|
|
|
}
|
|
|
|
require.Equal(t, 0, count3)
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func TestStateStore_ListVariablesByNamespaceAndPrefix(t *testing.T) {
|
2022-05-27 13:33:41 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
testState := testStateStore(t)
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// Generate some test variables and upsert them.
|
|
|
|
svs := []*structs.VariableEncrypted{}
|
2022-08-15 15:19:53 +00:00
|
|
|
for i := 0; i < 6; i++ {
|
2022-08-26 18:03:56 +00:00
|
|
|
sv := mock.VariableEncrypted()
|
2022-08-15 15:19:53 +00:00
|
|
|
svs = append(svs, sv)
|
|
|
|
}
|
|
|
|
|
2022-06-20 03:39:00 +00:00
|
|
|
svs[0].Path = "a/b"
|
2022-05-27 13:33:41 +00:00
|
|
|
svs[1].Path = "a/b/c"
|
2022-06-20 03:39:00 +00:00
|
|
|
svs[2].Path = "unrelated/b/c"
|
|
|
|
svs[3].Namespace = "other"
|
|
|
|
svs[3].Path = "a/b/c"
|
|
|
|
svs[4].Namespace = "other"
|
|
|
|
svs[4].Path = "a/q/z"
|
|
|
|
svs[5].Namespace = "other"
|
|
|
|
svs[5].Path = "a/z/z"
|
2022-05-27 13:33:41 +00:00
|
|
|
|
2022-08-02 13:32:09 +00:00
|
|
|
ns := mock.Namespace()
|
|
|
|
ns.Name = "other"
|
2022-05-27 13:33:41 +00:00
|
|
|
initialIndex := uint64(10)
|
2022-08-02 13:32:09 +00:00
|
|
|
require.NoError(t, testState.UpsertNamespaces(initialIndex, []*structs.Namespace{ns}))
|
|
|
|
|
2022-08-15 15:19:53 +00:00
|
|
|
for _, sv := range svs {
|
2022-08-26 18:03:56 +00:00
|
|
|
svReq := &structs.VarApplyStateRequest{
|
|
|
|
Op: structs.VarOpSet,
|
2022-08-15 15:19:53 +00:00
|
|
|
Var: sv,
|
|
|
|
}
|
|
|
|
initialIndex++
|
2022-08-26 18:03:56 +00:00
|
|
|
resp := testState.VarSet(initialIndex, svReq)
|
2022-08-15 15:19:53 +00:00
|
|
|
require.NoError(t, resp.Error)
|
|
|
|
}
|
2022-05-27 13:33:41 +00:00
|
|
|
|
2022-06-20 03:39:00 +00:00
|
|
|
t.Run("ByNamespace", func(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
namespace string
|
|
|
|
expectedCount int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "default",
|
|
|
|
namespace: "default",
|
|
|
|
expectedCount: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "other",
|
|
|
|
namespace: "other",
|
|
|
|
expectedCount: 3,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "nonexistent",
|
|
|
|
namespace: "BAD",
|
|
|
|
expectedCount: 0,
|
|
|
|
},
|
|
|
|
}
|
2022-05-27 13:33:41 +00:00
|
|
|
|
2022-06-20 03:39:00 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
for _, tC := range testCases {
|
|
|
|
t.Run(tC.desc, func(t *testing.T) {
|
2022-08-26 18:03:56 +00:00
|
|
|
iter, err := testState.GetVariablesByNamespace(ws, tC.namespace)
|
2022-06-20 03:39:00 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var count int = 0
|
|
|
|
for raw := iter.Next(); raw != nil; raw = iter.Next() {
|
|
|
|
count++
|
2022-08-26 18:03:56 +00:00
|
|
|
sv := raw.(*structs.VariableEncrypted)
|
2022-06-20 03:39:00 +00:00
|
|
|
require.Equal(t, tC.namespace, sv.Namespace)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2022-05-27 13:33:41 +00:00
|
|
|
|
2022-06-20 03:39:00 +00:00
|
|
|
t.Run("ByNamespaceAndPrefix", func(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
namespace string
|
|
|
|
prefix string
|
|
|
|
expectedCount int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "ns1 with good path",
|
|
|
|
namespace: "default",
|
|
|
|
prefix: "a",
|
|
|
|
expectedCount: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "ns2 with good path",
|
|
|
|
namespace: "other",
|
|
|
|
prefix: "a",
|
|
|
|
expectedCount: 3,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "ns1 path valid for ns2",
|
|
|
|
namespace: "default",
|
|
|
|
prefix: "a/b/c",
|
|
|
|
expectedCount: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "ns2 empty prefix",
|
|
|
|
namespace: "other",
|
|
|
|
prefix: "",
|
|
|
|
expectedCount: 3,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "nonexistent ns",
|
|
|
|
namespace: "BAD",
|
|
|
|
prefix: "",
|
|
|
|
expectedCount: 0,
|
|
|
|
},
|
|
|
|
}
|
2022-05-27 13:33:41 +00:00
|
|
|
|
2022-06-20 03:39:00 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
for _, tC := range testCases {
|
|
|
|
t.Run(tC.desc, func(t *testing.T) {
|
2022-08-26 18:03:56 +00:00
|
|
|
iter, err := testState.GetVariablesByNamespaceAndPrefix(ws, tC.namespace, tC.prefix)
|
2022-06-20 03:39:00 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var count int = 0
|
|
|
|
for raw := iter.Next(); raw != nil; raw = iter.Next() {
|
|
|
|
count++
|
2022-08-26 18:03:56 +00:00
|
|
|
sv := raw.(*structs.VariableEncrypted)
|
2022-06-20 03:39:00 +00:00
|
|
|
require.Equal(t, tC.namespace, sv.Namespace)
|
|
|
|
require.True(t, strings.HasPrefix(sv.Path, tC.prefix))
|
|
|
|
}
|
|
|
|
require.Equal(t, tC.expectedCount, count)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2022-05-27 13:33:41 +00:00
|
|
|
|
2022-06-20 03:39:00 +00:00
|
|
|
t.Run("ByPrefix", func(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
prefix string
|
|
|
|
expectedCount int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "bad prefix",
|
|
|
|
prefix: "bad",
|
|
|
|
expectedCount: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "multiple ns",
|
|
|
|
prefix: "a/b/c",
|
|
|
|
expectedCount: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "all",
|
|
|
|
prefix: "",
|
|
|
|
expectedCount: 6,
|
|
|
|
},
|
|
|
|
}
|
2022-05-27 13:33:41 +00:00
|
|
|
|
2022-06-20 03:39:00 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
for _, tC := range testCases {
|
|
|
|
t.Run(tC.desc, func(t *testing.T) {
|
2022-08-26 18:03:56 +00:00
|
|
|
iter, err := testState.GetVariablesByPrefix(ws, tC.prefix)
|
2022-06-20 03:39:00 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var count int = 0
|
|
|
|
for raw := iter.Next(); raw != nil; raw = iter.Next() {
|
|
|
|
count++
|
2022-08-26 18:03:56 +00:00
|
|
|
sv := raw.(*structs.VariableEncrypted)
|
2022-06-20 03:39:00 +00:00
|
|
|
require.True(t, strings.HasPrefix(sv.Path, tC.prefix))
|
|
|
|
}
|
|
|
|
require.Equal(t, tC.expectedCount, count)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
2022-08-26 18:03:56 +00:00
|
|
|
func TestStateStore_ListVariablesByKeyID(t *testing.T) {
|
2022-06-01 15:48:26 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
testState := testStateStore(t)
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// Generate some test variables and upsert them.
|
|
|
|
svs := []*structs.VariableEncrypted{}
|
2022-08-15 15:19:53 +00:00
|
|
|
for i := 0; i < 7; i++ {
|
2022-08-26 18:03:56 +00:00
|
|
|
sv := mock.VariableEncrypted()
|
2022-08-15 15:19:53 +00:00
|
|
|
sv.Path = uuid.Generate()
|
|
|
|
svs = append(svs, sv)
|
|
|
|
}
|
|
|
|
|
2022-06-01 15:48:26 +00:00
|
|
|
keyID := uuid.Generate()
|
|
|
|
|
|
|
|
expectedForKey := []string{}
|
|
|
|
for i := 0; i < 5; i++ {
|
2022-06-14 17:28:10 +00:00
|
|
|
svs[i].KeyID = keyID
|
2022-06-01 15:48:26 +00:00
|
|
|
expectedForKey = append(expectedForKey, svs[i].Path)
|
|
|
|
sort.Strings(expectedForKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedOrphaned := []string{svs[5].Path, svs[6].Path}
|
|
|
|
|
|
|
|
initialIndex := uint64(10)
|
2022-08-15 15:19:53 +00:00
|
|
|
|
|
|
|
for _, sv := range svs {
|
2022-08-26 18:03:56 +00:00
|
|
|
svReq := &structs.VarApplyStateRequest{
|
|
|
|
Op: structs.VarOpSet,
|
2022-08-15 15:19:53 +00:00
|
|
|
Var: sv,
|
|
|
|
}
|
|
|
|
initialIndex++
|
2022-08-26 18:03:56 +00:00
|
|
|
resp := testState.VarSet(initialIndex, svReq)
|
2022-08-15 15:19:53 +00:00
|
|
|
require.NoError(t, resp.Error)
|
|
|
|
}
|
2022-06-01 15:48:26 +00:00
|
|
|
|
|
|
|
ws := memdb.NewWatchSet()
|
2022-08-26 18:03:56 +00:00
|
|
|
iter, err := testState.GetVariablesByKeyID(ws, keyID)
|
2022-06-01 15:48:26 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var count int
|
|
|
|
for raw := iter.Next(); raw != nil; raw = iter.Next() {
|
2022-08-26 18:03:56 +00:00
|
|
|
sv := raw.(*structs.VariableEncrypted)
|
2022-06-14 17:28:10 +00:00
|
|
|
require.Equal(t, keyID, sv.KeyID)
|
2022-06-01 15:48:26 +00:00
|
|
|
require.Equal(t, expectedForKey[count], sv.Path)
|
|
|
|
require.NotContains(t, expectedOrphaned, sv.Path)
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
require.Equal(t, 5, count)
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func printVariable(tsv *structs.VariableEncrypted) string {
|
2022-05-27 13:33:41 +00:00
|
|
|
b, _ := json.Marshal(tsv)
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func printVariables(tsvs []*structs.VariableEncrypted) string {
|
2022-05-27 13:33:41 +00:00
|
|
|
if len(tsvs) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
var out strings.Builder
|
|
|
|
for _, tsv := range tsvs {
|
2022-08-26 18:03:56 +00:00
|
|
|
out.WriteString(printVariable(tsv) + "\n")
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
|
|
|
return out.String()
|
|
|
|
}
|
2022-09-13 14:12:08 +00:00
|
|
|
|
|
|
|
// TestStateStore_Variables_DeleteCAS
|
|
|
|
func TestStateStore_Variables_DeleteCAS(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
ts := testStateStore(t)
|
|
|
|
|
|
|
|
varNotExist := structs.VariableEncrypted{
|
|
|
|
VariableMetadata: structs.VariableMetadata{
|
|
|
|
Namespace: "default",
|
|
|
|
Path: "does/not/exist",
|
|
|
|
ModifyIndex: 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("missing_var-cas_0", func(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
varNotExist := varNotExist
|
|
|
|
// A CAS delete with index 0 should succeed when the variable does not
|
|
|
|
// exist in the state store.
|
|
|
|
resp := ts.VarDeleteCAS(10, &structs.VarApplyStateRequest{
|
|
|
|
Op: structs.VarOpDelete,
|
|
|
|
Var: &varNotExist,
|
|
|
|
})
|
|
|
|
require.True(t, resp.IsOk())
|
|
|
|
})
|
|
|
|
t.Run("missing_var-cas_1", func(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
varZero := varNotExist
|
|
|
|
varNotExist := varNotExist
|
|
|
|
// A CAS delete with a non-zero index should return a conflict when the
|
|
|
|
// variable does not exist in the state store. The conflict value should
|
|
|
|
// be a zero value having the same namespace and path.
|
|
|
|
varNotExist.ModifyIndex = 1
|
|
|
|
req := &structs.VarApplyStateRequest{
|
|
|
|
Op: structs.VarOpDelete,
|
|
|
|
Var: &varNotExist,
|
|
|
|
}
|
|
|
|
resp := ts.VarDeleteCAS(10, req)
|
|
|
|
require.True(t, resp.IsConflict())
|
|
|
|
require.NotNil(t, resp.Conflict)
|
|
|
|
require.Equal(t, varZero.VariableMetadata, resp.Conflict.VariableMetadata)
|
|
|
|
})
|
|
|
|
t.Run("real_var-cas_0", func(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
sv := mock.VariableEncrypted()
|
|
|
|
sv.CreateIndex = 0
|
|
|
|
sv.ModifyIndex = 0
|
|
|
|
sv.Path = "real_var/cas_0"
|
|
|
|
// Need to make a copy because VarSet mutates Var.
|
|
|
|
svZero := sv.Copy()
|
|
|
|
resp := ts.VarSet(10, &structs.VarApplyStateRequest{
|
|
|
|
Op: structs.VarOpSet,
|
|
|
|
Var: sv,
|
|
|
|
})
|
|
|
|
require.True(t, resp.IsOk(), "resp: %+v", resp)
|
|
|
|
|
|
|
|
// A CAS delete with a zero index should return a conflict when the
|
|
|
|
// variable exists in the state store. The conflict value should
|
|
|
|
// be the current state of the variable at the path.
|
|
|
|
req := &structs.VarApplyStateRequest{
|
|
|
|
Op: structs.VarOpDelete,
|
|
|
|
Var: &svZero,
|
|
|
|
}
|
|
|
|
resp = ts.VarDeleteCAS(0, req)
|
|
|
|
require.True(t, resp.IsConflict(), "resp: %+v", resp)
|
|
|
|
require.NotNil(t, resp.Conflict)
|
|
|
|
require.Equal(t, sv.VariableMetadata, resp.Conflict.VariableMetadata)
|
|
|
|
})
|
|
|
|
t.Run("real_var-cas_ok", func(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
sv := mock.VariableEncrypted()
|
|
|
|
sv.Path = "real_var/cas_ok"
|
|
|
|
resp := ts.VarSet(10, &structs.VarApplyStateRequest{
|
|
|
|
Op: structs.VarOpSet,
|
|
|
|
Var: sv,
|
|
|
|
})
|
|
|
|
require.True(t, resp.IsOk())
|
|
|
|
|
|
|
|
// A CAS delete with a correct index should succeed.
|
|
|
|
req := &structs.VarApplyStateRequest{
|
|
|
|
Op: structs.VarOpDelete,
|
|
|
|
Var: sv,
|
|
|
|
}
|
|
|
|
resp = ts.VarDeleteCAS(0, req)
|
|
|
|
require.True(t, resp.IsOk())
|
|
|
|
})
|
|
|
|
}
|