github.com/lusis/distribution@v2.0.1+incompatible/registry/storage/paths_test.go (about)

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