github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/integration/container/export_test.go (about)

     1  package container // import "github.com/Prakhar-Agarwal-byte/moby/integration/container"
     2  
     3  import (
     4  	"encoding/json"
     5  	"strings"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/Prakhar-Agarwal-byte/moby/api/types"
    10  	"github.com/Prakhar-Agarwal-byte/moby/api/types/filters"
    11  	"github.com/Prakhar-Agarwal-byte/moby/integration/internal/container"
    12  	"github.com/Prakhar-Agarwal-byte/moby/pkg/jsonmessage"
    13  	"github.com/Prakhar-Agarwal-byte/moby/testutil"
    14  	"github.com/Prakhar-Agarwal-byte/moby/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  	ctx := setupTest(t)
    26  	apiClient := testEnv.APIClient()
    27  
    28  	cID := container.Run(ctx, t, apiClient, container.WithCmd("true"))
    29  	poll.WaitOn(t, container.IsStopped(ctx, apiClient, cID), poll.WithDelay(100*time.Millisecond))
    30  
    31  	reference := "repo/" + strings.ToLower(t.Name()) + ":v1"
    32  	exportResp, err := apiClient.ContainerExport(ctx, cID)
    33  	assert.NilError(t, err)
    34  	importResp, err := apiClient.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 := apiClient.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  	ctx := testutil.StartSpan(baseContext, t)
    64  
    65  	d := daemon.New(t)
    66  	c := d.NewClientT(t)
    67  
    68  	d.StartWithBusybox(ctx, t)
    69  	defer d.Stop(t)
    70  
    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  }