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 <sdumitru@bloomberg.net>
  • Loading branch information
sorindumitru committed Jan 30, 2025
1 parent 81c4d29 commit 259c25c
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 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,10 +37,14 @@ 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() {
Expand Down

0 comments on commit 259c25c

Please sign in to comment.