github.com/rawahars/moby@v24.0.4+incompatible/integration/container/copy_test.go (about)

     1  package container // import "github.com/docker/docker/integration/container"
     2  
     3  import (
     4  	"archive/tar"
     5  	"bytes"
     6  	"context"
     7  	"encoding/json"
     8  	"io"
     9  	"os"
    10  	"path/filepath"
    11  	"testing"
    12  
    13  	"github.com/docker/docker/api/types"
    14  	"github.com/docker/docker/client"
    15  	"github.com/docker/docker/integration/internal/container"
    16  	"github.com/docker/docker/pkg/archive"
    17  	"github.com/docker/docker/pkg/jsonmessage"
    18  	"github.com/docker/docker/testutil/fakecontext"
    19  	"gotest.tools/v3/assert"
    20  	is "gotest.tools/v3/assert/cmp"
    21  	"gotest.tools/v3/skip"
    22  )
    23  
    24  func TestCopyFromContainerPathDoesNotExist(t *testing.T) {
    25  	defer setupTest(t)()
    26  
    27  	ctx := context.Background()
    28  	apiclient := testEnv.APIClient()
    29  	cid := container.Create(ctx, t, apiclient)
    30  
    31  	_, _, err := apiclient.CopyFromContainer(ctx, cid, "/dne")
    32  	assert.Check(t, client.IsErrNotFound(err))
    33  	assert.ErrorContains(t, err, "Could not find the file /dne in container "+cid)
    34  }
    35  
    36  func TestCopyFromContainerPathIsNotDir(t *testing.T) {
    37  	defer setupTest(t)()
    38  
    39  	ctx := context.Background()
    40  	apiclient := testEnv.APIClient()
    41  	cid := container.Create(ctx, t, apiclient)
    42  
    43  	path := "/etc/passwd/"
    44  	expected := "not a directory"
    45  	if testEnv.OSType == "windows" {
    46  		path = "c:/windows/system32/drivers/etc/hosts/"
    47  		expected = "The filename, directory name, or volume label syntax is incorrect."
    48  	}
    49  	_, _, err := apiclient.CopyFromContainer(ctx, cid, path)
    50  	assert.Assert(t, is.ErrorContains(err, expected))
    51  }
    52  
    53  func TestCopyToContainerPathDoesNotExist(t *testing.T) {
    54  	defer setupTest(t)()
    55  
    56  	ctx := context.Background()
    57  	apiclient := testEnv.APIClient()
    58  	cid := container.Create(ctx, t, apiclient)
    59  
    60  	err := apiclient.CopyToContainer(ctx, cid, "/dne", nil, types.CopyToContainerOptions{})
    61  	assert.Check(t, client.IsErrNotFound(err))
    62  	assert.ErrorContains(t, err, "Could not find the file /dne in container "+cid)
    63  }
    64  
    65  func TestCopyEmptyFile(t *testing.T) {
    66  	defer setupTest(t)()
    67  
    68  	ctx := context.Background()
    69  	apiclient := testEnv.APIClient()
    70  	cid := container.Create(ctx, t, apiclient)
    71  
    72  	// empty content
    73  	dstDir, _ := makeEmptyArchive(t)
    74  	err := apiclient.CopyToContainer(ctx, cid, dstDir, bytes.NewReader([]byte("")), types.CopyToContainerOptions{})
    75  	assert.NilError(t, err)
    76  
    77  	// tar with empty file
    78  	dstDir, preparedArchive := makeEmptyArchive(t)
    79  	err = apiclient.CopyToContainer(ctx, cid, dstDir, preparedArchive, types.CopyToContainerOptions{})
    80  	assert.NilError(t, err)
    81  
    82  	// tar with empty file archive mode
    83  	dstDir, preparedArchive = makeEmptyArchive(t)
    84  	err = apiclient.CopyToContainer(ctx, cid, dstDir, preparedArchive, types.CopyToContainerOptions{
    85  		CopyUIDGID: true,
    86  	})
    87  	assert.NilError(t, err)
    88  
    89  	// copy from empty file
    90  	rdr, _, err := apiclient.CopyFromContainer(ctx, cid, dstDir)
    91  	assert.NilError(t, err)
    92  	defer rdr.Close()
    93  }
    94  
    95  func makeEmptyArchive(t *testing.T) (string, io.ReadCloser) {
    96  	tmpDir := t.TempDir()
    97  	srcPath := filepath.Join(tmpDir, "empty-file.txt")
    98  	err := os.WriteFile(srcPath, []byte(""), 0400)
    99  	assert.NilError(t, err)
   100  
   101  	// TODO(thaJeztah) Add utilities to the client to make steps below less complicated.
   102  	// Code below is taken from copyToContainer() in docker/cli.
   103  	srcInfo, err := archive.CopyInfoSourcePath(srcPath, false)
   104  	assert.NilError(t, err)
   105  
   106  	srcArchive, err := archive.TarResource(srcInfo)
   107  	assert.NilError(t, err)
   108  	t.Cleanup(func() {
   109  		srcArchive.Close()
   110  	})
   111  
   112  	ctrPath := "/empty-file.txt"
   113  	dstInfo := archive.CopyInfo{Path: ctrPath}
   114  	dstDir, preparedArchive, err := archive.PrepareArchiveCopy(srcArchive, srcInfo, dstInfo)
   115  	assert.NilError(t, err)
   116  	t.Cleanup(func() {
   117  		preparedArchive.Close()
   118  	})
   119  	return dstDir, preparedArchive
   120  }
   121  
   122  func TestCopyToContainerPathIsNotDir(t *testing.T) {
   123  	defer setupTest(t)()
   124  
   125  	ctx := context.Background()
   126  	apiclient := testEnv.APIClient()
   127  	cid := container.Create(ctx, t, apiclient)
   128  
   129  	path := "/etc/passwd/"
   130  	if testEnv.OSType == "windows" {
   131  		path = "c:/windows/system32/drivers/etc/hosts/"
   132  	}
   133  	err := apiclient.CopyToContainer(ctx, cid, path, nil, types.CopyToContainerOptions{})
   134  	assert.Assert(t, is.ErrorContains(err, "not a directory"))
   135  }
   136  
   137  func TestCopyFromContainer(t *testing.T) {
   138  	skip.If(t, testEnv.DaemonInfo.OSType == "windows")
   139  	defer setupTest(t)()
   140  
   141  	ctx := context.Background()
   142  	apiClient := testEnv.APIClient()
   143  
   144  	dir, err := os.MkdirTemp("", t.Name())
   145  	assert.NilError(t, err)
   146  	defer os.RemoveAll(dir)
   147  
   148  	buildCtx := fakecontext.New(t, dir, fakecontext.WithFile("foo", "hello"), fakecontext.WithFile("baz", "world"), fakecontext.WithDockerfile(`
   149  		FROM busybox
   150  		COPY foo /foo
   151  		COPY baz /bar/quux/baz
   152  		RUN ln -s notexist /bar/notarget && ln -s quux/baz /bar/filesymlink && ln -s quux /bar/dirsymlink && ln -s / /bar/root
   153  		CMD /fake
   154  	`))
   155  	defer buildCtx.Close()
   156  
   157  	resp, err := apiClient.ImageBuild(ctx, buildCtx.AsTarReader(t), types.ImageBuildOptions{})
   158  	assert.NilError(t, err)
   159  	defer resp.Body.Close()
   160  
   161  	var imageID string
   162  	err = jsonmessage.DisplayJSONMessagesStream(resp.Body, io.Discard, 0, false, func(msg jsonmessage.JSONMessage) {
   163  		var r types.BuildResult
   164  		assert.NilError(t, json.Unmarshal(*msg.Aux, &r))
   165  		imageID = r.ID
   166  	})
   167  	assert.NilError(t, err)
   168  	assert.Assert(t, imageID != "")
   169  
   170  	cid := container.Create(ctx, t, apiClient, container.WithImage(imageID))
   171  
   172  	for _, x := range []struct {
   173  		src    string
   174  		expect map[string]string
   175  	}{
   176  		{"/", map[string]string{"/": "", "/foo": "hello", "/bar/quux/baz": "world", "/bar/filesymlink": "", "/bar/dirsymlink": "", "/bar/notarget": ""}},
   177  		{".", map[string]string{"./": "", "./foo": "hello", "./bar/quux/baz": "world", "./bar/filesymlink": "", "./bar/dirsymlink": "", "./bar/notarget": ""}},
   178  		{"/.", map[string]string{"./": "", "./foo": "hello", "./bar/quux/baz": "world", "./bar/filesymlink": "", "./bar/dirsymlink": "", "./bar/notarget": ""}},
   179  		{"./", map[string]string{"./": "", "./foo": "hello", "./bar/quux/baz": "world", "./bar/filesymlink": "", "./bar/dirsymlink": "", "./bar/notarget": ""}},
   180  		{"/./", map[string]string{"./": "", "./foo": "hello", "./bar/quux/baz": "world", "./bar/filesymlink": "", "./bar/dirsymlink": "", "./bar/notarget": ""}},
   181  		{"/bar/root", map[string]string{"root": ""}},
   182  		{"/bar/root/", map[string]string{"root/": "", "root/foo": "hello", "root/bar/quux/baz": "world", "root/bar/filesymlink": "", "root/bar/dirsymlink": "", "root/bar/notarget": ""}},
   183  		{"/bar/root/.", map[string]string{"./": "", "./foo": "hello", "./bar/quux/baz": "world", "./bar/filesymlink": "", "./bar/dirsymlink": "", "./bar/notarget": ""}},
   184  
   185  		{"bar/quux", map[string]string{"quux/": "", "quux/baz": "world"}},
   186  		{"bar/quux/", map[string]string{"quux/": "", "quux/baz": "world"}},
   187  		{"bar/quux/.", map[string]string{"./": "", "./baz": "world"}},
   188  		{"bar/quux/baz", map[string]string{"baz": "world"}},
   189  
   190  		{"bar/filesymlink", map[string]string{"filesymlink": ""}},
   191  		{"bar/dirsymlink", map[string]string{"dirsymlink": ""}},
   192  		{"bar/dirsymlink/", map[string]string{"dirsymlink/": "", "dirsymlink/baz": "world"}},
   193  		{"bar/dirsymlink/.", map[string]string{"./": "", "./baz": "world"}},
   194  		{"bar/notarget", map[string]string{"notarget": ""}},
   195  	} {
   196  		t.Run(x.src, func(t *testing.T) {
   197  			rdr, _, err := apiClient.CopyFromContainer(ctx, cid, x.src)
   198  			assert.NilError(t, err)
   199  			defer rdr.Close()
   200  
   201  			found := make(map[string]bool, len(x.expect))
   202  			var numFound int
   203  			tr := tar.NewReader(rdr)
   204  			for numFound < len(x.expect) {
   205  				h, err := tr.Next()
   206  				if err == io.EOF {
   207  					break
   208  				}
   209  				assert.NilError(t, err)
   210  
   211  				expected, exists := x.expect[h.Name]
   212  				if !exists {
   213  					// this archive will have extra stuff in it since we are copying from root
   214  					// and docker adds a bunch of stuff
   215  					continue
   216  				}
   217  
   218  				numFound++
   219  				found[h.Name] = true
   220  
   221  				buf, err := io.ReadAll(tr)
   222  				if err == nil {
   223  					assert.Check(t, is.Equal(string(buf), expected))
   224  				}
   225  			}
   226  
   227  			for f := range x.expect {
   228  				assert.Check(t, found[f], f+" not found in archive")
   229  			}
   230  		})
   231  	}
   232  }