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