@@ -30,6 +30,8 @@ import (
30
30
"github.com/docker/docker/api/types"
31
31
"github.com/docker/docker/api/types/container"
32
32
"github.com/docker/docker/api/types/filters"
33
+ "github.com/docker/docker/api/types/mount"
34
+ "github.com/docker/docker/api/types/volume"
33
35
"github.com/docker/docker/client"
34
36
"github.com/docker/docker/pkg/stdcopy"
35
37
"github.com/go-git/go-billy/v5/memfs"
@@ -1354,6 +1356,40 @@ COPY --from=a /root/date.txt /date.txt`, testImageAlpine, testImageAlpine),
1354
1356
})
1355
1357
}
1356
1358
1359
+ func TestChownHomedir (t * testing.T ) {
1360
+ t .Parallel ()
1361
+
1362
+ // Ensures that a Git repository with a devcontainer.json is cloned and built.
1363
+ srv := createGitServer (t , gitServerOptions {
1364
+ files : map [string ]string {
1365
+ ".devcontainer/devcontainer.json" : `{
1366
+ "name": "Test",
1367
+ "build": {
1368
+ "dockerfile": "Dockerfile"
1369
+ },
1370
+ }` ,
1371
+ ".devcontainer/Dockerfile" : fmt .Sprintf (`FROM %s
1372
+ RUN useradd test \
1373
+ --create-home \
1374
+ --shell=/bin/bash \
1375
+ --uid=1001 \
1376
+ --user-group
1377
+ USER test
1378
+ ` , testImageUbuntu ), // Note: this isn't reproducible with Alpine for some reason.
1379
+ },
1380
+ })
1381
+
1382
+ // Run envbuilder with a Docker volume mounted to homedir
1383
+ volName := fmt .Sprintf ("%s%d-home" , t .Name (), time .Now ().Unix ())
1384
+ ctr , err := runEnvbuilder (t , options {env : []string {
1385
+ envbuilderEnv ("GIT_URL" , srv .URL ),
1386
+ }, volumes : map [string ]string {volName : "/home/test" }})
1387
+ require .NoError (t , err )
1388
+
1389
+ output := execContainer (t , ctr , "stat -c %u:%g /home/test/" )
1390
+ require .Equal (t , "1001:1001" , strings .TrimSpace (output ))
1391
+ }
1392
+
1357
1393
type setupInMemoryRegistryOpts struct {
1358
1394
Username string
1359
1395
Password string
@@ -1465,8 +1501,9 @@ func cleanOldEnvbuilders() {
1465
1501
}
1466
1502
1467
1503
type options struct {
1468
- binds []string
1469
- env []string
1504
+ binds []string
1505
+ env []string
1506
+ volumes map [string ]string
1470
1507
}
1471
1508
1472
1509
// runEnvbuilder starts the envbuilder container with the given environment
@@ -1479,6 +1516,21 @@ func runEnvbuilder(t *testing.T, options options) (string, error) {
1479
1516
t .Cleanup (func () {
1480
1517
cli .Close ()
1481
1518
})
1519
+ mounts := make ([]mount.Mount , 0 )
1520
+ for volName , volPath := range options .volumes {
1521
+ mounts = append (mounts , mount.Mount {
1522
+ Type : mount .TypeVolume ,
1523
+ Source : volName ,
1524
+ Target : volPath ,
1525
+ })
1526
+ _ , err = cli .VolumeCreate (ctx , volume.CreateOptions {
1527
+ Name : volName ,
1528
+ })
1529
+ require .NoError (t , err )
1530
+ t .Cleanup (func () {
1531
+ _ = cli .VolumeRemove (ctx , volName , true )
1532
+ })
1533
+ }
1482
1534
ctr , err := cli .ContainerCreate (ctx , & container.Config {
1483
1535
Image : "envbuilder:latest" ,
1484
1536
Env : options .env ,
@@ -1488,10 +1540,11 @@ func runEnvbuilder(t *testing.T, options options) (string, error) {
1488
1540
}, & container.HostConfig {
1489
1541
NetworkMode : container .NetworkMode ("host" ),
1490
1542
Binds : options .binds ,
1543
+ Mounts : mounts ,
1491
1544
}, nil , nil , "" )
1492
1545
require .NoError (t , err )
1493
1546
t .Cleanup (func () {
1494
- cli .ContainerRemove (ctx , ctr .ID , container.RemoveOptions {
1547
+ _ = cli .ContainerRemove (ctx , ctr .ID , container.RemoveOptions {
1495
1548
RemoveVolumes : true ,
1496
1549
Force : true ,
1497
1550
})
0 commit comments