github.com/Heebron/moby@v0.0.0-20221111184709-6eab4f55faf7/integration/image/import_test.go (about)

     1  package image // import "github.com/docker/docker/integration/image"
     2  
     3  import (
     4  	"archive/tar"
     5  	"bytes"
     6  	"context"
     7  	"io"
     8  	"runtime"
     9  	"strconv"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/docker/docker/api/types"
    14  	"github.com/docker/docker/image"
    15  	"github.com/docker/docker/testutil"
    16  	"github.com/docker/docker/testutil/daemon"
    17  	"gotest.tools/v3/assert"
    18  	"gotest.tools/v3/skip"
    19  )
    20  
    21  // Ensure we don't regress on CVE-2017-14992.
    22  func TestImportExtremelyLargeImageWorks(t *testing.T) {
    23  	skip.If(t, testEnv.IsRemoteDaemon, "cannot run daemon when remote daemon")
    24  	skip.If(t, runtime.GOARCH == "arm64", "effective test will be time out")
    25  	skip.If(t, testEnv.OSType == "windows", "TODO enable on windows")
    26  	t.Parallel()
    27  
    28  	// Spin up a new daemon, so that we can run this test in parallel (it's a slow test)
    29  	d := daemon.New(t)
    30  	d.Start(t)
    31  	defer d.Stop(t)
    32  
    33  	client := d.NewClientT(t)
    34  
    35  	// Construct an empty tar archive with about 8GB of junk padding at the
    36  	// end. This should not cause any crashes (the padding should be mostly
    37  	// ignored).
    38  	var tarBuffer bytes.Buffer
    39  
    40  	tw := tar.NewWriter(&tarBuffer)
    41  	err := tw.Close()
    42  	assert.NilError(t, err)
    43  	imageRdr := io.MultiReader(&tarBuffer, io.LimitReader(testutil.DevZero, 8*1024*1024*1024))
    44  	reference := strings.ToLower(t.Name()) + ":v42"
    45  
    46  	_, err = client.ImageImport(context.Background(),
    47  		types.ImageImportSource{Source: imageRdr, SourceName: "-"},
    48  		reference,
    49  		types.ImageImportOptions{})
    50  	assert.NilError(t, err)
    51  }
    52  
    53  func TestImportWithCustomPlatform(t *testing.T) {
    54  	skip.If(t, testEnv.OSType == "windows", "TODO enable on windows")
    55  
    56  	defer setupTest(t)()
    57  	client := testEnv.APIClient()
    58  	ctx := context.Background()
    59  
    60  	// Construct an empty tar archive.
    61  	var tarBuffer bytes.Buffer
    62  
    63  	tw := tar.NewWriter(&tarBuffer)
    64  	err := tw.Close()
    65  	assert.NilError(t, err)
    66  	imageRdr := io.MultiReader(&tarBuffer, io.LimitReader(testutil.DevZero, 0))
    67  
    68  	tests := []struct {
    69  		name        string
    70  		platform    string
    71  		expected    image.V1Image
    72  		expectedErr string
    73  	}{
    74  		{
    75  			platform: "",
    76  			expected: image.V1Image{
    77  				OS:           runtime.GOOS,
    78  				Architecture: runtime.GOARCH, // this may fail on armhf due to normalization?
    79  			},
    80  		},
    81  		{
    82  			platform:    "       ",
    83  			expectedErr: "is an invalid component",
    84  		},
    85  		{
    86  			platform:    "/",
    87  			expectedErr: "is an invalid component",
    88  		},
    89  		{
    90  			platform: runtime.GOOS,
    91  			expected: image.V1Image{
    92  				OS:           runtime.GOOS,
    93  				Architecture: runtime.GOARCH, // this may fail on armhf due to normalization?
    94  			},
    95  		},
    96  		{
    97  			platform: strings.ToUpper(runtime.GOOS),
    98  			expected: image.V1Image{
    99  				OS:           runtime.GOOS,
   100  				Architecture: runtime.GOARCH, // this may fail on armhf due to normalization?
   101  			},
   102  		},
   103  		{
   104  			platform: runtime.GOOS + "/sparc64",
   105  			expected: image.V1Image{
   106  				OS:           runtime.GOOS,
   107  				Architecture: "sparc64",
   108  			},
   109  		},
   110  		{
   111  			platform:    "macos",
   112  			expectedErr: "operating system is not supported",
   113  		},
   114  		{
   115  			platform:    "macos/arm64",
   116  			expectedErr: "operating system is not supported",
   117  		},
   118  		{
   119  			// TODO: platforms.Normalize() only validates os or arch if a single component is passed,
   120  			//       but ignores unknown os/arch in other cases. See:
   121  			//       https://github.com/containerd/containerd/blob/7d4891783aac5adf6cd83f657852574a71875631/platforms/platforms.go#L183-L209
   122  			platform:    "nintendo64",
   123  			expectedErr: "unknown operating system or architecture",
   124  		},
   125  	}
   126  
   127  	for i, tc := range tests {
   128  		tc := tc
   129  		t.Run(tc.platform, func(t *testing.T) {
   130  			reference := "import-with-platform:tc-" + strconv.Itoa(i)
   131  			_, err = client.ImageImport(context.Background(),
   132  				types.ImageImportSource{Source: imageRdr, SourceName: "-"},
   133  				reference,
   134  				types.ImageImportOptions{Platform: tc.platform})
   135  			if tc.expectedErr != "" {
   136  				assert.ErrorContains(t, err, tc.expectedErr)
   137  			} else {
   138  				assert.NilError(t, err)
   139  
   140  				inspect, _, err := client.ImageInspectWithRaw(ctx, reference)
   141  				assert.NilError(t, err)
   142  				assert.Equal(t, inspect.Os, tc.expected.OS)
   143  				assert.Equal(t, inspect.Architecture, tc.expected.Architecture)
   144  			}
   145  		})
   146  	}
   147  }