package datastore
import (
"io/ioutil"
"log"
"os"
portainer "github.com/portainer/portainer/api"
"github.com/portainer/portainer/api/database"
"github.com/pkg/errors"
"github.com/portainer/portainer/api/filesystem"
)
var errTempDir = errors.New("can't create a temp dir")
func (store *Store) GetConnection() portainer.Connection {
return store.connection
}
func MustNewTestStore(init bool) (bool, *Store, func()) {
newStore, store, teardown, err := NewTestStore(init)
if err != nil {
if !errors.Is(err, errTempDir) {
teardown()
log.Fatal(err)
return newStore, store, teardown
func NewTestStore(init bool) (bool, *Store, func(), error) {
// Creates unique temp directory in a concurrency friendly manner.
storePath, err := ioutil.TempDir("", "test-store")
return false, nil, nil, errors.Wrap(errTempDir, err.Error())
fileService, err := filesystem.NewService(storePath, "")
return false, nil, nil, err
connection, err := database.NewDatabase("boltdb", storePath, []byte("apassphrasewhichneedstobe32bytes"))
panic(err)
store := NewStore(storePath, fileService, connection)
newStore, err := store.Open()
return newStore, nil, nil, err
if init {
err = store.Init()
if newStore {
// from MigrateData
store.VersionService.StoreDBVersion(portainer.DBVersion)
teardown := func() {
teardown(store, storePath)
return newStore, store, teardown, nil
func teardown(store *Store, storePath string) {
err := store.Close()
log.Fatalln(err)
err = os.RemoveAll(storePath)