...

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

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

     1  package storage
     2  
     3  import (
     4  	"regexp"
     5  	"testing"
     6  
     7  	"github.com/docker/distribution"
     8  	"github.com/docker/distribution/context"
     9  	"github.com/docker/distribution/manifest"
    10  	"github.com/docker/distribution/manifest/schema2"
    11  	"github.com/docker/distribution/registry/storage/driver/inmemory"
    12  )
    13  
    14  func TestVerifyManifestForeignLayer(t *testing.T) {
    15  	ctx := context.Background()
    16  	inmemoryDriver := inmemory.New()
    17  	registry := createRegistry(t, inmemoryDriver,
    18  		ManifestURLsAllowRegexp(regexp.MustCompile("^https?://foo")),
    19  		ManifestURLsDenyRegexp(regexp.MustCompile("^https?://foo/nope")))
    20  	repo := makeRepository(t, registry, "test")
    21  	manifestService := makeManifestService(t, repo)
    22  
    23  	config, err := repo.Blobs(ctx).Put(ctx, schema2.MediaTypeImageConfig, nil)
    24  	if err != nil {
    25  		t.Fatal(err)
    26  	}
    27  
    28  	layer, err := repo.Blobs(ctx).Put(ctx, schema2.MediaTypeLayer, nil)
    29  	if err != nil {
    30  		t.Fatal(err)
    31  	}
    32  
    33  	foreignLayer := distribution.Descriptor{
    34  		Digest:    "sha256:463435349086340864309863409683460843608348608934092322395278926a",
    35  		Size:      6323,
    36  		MediaType: schema2.MediaTypeForeignLayer,
    37  	}
    38  
    39  	template := schema2.Manifest{
    40  		Versioned: manifest.Versioned{
    41  			SchemaVersion: 2,
    42  			MediaType:     schema2.MediaTypeManifest,
    43  		},
    44  		Config: config,
    45  	}
    46  
    47  	type testcase struct {
    48  		BaseLayer distribution.Descriptor
    49  		URLs      []string
    50  		Err       error
    51  	}
    52  
    53  	cases := []testcase{
    54  		{
    55  			foreignLayer,
    56  			nil,
    57  			errMissingURL,
    58  		},
    59  		{
    60  			// regular layers may have foreign urls
    61  			layer,
    62  			[]string{"http://foo/bar"},
    63  			nil,
    64  		},
    65  		{
    66  			foreignLayer,
    67  			[]string{"file:///local/file"},
    68  			errInvalidURL,
    69  		},
    70  		{
    71  			foreignLayer,
    72  			[]string{"http://foo/bar#baz"},
    73  			errInvalidURL,
    74  		},
    75  		{
    76  			foreignLayer,
    77  			[]string{""},
    78  			errInvalidURL,
    79  		},
    80  		{
    81  			foreignLayer,
    82  			[]string{"https://foo/bar", ""},
    83  			errInvalidURL,
    84  		},
    85  		{
    86  			foreignLayer,
    87  			[]string{"", "https://foo/bar"},
    88  			errInvalidURL,
    89  		},
    90  		{
    91  			foreignLayer,
    92  			[]string{"http://nope/bar"},
    93  			errInvalidURL,
    94  		},
    95  		{
    96  			foreignLayer,
    97  			[]string{"http://foo/nope"},
    98  			errInvalidURL,
    99  		},
   100  		{
   101  			foreignLayer,
   102  			[]string{"http://foo/bar"},
   103  			nil,
   104  		},
   105  		{
   106  			foreignLayer,
   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 := schema2.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  			}
   131  		}
   132  		if err != c.Err {
   133  			t.Errorf("%#v: expected %v, got %v", l, c.Err, err)
   134  		}
   135  	}
   136  }
   137  

View as plain text