oras.land/oras-go/v2@v2.5.1-0.20240520045656-aef90e4d04c4/internal/resolver/memory_test.go (about) 1 /* 2 Copyright The ORAS Authors. 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 */ 15 16 package resolver 17 18 import ( 19 "context" 20 _ "crypto/sha256" 21 "errors" 22 "reflect" 23 "testing" 24 25 "github.com/opencontainers/go-digest" 26 ocispec "github.com/opencontainers/image-spec/specs-go/v1" 27 "oras.land/oras-go/v2/errdef" 28 ) 29 30 func TestMemorySuccess(t *testing.T) { 31 content := []byte("hello world") 32 desc := ocispec.Descriptor{ 33 MediaType: "test", 34 Digest: digest.FromBytes(content), 35 Size: int64(len(content)), 36 } 37 ref := "foobar" 38 39 s := NewMemory() 40 ctx := context.Background() 41 42 err := s.Tag(ctx, desc, ref) 43 if err != nil { 44 t.Fatal("Memory.Tag() error =", err) 45 } 46 47 got, err := s.Resolve(ctx, ref) 48 if err != nil { 49 t.Fatal("Memory.Resolve() error =", err) 50 } 51 if !reflect.DeepEqual(got, desc) { 52 t.Errorf("Memory.Resolve() = %v, want %v", got, desc) 53 } 54 if got := len(s.Map()); got != 1 { 55 t.Errorf("Memory.Map() = %v, want %v", got, 1) 56 } 57 58 s.Untag(ref) 59 _, err = s.Resolve(ctx, ref) 60 if !errors.Is(err, errdef.ErrNotFound) { 61 t.Errorf("Memory.Resolve() error = %v, want %v", err, errdef.ErrNotFound) 62 } 63 if got := len(s.Map()); got != 0 { 64 t.Errorf("Memory.Map() = %v, want %v", got, 0) 65 } 66 } 67 68 func TestMemoryNotFound(t *testing.T) { 69 ref := "foobar" 70 71 s := NewMemory() 72 ctx := context.Background() 73 74 _, err := s.Resolve(ctx, ref) 75 if !errors.Is(err, errdef.ErrNotFound) { 76 t.Errorf("Memory.Resolve() error = %v, want %v", err, errdef.ErrNotFound) 77 } 78 } 79 80 func TestTagSet(t *testing.T) { 81 refFoo := "foo" 82 refBar := "bar" 83 84 s := NewMemory() 85 ctx := context.Background() 86 87 content := []byte("hello world") 88 desc := ocispec.Descriptor{ 89 MediaType: "test", 90 Digest: digest.FromBytes(content), 91 Size: int64(len(content)), 92 } 93 94 s.Tag(ctx, desc, refFoo) 95 s.Tag(ctx, desc, refBar) 96 97 tagSet := s.TagSet(desc) 98 99 if !tagSet.Contains(refFoo) { 100 t.Fatalf("tagSet should contain %s", refFoo) 101 } 102 if !tagSet.Contains(refBar) { 103 t.Fatalf("tagSet should contain %s", refFoo) 104 } 105 if len(tagSet) != 2 { 106 t.Fatalf("expect size = %d, got %d", 2, len(tagSet)) 107 } 108 }