github.com/docker/engine@v22.0.0-20211208180946-d456264580cf+incompatible/integration/container/copy_test.go (about)

     1  package container // import "github.com/docker/docker/integration/container"
     2  
     3  import (
     4  	"archive/tar"
     5  	"context"
     6  	"encoding/json"
     7  	"fmt"
     8  	"io"
     9  	"os"
    10  	"testing"
    11  
    12  	"github.com/docker/docker/api/types"
    13  	"github.com/docker/docker/client"
    14  	"github.com/docker/docker/integration/internal/container"
    15  	"github.com/docker/docker/pkg/jsonmessage"
    16  	"github.com/docker/docker/testutil/fakecontext"
    17  	"gotest.tools/v3/assert"
    18  	is "gotest.tools/v3/assert/cmp"
    19  	"gotest.tools/v3/skip"
    20  )
    21  
    22  func TestCopyFromContainerPathDoesNotExist(t *testing.T) {
    23  	defer setupTest(t)()
    24  
    25  	ctx := context.Background()
    26  	apiclient := testEnv.APIClient()
    27  	cid := container.Create(ctx, t, apiclient)
    28  
    29  	_, _, err := apiclient.CopyFromContainer(ctx, cid, "/dne")
    30  	assert.Check(t, client.IsErrNotFound(err))
    31  	expected := fmt.Sprintf("No such container:path: %s:%s", cid, "/dne")
    32  	assert.Check(t, is.ErrorContains(err, expected))
    33  }
    34  
    35  func TestCopyFromContainerPathIsNotDir(t *testing.T) {
    36  	defer setupTest(t)()
    37  
    38  	ctx := context.Background()
    39  	apiclient := testEnv.APIClient()
    40  	cid := container.Create(ctx, t, apiclient)
    41  
    42  	path := "/etc/passwd/"
    43  	expected := "not a directory"
    44  	if testEnv.OSType == "windows" {
    45  		path = "c:/windows/system32/drivers/etc/hosts/"
    46  		expected = "The filename, directory name, or volume label syntax is incorrect."
    47  	}
    48  	_, _, err := apiclient.CopyFromContainer(ctx, cid, path)
    49  	assert.Assert(t, is.ErrorContains(err, expected))
    50  }
    51  
    52  func TestCopyToContainerPathDoesNotExist(t *testing.T) {
    53  	defer setupTest(t)()
    54  
    55  	ctx := context.Background()
    56  	apiclient := testEnv.APIClient()
    57  	cid := container.Create(ctx, t, apiclient)
    58  
    59  	err := apiclient.CopyToContainer(ctx, cid, "/dne", nil, types.CopyToContainerOptions{})
    60  	assert.Check(t, client.IsErrNotFound(err))
    61  	expected := fmt.Sprintf("No such container:path: %s:%s", cid, "/dne")
    62  	assert.Check(t, is.ErrorContains(err, expected))
    63  }
    64  
    65  func TestCopyToContainerPathIsNotDir(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  	path := "/etc/passwd/"
    73  	if testEnv.OSType == "windows" {
    74  		path = "c:/windows/system32/drivers/etc/hosts/"
    75  	}
    76  	err := apiclient.CopyToContainer(ctx, cid, path, nil, types.CopyToContainerOptions{})
    77  	assert.Assert(t, is.ErrorContains(err, "not a directory"))
    78  }
    79  
    80  func TestCopyFromContainer(t *testing.T) {
    81  	skip.If(t, testEnv.DaemonInfo.OSType == "windows")
    82  	defer setupTest(t)()
    83  
    84  	ctx := context.Background()
    85  	apiClient := testEnv.APIClient()
    86  
    87  	dir, err := os.MkdirTemp("", t.Name())
    88  	assert.NilError(t, err)
    89  	defer os.RemoveAll(dir)
    90  
    91  	buildCtx := fakecontext.New(t, dir, fakecontext.WithFile("foo", "hello"), fakecontext.WithFile("baz", "world"), fakecontext.WithDockerfile(`
    92  		FROM busybox
    93  		COPY foo /foo
    94  		COPY baz /bar/quux/baz
    95  		RUN ln -s notexist /bar/notarget && ln -s quux/baz /bar/filesymlink && ln -s quux /bar/dirsymlink && ln -s / /bar/root
    96  		CMD /fake
    97  	`))
    98  	defer buildCtx.Close()
    99  
   100  	resp, err := apiClient.ImageBuild(ctx, buildCtx.AsTarReader(t), types.ImageBuildOptions{})
   101  	assert.NilError(t, err)
   102  	defer resp.Body.Close()
   103  
   104  	var imageID string
   105  	err = jsonmessage.DisplayJSONMessagesStream(resp.Body, io.Discard, 0, false, func(msg jsonmessage.JSONMessage) {
   106  		var r types.BuildResult
   107  		assert.NilError(t, json.Unmarshal(*msg.Aux, &r))
   108  		imageID = r.ID
   109  	})
   110  	assert.NilError(t, err)
   111  	assert.Assert(t, imageID != "")
   112  
   113  	cid := container.Create(ctx, t, apiClient, container.WithImage(imageID))
   114  
   115  	for _, x := range []struct {
   116  		src    string
   117  		expect map[string]string
   118  	}{
   119  		{"/", map[string]string{"/": "", "/foo": "hello", "/bar/quux/baz": "world", "/bar/filesymlink": "", "/bar/dirsymlink": "", "/bar/notarget": ""}},
   120  		{"/bar/root", map[string]string{"root": ""}},
   121  		{"/bar/root/", map[string]string{"root/": "", "root/foo": "hello", "root/bar/quux/baz": "world", "root/bar/filesymlink": "", "root/bar/dirsymlink": "", "root/bar/notarget": ""}},
   122  
   123  		{"bar/quux", map[string]string{"quux/": "", "quux/baz": "world"}},
   124  		{"bar/quux/", map[string]string{"quux/": "", "quux/baz": "world"}},
   125  		{"bar/quux/baz", map[string]string{"baz": "world"}},
   126  
   127  		{"bar/filesymlink", map[string]string{"filesymlink": ""}},
   128  		{"bar/dirsymlink", map[string]string{"dirsymlink": ""}},
   129  		{"bar/dirsymlink/", map[string]string{"dirsymlink/": "", "dirsymlink/baz": "world"}},
   130  		{"bar/notarget", map[string]string{"notarget": ""}},
   131  	} {
   132  		t.Run(x.src, func(t *testing.T) {
   133  			rdr, _, err := apiClient.CopyFromContainer(ctx, cid, x.src)
   134  			assert.NilError(t, err)
   135  			defer rdr.Close()
   136  
   137  			found := make(map[string]bool, len(x.expect))
   138  			var numFound int
   139  			tr := tar.NewReader(rdr)
   140  			for numFound < len(x.expect) {
   141  				h, err := tr.Next()
   142  				if err == io.EOF {
   143  					break
   144  				}
   145  				assert.NilError(t, err)
   146  
   147  				expected, exists := x.expect[h.Name]
   148  				if !exists {
   149  					// this archive will have extra stuff in it since we are copying from root
   150  					// and docker adds a bunch of stuff
   151  					continue
   152  				}
   153  
   154  				numFound++
   155  				found[h.Name] = true
   156  
   157  				buf, err := io.ReadAll(tr)
   158  				if err == nil {
   159  					assert.Check(t, is.Equal(string(buf), expected))
   160  				}
   161  			}
   162  
   163  			for f := range x.expect {
   164  				assert.Check(t, found[f], f+" not found in archive")
   165  			}
   166  		})
   167  	}
   168  }