2018-02-05 16:05:59 -05:00
|
|
|
package dockerfile // import "github.com/docker/docker/builder/dockerfile"
|
2018-01-12 20:57:50 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2017-11-16 01:20:33 -05:00
|
|
|
"github.com/docker/docker/api/types"
|
2018-01-12 20:57:50 -05:00
|
|
|
"github.com/docker/docker/pkg/idtools"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2018-01-12 20:57:50 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestChownFlagParsing(t *testing.T) {
|
|
|
|
testFiles := map[string]string{
|
|
|
|
"passwd": `root:x:0:0::/bin:/bin/false
|
|
|
|
bin:x:1:1::/bin:/bin/false
|
|
|
|
wwwwww:x:21:33::/bin:/bin/false
|
|
|
|
unicorn:x:1001:1002::/bin:/bin/false
|
|
|
|
`,
|
|
|
|
"group": `root:x:0:
|
|
|
|
bin:x:1:
|
|
|
|
wwwwww:x:33:
|
|
|
|
unicorn:x:1002:
|
|
|
|
somegrp:x:5555:
|
|
|
|
othergrp:x:6666:
|
|
|
|
`,
|
|
|
|
}
|
|
|
|
// test mappings for validating use of maps
|
|
|
|
idMaps := []idtools.IDMap{
|
|
|
|
{
|
|
|
|
ContainerID: 0,
|
|
|
|
HostID: 100000,
|
|
|
|
Size: 65536,
|
|
|
|
},
|
|
|
|
}
|
2022-03-14 15:24:29 -04:00
|
|
|
remapped := idtools.IdentityMapping{UIDMaps: idMaps, GIDMaps: idMaps}
|
|
|
|
unmapped := idtools.IdentityMapping{}
|
2018-01-12 20:57:50 -05:00
|
|
|
|
|
|
|
contextDir, cleanup := createTestTempDir(t, "", "builder-chown-parse-test")
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
if err := os.Mkdir(filepath.Join(contextDir, "etc"), 0755); err != nil {
|
|
|
|
t.Fatalf("error creating test directory: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for filename, content := range testFiles {
|
|
|
|
createTestTempFile(t, filepath.Join(contextDir, "etc"), filename, content, 0644)
|
|
|
|
}
|
|
|
|
|
|
|
|
// positive tests
|
|
|
|
for _, testcase := range []struct {
|
2017-11-16 01:20:33 -05:00
|
|
|
builder *Builder
|
2018-01-12 20:57:50 -05:00
|
|
|
name string
|
|
|
|
chownStr string
|
2022-03-14 15:24:29 -04:00
|
|
|
idMapping idtools.IdentityMapping
|
2017-11-16 01:20:33 -05:00
|
|
|
state *dispatchState
|
|
|
|
expected idtools.Identity
|
2018-01-12 20:57:50 -05:00
|
|
|
}{
|
|
|
|
{
|
2017-11-16 01:20:33 -05:00
|
|
|
builder: &Builder{options: &types.ImageBuildOptions{Platform: "linux"}},
|
2018-01-12 20:57:50 -05:00
|
|
|
name: "UIDNoMap",
|
|
|
|
chownStr: "1",
|
|
|
|
idMapping: unmapped,
|
2017-11-16 01:20:33 -05:00
|
|
|
state: &dispatchState{},
|
|
|
|
expected: idtools.Identity{UID: 1, GID: 1},
|
2018-01-12 20:57:50 -05:00
|
|
|
},
|
|
|
|
{
|
2017-11-16 01:20:33 -05:00
|
|
|
builder: &Builder{options: &types.ImageBuildOptions{Platform: "linux"}},
|
2018-01-12 20:57:50 -05:00
|
|
|
name: "UIDGIDNoMap",
|
|
|
|
chownStr: "0:1",
|
|
|
|
idMapping: unmapped,
|
2017-11-16 01:20:33 -05:00
|
|
|
state: &dispatchState{},
|
|
|
|
expected: idtools.Identity{UID: 0, GID: 1},
|
2018-01-12 20:57:50 -05:00
|
|
|
},
|
|
|
|
{
|
2017-11-16 01:20:33 -05:00
|
|
|
builder: &Builder{options: &types.ImageBuildOptions{Platform: "linux"}},
|
2018-01-12 20:57:50 -05:00
|
|
|
name: "UIDWithMap",
|
|
|
|
chownStr: "0",
|
|
|
|
idMapping: remapped,
|
2017-11-16 01:20:33 -05:00
|
|
|
state: &dispatchState{},
|
|
|
|
expected: idtools.Identity{UID: 100000, GID: 100000},
|
2018-01-12 20:57:50 -05:00
|
|
|
},
|
|
|
|
{
|
2017-11-16 01:20:33 -05:00
|
|
|
builder: &Builder{options: &types.ImageBuildOptions{Platform: "linux"}},
|
2018-01-12 20:57:50 -05:00
|
|
|
name: "UIDGIDWithMap",
|
|
|
|
chownStr: "1:33",
|
|
|
|
idMapping: remapped,
|
2017-11-16 01:20:33 -05:00
|
|
|
state: &dispatchState{},
|
|
|
|
expected: idtools.Identity{UID: 100001, GID: 100033},
|
2018-01-12 20:57:50 -05:00
|
|
|
},
|
|
|
|
{
|
2017-11-16 01:20:33 -05:00
|
|
|
builder: &Builder{options: &types.ImageBuildOptions{Platform: "linux"}},
|
2018-01-12 20:57:50 -05:00
|
|
|
name: "UserNoMap",
|
|
|
|
chownStr: "bin:5555",
|
|
|
|
idMapping: unmapped,
|
2017-11-16 01:20:33 -05:00
|
|
|
state: &dispatchState{},
|
|
|
|
expected: idtools.Identity{UID: 1, GID: 5555},
|
2018-01-12 20:57:50 -05:00
|
|
|
},
|
|
|
|
{
|
2017-11-16 01:20:33 -05:00
|
|
|
builder: &Builder{options: &types.ImageBuildOptions{Platform: "linux"}},
|
2018-01-12 20:57:50 -05:00
|
|
|
name: "GroupWithMap",
|
|
|
|
chownStr: "0:unicorn",
|
|
|
|
idMapping: remapped,
|
2017-11-16 01:20:33 -05:00
|
|
|
state: &dispatchState{},
|
|
|
|
expected: idtools.Identity{UID: 100000, GID: 101002},
|
2018-01-12 20:57:50 -05:00
|
|
|
},
|
|
|
|
{
|
2017-11-16 01:20:33 -05:00
|
|
|
builder: &Builder{options: &types.ImageBuildOptions{Platform: "linux"}},
|
2018-01-12 20:57:50 -05:00
|
|
|
name: "UserOnlyWithMap",
|
|
|
|
chownStr: "unicorn",
|
|
|
|
idMapping: remapped,
|
2017-11-16 01:20:33 -05:00
|
|
|
state: &dispatchState{},
|
|
|
|
expected: idtools.Identity{UID: 101001, GID: 101002},
|
2018-01-12 20:57:50 -05:00
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(testcase.name, func(t *testing.T) {
|
2017-11-16 01:20:33 -05:00
|
|
|
idPair, err := parseChownFlag(testcase.builder, testcase.state, testcase.chownStr, contextDir, testcase.idMapping)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err, "Failed to parse chown flag: %q", testcase.chownStr)
|
|
|
|
assert.Check(t, is.DeepEqual(testcase.expected, idPair), "chown flag mapping failure")
|
2018-01-12 20:57:50 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// error tests
|
|
|
|
for _, testcase := range []struct {
|
2017-11-16 01:20:33 -05:00
|
|
|
builder *Builder
|
2018-01-12 20:57:50 -05:00
|
|
|
name string
|
|
|
|
chownStr string
|
2022-03-14 15:24:29 -04:00
|
|
|
idMapping idtools.IdentityMapping
|
2017-11-16 01:20:33 -05:00
|
|
|
state *dispatchState
|
2018-01-12 20:57:50 -05:00
|
|
|
descr string
|
|
|
|
}{
|
|
|
|
{
|
2017-11-16 01:20:33 -05:00
|
|
|
builder: &Builder{options: &types.ImageBuildOptions{Platform: "linux"}},
|
2018-01-12 20:57:50 -05:00
|
|
|
name: "BadChownFlagFormat",
|
|
|
|
chownStr: "bob:1:555",
|
|
|
|
idMapping: unmapped,
|
2017-11-16 01:20:33 -05:00
|
|
|
state: &dispatchState{},
|
2018-01-12 20:57:50 -05:00
|
|
|
descr: "invalid chown string format: bob:1:555",
|
|
|
|
},
|
|
|
|
{
|
2017-11-16 01:20:33 -05:00
|
|
|
builder: &Builder{options: &types.ImageBuildOptions{Platform: "linux"}},
|
2018-01-12 20:57:50 -05:00
|
|
|
name: "UserNoExist",
|
|
|
|
chownStr: "bob",
|
|
|
|
idMapping: unmapped,
|
2017-11-16 01:20:33 -05:00
|
|
|
state: &dispatchState{},
|
2018-01-12 20:57:50 -05:00
|
|
|
descr: "can't find uid for user bob: no such user: bob",
|
|
|
|
},
|
|
|
|
{
|
2017-11-16 01:20:33 -05:00
|
|
|
builder: &Builder{options: &types.ImageBuildOptions{Platform: "linux"}},
|
2018-01-12 20:57:50 -05:00
|
|
|
name: "GroupNoExist",
|
|
|
|
chownStr: "root:bob",
|
|
|
|
idMapping: unmapped,
|
2017-11-16 01:20:33 -05:00
|
|
|
state: &dispatchState{},
|
2018-01-12 20:57:50 -05:00
|
|
|
descr: "can't find gid for group bob: no such group: bob",
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(testcase.name, func(t *testing.T) {
|
2017-11-16 01:20:33 -05:00
|
|
|
_, err := parseChownFlag(testcase.builder, testcase.state, testcase.chownStr, contextDir, testcase.idMapping)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Error(err, testcase.descr), "Expected error string doesn't match")
|
2018-01-12 20:57:50 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|