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