internals_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package dockerfile // import "github.com/docker/docker/builder/dockerfile"
  2. import (
  3. "fmt"
  4. "os"
  5. "runtime"
  6. "testing"
  7. "github.com/docker/docker/api/types"
  8. "github.com/docker/docker/api/types/backend"
  9. "github.com/docker/docker/api/types/container"
  10. "github.com/docker/docker/builder"
  11. "github.com/docker/docker/builder/remotecontext"
  12. "github.com/docker/docker/pkg/archive"
  13. "github.com/docker/go-connections/nat"
  14. "gotest.tools/assert"
  15. is "gotest.tools/assert/cmp"
  16. "gotest.tools/skip"
  17. )
  18. func TestEmptyDockerfile(t *testing.T) {
  19. contextDir, cleanup := createTestTempDir(t, "", "builder-dockerfile-test")
  20. defer cleanup()
  21. createTestTempFile(t, contextDir, builder.DefaultDockerfileName, "", 0777)
  22. readAndCheckDockerfile(t, "emptyDockerfile", contextDir, "", "the Dockerfile (Dockerfile) cannot be empty")
  23. }
  24. func TestSymlinkDockerfile(t *testing.T) {
  25. contextDir, cleanup := createTestTempDir(t, "", "builder-dockerfile-test")
  26. defer cleanup()
  27. createTestSymlink(t, contextDir, builder.DefaultDockerfileName, "/etc/passwd")
  28. // The reason the error is "Cannot locate specified Dockerfile" is because
  29. // in the builder, the symlink is resolved within the context, therefore
  30. // Dockerfile -> /etc/passwd becomes etc/passwd from the context which is
  31. // a nonexistent file.
  32. expectedError := fmt.Sprintf("Cannot locate specified Dockerfile: %s", builder.DefaultDockerfileName)
  33. readAndCheckDockerfile(t, "symlinkDockerfile", contextDir, builder.DefaultDockerfileName, expectedError)
  34. }
  35. func TestDockerfileOutsideTheBuildContext(t *testing.T) {
  36. contextDir, cleanup := createTestTempDir(t, "", "builder-dockerfile-test")
  37. defer cleanup()
  38. expectedError := "Forbidden path outside the build context: ../../Dockerfile ()"
  39. if runtime.GOOS == "windows" {
  40. expectedError = "failed to resolve scoped path ../../Dockerfile ()"
  41. }
  42. readAndCheckDockerfile(t, "DockerfileOutsideTheBuildContext", contextDir, "../../Dockerfile", expectedError)
  43. }
  44. func TestNonExistingDockerfile(t *testing.T) {
  45. contextDir, cleanup := createTestTempDir(t, "", "builder-dockerfile-test")
  46. defer cleanup()
  47. expectedError := "Cannot locate specified Dockerfile: Dockerfile"
  48. readAndCheckDockerfile(t, "NonExistingDockerfile", contextDir, "Dockerfile", expectedError)
  49. }
  50. func readAndCheckDockerfile(t *testing.T, testName, contextDir, dockerfilePath, expectedError string) {
  51. if runtime.GOOS != "windows" {
  52. skip.If(t, os.Getuid() != 0, "skipping test that requires root")
  53. }
  54. tarStream, err := archive.Tar(contextDir, archive.Uncompressed)
  55. assert.NilError(t, err)
  56. defer func() {
  57. if err = tarStream.Close(); err != nil {
  58. t.Fatalf("Error when closing tar stream: %s", err)
  59. }
  60. }()
  61. if dockerfilePath == "" { // handled in BuildWithContext
  62. dockerfilePath = builder.DefaultDockerfileName
  63. }
  64. config := backend.BuildConfig{
  65. Options: &types.ImageBuildOptions{Dockerfile: dockerfilePath},
  66. Source: tarStream,
  67. }
  68. _, _, err = remotecontext.Detect(config)
  69. assert.Check(t, is.ErrorContains(err, expectedError))
  70. }
  71. func TestCopyRunConfig(t *testing.T) {
  72. defaultEnv := []string{"foo=1"}
  73. defaultCmd := []string{"old"}
  74. var testcases = []struct {
  75. doc string
  76. modifiers []runConfigModifier
  77. expected *container.Config
  78. }{
  79. {
  80. doc: "Set the command",
  81. modifiers: []runConfigModifier{withCmd([]string{"new"})},
  82. expected: &container.Config{
  83. Cmd: []string{"new"},
  84. Env: defaultEnv,
  85. },
  86. },
  87. {
  88. doc: "Set the command to a comment",
  89. modifiers: []runConfigModifier{withCmdComment("comment", runtime.GOOS)},
  90. expected: &container.Config{
  91. Cmd: append(defaultShellForOS(runtime.GOOS), "#(nop) ", "comment"),
  92. Env: defaultEnv,
  93. },
  94. },
  95. {
  96. doc: "Set the command and env",
  97. modifiers: []runConfigModifier{
  98. withCmd([]string{"new"}),
  99. withEnv([]string{"one", "two"}),
  100. },
  101. expected: &container.Config{
  102. Cmd: []string{"new"},
  103. Env: []string{"one", "two"},
  104. },
  105. },
  106. }
  107. for _, testcase := range testcases {
  108. runConfig := &container.Config{
  109. Cmd: defaultCmd,
  110. Env: defaultEnv,
  111. }
  112. runConfigCopy := copyRunConfig(runConfig, testcase.modifiers...)
  113. assert.Check(t, is.DeepEqual(testcase.expected, runConfigCopy), testcase.doc)
  114. // Assert the original was not modified
  115. assert.Check(t, runConfig != runConfigCopy, testcase.doc)
  116. }
  117. }
  118. func fullMutableRunConfig() *container.Config {
  119. return &container.Config{
  120. Cmd: []string{"command", "arg1"},
  121. Env: []string{"env1=foo", "env2=bar"},
  122. ExposedPorts: nat.PortSet{
  123. "1000/tcp": {},
  124. "1001/tcp": {},
  125. },
  126. Volumes: map[string]struct{}{
  127. "one": {},
  128. "two": {},
  129. },
  130. Entrypoint: []string{"entry", "arg1"},
  131. OnBuild: []string{"first", "next"},
  132. Labels: map[string]string{
  133. "label1": "value1",
  134. "label2": "value2",
  135. },
  136. Shell: []string{"shell", "-c"},
  137. }
  138. }
  139. func TestDeepCopyRunConfig(t *testing.T) {
  140. runConfig := fullMutableRunConfig()
  141. copy := copyRunConfig(runConfig)
  142. assert.Check(t, is.DeepEqual(fullMutableRunConfig(), copy))
  143. copy.Cmd[1] = "arg2"
  144. copy.Env[1] = "env2=new"
  145. copy.ExposedPorts["10002"] = struct{}{}
  146. copy.Volumes["three"] = struct{}{}
  147. copy.Entrypoint[1] = "arg2"
  148. copy.OnBuild[0] = "start"
  149. copy.Labels["label3"] = "value3"
  150. copy.Shell[0] = "sh"
  151. assert.Check(t, is.DeepEqual(fullMutableRunConfig(), runConfig))
  152. }