internals_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package dockerfile
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/docker/docker/api/types"
  6. "github.com/docker/docker/api/types/backend"
  7. "github.com/docker/docker/api/types/container"
  8. "github.com/docker/docker/builder"
  9. "github.com/docker/docker/builder/remotecontext"
  10. "github.com/docker/docker/pkg/archive"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/stretchr/testify/require"
  13. )
  14. func TestEmptyDockerfile(t *testing.T) {
  15. contextDir, cleanup := createTestTempDir(t, "", "builder-dockerfile-test")
  16. defer cleanup()
  17. createTestTempFile(t, contextDir, builder.DefaultDockerfileName, "", 0777)
  18. readAndCheckDockerfile(t, "emptyDockerfile", contextDir, "", "the Dockerfile (Dockerfile) cannot be empty")
  19. }
  20. func TestSymlinkDockerfile(t *testing.T) {
  21. contextDir, cleanup := createTestTempDir(t, "", "builder-dockerfile-test")
  22. defer cleanup()
  23. createTestSymlink(t, contextDir, builder.DefaultDockerfileName, "/etc/passwd")
  24. // The reason the error is "Cannot locate specified Dockerfile" is because
  25. // in the builder, the symlink is resolved within the context, therefore
  26. // Dockerfile -> /etc/passwd becomes etc/passwd from the context which is
  27. // a nonexistent file.
  28. expectedError := fmt.Sprintf("Cannot locate specified Dockerfile: %s", builder.DefaultDockerfileName)
  29. readAndCheckDockerfile(t, "symlinkDockerfile", contextDir, builder.DefaultDockerfileName, expectedError)
  30. }
  31. func TestDockerfileOutsideTheBuildContext(t *testing.T) {
  32. contextDir, cleanup := createTestTempDir(t, "", "builder-dockerfile-test")
  33. defer cleanup()
  34. expectedError := "Forbidden path outside the build context: ../../Dockerfile ()"
  35. readAndCheckDockerfile(t, "DockerfileOutsideTheBuildContext", contextDir, "../../Dockerfile", expectedError)
  36. }
  37. func TestNonExistingDockerfile(t *testing.T) {
  38. contextDir, cleanup := createTestTempDir(t, "", "builder-dockerfile-test")
  39. defer cleanup()
  40. expectedError := "Cannot locate specified Dockerfile: Dockerfile"
  41. readAndCheckDockerfile(t, "NonExistingDockerfile", contextDir, "Dockerfile", expectedError)
  42. }
  43. func readAndCheckDockerfile(t *testing.T, testName, contextDir, dockerfilePath, expectedError string) {
  44. tarStream, err := archive.Tar(contextDir, archive.Uncompressed)
  45. require.NoError(t, err)
  46. defer func() {
  47. if err = tarStream.Close(); err != nil {
  48. t.Fatalf("Error when closing tar stream: %s", err)
  49. }
  50. }()
  51. if dockerfilePath == "" { // handled in BuildWithContext
  52. dockerfilePath = builder.DefaultDockerfileName
  53. }
  54. config := backend.BuildConfig{
  55. Options: &types.ImageBuildOptions{Dockerfile: dockerfilePath},
  56. Source: tarStream,
  57. }
  58. _, _, err = remotecontext.Detect(config)
  59. assert.EqualError(t, err, expectedError)
  60. }
  61. func TestCopyRunConfig(t *testing.T) {
  62. defaultEnv := []string{"foo=1"}
  63. defaultCmd := []string{"old"}
  64. var testcases = []struct {
  65. doc string
  66. modifiers []runConfigModifier
  67. expected *container.Config
  68. }{
  69. {
  70. doc: "Set the command",
  71. modifiers: []runConfigModifier{withCmd([]string{"new"})},
  72. expected: &container.Config{
  73. Cmd: []string{"new"},
  74. Env: defaultEnv,
  75. },
  76. },
  77. {
  78. doc: "Set the command to a comment",
  79. modifiers: []runConfigModifier{withCmdComment("comment")},
  80. expected: &container.Config{
  81. Cmd: append(defaultShell, "#(nop) ", "comment"),
  82. Env: defaultEnv,
  83. },
  84. },
  85. {
  86. doc: "Set the command and env",
  87. modifiers: []runConfigModifier{
  88. withCmd([]string{"new"}),
  89. withEnv([]string{"one", "two"}),
  90. },
  91. expected: &container.Config{
  92. Cmd: []string{"new"},
  93. Env: []string{"one", "two"},
  94. },
  95. },
  96. }
  97. for _, testcase := range testcases {
  98. runConfig := &container.Config{
  99. Cmd: defaultCmd,
  100. Env: defaultEnv,
  101. }
  102. runConfigCopy := copyRunConfig(runConfig, testcase.modifiers...)
  103. assert.Equal(t, testcase.expected, runConfigCopy, testcase.doc)
  104. // Assert the original was not modified
  105. assert.NotEqual(t, runConfig, runConfigCopy, testcase.doc)
  106. }
  107. }