github.com/lalkh/containerd@v1.4.3/remotes/handlers_test.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package remotes
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"github.com/containerd/containerd/images"
    24  	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
    25  )
    26  
    27  func TestContextCustomKeyPrefix(t *testing.T) {
    28  	ctx := context.Background()
    29  	cmt := "testing/custom.media.type"
    30  	ctx = WithMediaTypeKeyPrefix(ctx, images.MediaTypeDockerSchema2Layer, "bananas")
    31  	ctx = WithMediaTypeKeyPrefix(ctx, cmt, "apples")
    32  
    33  	// makes sure that even though we've supplied some custom handling, the built-in still works
    34  	t.Run("normal supported case", func(t *testing.T) {
    35  		desc := ocispec.Descriptor{MediaType: ocispec.MediaTypeImageLayer}
    36  		expected := "layer-"
    37  
    38  		actual := MakeRefKey(ctx, desc)
    39  		if actual != expected {
    40  			t.Fatalf("unexpected ref key, expected %s, got: %s", expected, actual)
    41  		}
    42  	})
    43  
    44  	t.Run("unknown media type", func(t *testing.T) {
    45  		desc := ocispec.Descriptor{MediaType: "we.dont.know.what.this.is"}
    46  		expected := "unknown-"
    47  
    48  		actual := MakeRefKey(ctx, desc)
    49  		if actual != expected {
    50  			t.Fatalf("unexpected ref key, expected %s, got: %s", expected, actual)
    51  		}
    52  	})
    53  
    54  	t.Run("overwrite supported media type", func(t *testing.T) {
    55  		desc := ocispec.Descriptor{MediaType: images.MediaTypeDockerSchema2Layer}
    56  		expected := "bananas-"
    57  
    58  		actual := MakeRefKey(ctx, desc)
    59  		if actual != expected {
    60  			t.Fatalf("unexpected ref key, expected %s, got: %s", expected, actual)
    61  		}
    62  	})
    63  
    64  	t.Run("custom media type", func(t *testing.T) {
    65  		desc := ocispec.Descriptor{MediaType: cmt}
    66  		expected := "apples-"
    67  
    68  		actual := MakeRefKey(ctx, desc)
    69  		if actual != expected {
    70  			t.Fatalf("unexpected ref key, expected %s, got: %s", expected, actual)
    71  		}
    72  	})
    73  }