github.com/rita33cool1/iot-system-gateway@v0.0.0-20200911033302-e65bde238cc5/docker-engine/builder/dockerfile/internals_linux_test.go (about)

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