...

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

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

     1  package storage
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/opencontainers/go-digest"
     7  )
     8  
     9  func TestPathMapper(t *testing.T) {
    10  	for _, testcase := range []struct {
    11  		spec     pathSpec
    12  		expected string
    13  	}{
    14  		{
    15  			spec: manifestRevisionPathSpec{
    16  				name:     "foo/bar",
    17  				revision: "sha256:abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789",
    18  			},
    19  			expected: "/docker/registry/v2/repositories/foo/bar/_manifests/revisions/sha256/abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789",
    20  		},
    21  		{
    22  			spec: manifestRevisionLinkPathSpec{
    23  				name:     "foo/bar",
    24  				revision: "sha256:abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789",
    25  			},
    26  			expected: "/docker/registry/v2/repositories/foo/bar/_manifests/revisions/sha256/abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789/link",
    27  		},
    28  		{
    29  			spec: manifestTagsPathSpec{
    30  				name: "foo/bar",
    31  			},
    32  			expected: "/docker/registry/v2/repositories/foo/bar/_manifests/tags",
    33  		},
    34  		{
    35  			spec: manifestTagPathSpec{
    36  				name: "foo/bar",
    37  				tag:  "thetag",
    38  			},
    39  			expected: "/docker/registry/v2/repositories/foo/bar/_manifests/tags/thetag",
    40  		},
    41  		{
    42  			spec: manifestTagCurrentPathSpec{
    43  				name: "foo/bar",
    44  				tag:  "thetag",
    45  			},
    46  			expected: "/docker/registry/v2/repositories/foo/bar/_manifests/tags/thetag/current/link",
    47  		},
    48  		{
    49  			spec: manifestTagIndexPathSpec{
    50  				name: "foo/bar",
    51  				tag:  "thetag",
    52  			},
    53  			expected: "/docker/registry/v2/repositories/foo/bar/_manifests/tags/thetag/index",
    54  		},
    55  		{
    56  			spec: manifestTagIndexEntryPathSpec{
    57  				name:     "foo/bar",
    58  				tag:      "thetag",
    59  				revision: "sha256:abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789",
    60  			},
    61  			expected: "/docker/registry/v2/repositories/foo/bar/_manifests/tags/thetag/index/sha256/abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789",
    62  		},
    63  		{
    64  			spec: manifestTagIndexEntryLinkPathSpec{
    65  				name:     "foo/bar",
    66  				tag:      "thetag",
    67  				revision: "sha256:abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789",
    68  			},
    69  			expected: "/docker/registry/v2/repositories/foo/bar/_manifests/tags/thetag/index/sha256/abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789/link",
    70  		},
    71  
    72  		{
    73  			spec: uploadDataPathSpec{
    74  				name: "foo/bar",
    75  				id:   "asdf-asdf-asdf-adsf",
    76  			},
    77  			expected: "/docker/registry/v2/repositories/foo/bar/_uploads/asdf-asdf-asdf-adsf/data",
    78  		},
    79  		{
    80  			spec: uploadStartedAtPathSpec{
    81  				name: "foo/bar",
    82  				id:   "asdf-asdf-asdf-adsf",
    83  			},
    84  			expected: "/docker/registry/v2/repositories/foo/bar/_uploads/asdf-asdf-asdf-adsf/startedat",
    85  		},
    86  	} {
    87  		p, err := pathFor(testcase.spec)
    88  		if err != nil {
    89  			t.Fatalf("unexpected generating path (%T): %v", testcase.spec, err)
    90  		}
    91  
    92  		if p != testcase.expected {
    93  			t.Fatalf("unexpected path generated (%T): %q != %q", testcase.spec, p, testcase.expected)
    94  		}
    95  	}
    96  
    97  	// Add a few test cases to ensure we cover some errors
    98  
    99  	// Specify a path that requires a revision and get a digest validation error.
   100  	badpath, err := pathFor(manifestRevisionPathSpec{
   101  		name: "foo/bar",
   102  	})
   103  
   104  	if err == nil {
   105  		t.Fatalf("expected an error when mapping an invalid revision: %s", badpath)
   106  	}
   107  
   108  }
   109  
   110  func TestDigestFromPath(t *testing.T) {
   111  	for _, testcase := range []struct {
   112  		path       string
   113  		expected   digest.Digest
   114  		multilevel bool
   115  		err        error
   116  	}{
   117  		{
   118  			path:       "/docker/registry/v2/blobs/sha256/99/9943fffae777400c0344c58869c4c2619c329ca3ad4df540feda74d291dd7c86/data",
   119  			multilevel: true,
   120  			expected:   "sha256:9943fffae777400c0344c58869c4c2619c329ca3ad4df540feda74d291dd7c86",
   121  			err:        nil,
   122  		},
   123  	} {
   124  		result, err := digestFromPath(testcase.path)
   125  		if err != testcase.err {
   126  			t.Fatalf("Unexpected error value %v when we wanted %v", err, testcase.err)
   127  		}
   128  
   129  		if result != testcase.expected {
   130  			t.Fatalf("Unexpected result value %v when we wanted %v", result, testcase.expected)
   131  
   132  		}
   133  	}
   134  }
   135  

View as plain text