Skip to content

Commit

Permalink
Use a temporary file for the fakedatastore
Browse files Browse the repository at this point in the history
With a in memory sqlite datastore the database gets cleaned up when all connections to it get closed. If that happens, the next connection to be opened will see an empty database and usually error out.

I've noticed this in tests ocassionally and this seems to make running the db using  not fail.

Signed-off-by: Sorin Dumitru <[email protected]>
  • Loading branch information
sorindumitru committed Jan 24, 2025
1 parent aa532ce commit 485d18d
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions test/fakes/fakedatastore/fakedatastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package fakedatastore
import (
"context"
"fmt"
"net/url"
"path/filepath"
"sort"
"sync/atomic"
"testing"
"time"

Expand All @@ -21,8 +22,6 @@ import (

var (
ctx = context.Background()

nextID uint32
)

type DataStore struct {
Expand All @@ -38,12 +37,20 @@ func New(tb testing.TB) *DataStore {
ds := sql.New(log)
ds.SetUseServerTimestamps(true)

tmpDir := tb.TempDir()
dbPath := filepath.Join(tmpDir, "spire.db")
dbPath = url.PathEscape(dbPath)

err := ds.Configure(ctx, fmt.Sprintf(`
database_type = "sqlite3"
connection_string = "file:memdb%d?mode=memory&cache=shared"
`, atomic.AddUint32(&nextID, 1)))
connection_string = "file:%s"
`, dbPath))
require.NoError(tb, err)

tb.Cleanup(func() {
ds.Close()
})

return &DataStore{
ds: ds,
}
Expand Down

0 comments on commit 485d18d

Please sign in to comment.