internals_test.go 4.8 KB

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