internals_linux_test.go 3.3 KB

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