1
0
Fork 0
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:
Chao Geng 2022-09-14 13:59:47 +08:00 committed by GitHub
parent 1a9d793f2f
commit a7d458f0bd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
36 changed files with 120 additions and 226 deletions

View file

@ -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)
}
}