evaluator_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package dockerfile // import "github.com/docker/docker/builder/dockerfile"
  2. import (
  3. "os"
  4. "runtime"
  5. "testing"
  6. "github.com/docker/docker/builder/remotecontext"
  7. "github.com/docker/docker/pkg/archive"
  8. "github.com/docker/docker/pkg/reexec"
  9. "github.com/moby/buildkit/frontend/dockerfile/instructions"
  10. "gotest.tools/v3/assert"
  11. is "gotest.tools/v3/assert/cmp"
  12. "gotest.tools/v3/skip"
  13. )
  14. type dispatchTestCase struct {
  15. name, expectedError string
  16. cmd instructions.Command
  17. files map[string]string
  18. }
  19. func init() {
  20. reexec.Init()
  21. }
  22. func TestDispatch(t *testing.T) {
  23. if runtime.GOOS != "windows" {
  24. skip.If(t, os.Getuid() != 0, "skipping test that requires root")
  25. }
  26. testCases := []dispatchTestCase{
  27. {
  28. name: "ADD multiple files to file",
  29. cmd: &instructions.AddCommand{SourcesAndDest: instructions.SourcesAndDest{
  30. "file1.txt",
  31. "file2.txt",
  32. "test",
  33. }},
  34. expectedError: "When using ADD with more than one source file, the destination must be a directory and end with a /",
  35. files: map[string]string{"file1.txt": "test1", "file2.txt": "test2"},
  36. },
  37. {
  38. name: "Wildcard ADD multiple files to file",
  39. cmd: &instructions.AddCommand{SourcesAndDest: instructions.SourcesAndDest{
  40. "file*.txt",
  41. "test",
  42. }},
  43. expectedError: "When using ADD with more than one source file, the destination must be a directory and end with a /",
  44. files: map[string]string{"file1.txt": "test1", "file2.txt": "test2"},
  45. },
  46. {
  47. name: "COPY multiple files to file",
  48. cmd: &instructions.CopyCommand{SourcesAndDest: instructions.SourcesAndDest{
  49. "file1.txt",
  50. "file2.txt",
  51. "test",
  52. }},
  53. expectedError: "When using COPY with more than one source file, the destination must be a directory and end with a /",
  54. files: map[string]string{"file1.txt": "test1", "file2.txt": "test2"},
  55. },
  56. {
  57. name: "ADD multiple files to file with whitespace",
  58. cmd: &instructions.AddCommand{SourcesAndDest: instructions.SourcesAndDest{
  59. "test file1.txt",
  60. "test file2.txt",
  61. "test",
  62. }},
  63. expectedError: "When using ADD with more than one source file, the destination must be a directory and end with a /",
  64. files: map[string]string{"test file1.txt": "test1", "test file2.txt": "test2"},
  65. },
  66. {
  67. name: "COPY multiple files to file with whitespace",
  68. cmd: &instructions.CopyCommand{SourcesAndDest: instructions.SourcesAndDest{
  69. "test file1.txt",
  70. "test file2.txt",
  71. "test",
  72. }},
  73. expectedError: "When using COPY with more than one source file, the destination must be a directory and end with a /",
  74. files: map[string]string{"test file1.txt": "test1", "test file2.txt": "test2"},
  75. },
  76. {
  77. name: "COPY wildcard no files",
  78. cmd: &instructions.CopyCommand{SourcesAndDest: instructions.SourcesAndDest{
  79. "file*.txt",
  80. "/tmp/",
  81. }},
  82. expectedError: "COPY failed: no source files were specified",
  83. files: nil,
  84. },
  85. {
  86. name: "COPY url",
  87. cmd: &instructions.CopyCommand{SourcesAndDest: instructions.SourcesAndDest{
  88. "https://index.docker.io/robots.txt",
  89. "/",
  90. }},
  91. expectedError: "source can't be a URL for COPY",
  92. files: nil,
  93. },
  94. }
  95. for _, tc := range testCases {
  96. t.Run(tc.name, func(t *testing.T) {
  97. contextDir, cleanup := createTestTempDir(t, "", "builder-dockerfile-test")
  98. defer cleanup()
  99. for filename, content := range tc.files {
  100. createTestTempFile(t, contextDir, filename, content, 0777)
  101. }
  102. tarStream, err := archive.Tar(contextDir, archive.Uncompressed)
  103. if err != nil {
  104. t.Fatalf("Error when creating tar stream: %s", err)
  105. }
  106. defer func() {
  107. if err = tarStream.Close(); err != nil {
  108. t.Fatalf("Error when closing tar stream: %s", err)
  109. }
  110. }()
  111. context, err := remotecontext.FromArchive(tarStream)
  112. if err != nil {
  113. t.Fatalf("Error when creating tar context: %s", err)
  114. }
  115. defer func() {
  116. if err = context.Close(); err != nil {
  117. t.Fatalf("Error when closing tar context: %s", err)
  118. }
  119. }()
  120. b := newBuilderWithMockBackend()
  121. sb := newDispatchRequest(b, '`', context, NewBuildArgs(make(map[string]*string)), newStagesBuildResults())
  122. err = dispatch(sb, tc.cmd)
  123. assert.Check(t, is.ErrorContains(err, tc.expectedError))
  124. })
  125. }
  126. }