Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graphdriver test cleanups #2228

Merged
merged 4 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions drivers/btrfs/btrfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
// This avoids creating a new driver for each test if all tests are run
// Make sure to put new tests between TestBtrfsSetup and TestBtrfsTeardown
func TestBtrfsSetup(t *testing.T) {
graphtest.GetDriver(t, "btrfs")
graphtest.GetDriverNoCleanup(t, "btrfs")
}

func TestBtrfsCreateEmpty(t *testing.T) {
Expand All @@ -38,7 +38,6 @@ func TestBtrfsSubvolDelete(t *testing.T) {
if err := d.CreateReadWrite("test", "", nil); err != nil {
t.Fatal(err)
}
defer graphtest.PutDriver(t)

dir, err := d.Get("test", graphdriver.MountOpts{})
if err != nil {
Expand Down
7 changes: 0 additions & 7 deletions drivers/graphtest/graphbench_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
// DriverBenchExists benchmarks calls to exist
func DriverBenchExists(b *testing.B, drivername string, driveroptions ...string) {
driver := GetDriver(b, drivername, driveroptions...)
defer PutDriver(b)

base := stringid.GenerateRandomID()

Expand All @@ -35,7 +34,6 @@ func DriverBenchExists(b *testing.B, drivername string, driveroptions ...string)
// DriverBenchGetEmpty benchmarks calls to get on an empty layer
func DriverBenchGetEmpty(b *testing.B, drivername string, driveroptions ...string) {
driver := GetDriver(b, drivername, driveroptions...)
defer PutDriver(b)

base := stringid.GenerateRandomID()

Expand All @@ -60,7 +58,6 @@ func DriverBenchGetEmpty(b *testing.B, drivername string, driveroptions ...strin
// DriverBenchDiffBase benchmarks calls to diff on a root layer
func DriverBenchDiffBase(b *testing.B, drivername string, driveroptions ...string) {
driver := GetDriver(b, drivername, driveroptions...)
defer PutDriver(b)

base := stringid.GenerateRandomID()
if err := driver.Create(base, "", nil); err != nil {
Expand Down Expand Up @@ -89,7 +86,6 @@ func DriverBenchDiffBase(b *testing.B, drivername string, driveroptions ...strin
// a provided number of files on the lower and upper layers.
func DriverBenchDiffN(b *testing.B, bottom, top int, drivername string, driveroptions ...string) {
driver := GetDriver(b, drivername, driveroptions...)
defer PutDriver(b)
base := stringid.GenerateRandomID()
upper := stringid.GenerateRandomID()
if err := driver.Create(base, "", nil); err != nil {
Expand Down Expand Up @@ -124,7 +120,6 @@ func DriverBenchDiffN(b *testing.B, bottom, top int, drivername string, driverop
// DriverBenchDiffApplyN benchmarks calls to diff and apply together
func DriverBenchDiffApplyN(b *testing.B, fileCount int, drivername string, driveroptions ...string) {
driver := GetDriver(b, drivername, driveroptions...)
defer PutDriver(b)
base := stringid.GenerateRandomID()
upper := stringid.GenerateRandomID()
if err := driver.Create(base, "", nil); err != nil {
Expand Down Expand Up @@ -185,7 +180,6 @@ func DriverBenchDiffApplyN(b *testing.B, fileCount int, drivername string, drive
// DriverBenchDeepLayerDiff benchmarks calls to diff on top of a given number of layers.
func DriverBenchDeepLayerDiff(b *testing.B, layerCount int, drivername string, driveroptions ...string) {
driver := GetDriver(b, drivername, driveroptions...)
defer PutDriver(b)

base := stringid.GenerateRandomID()
if err := driver.Create(base, "", nil); err != nil {
Expand Down Expand Up @@ -218,7 +212,6 @@ func DriverBenchDeepLayerDiff(b *testing.B, layerCount int, drivername string, d
// DriverBenchDeepLayerRead benchmarks calls to read a file under a given number of layers.
func DriverBenchDeepLayerRead(b *testing.B, layerCount int, drivername string, driveroptions ...string) {
driver := GetDriver(b, drivername, driveroptions...)
defer PutDriver(b)

base := stringid.GenerateRandomID()
if err := driver.Create(base, "", nil); err != nil {
Expand Down
45 changes: 25 additions & 20 deletions drivers/graphtest/graphtest_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ const (
type Driver struct {
graphdriver.Driver
root string
runroot string
runRoot string
refCount int
}

func newGraphDriver(t testing.TB, name string, options []string, root string, runroot string) graphdriver.Driver {
d, err := graphdriver.GetDriver(name, graphdriver.Options{DriverOptions: options, Root: root, RunRoot: runroot})
func newGraphDriver(t testing.TB, name string, options []string, root string, runRoot string) graphdriver.Driver {
d, err := graphdriver.GetDriver(name, graphdriver.Options{DriverOptions: options, Root: root, RunRoot: runRoot})
if err != nil {
t.Logf("graphdriver: %v\n", err)
if errors.Is(err, graphdriver.ErrNotSupported) || errors.Is(err, graphdriver.ErrPrerequisites) || errors.Is(err, graphdriver.ErrIncompatibleFS) {
Expand All @@ -62,23 +62,32 @@ func newGraphDriver(t testing.TB, name string, options []string, root string, ru
func newDriver(t testing.TB, name string, options []string) *Driver {
root, err := os.MkdirTemp("", "storage-graphtest-")
require.NoError(t, err)
runroot, err := os.MkdirTemp("", "storage-graphtest-")
runRoot, err := os.MkdirTemp("", "storage-graphtest-")
require.NoError(t, err)

require.NoError(t, os.MkdirAll(root, 0o755))
return &Driver{newGraphDriver(t, name, options, root, runroot), root, runroot, 1}
defer func() {
// Cannot use t.Cleanup(), some test files persist the
// driver across test functions.
if t.Failed() || t.Skipped() {
os.RemoveAll(runRoot)
os.RemoveAll(root)
}
}()
return &Driver{newGraphDriver(t, name, options, root, runRoot), root, runRoot, 1}
}

func cleanup(t testing.TB, d *Driver) {
if err := drv.Cleanup(); err != nil {
t.Fatal(err)
}
os.RemoveAll(d.runroot)
os.RemoveAll(d.runRoot)
os.RemoveAll(d.root)
}

// GetDriver create a new driver with given name or return an existing driver with the name updating the reference count.
func GetDriver(t testing.TB, name string, options ...string) graphdriver.Driver {
// GetDriverNoCleanup create a new driver with given name or return an
// existing driver with the name updating the reference count. Call
// PutDriver when done with the driver.
func GetDriverNoCleanup(t testing.TB, name string, options ...string) graphdriver.Driver {
if drv == nil {
drv = newDriver(t, name, options)
} else {
Expand All @@ -87,11 +96,17 @@ func GetDriver(t testing.TB, name string, options ...string) graphdriver.Driver
return drv
}

func GetDriver(t testing.TB, name string, options ...string) graphdriver.Driver {
d := GetDriverNoCleanup(t, name, options...)
t.Cleanup(func() { PutDriver(t) })
return d
}

func ReconfigureDriver(t testing.TB, name string, options ...string) {
if err := drv.Cleanup(); err != nil {
t.Fatal(err)
}
drv.Driver = newGraphDriver(t, name, options, drv.root, drv.runroot)
drv.Driver = newGraphDriver(t, name, options, drv.root, drv.runRoot)
}

// PutDriver removes the driver if it is no longer used and updates the reference count.
Expand All @@ -110,7 +125,6 @@ func PutDriver(t testing.TB) {
func DriverTestCreateEmpty(t testing.TB, drivername string, driverOptions ...string) {
driver := GetDriver(t, drivername, driverOptions...)
require.NotNil(t, drv.Driver, "initializing driver")
defer PutDriver(t)

err := driver.Create("empty", "", nil)
require.NoError(t, err)
Expand Down Expand Up @@ -138,7 +152,6 @@ func DriverTestCreateEmpty(t testing.TB, drivername string, driverOptions ...str
func DriverTestCreateBase(t testing.TB, drivername string, driverOptions ...string) {
driver := GetDriver(t, drivername, driverOptions...)
require.NotNil(t, drv.Driver, "initializing driver")
defer PutDriver(t)

createBase(t, driver, "Base1")
verifyBase(t, driver, "Base1", defaultPerms)
Expand All @@ -148,7 +161,6 @@ func DriverTestCreateBase(t testing.TB, drivername string, driverOptions ...stri
func DriverTestCreateSnap(t testing.TB, drivername string, driverOptions ...string) {
driver := GetDriver(t, drivername, driverOptions...)
require.NotNil(t, drv.Driver, "initializing driver")
defer PutDriver(t)

createBase(t, driver, "Base2")

Expand Down Expand Up @@ -177,7 +189,6 @@ func DriverTestCreateSnap(t testing.TB, drivername string, driverOptions ...stri
func DriverTestCreateFromTemplate(t testing.TB, drivername string, driverOptions ...string) {
driver := GetDriver(t, drivername, driverOptions...)
require.NotNil(t, drv.Driver, "initializing driver")
defer PutDriver(t)

createBase(t, driver, "Base3")
verifyBase(t, driver, "Base3", defaultPerms)
Expand Down Expand Up @@ -252,7 +263,6 @@ func DriverTestCreateFromTemplate(t testing.TB, drivername string, driverOptions
func DriverTestDeepLayerRead(t testing.TB, layerCount int, drivername string, driverOptions ...string) {
driver := GetDriver(t, drivername, driverOptions...)
require.NotNil(t, drv.Driver, "initializing driver")
defer PutDriver(t)

base := stringid.GenerateRandomID()
if err := driver.Create(base, "", nil); err != nil {
Expand Down Expand Up @@ -284,7 +294,6 @@ func DriverTestDeepLayerRead(t testing.TB, layerCount int, drivername string, dr
func DriverTestDiffApply(t testing.TB, fileCount int, drivername string, driverOptions ...string) {
driver := GetDriver(t, drivername, driverOptions...)
require.NotNil(t, drv.Driver, "initializing driver")
defer PutDriver(t)
base := stringid.GenerateRandomID()
upper := stringid.GenerateRandomID()
deleteFile := "file-remove.txt"
Expand Down Expand Up @@ -379,7 +388,6 @@ func DriverTestDiffApply(t testing.TB, fileCount int, drivername string, driverO
func DriverTestChanges(t testing.TB, drivername string, driverOptions ...string) {
driver := GetDriver(t, drivername, driverOptions...)
require.NotNil(t, drv.Driver, "initializing driver")
defer PutDriver(t)
base := stringid.GenerateRandomID()
upper := stringid.GenerateRandomID()

Expand Down Expand Up @@ -424,7 +432,6 @@ func writeRandomFile(path string, size uint64) error {
func DriverTestSetQuota(t *testing.T, drivername string) {
driver := GetDriver(t, drivername)
require.NotNil(t, drv.Driver, "initializing driver")
defer PutDriver(t)

createBase(t, driver, "Base4")
verifyBase(t, driver, "Base4", defaultPerms)
Expand Down Expand Up @@ -457,7 +464,6 @@ func DriverTestSetQuota(t *testing.T, drivername string) {
func DriverTestEcho(t testing.TB, drivername string, driverOptions ...string) {
driver := GetDriver(t, drivername, driverOptions...)
require.NotNil(t, drv.Driver, "initializing driver")
defer PutDriver(t)
var err error
var root string
components := 10
Expand Down Expand Up @@ -574,7 +580,6 @@ func DriverTestEcho(t testing.TB, drivername string, driverOptions ...string) {
func DriverTestListLayers(t testing.TB, drivername string, driverOptions ...string) {
driver := GetDriver(t, drivername, driverOptions...)
require.NotNil(t, drv.Driver, "initializing driver")
defer PutDriver(t)
base := stringid.GenerateRandomID()
mid := stringid.GenerateRandomID()
upper := stringid.GenerateRandomID()
Expand Down
1 change: 0 additions & 1 deletion drivers/overlay/overlay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func skipIfNaive(t *testing.T) {
// different from the other tests.
func TestContainersOverlayXattr(t *testing.T) {
driver := graphtest.GetDriver(t, driverName, "force_mask=700")
defer graphtest.PutDriver(t)
require.NoError(t, driver.Create("lower", "", nil))
graphtest.ReconfigureDriver(t, driverName)
require.NoError(t, driver.Create("upper", "lower", nil))
Expand Down
2 changes: 1 addition & 1 deletion drivers/vfs/vfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func init() {
// This avoids creating a new driver for each test if all tests are run
// Make sure to put new tests between TestVfsSetup and TestVfsTeardown
func TestVfsSetup(t *testing.T) {
graphtest.GetDriver(t, "vfs")
graphtest.GetDriverNoCleanup(t, "vfs")
}

func TestVfsCreateEmpty(t *testing.T) {
Expand Down