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

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