mirror of
https://github.com/portainer/portainer.git
synced 2025-07-23 15:29:42 +02:00
chore(tests): use t.TempDir to create temporary test directory [EE-3700] (#7612)
* create temporary test directory with t.TempDir
This commit is contained in:
parent
1a9d793f2f
commit
a7d458f0bd
36 changed files with 120 additions and 226 deletions
|
@ -1,9 +1,8 @@
|
|||
package datastore
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
portainer "github.com/portainer/portainer/api"
|
||||
"github.com/portainer/portainer/api/database"
|
||||
|
@ -18,8 +17,8 @@ func (store *Store) GetConnection() portainer.Connection {
|
|||
return store.connection
|
||||
}
|
||||
|
||||
func MustNewTestStore(init, secure bool) (bool, *Store, func()) {
|
||||
newStore, store, teardown, err := NewTestStore(init, secure)
|
||||
func MustNewTestStore(t *testing.T, init, secure bool) (bool, *Store, func()) {
|
||||
newStore, store, teardown, err := NewTestStore(t, init, secure)
|
||||
if err != nil {
|
||||
if !errors.Is(err, errTempDir) {
|
||||
teardown()
|
||||
|
@ -30,13 +29,9 @@ func MustNewTestStore(init, secure bool) (bool, *Store, func()) {
|
|||
return newStore, store, teardown
|
||||
}
|
||||
|
||||
func NewTestStore(init, secure bool) (bool, *Store, func(), error) {
|
||||
func NewTestStore(t *testing.T, init, secure bool) (bool, *Store, func(), error) {
|
||||
// Creates unique temp directory in a concurrency friendly manner.
|
||||
storePath, err := ioutil.TempDir("", "test-store")
|
||||
if err != nil {
|
||||
return false, nil, nil, errors.Wrap(errTempDir, err.Error())
|
||||
}
|
||||
|
||||
storePath := t.TempDir()
|
||||
fileService, err := filesystem.NewService(storePath, "")
|
||||
if err != nil {
|
||||
return false, nil, nil, err
|
||||
|
@ -73,20 +68,15 @@ func NewTestStore(init, secure bool) (bool, *Store, func(), error) {
|
|||
}
|
||||
|
||||
teardown := func() {
|
||||
teardown(store, storePath)
|
||||
teardown(store)
|
||||
}
|
||||
|
||||
return newStore, store, teardown, nil
|
||||
}
|
||||
|
||||
func teardown(store *Store, storePath string) {
|
||||
func teardown(store *Store) {
|
||||
err := store.Close()
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
err = os.RemoveAll(storePath)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue