github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/builder/dockerfile/evaluator_test.go (about)

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