File: attach_test.go

package info (click to toggle)
golang-github-containers-storage 1.59.1%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,184 kB
  • sloc: sh: 630; ansic: 389; makefile: 143; awk: 12
file content (58 lines) | stat: -rw-r--r-- 1,126 bytes parent folder | download | duplicates (3)
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()
}