@@ -27,8 +27,8 @@ import (
27
27
)
28
28
29
29
const (
30
- devicePath string = "testDevice"
31
- deviceName string = "testDeviceName"
30
+ defaultBackingImage = "testDevice"
31
+ deviceName = "testDeviceName"
32
32
)
33
33
34
34
var toolsEnvs = []string {"CP" , "DD" , "RM" , "FSCK_EXT4" , "MKFS_EXT4" , "BLKID" , "FSCK" , "MOUNT" , "UMOUNT" }
@@ -57,20 +57,35 @@ func addToolsToPATH() error {
57
57
return nil
58
58
}
59
59
60
- func setup (devicePath string ) {
61
- if err := exec .Command ("dd" , "if=/dev/zero" , fmt .Sprintf ("of=%s" , devicePath ), "bs=64M" , "count=1" ).Run (); err != nil {
60
+ func setup (backingDisk string ) string {
61
+ if err := exec .Command ("dd" , "if=/dev/zero" , fmt .Sprintf ("of=%s" , backingDisk ), "bs=64M" , "count=1" ).Run (); err != nil {
62
62
panic (err )
63
63
}
64
+ out , err := exec .Command ("losetup" , "-f" , "--show" , backingDisk ).CombinedOutput ()
65
+ if err != nil {
66
+ panic (err )
67
+ }
68
+ return strings .TrimSpace (string (out ))
64
69
}
65
70
66
- func teardown (devicePath string ) {
67
- if err := exec .Command ("rm" , "-f" , devicePath ).Run (); err != nil {
71
+ func teardown (backingImage , devicePath string ) {
72
+ if err := exec .Command ("losetup" , "-d" , devicePath ).Run (); err != nil {
73
+ panic (err )
74
+ }
75
+ if err := exec .Command ("rm" , "-f" , backingImage ).Run (); err != nil {
68
76
panic (err )
69
77
}
70
78
}
71
79
72
- func cp (source , target string ) error {
73
- return exec .Command ("cp" , source , target ).Run ()
80
+ func cp (source , target string ) (string , error ) {
81
+ if err := exec .Command ("cp" , source , target ).Run (); err != nil {
82
+ return "" , err
83
+ }
84
+ out , err := exec .Command ("losetup" , "-f" , "--show" , target ).CombinedOutput ()
85
+ if err != nil {
86
+ return "" , err
87
+ }
88
+ return strings .TrimSpace (string (out )), nil
74
89
}
75
90
76
91
func resize (devicePath string ) {
@@ -100,8 +115,8 @@ func TestMain(m *testing.M) {
100
115
func TestOpenAndClose (t * testing.T ) {
101
116
assert := assert .New (t )
102
117
require := require .New (t )
103
- setup (devicePath )
104
- defer teardown (devicePath )
118
+ devicePath := setup (defaultBackingImage )
119
+ defer teardown (defaultBackingImage , devicePath )
105
120
106
121
mapper := cryptmapper .New (& fakeKMS {})
107
122
@@ -124,7 +139,7 @@ func TestOpenAndClose(t *testing.T) {
124
139
assert .Equal (newPath , newPath2 )
125
140
126
141
// Resize the device
127
- resize (devicePath )
142
+ resize (defaultBackingImage )
128
143
129
144
resizedPath , err := mapper .ResizeCryptDevice (t .Context (), deviceName )
130
145
require .NoError (err )
@@ -145,8 +160,8 @@ func TestOpenAndClose(t *testing.T) {
145
160
func TestOpenAndCloseIntegrity (t * testing.T ) {
146
161
assert := assert .New (t )
147
162
require := require .New (t )
148
- setup (devicePath )
149
- defer teardown (devicePath )
163
+ devicePath := setup (defaultBackingImage )
164
+ defer teardown (defaultBackingImage , devicePath )
150
165
151
166
mapper := cryptmapper .New (& fakeKMS {})
152
167
@@ -167,7 +182,7 @@ func TestOpenAndCloseIntegrity(t *testing.T) {
167
182
assert .Equal (newPath , newPath2 )
168
183
169
184
// integrity devices do not support resizing
170
- resize (devicePath )
185
+ resize (defaultBackingImage )
171
186
_ , err = mapper .ResizeCryptDevice (t .Context (), deviceName )
172
187
assert .Error (err )
173
188
@@ -189,18 +204,19 @@ func TestOpenAndCloseIntegrity(t *testing.T) {
189
204
func TestDeviceCloning (t * testing.T ) {
190
205
assert := assert .New (t )
191
206
require := require .New (t )
192
- setup (devicePath )
193
- defer teardown (devicePath )
207
+ devicePath := setup (defaultBackingImage )
208
+ defer teardown (defaultBackingImage , devicePath )
194
209
195
210
mapper := cryptmapper .New (& dynamicKMS {})
196
211
197
212
_ , err := mapper .OpenCryptDevice (t .Context (), devicePath , deviceName , false )
198
213
assert .NoError (err )
199
214
200
- require .NoError (cp (devicePath , devicePath + "-copy" ))
201
- defer teardown (devicePath + "-copy" )
215
+ cpDevice , err := cp (defaultBackingImage , defaultBackingImage + "-copy" )
216
+ require .NoError (err )
217
+ defer teardown (defaultBackingImage + "-copy" , cpDevice )
202
218
203
- _ , err = mapper .OpenCryptDevice (t .Context (), devicePath + "-copy" , deviceName + "-copy" , false )
219
+ _ , err = mapper .OpenCryptDevice (t .Context (), cpDevice , deviceName + "-copy" , false )
204
220
assert .NoError (err )
205
221
206
222
assert .NoError (mapper .CloseCryptDevice (deviceName ))
@@ -209,12 +225,12 @@ func TestDeviceCloning(t *testing.T) {
209
225
210
226
func TestConcurrency (t * testing.T ) {
211
227
assert := assert .New (t )
212
- setup (devicePath )
213
- defer teardown (devicePath )
228
+ devicePath := setup (defaultBackingImage )
229
+ defer teardown (defaultBackingImage , devicePath )
214
230
215
- device2 := devicePath + "-2"
216
- setup (device2 )
217
- defer teardown (device2 )
231
+ backingImage2 := defaultBackingImage + "-2"
232
+ device2 := setup (backingImage2 )
233
+ defer teardown (backingImage2 , device2 )
218
234
219
235
mapper := cryptmapper .New (& fakeKMS {})
220
236
0 commit comments