github.com/rumpl/bof@v23.0.0-rc.2+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  	tmpDir := t.TempDir()
    69  	srcPath := filepath.Join(tmpDir, "empty-file.txt")
    70  	err := os.WriteFile(srcPath, []byte(""), 0400)
    71  	assert.NilError(t, err)
    72  
    73  	// TODO(thaJeztah) Add utilities to the client to make steps below less complicated.
    74  	// Code below is taken from copyToContainer() in docker/cli.
    75  	srcInfo, err := archive.CopyInfoSourcePath(srcPath, false)
    76  	assert.NilError(t, err)
    77  
    78  	srcArchive, err := archive.TarResource(srcInfo)
    79  	assert.NilError(t, err)
    80  	defer srcArchive.Close()
    81  
    82  	ctrPath := "/empty-file.txt"
    83  	dstInfo := archive.CopyInfo{Path: ctrPath}
    84  	dstDir, preparedArchive, err := archive.PrepareArchiveCopy(srcArchive, srcInfo, dstInfo)
    85  	assert.NilError(t, err)
    86  	defer preparedArchive.Close()
    87  
    88  	ctx := context.Background()
    89  	apiclient := testEnv.APIClient()
    90  	cid := container.Create(ctx, t, apiclient)
    91  
    92  	// empty content
    93  	err = apiclient.CopyToContainer(ctx, cid, dstDir, bytes.NewReader([]byte("")), types.CopyToContainerOptions{})
    94  	assert.NilError(t, err)
    95  
    96  	// tar with empty file
    97  	err = apiclient.CopyToContainer(ctx, cid, dstDir, preparedArchive, types.CopyToContainerOptions{})
    98  	assert.NilError(t, err)
    99  
   100  	// copy from empty file
   101  	rdr, _, err := apiclient.CopyFromContainer(ctx, cid, dstDir)
   102  	assert.NilError(t, err)
   103  	defer rdr.Close()
   104  }
   105  
   106  func TestCopyToContainerPathIsNotDir(t *testing.T) {
   107  	defer setupTest(t)()
   108  
   109  	ctx := context.Background()
   110  	apiclient := testEnv.APIClient()
   111  	cid := container.Create(ctx, t, apiclient)
   112  
   113  	path := "/etc/passwd/"
   114  	if testEnv.OSType == "windows" {
   115  		path = "c:/windows/system32/drivers/etc/hosts/"
   116  	}
   117  	err := apiclient.CopyToContainer(ctx, cid, path, nil, types.CopyToContainerOptions{})
   118  	assert.Assert(t, is.ErrorContains(err, "not a directory"))
   119  }
   120  
   121  func TestCopyFromContainer(t *testing.T) {
   122  	skip.If(t, testEnv.DaemonInfo.OSType == "windows")
   123  	defer setupTest(t)()
   124  
   125  	ctx := context.Background()
   126  	apiClient := testEnv.APIClient()
   127  
   128  	dir, err := os.MkdirTemp("", t.Name())
   129  	assert.NilError(t, err)
   130  	defer os.RemoveAll(dir)
   131  
   132  	buildCtx := fakecontext.New(t, dir, fakecontext.WithFile("foo", "hello"), fakecontext.WithFile("baz", "world"), fakecontext.WithDockerfile(`
   133  		FROM busybox
   134  		COPY foo /foo
   135  		COPY baz /bar/quux/baz
   136  		RUN ln -s notexist /bar/notarget && ln -s quux/baz /bar/filesymlink && ln -s quux /bar/dirsymlink && ln -s / /bar/root
   137  		CMD /fake
   138  	`))
   139  	defer buildCtx.Close()
   140  
   141  	resp, err := apiClient.ImageBuild(ctx, buildCtx.AsTarReader(t), types.ImageBuildOptions{})
   142  	assert.NilError(t, err)
   143  	defer resp.Body.Close()
   144  
   145  	var imageID string
   146  	err = jsonmessage.DisplayJSONMessagesStream(resp.Body, io.Discard, 0, false, func(msg jsonmessage.JSONMessage) {
   147  		var r types.BuildResult
   148  		assert.NilError(t, json.Unmarshal(*msg.Aux, &r))
   149  		imageID = r.ID
   150  	})
   151  	assert.NilError(t, err)
   152  	assert.Assert(t, imageID != "")
   153  
   154  	cid := container.Create(ctx, t, apiClient, container.WithImage(imageID))
   155  
   156  	for _, x := range []struct {
   157  		src    string
   158  		expect map[string]string
   159  	}{
   160  		{"/", map[string]string{"/": "", "/foo": "hello", "/bar/quux/baz": "world", "/bar/filesymlink": "", "/bar/dirsymlink": "", "/bar/notarget": ""}},
   161  		{"/bar/root", map[string]string{"root": ""}},
   162  		{"/bar/root/", map[string]string{"root/": "", "root/foo": "hello", "root/bar/quux/baz": "world", "root/bar/filesymlink": "", "root/bar/dirsymlink": "", "root/bar/notarget": ""}},
   163  
   164  		{"bar/quux", map[string]string{"quux/": "", "quux/baz": "world"}},
   165  		{"bar/quux/", map[string]string{"quux/": "", "quux/baz": "world"}},
   166  		{"bar/quux/baz", map[string]string{"baz": "world"}},
   167  
   168  		{"bar/filesymlink", map[string]string{"filesymlink": ""}},
   169  		{"bar/dirsymlink", map[string]string{"dirsymlink": ""}},
   170  		{"bar/dirsymlink/", map[string]string{"dirsymlink/": "", "dirsymlink/baz": "world"}},
   171  		{"bar/notarget", map[string]string{"notarget": ""}},
   172  	} {
   173  		t.Run(x.src, func(t *testing.T) {
   174  			rdr, _, err := apiClient.CopyFromContainer(ctx, cid, x.src)
   175  			assert.NilError(t, err)
   176  			defer rdr.Close()
   177  
   178  			found := make(map[string]bool, len(x.expect))
   179  			var numFound int
   180  			tr := tar.NewReader(rdr)
   181  			for numFound < len(x.expect) {
   182  				h, err := tr.Next()
   183  				if err == io.EOF {
   184  					break
   185  				}
   186  				assert.NilError(t, err)
   187  
   188  				expected, exists := x.expect[h.Name]
   189  				if !exists {
   190  					// this archive will have extra stuff in it since we are copying from root
   191  					// and docker adds a bunch of stuff
   192  					continue
   193  				}
   194  
   195  				numFound++
   196  				found[h.Name] = true
   197  
   198  				buf, err := io.ReadAll(tr)
   199  				if err == nil {
   200  					assert.Check(t, is.Equal(string(buf), expected))
   201  				}
   202  			}
   203  
   204  			for f := range x.expect {
   205  				assert.Check(t, found[f], f+" not found in archive")
   206  			}
   207  		})
   208  	}
   209  }