github.com/lacework-dev/go-moby@v20.10.12+incompatible/integration/container/export_test.go (about)

     1  package container // import "github.com/docker/docker/integration/container"
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"strings"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/docker/docker/api/types"
    11  	"github.com/docker/docker/api/types/filters"
    12  	"github.com/docker/docker/integration/internal/container"
    13  	"github.com/docker/docker/pkg/jsonmessage"
    14  	"github.com/docker/docker/testutil/daemon"
    15  	"gotest.tools/v3/assert"
    16  	is "gotest.tools/v3/assert/cmp"
    17  	"gotest.tools/v3/poll"
    18  	"gotest.tools/v3/skip"
    19  )
    20  
    21  // export an image and try to import it into a new one
    22  func TestExportContainerAndImportImage(t *testing.T) {
    23  	skip.If(t, testEnv.DaemonInfo.OSType == "windows")
    24  
    25  	defer setupTest(t)()
    26  	client := testEnv.APIClient()
    27  	ctx := context.Background()
    28  
    29  	cID := container.Run(ctx, t, client, container.WithCmd("true"))
    30  	poll.WaitOn(t, container.IsStopped(ctx, client, cID), poll.WithDelay(100*time.Millisecond))
    31  
    32  	reference := "repo/" + strings.ToLower(t.Name()) + ":v1"
    33  	exportResp, err := client.ContainerExport(ctx, cID)
    34  	assert.NilError(t, err)
    35  	importResp, err := client.ImageImport(ctx, types.ImageImportSource{
    36  		Source:     exportResp,
    37  		SourceName: "-",
    38  	}, reference, types.ImageImportOptions{})
    39  	assert.NilError(t, err)
    40  
    41  	// If the import is successfully, then the message output should contain
    42  	// the image ID and match with the output from `docker images`.
    43  
    44  	dec := json.NewDecoder(importResp)
    45  	var jm jsonmessage.JSONMessage
    46  	err = dec.Decode(&jm)
    47  	assert.NilError(t, err)
    48  
    49  	images, err := client.ImageList(ctx, types.ImageListOptions{
    50  		Filters: filters.NewArgs(filters.Arg("reference", reference)),
    51  	})
    52  	assert.NilError(t, err)
    53  	assert.Check(t, is.Equal(jm.Status, images[0].ID))
    54  }
    55  
    56  // TestExportContainerAfterDaemonRestart checks that a container
    57  // created before start of the currently running dockerd
    58  // can be exported (as reported in #36561). To satisfy this
    59  // condition, daemon restart is needed after container creation.
    60  func TestExportContainerAfterDaemonRestart(t *testing.T) {
    61  	skip.If(t, testEnv.DaemonInfo.OSType == "windows")
    62  	skip.If(t, testEnv.IsRemoteDaemon)
    63  
    64  	d := daemon.New(t)
    65  	c := d.NewClientT(t)
    66  
    67  	d.StartWithBusybox(t)
    68  	defer d.Stop(t)
    69  
    70  	ctx := context.Background()
    71  	ctrID := container.Create(ctx, t, c)
    72  
    73  	d.Restart(t)
    74  
    75  	_, err := c.ContainerExport(ctx, ctrID)
    76  	assert.NilError(t, err)
    77  }