internals_test.go 3.9 KB

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