github.com/tonistiigi/docker@v0.10.1-0.20240229224939-974013b0dc6a/integration/container/export_test.go (about)

     1  package container // import "github.com/docker/docker/integration/container"
     2  
     3  import (
     4  	"encoding/json"
     5  	"strings"
     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/api/types/image"
    12  	"github.com/docker/docker/integration/internal/container"
    13  	"github.com/docker/docker/pkg/jsonmessage"
    14  	"github.com/docker/docker/testutil"
    15  	"github.com/docker/docker/testutil/daemon"
    16  	"gotest.tools/v3/assert"
    17  	is "gotest.tools/v3/assert/cmp"
    18  	"gotest.tools/v3/poll"
    19  	"gotest.tools/v3/skip"
    20  )
    21  
    22  // export an image and try to import it into a new one
    23  func TestExportContainerAndImportImage(t *testing.T) {
    24  	skip.If(t, testEnv.DaemonInfo.OSType == "windows")
    25  
    26  	ctx := setupTest(t)
    27  	apiClient := testEnv.APIClient()
    28  
    29  	cID := container.Run(ctx, t, apiClient, container.WithCmd("true"))
    30  	poll.WaitOn(t, container.IsStopped(ctx, apiClient, cID), poll.WithDelay(100*time.Millisecond))
    31  
    32  	reference := "repo/" + strings.ToLower(t.Name()) + ":v1"
    33  	exportResp, err := apiClient.ContainerExport(ctx, cID)
    34  	assert.NilError(t, err)
    35  	importResp, err := apiClient.ImageImport(ctx, types.ImageImportSource{
    36  		Source:     exportResp,
    37  		SourceName: "-",
    38  	}, reference, image.ImportOptions{})
    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 := apiClient.ImageList(ctx, image.ListOptions{
    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  	ctx := testutil.StartSpan(baseContext, t)
    65  
    66  	d := daemon.New(t)
    67  	c := d.NewClientT(t)
    68  
    69  	d.StartWithBusybox(ctx, t)
    70  	defer d.Stop(t)
    71  
    72  	ctrID := container.Create(ctx, t, c)
    73  
    74  	d.Restart(t)
    75  
    76  	_, err := c.ContainerExport(ctx, ctrID)
    77  	assert.NilError(t, err)
    78  }