| 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 cas |
| 17 |
|
| 18 |
import ( |
| 19 |
"bytes" |
| 20 |
"context" |
| 21 |
_ "crypto/sha256" |
| 22 |
"errors" |
| 23 |
"io" |
| 24 |
"strings" |
| 25 |
"testing" |
| 26 |
|
| 27 |
"github.com/opencontainers/go-digest" |
| 28 |
ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 29 |
"oras.land/oras-go/v2/errdef" |
| 30 |
) |
| 31 |
|
| 32 |
func TestMemorySuccess(t *testing.T) { |
| 33 |
content := []byte("hello world") |
| 34 |
desc := ocispec.Descriptor{ |
| 35 |
MediaType: "test", |
| 36 |
Digest: digest.FromBytes(content), |
| 37 |
Size: int64(len(content)), |
| 38 |
} |
| 39 |
|
| 40 |
s := NewMemory() |
| 41 |
ctx := context.Background() |
| 42 |
|
| 43 |
err := s.Push(ctx, desc, bytes.NewReader(content)) |
| 44 |
if err != nil { |
| 45 |
t.Fatal("Memory.Push() error =", err) |
| 46 |
} |
| 47 |
|
| 48 |
exists, err := s.Exists(ctx, desc) |
| 49 |
if err != nil { |
| 50 |
t.Fatal("Memory.Exists() error =", err) |
| 51 |
} |
| 52 |
if !exists { |
| 53 |
t.Errorf("Memory.Exists() = %v, want %v", exists, true) |
| 54 |
} |
| 55 |
|
| 56 |
rc, err := s.Fetch(ctx, desc) |
| 57 |
if err != nil { |
| 58 |
t.Fatal("Memory.Fetch() error =", err) |
| 59 |
} |
| 60 |
got, err := io.ReadAll(rc) |
| 61 |
if err != nil { |
| 62 |
t.Fatal("Memory.Fetch().Read() error =", err) |
| 63 |
} |
| 64 |
err = rc.Close() |
| 65 |
if err != nil { |
| 66 |
t.Error("Memory.Fetch().Close() error =", err) |
| 67 |
} |
| 68 |
if !bytes.Equal(got, content) { |
| 69 |
t.Errorf("Memory.Fetch() = %v, want %v", got, content) |
| 70 |
} |
| 71 |
if got := len(s.Map()); got != 1 { |
| 72 |
t.Errorf("Memory.Map() = %v, want %v", got, 1) |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
func TestMemoryNotFound(t *testing.T) { |
| 77 |
content := []byte("hello world") |
| 78 |
desc := ocispec.Descriptor{ |
| 79 |
MediaType: "test", |
| 80 |
Digest: digest.FromBytes(content), |
| 81 |
Size: int64(len(content)), |
| 82 |
} |
| 83 |
|
| 84 |
s := NewMemory() |
| 85 |
ctx := context.Background() |
| 86 |
|
| 87 |
exists, err := s.Exists(ctx, desc) |
| 88 |
if err != nil { |
| 89 |
t.Error("Memory.Exists() error =", err) |
| 90 |
} |
| 91 |
if exists { |
| 92 |
t.Errorf("Memory.Exists() = %v, want %v", exists, false) |
| 93 |
} |
| 94 |
|
| 95 |
_, err = s.Fetch(ctx, desc) |
| 96 |
if !errors.Is(err, errdef.ErrNotFound) { |
| 97 |
t.Errorf("Memory.Fetch() error = %v, want %v", err, errdef.ErrNotFound) |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
func TestMemoryAlreadyExists(t *testing.T) { |
| 102 |
content := []byte("hello world") |
| 103 |
desc := ocispec.Descriptor{ |
| 104 |
MediaType: "test", |
| 105 |
Digest: digest.FromBytes(content), |
| 106 |
Size: int64(len(content)), |
| 107 |
} |
| 108 |
|
| 109 |
s := NewMemory() |
| 110 |
ctx := context.Background() |
| 111 |
|
| 112 |
err := s.Push(ctx, desc, bytes.NewReader(content)) |
| 113 |
if err != nil { |
| 114 |
t.Fatal("Memory.Push() error =", err) |
| 115 |
} |
| 116 |
|
| 117 |
err = s.Push(ctx, desc, bytes.NewReader(content)) |
| 118 |
if !errors.Is(err, errdef.ErrAlreadyExists) { |
| 119 |
t.Errorf("Memory.Push() error = %v, want %v", err, errdef.ErrAlreadyExists) |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
func TestMemoryBadPush(t *testing.T) { |
| 124 |
content := []byte("hello world") |
| 125 |
desc := ocispec.Descriptor{ |
| 126 |
MediaType: "test", |
| 127 |
Digest: digest.FromBytes(content), |
| 128 |
Size: int64(len(content)), |
| 129 |
} |
| 130 |
|
| 131 |
s := NewMemory() |
| 132 |
ctx := context.Background() |
| 133 |
|
| 134 |
err := s.Push(ctx, desc, strings.NewReader("foobar")) |
| 135 |
if err == nil { |
| 136 |
t.Errorf("Memory.Push() error = %v, wantErr %v", err, true) |
| 137 |
} |
| 138 |
} |