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