@@ -21,7 +21,6 @@ import (
21
21
"github.com/aws/aws-sdk-go/aws"
22
22
"github.com/aws/aws-sdk-go/service/ecs"
23
23
"github.com/docker/libcompose/config"
24
- "github.com/docker/libcompose/project"
25
24
"github.com/docker/libcompose/yaml"
26
25
"github.com/stretchr/testify/assert"
27
26
)
@@ -205,20 +204,22 @@ func verifyPortMapping(t *testing.T, output *ecs.PortMapping, hostPort, containe
205
204
}
206
205
207
206
func TestConvertToMountPoints (t * testing.T ) {
207
+ // Valid inputs with host and container paths set:
208
+ // /tmp/cache
209
+ // /tmp/cache2
210
+ // ./cache:/tmp/cache
211
+ // /tmp/cache:ro
212
+ // ./cache:/tmp/cache:ro
213
+ // ./cache:/tmp/cache:rw
214
+ // named_volume:/tmp/cache
208
215
onlyContainerPath := yaml.Volume {Destination : containerPath }
209
216
onlyContainerPath2 := yaml.Volume {Destination : containerPath2 }
210
- hostAndContainerPath := yaml.Volume {Source : hostPath , Destination : containerPath } // "./cache:/tmp/cache"
217
+ hostAndContainerPath := yaml.Volume {Source : hostPath , Destination : containerPath }
211
218
onlyContainerPathWithRO := yaml.Volume {Destination : containerPath , AccessMode : "ro" }
212
- hostAndContainerPathWithRO := yaml.Volume {Source : hostPath , Destination : containerPath , AccessMode : "ro" } // "./cache:/tmp/cache:ro"
219
+ hostAndContainerPathWithRO := yaml.Volume {Source : hostPath , Destination : containerPath , AccessMode : "ro" }
213
220
hostAndContainerPathWithRW := yaml.Volume {Source : hostPath , Destination : containerPath , AccessMode : "rw" }
214
221
namedVolumeAndContainerPath := yaml.Volume {Source : namedVolume , Destination : containerPath }
215
222
216
- volumes := & Volumes {
217
- VolumeWithHost : make (map [string ]string ), // map with key:=hostSourcePath value:=VolumeName
218
- VolumeEmptyHost : []string {namedVolume }, // Declare one volume with an empty host
219
- }
220
-
221
- // Valid inputs with host and container paths set
222
223
mountPointsIn := yaml.Volumes {
223
224
Volumes : []* yaml.Volume {
224
225
& onlyContainerPath ,
@@ -231,21 +232,59 @@ func TestConvertToMountPoints(t *testing.T) {
231
232
},
232
233
}
233
234
234
- mountPointsOut , err := ConvertToMountPoints (& mountPointsIn , volumes )
235
- if err != nil {
236
- t .Fatalf ("Expected to convert [%v] mountPoints without errors. But got [%v]" , mountPointsIn , err )
235
+ expectedVolumeWithHost := map [string ]string {hostPath : "volume-3" }
236
+ expectedVolumeEmptyHost := []string {namedVolume , "volume-1" , "volume-2" , "volume-4" }
237
+ expectedMountPoints := []* ecs.MountPoint {
238
+ {
239
+ ContainerPath : aws .String ("/tmp/cache" ),
240
+ ReadOnly : aws .Bool (false ),
241
+ SourceVolume : aws .String ("volume-1" ),
242
+ },
243
+ {
244
+ ContainerPath : aws .String ("/tmp/cache2" ),
245
+ ReadOnly : aws .Bool (false ),
246
+ SourceVolume : aws .String ("volume-2" ),
247
+ },
248
+ {
249
+ ContainerPath : aws .String ("/tmp/cache" ),
250
+ ReadOnly : aws .Bool (false ),
251
+ SourceVolume : aws .String ("volume-3" ),
252
+ },
253
+ {
254
+ ContainerPath : aws .String ("/tmp/cache" ),
255
+ ReadOnly : aws .Bool (true ),
256
+ SourceVolume : aws .String ("volume-4" ),
257
+ },
258
+ {
259
+ ContainerPath : aws .String ("/tmp/cache" ),
260
+ ReadOnly : aws .Bool (true ),
261
+ SourceVolume : aws .String ("volume-3" ),
262
+ },
263
+ {
264
+ ContainerPath : aws .String ("/tmp/cache" ),
265
+ ReadOnly : aws .Bool (false ),
266
+ SourceVolume : aws .String ("volume-3" ),
267
+ },
268
+ {
269
+ ContainerPath : aws .String ("/tmp/cache" ),
270
+ ReadOnly : aws .Bool (false ),
271
+ SourceVolume : aws .String ("named_volume" ),
272
+ },
237
273
}
238
- if len (mountPointsIn .Volumes ) != len (mountPointsOut ) {
239
- t .Errorf ("Incorrect conversion. Input [%v] Output [%v]" , mountPointsIn , mountPointsOut )
274
+
275
+ volumes := & Volumes {
276
+ VolumeWithHost : make (map [string ]string ), // This field should be empty before ConvertToMountPoints is called
277
+ VolumeEmptyHost : []string {namedVolume }, // We expect ConvertToVolumes to already have been called, so any named volumes should have been added to VolumeEmptyHost
240
278
}
241
279
242
- verifyMountPoint (t , mountPointsOut [0 ], volumes , "" , containerPath , false , 1 ) // 1 is the counter for the first volume with an empty host path
243
- verifyMountPoint (t , mountPointsOut [1 ], volumes , "" , containerPath2 , false , 2 ) // 2 is the counter for the second volume with an empty host path
244
- verifyMountPoint (t , mountPointsOut [2 ], volumes , hostPath , containerPath , false , 2 )
245
- verifyMountPoint (t , mountPointsOut [3 ], volumes , "" , containerPath , true , 3 ) // 3 is the counter for the third volume with an empty host path
246
- verifyMountPoint (t , mountPointsOut [4 ], volumes , hostPath , containerPath , true , 3 )
247
- verifyMountPoint (t , mountPointsOut [5 ], volumes , hostPath , containerPath , false , 3 )
248
- verifyMountPoint (t , mountPointsOut [6 ], volumes , namedVolume , containerPath , false , 3 )
280
+ mountPointsOut , err := ConvertToMountPoints (& mountPointsIn , volumes )
281
+ assert .NoError (t , err , "Unexpected error converting MountPoints" )
282
+
283
+ // Expect top-level volumes fields to be populated
284
+ assert .Equal (t , expectedVolumeWithHost , volumes .VolumeWithHost , "Expected volumeWithHost to match" )
285
+ assert .Equal (t , expectedVolumeEmptyHost , volumes .VolumeEmptyHost , "Expected volumeEmptyHost to match" )
286
+
287
+ assert .ElementsMatch (t , expectedMountPoints , mountPointsOut , "Expected Mount Points to match" )
249
288
250
289
if mountPointsOut [0 ].SourceVolume == mountPointsOut [1 ].SourceVolume {
251
290
t .Errorf ("Expected volume %v (onlyContainerPath) and %v (onlyContainerPath2) to be different" , mountPointsOut [0 ].SourceVolume , mountPointsOut [1 ].SourceVolume )
@@ -273,10 +312,7 @@ func TestConvertToMountPointsWithInvalidAccessMode(t *testing.T) {
273
312
}
274
313
275
314
_ , err := ConvertToMountPoints (& mountPointsIn , volumes )
276
-
277
- if err == nil {
278
- t .Errorf ("Expected to get error for mountPoint[%s] but didn't." , hostAndContainerPathWithIncorrectAccess )
279
- }
315
+ assert .Error (t , err , "Expected error converting MountPoints" )
280
316
}
281
317
282
318
func TestConvertToMountPointsNullContainerVolumes (t * testing.T ) {
@@ -285,33 +321,27 @@ func TestConvertToMountPointsNullContainerVolumes(t *testing.T) {
285
321
VolumeEmptyHost : []string {namedVolume },
286
322
}
287
323
mountPointsOut , err := ConvertToMountPoints (nil , volumes )
288
- if err != nil {
289
- t .Fatalf ("Expected to convert nil mountPoints without errors. But got [%v]" , err )
290
- }
291
- if len (mountPointsOut ) != 0 {
292
- t .Errorf ("Incorrect conversion. Input nil Output [%v]" , mountPointsOut )
293
- }
324
+ assert .NoError (t , err , "Unexpected error converting MountPoints" )
325
+ assert .Empty (t , mountPointsOut , "Expected mount points to be empty" )
294
326
}
295
327
296
- func verifyMountPoint (t * testing.T , output * ecs.MountPoint , volumes * Volumes ,
297
- source , containerPath string , readonly bool , EmptyHostCtr int ) {
298
- sourceVolume := ""
299
- if containerPath != * output .ContainerPath {
300
- t .Errorf ("Expected containerPath [%s] But was [%s]" , containerPath , * output .ContainerPath )
301
- }
302
- if source == "" {
303
- sourceVolume = volumes .VolumeEmptyHost [EmptyHostCtr ]
304
- } else if project .IsNamedVolume (source ) {
305
- sourceVolume = source
306
- } else {
307
- sourceVolume = volumes .VolumeWithHost [source ]
328
+ func TestConvertToMountPointsWithNoCorrespondingNamedVolume (t * testing.T ) {
329
+ volumes := & Volumes {
330
+ VolumeWithHost : make (map [string ]string ),
331
+ VolumeEmptyHost : []string {}, // Top-level named volumes is empty
308
332
}
309
- if sourceVolume != * output .SourceVolume {
310
- t .Errorf ("Expected sourceVolume [%s] But was [%s]" , sourceVolume , * output .SourceVolume )
333
+
334
+ namedVolume := yaml.Volume {
335
+ Source : namedVolume ,
336
+ Destination : containerPath ,
311
337
}
312
- if readonly != * output .ReadOnly {
313
- t .Errorf ("Expected readonly [%v] But was [%v]" , readonly , * output .ReadOnly )
338
+
339
+ mountPointsIn := yaml.Volumes {
340
+ Volumes : []* yaml.Volume {& namedVolume },
314
341
}
342
+
343
+ _ , err := ConvertToMountPoints (& mountPointsIn , volumes )
344
+ assert .Error (t , err , "Expected error converting MountPoints" )
315
345
}
316
346
317
347
func TestConvertToExtraHosts (t * testing.T ) {
0 commit comments