github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/helper/raftutil/state_test.go (about) 1 package raftutil 2 3 import ( 4 "path/filepath" 5 "testing" 6 7 "github.com/hashicorp/nomad/ci" 8 raftboltdb "github.com/hashicorp/raft-boltdb/v2" 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 ) 12 13 // TestRaftStateInfo_InUse asserts that commands that inspect raft 14 // state such as "nomad operator raft info" and "nomad operator raft 15 // logs" fail with a helpful error message when called on an inuse 16 // database. 17 func TestRaftStateInfo_InUse(t *testing.T) { 18 ci.Parallel(t) // since there's a 1s timeout. 19 20 // First create an empty raft db 21 dir := filepath.Join(t.TempDir(), "raft.db") 22 23 fakedb, err := raftboltdb.NewBoltStore(dir) 24 require.NoError(t, err) 25 26 // Next try to read the db without closing it 27 s, _, _, err := RaftStateInfo(dir) 28 assert.Nil(t, s) 29 require.EqualError(t, err, errAlreadyOpen.Error()) 30 31 // LogEntries should produce the same error 32 _, _, err = LogEntries(dir) 33 require.EqualError(t, err, "failed to open raft logs: "+errAlreadyOpen.Error()) 34 35 // Commands should work once the db is closed 36 require.NoError(t, fakedb.Close()) 37 38 s, _, _, err = RaftStateInfo(dir) 39 assert.NotNil(t, s) 40 require.NoError(t, err) 41 require.NoError(t, s.Close()) 42 43 logCh, errCh, err := LogEntries(dir) 44 require.NoError(t, err) 45 46 // Consume entries to cleanly close db 47 for closed := false; closed; { 48 select { 49 case _, closed = <-logCh: 50 case <-errCh: 51 } 52 } 53 }