github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/builder/dockerfile/internals_linux_test.go (about) 1 package dockerfile // import "github.com/demonoid81/moby/builder/dockerfile" 2 3 import ( 4 "os" 5 "path/filepath" 6 "testing" 7 8 "github.com/demonoid81/moby/api/types" 9 "github.com/demonoid81/moby/pkg/idtools" 10 "gotest.tools/v3/assert" 11 is "gotest.tools/v3/assert/cmp" 12 ) 13 14 func TestChownFlagParsing(t *testing.T) { 15 testFiles := map[string]string{ 16 "passwd": `root:x:0:0::/bin:/bin/false 17 bin:x:1:1::/bin:/bin/false 18 wwwwww:x:21:33::/bin:/bin/false 19 unicorn:x:1001:1002::/bin:/bin/false 20 `, 21 "group": `root:x:0: 22 bin:x:1: 23 wwwwww:x:33: 24 unicorn:x:1002: 25 somegrp:x:5555: 26 othergrp:x:6666: 27 `, 28 } 29 // test mappings for validating use of maps 30 idMaps := []idtools.IDMap{ 31 { 32 ContainerID: 0, 33 HostID: 100000, 34 Size: 65536, 35 }, 36 } 37 remapped := idtools.NewIDMappingsFromMaps(idMaps, idMaps) 38 unmapped := &idtools.IdentityMapping{} 39 40 contextDir, cleanup := createTestTempDir(t, "", "builder-chown-parse-test") 41 defer cleanup() 42 43 if err := os.Mkdir(filepath.Join(contextDir, "etc"), 0755); err != nil { 44 t.Fatalf("error creating test directory: %v", err) 45 } 46 47 for filename, content := range testFiles { 48 createTestTempFile(t, filepath.Join(contextDir, "etc"), filename, content, 0644) 49 } 50 51 // positive tests 52 for _, testcase := range []struct { 53 builder *Builder 54 name string 55 chownStr string 56 idMapping *idtools.IdentityMapping 57 state *dispatchState 58 expected idtools.Identity 59 }{ 60 { 61 builder: &Builder{options: &types.ImageBuildOptions{Platform: "linux"}}, 62 name: "UIDNoMap", 63 chownStr: "1", 64 idMapping: unmapped, 65 state: &dispatchState{}, 66 expected: idtools.Identity{UID: 1, GID: 1}, 67 }, 68 { 69 builder: &Builder{options: &types.ImageBuildOptions{Platform: "linux"}}, 70 name: "UIDGIDNoMap", 71 chownStr: "0:1", 72 idMapping: unmapped, 73 state: &dispatchState{}, 74 expected: idtools.Identity{UID: 0, GID: 1}, 75 }, 76 { 77 builder: &Builder{options: &types.ImageBuildOptions{Platform: "linux"}}, 78 name: "UIDWithMap", 79 chownStr: "0", 80 idMapping: remapped, 81 state: &dispatchState{}, 82 expected: idtools.Identity{UID: 100000, GID: 100000}, 83 }, 84 { 85 builder: &Builder{options: &types.ImageBuildOptions{Platform: "linux"}}, 86 name: "UIDGIDWithMap", 87 chownStr: "1:33", 88 idMapping: remapped, 89 state: &dispatchState{}, 90 expected: idtools.Identity{UID: 100001, GID: 100033}, 91 }, 92 { 93 builder: &Builder{options: &types.ImageBuildOptions{Platform: "linux"}}, 94 name: "UserNoMap", 95 chownStr: "bin:5555", 96 idMapping: unmapped, 97 state: &dispatchState{}, 98 expected: idtools.Identity{UID: 1, GID: 5555}, 99 }, 100 { 101 builder: &Builder{options: &types.ImageBuildOptions{Platform: "linux"}}, 102 name: "GroupWithMap", 103 chownStr: "0:unicorn", 104 idMapping: remapped, 105 state: &dispatchState{}, 106 expected: idtools.Identity{UID: 100000, GID: 101002}, 107 }, 108 { 109 builder: &Builder{options: &types.ImageBuildOptions{Platform: "linux"}}, 110 name: "UserOnlyWithMap", 111 chownStr: "unicorn", 112 idMapping: remapped, 113 state: &dispatchState{}, 114 expected: idtools.Identity{UID: 101001, GID: 101002}, 115 }, 116 } { 117 t.Run(testcase.name, func(t *testing.T) { 118 idPair, err := parseChownFlag(testcase.builder, testcase.state, testcase.chownStr, contextDir, testcase.idMapping) 119 assert.NilError(t, err, "Failed to parse chown flag: %q", testcase.chownStr) 120 assert.Check(t, is.DeepEqual(testcase.expected, idPair), "chown flag mapping failure") 121 }) 122 } 123 124 // error tests 125 for _, testcase := range []struct { 126 builder *Builder 127 name string 128 chownStr string 129 idMapping *idtools.IdentityMapping 130 state *dispatchState 131 descr string 132 }{ 133 { 134 builder: &Builder{options: &types.ImageBuildOptions{Platform: "linux"}}, 135 name: "BadChownFlagFormat", 136 chownStr: "bob:1:555", 137 idMapping: unmapped, 138 state: &dispatchState{}, 139 descr: "invalid chown string format: bob:1:555", 140 }, 141 { 142 builder: &Builder{options: &types.ImageBuildOptions{Platform: "linux"}}, 143 name: "UserNoExist", 144 chownStr: "bob", 145 idMapping: unmapped, 146 state: &dispatchState{}, 147 descr: "can't find uid for user bob: no such user: bob", 148 }, 149 { 150 builder: &Builder{options: &types.ImageBuildOptions{Platform: "linux"}}, 151 name: "GroupNoExist", 152 chownStr: "root:bob", 153 idMapping: unmapped, 154 state: &dispatchState{}, 155 descr: "can't find gid for group bob: no such group: bob", 156 }, 157 } { 158 t.Run(testcase.name, func(t *testing.T) { 159 _, err := parseChownFlag(testcase.builder, testcase.state, testcase.chownStr, contextDir, testcase.idMapping) 160 assert.Check(t, is.Error(err, testcase.descr), "Expected error string doesn't match") 161 }) 162 } 163 }