...

Source file src/github.com/docker/distribution/registry/storage/ocimanifesthandler_test.go

Documentation: github.com/docker/distribution/registry/storage

     1  package storage
     2  
     3  import (
     4  	"context"
     5  	"regexp"
     6  	"testing"
     7  
     8  	"github.com/docker/distribution"
     9  	"github.com/docker/distribution/manifest"
    10  	"github.com/docker/distribution/manifest/ocischema"
    11  	"github.com/docker/distribution/registry/storage/driver/inmemory"
    12  	v1 "github.com/opencontainers/image-spec/specs-go/v1"
    13  )
    14  
    15  func TestVerifyOCIManifestNonDistributableLayer(t *testing.T) {
    16  	ctx := context.Background()
    17  	inmemoryDriver := inmemory.New()
    18  	registry := createRegistry(t, inmemoryDriver,
    19  		ManifestURLsAllowRegexp(regexp.MustCompile("^https?://foo")),
    20  		ManifestURLsDenyRegexp(regexp.MustCompile("^https?://foo/nope")))
    21  	repo := makeRepository(t, registry, "test")
    22  	manifestService := makeManifestService(t, repo)
    23  
    24  	config, err := repo.Blobs(ctx).Put(ctx, v1.MediaTypeImageConfig, nil)
    25  	if err != nil {
    26  		t.Fatal(err)
    27  	}
    28  
    29  	layer, err := repo.Blobs(ctx).Put(ctx, v1.MediaTypeImageLayerGzip, nil)
    30  	if err != nil {
    31  		t.Fatal(err)
    32  	}
    33  
    34  	nonDistributableLayer := distribution.Descriptor{
    35  		Digest:    "sha256:463435349086340864309863409683460843608348608934092322395278926a",
    36  		Size:      6323,
    37  		MediaType: v1.MediaTypeImageLayerNonDistributableGzip,
    38  	}
    39  
    40  	template := ocischema.Manifest{
    41  		Versioned: manifest.Versioned{
    42  			SchemaVersion: 2,
    43  			MediaType:     v1.MediaTypeImageManifest,
    44  		},
    45  		Config: config,
    46  	}
    47  
    48  	type testcase struct {
    49  		BaseLayer distribution.Descriptor
    50  		URLs      []string
    51  		Err       error
    52  	}
    53  
    54  	cases := []testcase{
    55  		{
    56  			nonDistributableLayer,
    57  			nil,
    58  			distribution.ErrManifestBlobUnknown{Digest: nonDistributableLayer.Digest},
    59  		},
    60  		{
    61  			layer,
    62  			[]string{"http://foo/bar"},
    63  			nil,
    64  		},
    65  		{
    66  			nonDistributableLayer,
    67  			[]string{"file:///local/file"},
    68  			errInvalidURL,
    69  		},
    70  		{
    71  			nonDistributableLayer,
    72  			[]string{"http://foo/bar#baz"},
    73  			errInvalidURL,
    74  		},
    75  		{
    76  			nonDistributableLayer,
    77  			[]string{""},
    78  			errInvalidURL,
    79  		},
    80  		{
    81  			nonDistributableLayer,
    82  			[]string{"https://foo/bar", ""},
    83  			errInvalidURL,
    84  		},
    85  		{
    86  			nonDistributableLayer,
    87  			[]string{"", "https://foo/bar"},
    88  			errInvalidURL,
    89  		},
    90  		{
    91  			nonDistributableLayer,
    92  			[]string{"http://nope/bar"},
    93  			errInvalidURL,
    94  		},
    95  		{
    96  			nonDistributableLayer,
    97  			[]string{"http://foo/nope"},
    98  			errInvalidURL,
    99  		},
   100  		{
   101  			nonDistributableLayer,
   102  			[]string{"http://foo/bar"},
   103  			nil,
   104  		},
   105  		{
   106  			nonDistributableLayer,
   107  			[]string{"https://foo/bar"},
   108  			nil,
   109  		},
   110  	}
   111  
   112  	for _, c := range cases {
   113  		m := template
   114  		l := c.BaseLayer
   115  		l.URLs = c.URLs
   116  		m.Layers = []distribution.Descriptor{l}
   117  		dm, err := ocischema.FromStruct(m)
   118  		if err != nil {
   119  			t.Error(err)
   120  			continue
   121  		}
   122  
   123  		_, err = manifestService.Put(ctx, dm)
   124  		if verr, ok := err.(distribution.ErrManifestVerification); ok {
   125  			// Extract the first error
   126  			if len(verr) == 2 {
   127  				if _, ok = verr[1].(distribution.ErrManifestBlobUnknown); ok {
   128  					err = verr[0]
   129  				}
   130  			} else if len(verr) == 1 {
   131  				err = verr[0]
   132  			}
   133  		}
   134  		if err != c.Err {
   135  			t.Errorf("%#v: expected %v, got %v", l, c.Err, err)
   136  		}
   137  	}
   138  }
   139  

View as plain text