2021-12-16 19:41:01 +00:00
|
|
|
package raftutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2022-02-23 20:43:59 +00:00
|
|
|
raftboltdb "github.com/hashicorp/raft-boltdb/v2"
|
2021-12-16 19:41:01 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestRaftStateInfo_InUse asserts that commands that inspect raft
|
|
|
|
// state such as "nomad operator raft info" and "nomad operator raft
|
|
|
|
// logs" fail with a helpful error message when called on an inuse
|
|
|
|
// database.
|
|
|
|
func TestRaftStateInfo_InUse(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t) // since there's a 1s timeout.
|
2021-12-16 19:41:01 +00:00
|
|
|
|
|
|
|
// First create an empty raft db
|
|
|
|
dir := filepath.Join(t.TempDir(), "raft.db")
|
|
|
|
|
|
|
|
fakedb, err := raftboltdb.NewBoltStore(dir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Next try to read the db without closing it
|
|
|
|
s, _, _, err := RaftStateInfo(dir)
|
|
|
|
assert.Nil(t, s)
|
|
|
|
require.EqualError(t, err, errAlreadyOpen.Error())
|
|
|
|
|
|
|
|
// LogEntries should produce the same error
|
|
|
|
_, _, err = LogEntries(dir)
|
|
|
|
require.EqualError(t, err, "failed to open raft logs: "+errAlreadyOpen.Error())
|
|
|
|
|
|
|
|
// Commands should work once the db is closed
|
|
|
|
require.NoError(t, fakedb.Close())
|
|
|
|
|
|
|
|
s, _, _, err = RaftStateInfo(dir)
|
|
|
|
assert.NotNil(t, s)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, s.Close())
|
|
|
|
|
|
|
|
logCh, errCh, err := LogEntries(dir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Consume entries to cleanly close db
|
|
|
|
for closed := false; closed; {
|
|
|
|
select {
|
|
|
|
case _, closed = <-logCh:
|
|
|
|
case <-errCh:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|