1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
//go:build linux
package loopback
import (
"os"
"sync"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
maxDevicesPerGoroutine = 1000
maxGoroutines = 10
)
func TestAttachLoopbackDeviceRace(t *testing.T) {
_, err := os.Stat("/dev/loop-control")
if err != nil {
t.Skip("Cannot execute test with '/dev/loop-control' absent")
}
f, err := os.OpenFile("/dev/loop-control", os.O_RDONLY, 0o644)
if err != nil {
t.Skip("Cannot execute test with '/dev/loop-control' unreadable")
}
f.Close()
createLoopbackDevice := func() {
// Create a file to use as a backing file
f, err := os.CreateTemp(t.TempDir(), "loopback-test")
require.NoError(t, err)
defer f.Close()
defer os.Remove(f.Name())
lp, err := AttachLoopDevice(f.Name())
assert.NoError(t, err)
assert.NotNil(t, lp, "loopback device file should not be nil")
if lp != nil {
lp.Close()
}
}
wg := sync.WaitGroup{}
for range maxGoroutines {
wg.Add(1)
go func() {
defer wg.Done()
for range maxDevicesPerGoroutine {
createLoopbackDevice()
}
}()
}
wg.Wait()
}
|