@@ -21,13 +21,14 @@ import (
21
21
"os/exec"
22
22
"path/filepath"
23
23
"strings"
24
- "syscall"
25
24
"testing"
26
25
27
26
"github.com/pkg/errors"
28
27
"github.com/stretchr/testify/require"
29
28
)
30
29
30
+ const mib = 1024 * 1024
31
+
31
32
// CreateFSImg will create a file containing a filesystem image of the provided type containing
32
33
// the provided files. It returns the path at which the image file can be found.
33
34
func CreateFSImg (ctx context.Context , t * testing.T , fsType string , testFiles ... FSImgFile ) string {
@@ -73,46 +74,37 @@ func createTestExtImg(ctx context.Context, t *testing.T, extName string, testFil
73
74
return imgFile .Name ()
74
75
}
75
76
76
- // BlockDeviceFile represents a block device
77
- type BlockDeviceFile struct {
78
- Subpath string
79
- UID int
80
- GID int
81
- Dev int // major number
82
- }
83
-
84
77
// CreateBlockDevice creates a block device, or block special file for testing
85
- func CreateBlockDevice (ctx context.Context , t * testing.T , fsType string , testFile BlockDeviceFile ) string {
78
+ func CreateBlockDevice (ctx context.Context , t * testing.T ) ( string , func ()) {
86
79
t .Helper ()
87
80
88
- switch fsType {
89
- case "ext4" :
90
- return createTestBlockDevice (ctx , t , fsType , testFile )
91
- default :
92
- require .FailNowf (t , "unsupported fs type %q" , fsType )
93
- return ""
94
- }
95
- }
81
+ f , err := ioutil .TempFile ("" , "" )
82
+ require .NoError (t , err )
96
83
97
- func createTestBlockDevice ( ctx context. Context , t * testing. T , extName string , testFile BlockDeviceFile ) string {
98
- t . Helper ( )
84
+ err = f . Truncate ( 32 * mib )
85
+ require . NoError ( t , err )
99
86
100
- deviceFile := filepath .Join ("/tmp/blockExample" , testFile .Subpath )
101
- err := os .MkdirAll (filepath .Dir (deviceFile ), 0750 )
102
- require .NoError (t , err , "failed to mkdir for the block device" )
87
+ out , err := exec .CommandContext (ctx , "mkfs.ext4" , "-v" , f .Name ()).CombinedOutput ()
88
+ require .NoErrorf (t , err , "failed to create ext img, command out:%s \n " , string (out ))
103
89
104
- err = syscall . Mknod ( deviceFile , syscall . S_IFBLK , testFile . Dev )
105
- require .NoError (t , err , "failed to create a new block device" )
90
+ err = f . Close ( )
91
+ require .NoError (t , err )
106
92
107
- err = os . Chmod ( deviceFile , 0600 )
108
- require .NoError (t , err , "failed to change file mode for the new created block device" )
93
+ out , err = exec . CommandContext ( ctx , "losetup" , "--show" , "--find" , f . Name ()). CombinedOutput ( )
94
+ require .NoError (t , err )
109
95
110
- err = os .Chown (deviceFile , testFile .UID , testFile .GID )
111
- require .NoError (t , err , "failed to grant permission to the new created block device" )
96
+ device := strings .TrimRight (string (out ), "\n " )
112
97
113
- output , err := exec .CommandContext (ctx , "mkfs." + extName , "-v" , deviceFile ).CombinedOutput ()
114
- require .NoErrorf (t , err , "failed to create ext img, command output:%s \n " , string (output ))
115
- return deviceFile
98
+ err = os .Chmod (device , 0600 )
99
+ require .NoError (t , err , "failed to change file mode for the new created block device" )
100
+
101
+ return device , func () {
102
+ out , err := exec .CommandContext (ctx , "losetup" , "--detach" , device ).CombinedOutput ()
103
+ if len (out ) > 0 {
104
+ t .Logf ("losetup --detach: %s" , out )
105
+ }
106
+ require .NoError (t , err )
107
+ }
116
108
}
117
109
118
110
// MountInfo holds data parsed from a line of /proc/mounts
0 commit comments