github.com/dctrud/umoci@v0.4.3-0.20191016193643-05a1d37de015/oci/layer/unpack_test.go (about)

     1  /*
     2   * umoci: Umoci Modifies Open Containers' Images
     3   * Copyright (C) 2017, 2018 SUSE LLC.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *    http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package layer
    19  
    20  import (
    21  	"bytes"
    22  	"encoding/base64"
    23  	"io"
    24  	"io/ioutil"
    25  	"os"
    26  	"path/filepath"
    27  	"testing"
    28  
    29  	"github.com/openSUSE/umoci/oci/cas/dir"
    30  	"github.com/openSUSE/umoci/oci/casext"
    31  	"github.com/opencontainers/go-digest"
    32  	"github.com/opencontainers/image-spec/specs-go"
    33  	ispec "github.com/opencontainers/image-spec/specs-go/v1"
    34  	rspec "github.com/opencontainers/runtime-spec/specs-go"
    35  	"golang.org/x/net/context"
    36  )
    37  
    38  func mustDecodeString(s string) []byte {
    39  	b, err := base64.StdEncoding.DecodeString(s)
    40  	if err != nil {
    41  		panic(err)
    42  	}
    43  	return b
    44  }
    45  
    46  // Ensure that "custom layers" generated by other programs (such as a manual
    47  // tar+gzip) are still correctly handled by us (this used to not work because
    48  // that "archive/tar" parser doesn't consume the whole tar stream if it detects
    49  // that there is no more metadata it is interested in in the tar stream).
    50  func TestUnpackManifestCustomLayer(t *testing.T) {
    51  	ctx := context.Background()
    52  
    53  	// These layers were manually generated using GNU tar + GNU gzip.
    54  	// XXX: In future we should also add libarchive tar archives.
    55  	var layers = []struct {
    56  		base64 string
    57  		digest digest.Digest
    58  	}{
    59  		{
    60  			base64: `
    61  H4sIAAsoz1kAA+3XvW7CMBAH8Mx9Cj+Bcz7bZxjYO3brWEXBCCS+lBiJvn2dVColAUpUEop6v8UR
    62  jrGj6P7RyfQl2z/7bOqLUloNJpXJrUFExtRj1BxBaUyUVqQJtSWVgAJnVSL2Nz/JCbsyZEU8yhB7
    63  /UEaxCosVn6iLJAzI3RaIhEhGjJPcTZrzhLUs85Vs/n5tfd+MnYNmfa/R1XjztpqVM5+1r065EGd
    64  Bcf1rxxSImz/RzvUf/6+nceLC/fFhLzwP81wexCylf/Bl+Fttlj6m+3RKf+1ju8freP8H0Qr/62S
    65  qA2hInCt/NcAcgRYvyfbzv+jtfd+MnYN2UO9N32r/5P5r9A16j9exfwfpCb/ef6/zjci36xDsVmW
    66  Isy92GZlOP5ltgu7wkvRvrXwpV+H9nrJxf8oznz/p4sLpdBV9/4Pjebv/yC69X8/fP9HJMdYrR2P
    67  LUfAQ5Bf9d5fI1j3f+A69H/aGuD+jzHGGGOMMcYYY4wxxn7jA5XNY6oAKAAA`,
    68  			digest: digest.NewDigestFromHex(digest.SHA256.String(), "e489a16a8ca0d682394867ad8a8183f0a47cbad80b3134a83412a6796ad9242a"),
    69  		},
    70  		{
    71  			base64: `
    72  H4sIAJ4oz1kAA+3Wu27CMBQG4Mw8xSldK8d3p0OHbu3WN6hCYhELCMh2Bbx9HTogwkWtBLSo51uM
    73  dBLsSPl/heRv5erFlrX1gWimHnOSnRtNtJSbNemvlAmeMcG00FxyajLKqNE0g9XZT3LAR4ilT0e5
    74  xl5/kKAwi25mn5ii2shCKUaKgmshBOWDNC13p5xIvply0U2r4/f+9pOh7yD55ffoMm6U6lZm1Ffu
    75  2bYPNl2wm39muMxAXf5o2/xX60WTfpy4LjXkif/pl9uNIHv9H22I76HybhFJaM6xx8/7XyhjsP+v
    76  4WD/m+JY/2tBKOumRqj9/teUCCXTVAvs/9tALpD3vhRxear/mZC9/Kd3KH3/XSWT/7z/7+/ykWvz
    77  0AwGtmrmMHyNsCwDlDDybtxEqObTGupyDa6F54V30wco2xpiY6GazqtJgKX1FkL0buLacRo4H61t
    78  yRAbACGEEEIIIYQQQgghhBBCCKEr+wTE0sQyACgAAA==`,
    79  			digest: digest.NewDigestFromHex(digest.SHA256.String(), "39f100ed000b187ba74b3132cc207c63ad1765adaeb783aa7f242f1f7b6f5ea2"),
    80  		},
    81  	}
    82  
    83  	root, err := ioutil.TempDir("", "umoci-TestUnpackManifestCustomLayer")
    84  	if err != nil {
    85  		t.Fatal(err)
    86  	}
    87  	defer os.RemoveAll(root)
    88  
    89  	// Create our image.
    90  	image := filepath.Join(root, "image")
    91  	if err := dir.Create(image); err != nil {
    92  		t.Fatal(err)
    93  	}
    94  	engine, err := dir.Open(image)
    95  	if err != nil {
    96  		t.Fatal(err)
    97  	}
    98  	engineExt := casext.NewEngine(engine)
    99  
   100  	// Set up the CAS and an image from the above layers.
   101  	var layerDigests []digest.Digest
   102  	var layerDescriptors []ispec.Descriptor
   103  	for _, layer := range layers {
   104  		var layerReader io.Reader
   105  
   106  		// Since we already have the digests we don't need to jump through the
   107  		// hoops of decompressing our already-compressed blobs above to get the
   108  		// DiffIDs.
   109  		layerReader = bytes.NewBuffer(mustDecodeString(layer.base64))
   110  		layerDigest, layerSize, err := engineExt.PutBlob(ctx, layerReader)
   111  		if err != nil {
   112  			t.Fatal(err)
   113  		}
   114  
   115  		layerDigests = append(layerDigests, layer.digest)
   116  		layerDescriptors = append(layerDescriptors, ispec.Descriptor{
   117  			MediaType: ispec.MediaTypeImageLayerGzip,
   118  			Digest:    layerDigest,
   119  			Size:      layerSize,
   120  		})
   121  	}
   122  
   123  	// Create the config.
   124  	config := ispec.Image{
   125  		OS: "linux",
   126  		RootFS: ispec.RootFS{
   127  			Type:    "layers",
   128  			DiffIDs: layerDigests,
   129  		},
   130  	}
   131  	configDigest, configSize, err := engineExt.PutBlobJSON(ctx, config)
   132  	if err != nil {
   133  		t.Fatal(err)
   134  	}
   135  	configDescriptor := ispec.Descriptor{
   136  		MediaType: ispec.MediaTypeImageConfig,
   137  		Digest:    configDigest,
   138  		Size:      configSize,
   139  	}
   140  
   141  	// Create the manifest.
   142  	manifest := ispec.Manifest{
   143  		Versioned: specs.Versioned{
   144  			SchemaVersion: 2,
   145  		},
   146  		Config: configDescriptor,
   147  		Layers: layerDescriptors,
   148  	}
   149  
   150  	bundle, err := ioutil.TempDir("", "umoci-TestUnpackManifestCustomLayer_bundle")
   151  	if err != nil {
   152  		t.Fatal(err)
   153  	}
   154  	defer os.RemoveAll(bundle)
   155  
   156  	// Unpack (we map both root and the uid/gid in the archives to the current user).
   157  	mapOptions := &MapOptions{
   158  		UIDMappings: []rspec.LinuxIDMapping{
   159  			{HostID: uint32(os.Geteuid()), ContainerID: 0, Size: 1},
   160  			{HostID: uint32(os.Geteuid()), ContainerID: 1000, Size: 1},
   161  		},
   162  		GIDMappings: []rspec.LinuxIDMapping{
   163  			{HostID: uint32(os.Getegid()), ContainerID: 0, Size: 1},
   164  			{HostID: uint32(os.Getegid()), ContainerID: 100, Size: 1},
   165  		},
   166  		Rootless: os.Geteuid() != 0,
   167  	}
   168  	if err := UnpackManifest(ctx, engineExt, bundle, manifest, mapOptions); err != nil {
   169  		t.Errorf("unexpected UnpackManifest error: %+v\n", err)
   170  	}
   171  }