internals_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. readAndCheckDockerfile(t, "DockerfileOutsideTheBuildContext", contextDir, "../../Dockerfile", expectedError)
  40. }
  41. func TestNonExistingDockerfile(t *testing.T) {
  42. contextDir, cleanup := createTestTempDir(t, "", "builder-dockerfile-test")
  43. defer cleanup()
  44. expectedError := "Cannot locate specified Dockerfile: Dockerfile"
  45. readAndCheckDockerfile(t, "NonExistingDockerfile", contextDir, "Dockerfile", expectedError)
  46. }
  47. func readAndCheckDockerfile(t *testing.T, testName, contextDir, dockerfilePath, expectedError string) {
  48. skip.If(t, os.Getuid() != 0, "skipping test that requires root")
  49. tarStream, err := archive.Tar(contextDir, archive.Uncompressed)
  50. assert.NilError(t, err)
  51. defer func() {
  52. if err = tarStream.Close(); err != nil {
  53. t.Fatalf("Error when closing tar stream: %s", err)
  54. }
  55. }()
  56. if dockerfilePath == "" { // handled in BuildWithContext
  57. dockerfilePath = builder.DefaultDockerfileName
  58. }
  59. config := backend.BuildConfig{
  60. Options: &types.ImageBuildOptions{Dockerfile: dockerfilePath},
  61. Source: tarStream,
  62. }
  63. _, _, err = remotecontext.Detect(config)
  64. assert.Check(t, is.Error(err, expectedError))
  65. }
  66. func TestCopyRunConfig(t *testing.T) {
  67. defaultEnv := []string{"foo=1"}
  68. defaultCmd := []string{"old"}
  69. var testcases = []struct {
  70. doc string
  71. modifiers []runConfigModifier
  72. expected *container.Config
  73. }{
  74. {
  75. doc: "Set the command",
  76. modifiers: []runConfigModifier{withCmd([]string{"new"})},
  77. expected: &container.Config{
  78. Cmd: []string{"new"},
  79. Env: defaultEnv,
  80. },
  81. },
  82. {
  83. doc: "Set the command to a comment",
  84. modifiers: []runConfigModifier{withCmdComment("comment", runtime.GOOS)},
  85. expected: &container.Config{
  86. Cmd: append(defaultShellForOS(runtime.GOOS), "#(nop) ", "comment"),
  87. Env: defaultEnv,
  88. },
  89. },
  90. {
  91. doc: "Set the command and env",
  92. modifiers: []runConfigModifier{
  93. withCmd([]string{"new"}),
  94. withEnv([]string{"one", "two"}),
  95. },
  96. expected: &container.Config{
  97. Cmd: []string{"new"},
  98. Env: []string{"one", "two"},
  99. },
  100. },
  101. }
  102. for _, testcase := range testcases {
  103. runConfig := &container.Config{
  104. Cmd: defaultCmd,
  105. Env: defaultEnv,
  106. }
  107. runConfigCopy := copyRunConfig(runConfig, testcase.modifiers...)
  108. assert.Check(t, is.DeepEqual(testcase.expected, runConfigCopy), testcase.doc)
  109. // Assert the original was not modified
  110. assert.Check(t, runConfig != runConfigCopy, testcase.doc)
  111. }
  112. }
  113. func fullMutableRunConfig() *container.Config {
  114. return &container.Config{
  115. Cmd: []string{"command", "arg1"},
  116. Env: []string{"env1=foo", "env2=bar"},
  117. ExposedPorts: nat.PortSet{
  118. "1000/tcp": {},
  119. "1001/tcp": {},
  120. },
  121. Volumes: map[string]struct{}{
  122. "one": {},
  123. "two": {},
  124. },
  125. Entrypoint: []string{"entry", "arg1"},
  126. OnBuild: []string{"first", "next"},
  127. Labels: map[string]string{
  128. "label1": "value1",
  129. "label2": "value2",
  130. },
  131. Shell: []string{"shell", "-c"},
  132. }
  133. }
  134. func TestDeepCopyRunConfig(t *testing.T) {
  135. runConfig := fullMutableRunConfig()
  136. copy := copyRunConfig(runConfig)
  137. assert.Check(t, is.DeepEqual(fullMutableRunConfig(), copy))
  138. copy.Cmd[1] = "arg2"
  139. copy.Env[1] = "env2=new"
  140. copy.ExposedPorts["10002"] = struct{}{}
  141. copy.Volumes["three"] = struct{}{}
  142. copy.Entrypoint[1] = "arg2"
  143. copy.OnBuild[0] = "start"
  144. copy.Labels["label3"] = "value3"
  145. copy.Shell[0] = "sh"
  146. assert.Check(t, is.DeepEqual(fullMutableRunConfig(), runConfig))
  147. }