libpod: modernize state test

Use t.Helper, t.TempDir, and t.Cleanup in getEmptySqliteState,
simplifying its code and its users.

Simplify runForAllStates: remove redundant t.Fail call, and move
getEmptySqliteState call under t.Run.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
Kir Kolyshkin
2026-02-05 15:25:26 -08:00
parent ee64636cea
commit 49cce3ec16
2 changed files with 13 additions and 44 deletions

View File

@@ -3,17 +3,13 @@
package libpod
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_generateName(t *testing.T) {
state, path, _, err := getEmptySqliteState()
assert.NoError(t, err)
defer os.RemoveAll(path)
defer state.Close()
state, _ := getEmptySqliteState(t)
r := &Runtime{
state: state,

View File

@@ -3,7 +3,6 @@
package libpod
import (
"os"
"strings"
"testing"
"time"
@@ -16,35 +15,17 @@ import (
"go.podman.io/storage"
)
// Returns state, tmp directory containing all state files, lock manager, and
// error.
// Closing the state and removing the given tmp directory should be sufficient
// to clean up.
type emptyStateFunc func() (State, string, lock.Manager, error)
const (
tmpDirPrefix = "libpod_state_test_"
)
type emptyStateFunc func(t *testing.T) (State, lock.Manager)
var testedStates = map[string]emptyStateFunc{
"sqlite": getEmptySqliteState,
}
func getEmptySqliteState() (_ State, _ string, _ lock.Manager, retErr error) {
tmpDir, err := os.MkdirTemp("", tmpDirPrefix)
if err != nil {
return nil, "", nil, err
}
defer func() {
if retErr != nil {
os.RemoveAll(tmpDir)
}
}()
func getEmptySqliteState(t *testing.T) (State, lock.Manager) {
t.Helper()
tmpDir := t.TempDir()
lockManager, err := lock.NewInMemoryManager(16)
if err != nil {
return nil, "", nil, err
}
require.NoError(t, err)
runtime := new(Runtime)
runtime.config = new(config.Config)
@@ -56,28 +37,20 @@ func getEmptySqliteState() (_ State, _ string, _ lock.Manager, retErr error) {
runtime.storageSet.StaticDirSet = true
state, err := NewSqliteState(runtime)
if err != nil {
return nil, "", nil, err
}
require.NoError(t, err)
t.Cleanup(func() {
state.Close()
})
return state, tmpDir, lockManager, nil
return state, lockManager
}
func runForAllStates(t *testing.T, testFunc func(*testing.T, State, lock.Manager)) {
for stateName, stateFunc := range testedStates {
state, path, manager, err := stateFunc()
if err != nil {
t.Fatalf("Error initializing state %s: %v", stateName, err)
}
defer os.RemoveAll(path)
defer state.Close()
success := t.Run(stateName, func(t *testing.T) {
t.Run(stateName, func(t *testing.T) {
state, manager := stateFunc(t)
testFunc(t, state, manager)
})
if !success {
t.Fail()
}
}
}