| 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 oras_test |
| 17 |
|
| 18 |
import ( |
| 19 |
"bytes" |
| 20 |
"context" |
| 21 |
_ "crypto/sha256" |
| 22 |
"encoding/json" |
| 23 |
"errors" |
| 24 |
"fmt" |
| 25 |
"io" |
| 26 |
"reflect" |
| 27 |
"sync/atomic" |
| 28 |
"testing" |
| 29 |
|
| 30 |
"github.com/opencontainers/go-digest" |
| 31 |
ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 32 |
"oras.land/oras-go/v2" |
| 33 |
"oras.land/oras-go/v2/content" |
| 34 |
"oras.land/oras-go/v2/content/file" |
| 35 |
"oras.land/oras-go/v2/content/memory" |
| 36 |
"oras.land/oras-go/v2/errdef" |
| 37 |
"oras.land/oras-go/v2/internal/cas" |
| 38 |
"oras.land/oras-go/v2/internal/docker" |
| 39 |
"oras.land/oras-go/v2/internal/spec" |
| 40 |
) |
| 41 |
|
| 42 |
// storageTracker tracks storage API counts. |
| 43 |
type storageTracker struct { |
| 44 |
content.Storage |
| 45 |
fetch int64 |
| 46 |
push int64 |
| 47 |
exists int64 |
| 48 |
} |
| 49 |
|
| 50 |
func (t *storageTracker) Fetch(ctx context.Context, target ocispec.Descriptor) (io.ReadCloser, error) { |
| 51 |
atomic.AddInt64(&t.fetch, 1) |
| 52 |
return t.Storage.Fetch(ctx, target) |
| 53 |
} |
| 54 |
|
| 55 |
func (t *storageTracker) Push(ctx context.Context, expected ocispec.Descriptor, content io.Reader) error { |
| 56 |
atomic.AddInt64(&t.push, 1) |
| 57 |
return t.Storage.Push(ctx, expected, content) |
| 58 |
} |
| 59 |
|
| 60 |
func (t *storageTracker) Exists(ctx context.Context, target ocispec.Descriptor) (bool, error) { |
| 61 |
atomic.AddInt64(&t.exists, 1) |
| 62 |
return t.Storage.Exists(ctx, target) |
| 63 |
} |
| 64 |
|
| 65 |
type mockReferencePusher struct { |
| 66 |
oras.Target |
| 67 |
pushReference int64 |
| 68 |
} |
| 69 |
|
| 70 |
func (p *mockReferencePusher) PushReference(ctx context.Context, expected ocispec.Descriptor, content io.Reader, reference string) error { |
| 71 |
atomic.AddInt64(&p.pushReference, 1) |
| 72 |
if err := p.Target.Push(ctx, expected, content); err != nil { |
| 73 |
return err |
| 74 |
} |
| 75 |
return p.Target.Tag(ctx, expected, reference) |
| 76 |
} |
| 77 |
|
| 78 |
func TestCopy_FullCopy(t *testing.T) { |
| 79 |
src := memory.New() |
| 80 |
dst := memory.New() |
| 81 |
|
| 82 |
// generate test content |
| 83 |
var blobs [][]byte |
| 84 |
var descs []ocispec.Descriptor |
| 85 |
appendBlob := func(mediaType string, blob []byte) { |
| 86 |
blobs = append(blobs, blob) |
| 87 |
descs = append(descs, ocispec.Descriptor{ |
| 88 |
MediaType: mediaType, |
| 89 |
Digest: digest.FromBytes(blob), |
| 90 |
Size: int64(len(blob)), |
| 91 |
}) |
| 92 |
} |
| 93 |
generateManifest := func(config ocispec.Descriptor, layers ...ocispec.Descriptor) { |
| 94 |
manifest := ocispec.Manifest{ |
| 95 |
Config: config, |
| 96 |
Layers: layers, |
| 97 |
} |
| 98 |
manifestJSON, err := json.Marshal(manifest) |
| 99 |
if err != nil { |
| 100 |
t.Fatal(err) |
| 101 |
} |
| 102 |
appendBlob(ocispec.MediaTypeImageManifest, manifestJSON) |
| 103 |
} |
| 104 |
|
| 105 |
appendBlob(ocispec.MediaTypeImageConfig, []byte("config")) // Blob 0 |
| 106 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("foo")) // Blob 1 |
| 107 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("bar")) // Blob 2 |
| 108 |
generateManifest(descs[0], descs[1:3]...) // Blob 3 |
| 109 |
|
| 110 |
ctx := context.Background() |
| 111 |
for i := range blobs { |
| 112 |
err := src.Push(ctx, descs[i], bytes.NewReader(blobs[i])) |
| 113 |
if err != nil { |
| 114 |
t.Fatalf("failed to push test content to src: %d: %v", i, err) |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
root := descs[3] |
| 119 |
ref := "foobar" |
| 120 |
err := src.Tag(ctx, root, ref) |
| 121 |
if err != nil { |
| 122 |
t.Fatal("fail to tag root node", err) |
| 123 |
} |
| 124 |
|
| 125 |
// test copy |
| 126 |
gotDesc, err := oras.Copy(ctx, src, ref, dst, "", oras.CopyOptions{}) |
| 127 |
if err != nil { |
| 128 |
t.Fatalf("Copy() error = %v, wantErr %v", err, false) |
| 129 |
} |
| 130 |
if !reflect.DeepEqual(gotDesc, root) { |
| 131 |
t.Errorf("Copy() = %v, want %v", gotDesc, root) |
| 132 |
} |
| 133 |
|
| 134 |
// verify contents |
| 135 |
for i, desc := range descs { |
| 136 |
exists, err := dst.Exists(ctx, desc) |
| 137 |
if err != nil { |
| 138 |
t.Fatalf("dst.Exists(%d) error = %v", i, err) |
| 139 |
} |
| 140 |
if !exists { |
| 141 |
t.Errorf("dst.Exists(%d) = %v, want %v", i, exists, true) |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
// verify tag |
| 146 |
gotDesc, err = dst.Resolve(ctx, ref) |
| 147 |
if err != nil { |
| 148 |
t.Fatal("dst.Resolve() error =", err) |
| 149 |
} |
| 150 |
if !reflect.DeepEqual(gotDesc, root) { |
| 151 |
t.Errorf("dst.Resolve() = %v, want %v", gotDesc, root) |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
func TestCopy_ExistedRoot(t *testing.T) { |
| 156 |
src := memory.New() |
| 157 |
dst := memory.New() |
| 158 |
|
| 159 |
// generate test content |
| 160 |
var blobs [][]byte |
| 161 |
var descs []ocispec.Descriptor |
| 162 |
appendBlob := func(mediaType string, blob []byte) { |
| 163 |
blobs = append(blobs, blob) |
| 164 |
descs = append(descs, ocispec.Descriptor{ |
| 165 |
MediaType: mediaType, |
| 166 |
Digest: digest.FromBytes(blob), |
| 167 |
Size: int64(len(blob)), |
| 168 |
}) |
| 169 |
} |
| 170 |
generateManifest := func(config ocispec.Descriptor, layers ...ocispec.Descriptor) { |
| 171 |
manifest := ocispec.Manifest{ |
| 172 |
Config: config, |
| 173 |
Layers: layers, |
| 174 |
} |
| 175 |
manifestJSON, err := json.Marshal(manifest) |
| 176 |
if err != nil { |
| 177 |
t.Fatal(err) |
| 178 |
} |
| 179 |
appendBlob(ocispec.MediaTypeImageManifest, manifestJSON) |
| 180 |
} |
| 181 |
|
| 182 |
appendBlob(ocispec.MediaTypeImageConfig, []byte("config")) // Blob 0 |
| 183 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("foo")) // Blob 1 |
| 184 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("bar")) // Blob 2 |
| 185 |
generateManifest(descs[0], descs[1:3]...) // Blob 3 |
| 186 |
|
| 187 |
ctx := context.Background() |
| 188 |
for i := range blobs { |
| 189 |
err := src.Push(ctx, descs[i], bytes.NewReader(blobs[i])) |
| 190 |
if err != nil { |
| 191 |
t.Fatalf("failed to push test content to src: %d: %v", i, err) |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
root := descs[3] |
| 196 |
ref := "foobar" |
| 197 |
newTag := "newtag" |
| 198 |
err := src.Tag(ctx, root, ref) |
| 199 |
if err != nil { |
| 200 |
t.Fatal("fail to tag root node", err) |
| 201 |
} |
| 202 |
|
| 203 |
var skippedCount int64 |
| 204 |
copyOpts := oras.CopyOptions{ |
| 205 |
CopyGraphOptions: oras.CopyGraphOptions{ |
| 206 |
OnCopySkipped: func(ctx context.Context, desc ocispec.Descriptor) error { |
| 207 |
atomic.AddInt64(&skippedCount, 1) |
| 208 |
return nil |
| 209 |
}, |
| 210 |
}, |
| 211 |
} |
| 212 |
|
| 213 |
// copy with src tag |
| 214 |
gotDesc, err := oras.Copy(ctx, src, ref, dst, "", copyOpts) |
| 215 |
if err != nil { |
| 216 |
t.Fatalf("Copy() error = %v, wantErr %v", err, false) |
| 217 |
} |
| 218 |
if !reflect.DeepEqual(gotDesc, root) { |
| 219 |
t.Errorf("Copy() = %v, want %v", gotDesc, root) |
| 220 |
} |
| 221 |
// copy with new tag |
| 222 |
gotDesc, err = oras.Copy(ctx, src, ref, dst, newTag, copyOpts) |
| 223 |
if err != nil { |
| 224 |
t.Fatalf("Copy() error = %v, wantErr %v", err, false) |
| 225 |
} |
| 226 |
if !reflect.DeepEqual(gotDesc, root) { |
| 227 |
t.Errorf("Copy() = %v, want %v", gotDesc, root) |
| 228 |
} |
| 229 |
|
| 230 |
// verify contents |
| 231 |
for i, desc := range descs { |
| 232 |
exists, err := dst.Exists(ctx, desc) |
| 233 |
if err != nil { |
| 234 |
t.Fatalf("dst.Exists(%d) error = %v", i, err) |
| 235 |
} |
| 236 |
if !exists { |
| 237 |
t.Errorf("dst.Exists(%d) = %v, want %v", i, exists, true) |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
// verify src tag |
| 242 |
gotDesc, err = dst.Resolve(ctx, ref) |
| 243 |
if err != nil { |
| 244 |
t.Fatal("dst.Resolve() error =", err) |
| 245 |
} |
| 246 |
if !reflect.DeepEqual(gotDesc, root) { |
| 247 |
t.Errorf("dst.Resolve() = %v, want %v", gotDesc, root) |
| 248 |
} |
| 249 |
// verify new tag |
| 250 |
gotDesc, err = dst.Resolve(ctx, newTag) |
| 251 |
if err != nil { |
| 252 |
t.Fatal("dst.Resolve() error =", err) |
| 253 |
} |
| 254 |
if !reflect.DeepEqual(gotDesc, root) { |
| 255 |
t.Errorf("dst.Resolve() = %v, want %v", gotDesc, root) |
| 256 |
} |
| 257 |
// verify invocation of onCopySkipped() |
| 258 |
if got, want := skippedCount, int64(1); got != want { |
| 259 |
t.Errorf("count(OnCopySkipped()) = %v, want %v", got, want) |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
func TestCopyGraph_FullCopy(t *testing.T) { |
| 264 |
src := cas.NewMemory() |
| 265 |
dst := cas.NewMemory() |
| 266 |
|
| 267 |
// generate test content |
| 268 |
var blobs [][]byte |
| 269 |
var descs []ocispec.Descriptor |
| 270 |
appendBlob := func(mediaType string, blob []byte) { |
| 271 |
blobs = append(blobs, blob) |
| 272 |
descs = append(descs, ocispec.Descriptor{ |
| 273 |
MediaType: mediaType, |
| 274 |
Digest: digest.FromBytes(blob), |
| 275 |
Size: int64(len(blob)), |
| 276 |
}) |
| 277 |
} |
| 278 |
generateManifest := func(config ocispec.Descriptor, layers ...ocispec.Descriptor) { |
| 279 |
manifest := ocispec.Manifest{ |
| 280 |
Config: config, |
| 281 |
Layers: layers, |
| 282 |
} |
| 283 |
manifestJSON, err := json.Marshal(manifest) |
| 284 |
if err != nil { |
| 285 |
t.Fatal(err) |
| 286 |
} |
| 287 |
appendBlob(ocispec.MediaTypeImageManifest, manifestJSON) |
| 288 |
} |
| 289 |
generateIndex := func(manifests ...ocispec.Descriptor) { |
| 290 |
index := ocispec.Index{ |
| 291 |
Manifests: manifests, |
| 292 |
} |
| 293 |
indexJSON, err := json.Marshal(index) |
| 294 |
if err != nil { |
| 295 |
t.Fatal(err) |
| 296 |
} |
| 297 |
appendBlob(ocispec.MediaTypeImageIndex, indexJSON) |
| 298 |
} |
| 299 |
|
| 300 |
appendBlob(ocispec.MediaTypeImageConfig, []byte("config")) // Blob 0 |
| 301 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("foo")) // Blob 1 |
| 302 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("bar")) // Blob 2 |
| 303 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("hello")) // Blob 3 |
| 304 |
generateManifest(descs[0], descs[1:3]...) // Blob 4 |
| 305 |
generateManifest(descs[0], descs[3]) // Blob 5 |
| 306 |
generateManifest(descs[0], descs[1:4]...) // Blob 6 |
| 307 |
generateIndex(descs[4:6]...) // Blob 7 |
| 308 |
generateIndex(descs[6]) // Blob 8 |
| 309 |
generateIndex() // Blob 9 |
| 310 |
generateIndex(descs[7:10]...) // Blob 10 |
| 311 |
|
| 312 |
ctx := context.Background() |
| 313 |
for i := range blobs { |
| 314 |
err := src.Push(ctx, descs[i], bytes.NewReader(blobs[i])) |
| 315 |
if err != nil { |
| 316 |
t.Fatalf("failed to push test content to src: %d: %v", i, err) |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
// test copy graph |
| 321 |
srcTracker := &storageTracker{Storage: src} |
| 322 |
dstTracker := &storageTracker{Storage: dst} |
| 323 |
root := descs[len(descs)-1] |
| 324 |
if err := oras.CopyGraph(ctx, srcTracker, dstTracker, root, oras.CopyGraphOptions{}); err != nil { |
| 325 |
t.Fatalf("CopyGraph() error = %v, wantErr %v", err, false) |
| 326 |
} |
| 327 |
|
| 328 |
// verify contents |
| 329 |
contents := dst.Map() |
| 330 |
if got, want := len(contents), len(blobs); got != want { |
| 331 |
t.Errorf("len(dst) = %v, wantErr %v", got, want) |
| 332 |
} |
| 333 |
for i := range blobs { |
| 334 |
got, err := content.FetchAll(ctx, dst, descs[i]) |
| 335 |
if err != nil { |
| 336 |
t.Errorf("content[%d] error = %v, wantErr %v", i, err, false) |
| 337 |
continue |
| 338 |
} |
| 339 |
if want := blobs[i]; !bytes.Equal(got, want) { |
| 340 |
t.Errorf("content[%d] = %v, want %v", i, got, want) |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
// verify API counts |
| 345 |
if got, want := srcTracker.fetch, int64(len(blobs)); got != want { |
| 346 |
t.Errorf("count(src.Fetch()) = %v, want %v", got, want) |
| 347 |
} |
| 348 |
if got, want := srcTracker.push, int64(0); got != want { |
| 349 |
t.Errorf("count(src.Push()) = %v, want %v", got, want) |
| 350 |
} |
| 351 |
if got, want := srcTracker.exists, int64(0); got != want { |
| 352 |
t.Errorf("count(src.Exists()) = %v, want %v", got, want) |
| 353 |
} |
| 354 |
if got, want := dstTracker.fetch, int64(0); got != want { |
| 355 |
t.Errorf("count(dst.Fetch()) = %v, want %v", got, want) |
| 356 |
} |
| 357 |
if got, want := dstTracker.push, int64(len(blobs)); got != want { |
| 358 |
t.Errorf("count(dst.Push()) = %v, want %v", got, want) |
| 359 |
} |
| 360 |
if got, want := dstTracker.exists, int64(len(blobs)); got != want { |
| 361 |
t.Errorf("count(dst.Exists()) = %v, want %v", got, want) |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
func TestCopyGraph_PartialCopy(t *testing.T) { |
| 366 |
src := cas.NewMemory() |
| 367 |
dst := cas.NewMemory() |
| 368 |
|
| 369 |
// generate test content |
| 370 |
var blobs [][]byte |
| 371 |
var descs []ocispec.Descriptor |
| 372 |
appendBlob := func(mediaType string, blob []byte) { |
| 373 |
blobs = append(blobs, blob) |
| 374 |
descs = append(descs, ocispec.Descriptor{ |
| 375 |
MediaType: mediaType, |
| 376 |
Digest: digest.FromBytes(blob), |
| 377 |
Size: int64(len(blob)), |
| 378 |
}) |
| 379 |
} |
| 380 |
generateManifest := func(config ocispec.Descriptor, layers ...ocispec.Descriptor) { |
| 381 |
manifest := ocispec.Manifest{ |
| 382 |
Config: config, |
| 383 |
Layers: layers, |
| 384 |
} |
| 385 |
manifestJSON, err := json.Marshal(manifest) |
| 386 |
if err != nil { |
| 387 |
t.Fatal(err) |
| 388 |
} |
| 389 |
appendBlob(ocispec.MediaTypeImageManifest, manifestJSON) |
| 390 |
} |
| 391 |
generateIndex := func(manifests ...ocispec.Descriptor) { |
| 392 |
index := ocispec.Index{ |
| 393 |
Manifests: manifests, |
| 394 |
} |
| 395 |
indexJSON, err := json.Marshal(index) |
| 396 |
if err != nil { |
| 397 |
t.Fatal(err) |
| 398 |
} |
| 399 |
appendBlob(ocispec.MediaTypeImageIndex, indexJSON) |
| 400 |
} |
| 401 |
|
| 402 |
appendBlob(ocispec.MediaTypeImageConfig, []byte("config")) // Blob 0 |
| 403 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("foo")) // Blob 1 |
| 404 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("bar")) // Blob 2 |
| 405 |
generateManifest(descs[0], descs[1:3]...) // Blob 3 |
| 406 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("hello")) // Blob 4 |
| 407 |
generateManifest(descs[0], descs[4]) // Blob 5 |
| 408 |
generateIndex(descs[3], descs[5]) // Blob 6 |
| 409 |
|
| 410 |
ctx := context.Background() |
| 411 |
for i := range blobs { |
| 412 |
err := src.Push(ctx, descs[i], bytes.NewReader(blobs[i])) |
| 413 |
if err != nil { |
| 414 |
t.Fatalf("failed to push test content to src: %d: %v", i, err) |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
// initial copy |
| 419 |
root := descs[3] |
| 420 |
if err := oras.CopyGraph(ctx, src, dst, root, oras.CopyGraphOptions{}); err != nil { |
| 421 |
t.Fatalf("CopyGraph() error = %v, wantErr %v", err, false) |
| 422 |
} |
| 423 |
// verify contents |
| 424 |
contents := dst.Map() |
| 425 |
if got, want := len(contents), len(blobs[:4]); got != want { |
| 426 |
t.Fatalf("len(dst) = %v, wantErr %v", got, want) |
| 427 |
} |
| 428 |
for i := range blobs[:4] { |
| 429 |
got, err := content.FetchAll(ctx, dst, descs[i]) |
| 430 |
if err != nil { |
| 431 |
t.Fatalf("content[%d] error = %v, wantErr %v", i, err, false) |
| 432 |
} |
| 433 |
if want := blobs[i]; !bytes.Equal(got, want) { |
| 434 |
t.Fatalf("content[%d] = %v, want %v", i, got, want) |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
// test copy |
| 439 |
srcTracker := &storageTracker{Storage: src} |
| 440 |
dstTracker := &storageTracker{Storage: dst} |
| 441 |
root = descs[len(descs)-1] |
| 442 |
if err := oras.CopyGraph(ctx, srcTracker, dstTracker, root, oras.CopyGraphOptions{}); err != nil { |
| 443 |
t.Fatalf("CopyGraph() error = %v, wantErr %v", err, false) |
| 444 |
} |
| 445 |
|
| 446 |
// verify contents |
| 447 |
contents = dst.Map() |
| 448 |
if got, want := len(contents), len(blobs); got != want { |
| 449 |
t.Errorf("len(dst) = %v, wantErr %v", got, want) |
| 450 |
} |
| 451 |
for i := range blobs { |
| 452 |
got, err := content.FetchAll(ctx, dst, descs[i]) |
| 453 |
if err != nil { |
| 454 |
t.Errorf("content[%d] error = %v, wantErr %v", i, err, false) |
| 455 |
continue |
| 456 |
} |
| 457 |
if want := blobs[i]; !bytes.Equal(got, want) { |
| 458 |
t.Errorf("content[%d] = %v, want %v", i, got, want) |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
// verify API counts |
| 463 |
if got, want := srcTracker.fetch, int64(3); got != want { |
| 464 |
t.Errorf("count(src.Fetch()) = %v, want %v", got, want) |
| 465 |
} |
| 466 |
if got, want := srcTracker.push, int64(0); got != want { |
| 467 |
t.Errorf("count(src.Push()) = %v, want %v", got, want) |
| 468 |
} |
| 469 |
if got, want := srcTracker.exists, int64(0); got != want { |
| 470 |
t.Errorf("count(src.Exists()) = %v, want %v", got, want) |
| 471 |
} |
| 472 |
if got, want := dstTracker.fetch, int64(0); got != want { |
| 473 |
t.Errorf("count(dst.Fetch()) = %v, want %v", got, want) |
| 474 |
} |
| 475 |
if got, want := dstTracker.push, int64(3); got != want { |
| 476 |
t.Errorf("count(dst.Push()) = %v, want %v", got, want) |
| 477 |
} |
| 478 |
if got, want := dstTracker.exists, int64(5); got != want { |
| 479 |
t.Errorf("count(dst.Exists()) = %v, want %v", got, want) |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
func TestCopy_WithOptions(t *testing.T) { |
| 484 |
src := memory.New() |
| 485 |
|
| 486 |
// generate test content |
| 487 |
var blobs [][]byte |
| 488 |
var descs []ocispec.Descriptor |
| 489 |
appendBlob := func(mediaType string, blob []byte) { |
| 490 |
blobs = append(blobs, blob) |
| 491 |
descs = append(descs, ocispec.Descriptor{ |
| 492 |
MediaType: mediaType, |
| 493 |
Digest: digest.FromBytes(blob), |
| 494 |
Size: int64(len(blob)), |
| 495 |
}) |
| 496 |
} |
| 497 |
appendManifest := func(arc, os string, mediaType string, blob []byte) { |
| 498 |
blobs = append(blobs, blob) |
| 499 |
descs = append(descs, ocispec.Descriptor{ |
| 500 |
MediaType: mediaType, |
| 501 |
Digest: digest.FromBytes(blob), |
| 502 |
Size: int64(len(blob)), |
| 503 |
Platform: &ocispec.Platform{ |
| 504 |
Architecture: arc, |
| 505 |
OS: os, |
| 506 |
}, |
| 507 |
}) |
| 508 |
} |
| 509 |
generateManifest := func(arc, os string, config ocispec.Descriptor, layers ...ocispec.Descriptor) { |
| 510 |
manifest := ocispec.Manifest{ |
| 511 |
Config: config, |
| 512 |
Layers: layers, |
| 513 |
} |
| 514 |
manifestJSON, err := json.Marshal(manifest) |
| 515 |
if err != nil { |
| 516 |
t.Fatal(err) |
| 517 |
} |
| 518 |
appendManifest(arc, os, ocispec.MediaTypeImageManifest, manifestJSON) |
| 519 |
} |
| 520 |
generateIndex := func(manifests ...ocispec.Descriptor) { |
| 521 |
index := ocispec.Index{ |
| 522 |
Manifests: manifests, |
| 523 |
} |
| 524 |
indexJSON, err := json.Marshal(index) |
| 525 |
if err != nil { |
| 526 |
t.Fatal(err) |
| 527 |
} |
| 528 |
appendBlob(ocispec.MediaTypeImageIndex, indexJSON) |
| 529 |
} |
| 530 |
|
| 531 |
appendBlob(ocispec.MediaTypeImageConfig, []byte("config")) // Blob 0 |
| 532 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("foo")) // Blob 1 |
| 533 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("bar")) // Blob 2 |
| 534 |
generateManifest("test-arc-1", "test-os-1", descs[0], descs[1:3]...) // Blob 3 |
| 535 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("hello")) // Blob 4 |
| 536 |
generateManifest("test-arc-2", "test-os-2", descs[0], descs[4]) // Blob 5 |
| 537 |
generateIndex(descs[3], descs[5]) // Blob 6 |
| 538 |
|
| 539 |
ctx := context.Background() |
| 540 |
for i := range blobs { |
| 541 |
err := src.Push(ctx, descs[i], bytes.NewReader(blobs[i])) |
| 542 |
if err != nil { |
| 543 |
t.Fatalf("failed to push test content to src: %d: %v", i, err) |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
root := descs[6] |
| 548 |
ref := "foobar" |
| 549 |
err := src.Tag(ctx, root, ref) |
| 550 |
if err != nil { |
| 551 |
t.Fatal("fail to tag root node", err) |
| 552 |
} |
| 553 |
|
| 554 |
// test copy with media type filter |
| 555 |
dst := memory.New() |
| 556 |
opts := oras.DefaultCopyOptions |
| 557 |
opts.MapRoot = func(ctx context.Context, src content.ReadOnlyStorage, root ocispec.Descriptor) (ocispec.Descriptor, error) { |
| 558 |
if root.MediaType == ocispec.MediaTypeImageIndex { |
| 559 |
return root, nil |
| 560 |
} else { |
| 561 |
return ocispec.Descriptor{}, errdef.ErrNotFound |
| 562 |
} |
| 563 |
} |
| 564 |
gotDesc, err := oras.Copy(ctx, src, ref, dst, "", opts) |
| 565 |
if err != nil { |
| 566 |
t.Fatalf("Copy() error = %v, wantErr %v", err, false) |
| 567 |
} |
| 568 |
if !reflect.DeepEqual(gotDesc, root) { |
| 569 |
t.Errorf("Copy() = %v, want %v", gotDesc, root) |
| 570 |
} |
| 571 |
|
| 572 |
// verify contents |
| 573 |
for i, desc := range descs { |
| 574 |
exists, err := dst.Exists(ctx, desc) |
| 575 |
if err != nil { |
| 576 |
t.Fatalf("dst.Exists(%d) error = %v", i, err) |
| 577 |
} |
| 578 |
if !exists { |
| 579 |
t.Errorf("dst.Exists(%d) = %v, want %v", i, exists, true) |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
// verify tag |
| 584 |
gotDesc, err = dst.Resolve(ctx, ref) |
| 585 |
if err != nil { |
| 586 |
t.Fatal("dst.Resolve() error =", err) |
| 587 |
} |
| 588 |
if !reflect.DeepEqual(gotDesc, root) { |
| 589 |
t.Errorf("dst.Resolve() = %v, want %v", gotDesc, root) |
| 590 |
} |
| 591 |
|
| 592 |
// test copy with platform filter and hooks |
| 593 |
dst = memory.New() |
| 594 |
preCopyCount := int64(0) |
| 595 |
postCopyCount := int64(0) |
| 596 |
opts = oras.CopyOptions{ |
| 597 |
MapRoot: func(ctx context.Context, src content.ReadOnlyStorage, root ocispec.Descriptor) (ocispec.Descriptor, error) { |
| 598 |
manifests, err := content.Successors(ctx, src, root) |
| 599 |
if err != nil { |
| 600 |
return ocispec.Descriptor{}, errdef.ErrNotFound |
| 601 |
} |
| 602 |
|
| 603 |
// platform filter |
| 604 |
for _, m := range manifests { |
| 605 |
if m.Platform.Architecture == "test-arc-2" && m.Platform.OS == "test-os-2" { |
| 606 |
return m, nil |
| 607 |
} |
| 608 |
} |
| 609 |
return ocispec.Descriptor{}, errdef.ErrNotFound |
| 610 |
}, |
| 611 |
CopyGraphOptions: oras.CopyGraphOptions{ |
| 612 |
PreCopy: func(ctx context.Context, desc ocispec.Descriptor) error { |
| 613 |
atomic.AddInt64(&preCopyCount, 1) |
| 614 |
return nil |
| 615 |
}, |
| 616 |
PostCopy: func(ctx context.Context, desc ocispec.Descriptor) error { |
| 617 |
atomic.AddInt64(&postCopyCount, 1) |
| 618 |
return nil |
| 619 |
}, |
| 620 |
}, |
| 621 |
} |
| 622 |
wantDesc := descs[5] |
| 623 |
gotDesc, err = oras.Copy(ctx, src, ref, dst, "", opts) |
| 624 |
if err != nil { |
| 625 |
t.Fatalf("Copy() error = %v, wantErr %v", err, false) |
| 626 |
} |
| 627 |
if !reflect.DeepEqual(gotDesc, wantDesc) { |
| 628 |
t.Errorf("Copy() = %v, want %v", gotDesc, wantDesc) |
| 629 |
} |
| 630 |
|
| 631 |
// verify contents |
| 632 |
for i, desc := range append([]ocispec.Descriptor{descs[0]}, descs[4:6]...) { |
| 633 |
exists, err := dst.Exists(ctx, desc) |
| 634 |
if err != nil { |
| 635 |
t.Fatalf("dst.Exists(%d) error = %v", i, err) |
| 636 |
} |
| 637 |
if !exists { |
| 638 |
t.Errorf("dst.Exists(%d) = %v, want %v", i, exists, true) |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
// verify tag |
| 643 |
gotDesc, err = dst.Resolve(ctx, ref) |
| 644 |
if err != nil { |
| 645 |
t.Fatal("dst.Resolve() error =", err) |
| 646 |
} |
| 647 |
if !reflect.DeepEqual(gotDesc, wantDesc) { |
| 648 |
t.Errorf("dst.Resolve() = %v, want %v", gotDesc, wantDesc) |
| 649 |
} |
| 650 |
|
| 651 |
// verify API counts |
| 652 |
if got, want := preCopyCount, int64(3); got != want { |
| 653 |
t.Errorf("count(PreCopy()) = %v, want %v", got, want) |
| 654 |
} |
| 655 |
if got, want := postCopyCount, int64(3); got != want { |
| 656 |
t.Errorf("count(PostCopy()) = %v, want %v", got, want) |
| 657 |
} |
| 658 |
|
| 659 |
// test copy with root filter, but no matching node can be found |
| 660 |
dst = memory.New() |
| 661 |
opts = oras.CopyOptions{ |
| 662 |
MapRoot: func(ctx context.Context, src content.ReadOnlyStorage, root ocispec.Descriptor) (ocispec.Descriptor, error) { |
| 663 |
if root.MediaType == "test" { |
| 664 |
return root, nil |
| 665 |
} else { |
| 666 |
return ocispec.Descriptor{}, errdef.ErrNotFound |
| 667 |
} |
| 668 |
}, |
| 669 |
CopyGraphOptions: oras.DefaultCopyGraphOptions, |
| 670 |
} |
| 671 |
|
| 672 |
_, err = oras.Copy(ctx, src, ref, dst, "", opts) |
| 673 |
if !errors.Is(err, errdef.ErrNotFound) { |
| 674 |
t.Fatalf("Copy() error = %v, wantErr %v", err, errdef.ErrNotFound) |
| 675 |
} |
| 676 |
|
| 677 |
// test copy with MaxMetadataBytes = 1 |
| 678 |
dst = memory.New() |
| 679 |
opts = oras.CopyOptions{ |
| 680 |
CopyGraphOptions: oras.CopyGraphOptions{ |
| 681 |
MaxMetadataBytes: 1, |
| 682 |
}, |
| 683 |
} |
| 684 |
if _, err := oras.Copy(ctx, src, ref, dst, "", opts); !errors.Is(err, errdef.ErrSizeExceedsLimit) { |
| 685 |
t.Fatalf("CopyGraph() error = %v, wantErr %v", err, errdef.ErrSizeExceedsLimit) |
| 686 |
} |
| 687 |
} |
| 688 |
|
| 689 |
func TestCopy_WithTargetPlatformOptions(t *testing.T) { |
| 690 |
src := memory.New() |
| 691 |
arc_1 := "test-arc-1" |
| 692 |
os_1 := "test-os-1" |
| 693 |
variant_1 := "v1" |
| 694 |
arc_2 := "test-arc-2" |
| 695 |
os_2 := "test-os-2" |
| 696 |
variant_2 := "v2" |
| 697 |
|
| 698 |
// generate test content |
| 699 |
var blobs [][]byte |
| 700 |
var descs []ocispec.Descriptor |
| 701 |
appendBlob := func(mediaType string, blob []byte) { |
| 702 |
blobs = append(blobs, blob) |
| 703 |
descs = append(descs, ocispec.Descriptor{ |
| 704 |
MediaType: mediaType, |
| 705 |
Digest: digest.FromBytes(blob), |
| 706 |
Size: int64(len(blob)), |
| 707 |
}) |
| 708 |
} |
| 709 |
appendManifest := func(arc, os, variant string, mediaType string, blob []byte) { |
| 710 |
blobs = append(blobs, blob) |
| 711 |
descs = append(descs, ocispec.Descriptor{ |
| 712 |
MediaType: mediaType, |
| 713 |
Digest: digest.FromBytes(blob), |
| 714 |
Size: int64(len(blob)), |
| 715 |
Platform: &ocispec.Platform{ |
| 716 |
Architecture: arc, |
| 717 |
OS: os, |
| 718 |
Variant: variant, |
| 719 |
}, |
| 720 |
}) |
| 721 |
} |
| 722 |
generateManifest := func(arc, os, variant string, config ocispec.Descriptor, layers ...ocispec.Descriptor) { |
| 723 |
manifest := ocispec.Manifest{ |
| 724 |
Config: config, |
| 725 |
Layers: layers, |
| 726 |
} |
| 727 |
manifestJSON, err := json.Marshal(manifest) |
| 728 |
if err != nil { |
| 729 |
t.Fatal(err) |
| 730 |
} |
| 731 |
appendManifest(arc, os, variant, ocispec.MediaTypeImageManifest, manifestJSON) |
| 732 |
} |
| 733 |
generateIndex := func(manifests ...ocispec.Descriptor) { |
| 734 |
index := ocispec.Index{ |
| 735 |
Manifests: manifests, |
| 736 |
} |
| 737 |
indexJSON, err := json.Marshal(index) |
| 738 |
if err != nil { |
| 739 |
t.Fatal(err) |
| 740 |
} |
| 741 |
appendBlob(ocispec.MediaTypeImageIndex, indexJSON) |
| 742 |
} |
| 743 |
|
| 744 |
appendBlob(ocispec.MediaTypeImageConfig, []byte(`{"mediaType":"application/vnd.oci.image.config.v1+json", |
| 745 |
"created":"2022-07-29T08:13:55Z", |
| 746 |
"author":"test author", |
| 747 |
"architecture":"test-arc-1", |
| 748 |
"os":"test-os-1", |
| 749 |
"variant":"v1"}`)) // Blob 0 |
| 750 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("foo")) // Blob 1 |
| 751 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("bar")) // Blob 2 |
| 752 |
generateManifest(arc_1, os_1, variant_1, descs[0], descs[1:3]...) // Blob 3 |
| 753 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("hello1")) // Blob 4 |
| 754 |
generateManifest(arc_2, os_2, variant_1, descs[0], descs[4]) // Blob 5 |
| 755 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("hello2")) // Blob 6 |
| 756 |
generateManifest(arc_1, os_1, variant_2, descs[0], descs[6]) // Blob 7 |
| 757 |
generateIndex(descs[3], descs[5], descs[7]) // Blob 8 |
| 758 |
|
| 759 |
ctx := context.Background() |
| 760 |
for i := range blobs { |
| 761 |
err := src.Push(ctx, descs[i], bytes.NewReader(blobs[i])) |
| 762 |
if err != nil { |
| 763 |
t.Fatalf("failed to push test content to src: %d: %v", i, err) |
| 764 |
} |
| 765 |
} |
| 766 |
|
| 767 |
root := descs[8] |
| 768 |
ref := "foobar" |
| 769 |
err := src.Tag(ctx, root, ref) |
| 770 |
if err != nil { |
| 771 |
t.Fatal("fail to tag root node", err) |
| 772 |
} |
| 773 |
|
| 774 |
// test copy with platform filter for the image index |
| 775 |
dst := memory.New() |
| 776 |
opts := oras.CopyOptions{} |
| 777 |
targetPlatform := ocispec.Platform{ |
| 778 |
Architecture: arc_2, |
| 779 |
OS: os_2, |
| 780 |
} |
| 781 |
opts.WithTargetPlatform(&targetPlatform) |
| 782 |
wantDesc := descs[5] |
| 783 |
gotDesc, err := oras.Copy(ctx, src, ref, dst, "", opts) |
| 784 |
if err != nil { |
| 785 |
t.Fatalf("Copy() error = %v, wantErr %v", err, false) |
| 786 |
} |
| 787 |
if !reflect.DeepEqual(gotDesc, wantDesc) { |
| 788 |
t.Errorf("Copy() = %v, want %v", gotDesc, wantDesc) |
| 789 |
} |
| 790 |
|
| 791 |
// verify contents |
| 792 |
for i, desc := range append([]ocispec.Descriptor{descs[0]}, descs[4:6]...) { |
| 793 |
exists, err := dst.Exists(ctx, desc) |
| 794 |
if err != nil { |
| 795 |
t.Fatalf("dst.Exists(%d) error = %v", i, err) |
| 796 |
} |
| 797 |
if !exists { |
| 798 |
t.Errorf("dst.Exists(%d) = %v, want %v", i, exists, true) |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
// verify tag |
| 803 |
gotDesc, err = dst.Resolve(ctx, ref) |
| 804 |
if err != nil { |
| 805 |
t.Fatal("dst.Resolve() error =", err) |
| 806 |
} |
| 807 |
if !reflect.DeepEqual(gotDesc, wantDesc) { |
| 808 |
t.Errorf("dst.Resolve() = %v, want %v", gotDesc, wantDesc) |
| 809 |
} |
| 810 |
|
| 811 |
// test copy with platform filter for the image index, and multiple |
| 812 |
// manifests match the required platform. Should return the first matching |
| 813 |
// entry. |
| 814 |
dst = memory.New() |
| 815 |
targetPlatform = ocispec.Platform{ |
| 816 |
Architecture: arc_1, |
| 817 |
OS: os_1, |
| 818 |
} |
| 819 |
opts = oras.CopyOptions{} |
| 820 |
opts.WithTargetPlatform(&targetPlatform) |
| 821 |
wantDesc = descs[3] |
| 822 |
gotDesc, err = oras.Copy(ctx, src, ref, dst, "", opts) |
| 823 |
if err != nil { |
| 824 |
t.Fatalf("Copy() error = %v, wantErr %v", err, false) |
| 825 |
} |
| 826 |
if !reflect.DeepEqual(gotDesc, wantDesc) { |
| 827 |
t.Errorf("Copy() = %v, want %v", gotDesc, wantDesc) |
| 828 |
} |
| 829 |
|
| 830 |
// verify contents |
| 831 |
for i, desc := range append([]ocispec.Descriptor{descs[0]}, descs[1:3]...) { |
| 832 |
exists, err := dst.Exists(ctx, desc) |
| 833 |
if err != nil { |
| 834 |
t.Fatalf("dst.Exists(%d) error = %v", i, err) |
| 835 |
} |
| 836 |
if !exists { |
| 837 |
t.Errorf("dst.Exists(%d) = %v, want %v", i, exists, true) |
| 838 |
} |
| 839 |
} |
| 840 |
|
| 841 |
// verify tag |
| 842 |
gotDesc, err = dst.Resolve(ctx, ref) |
| 843 |
if err != nil { |
| 844 |
t.Fatal("dst.Resolve() error =", err) |
| 845 |
} |
| 846 |
if !reflect.DeepEqual(gotDesc, wantDesc) { |
| 847 |
t.Errorf("dst.Resolve() = %v, want %v", gotDesc, wantDesc) |
| 848 |
} |
| 849 |
|
| 850 |
// test copy with platform filter and existing MapRoot func for the image |
| 851 |
// index, but there is no matching node. Should return not found error. |
| 852 |
dst = memory.New() |
| 853 |
opts = oras.CopyOptions{ |
| 854 |
MapRoot: func(ctx context.Context, src content.ReadOnlyStorage, root ocispec.Descriptor) (ocispec.Descriptor, error) { |
| 855 |
if root.MediaType == ocispec.MediaTypeImageIndex { |
| 856 |
return root, nil |
| 857 |
} else { |
| 858 |
return ocispec.Descriptor{}, errdef.ErrNotFound |
| 859 |
} |
| 860 |
}, |
| 861 |
} |
| 862 |
targetPlatform = ocispec.Platform{ |
| 863 |
Architecture: arc_1, |
| 864 |
OS: os_2, |
| 865 |
} |
| 866 |
opts.WithTargetPlatform(&targetPlatform) |
| 867 |
|
| 868 |
_, err = oras.Copy(ctx, src, ref, dst, "", opts) |
| 869 |
if wantErr := errdef.ErrNotFound; !errors.Is(err, wantErr) { |
| 870 |
t.Errorf("Copy() error = %v, wantErr %v", err, wantErr) |
| 871 |
} |
| 872 |
|
| 873 |
// test copy with platform filter for the manifest |
| 874 |
dst = memory.New() |
| 875 |
opts = oras.CopyOptions{} |
| 876 |
targetPlatform = ocispec.Platform{ |
| 877 |
Architecture: arc_1, |
| 878 |
OS: os_1, |
| 879 |
} |
| 880 |
opts.WithTargetPlatform(&targetPlatform) |
| 881 |
|
| 882 |
root = descs[7] |
| 883 |
err = src.Tag(ctx, root, ref) |
| 884 |
if err != nil { |
| 885 |
t.Fatal("fail to tag root node", err) |
| 886 |
} |
| 887 |
|
| 888 |
wantDesc = descs[7] |
| 889 |
gotDesc, err = oras.Copy(ctx, src, ref, dst, "", opts) |
| 890 |
if err != nil { |
| 891 |
t.Fatalf("Copy() error = %v, wantErr %v", err, false) |
| 892 |
} |
| 893 |
if !reflect.DeepEqual(gotDesc, wantDesc) { |
| 894 |
t.Errorf("Copy() = %v, want %v", gotDesc, wantDesc) |
| 895 |
} |
| 896 |
|
| 897 |
// verify contents |
| 898 |
for i, desc := range append([]ocispec.Descriptor{descs[0]}, descs[6]) { |
| 899 |
exists, err := dst.Exists(ctx, desc) |
| 900 |
if err != nil { |
| 901 |
t.Fatalf("dst.Exists(%d) error = %v", i, err) |
| 902 |
} |
| 903 |
if !exists { |
| 904 |
t.Errorf("dst.Exists(%d) = %v, want %v", i, exists, true) |
| 905 |
} |
| 906 |
} |
| 907 |
|
| 908 |
// verify tag |
| 909 |
gotDesc, err = dst.Resolve(ctx, ref) |
| 910 |
if err != nil { |
| 911 |
t.Fatal("dst.Resolve() error =", err) |
| 912 |
} |
| 913 |
if !reflect.DeepEqual(gotDesc, wantDesc) { |
| 914 |
t.Errorf("dst.Resolve() = %v, want %v", gotDesc, wantDesc) |
| 915 |
} |
| 916 |
|
| 917 |
// test copy with platform filter for the manifest, but there is no matching |
| 918 |
// node. Should return not found error. |
| 919 |
dst = memory.New() |
| 920 |
opts = oras.CopyOptions{} |
| 921 |
targetPlatform = ocispec.Platform{ |
| 922 |
Architecture: arc_1, |
| 923 |
OS: os_1, |
| 924 |
Variant: variant_2, |
| 925 |
} |
| 926 |
opts.WithTargetPlatform(&targetPlatform) |
| 927 |
|
| 928 |
_, err = oras.Copy(ctx, src, ref, dst, "", opts) |
| 929 |
if wantErr := errdef.ErrNotFound; !errors.Is(err, wantErr) { |
| 930 |
t.Errorf("Copy() error = %v, wantErr %v", err, wantErr) |
| 931 |
} |
| 932 |
|
| 933 |
// test copy with platform filter, but the node's media type is not |
| 934 |
// supported. Should return unsupported error |
| 935 |
dst = memory.New() |
| 936 |
opts = oras.CopyOptions{} |
| 937 |
targetPlatform = ocispec.Platform{ |
| 938 |
Architecture: arc_1, |
| 939 |
OS: os_1, |
| 940 |
} |
| 941 |
opts.WithTargetPlatform(&targetPlatform) |
| 942 |
|
| 943 |
root = descs[1] |
| 944 |
err = src.Tag(ctx, root, ref) |
| 945 |
if err != nil { |
| 946 |
t.Fatal("fail to tag root node", err) |
| 947 |
} |
| 948 |
|
| 949 |
_, err = oras.Copy(ctx, src, ref, dst, "", opts) |
| 950 |
if !errors.Is(err, errdef.ErrUnsupported) { |
| 951 |
t.Fatalf("Copy() error = %v, wantErr %v", err, errdef.ErrUnsupported) |
| 952 |
} |
| 953 |
|
| 954 |
// generate incorrect test content |
| 955 |
blobs = nil |
| 956 |
descs = nil |
| 957 |
appendBlob(docker.MediaTypeConfig, []byte(`{"mediaType":"application/vnd.oci.image.config.v1+json", |
| 958 |
"created":"2022-07-29T08:13:55Z", |
| 959 |
"author":"test author 1", |
| 960 |
"architecture":"test-arc-1", |
| 961 |
"os":"test-os-1", |
| 962 |
"variant":"v1"}`)) // Blob 0 |
| 963 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("foo1")) // Blob 1 |
| 964 |
generateManifest(arc_1, os_1, variant_1, descs[0], descs[1]) // Blob 2 |
| 965 |
generateIndex(descs[2]) // Blob 3 |
| 966 |
|
| 967 |
ctx = context.Background() |
| 968 |
for i := range blobs { |
| 969 |
err := src.Push(ctx, descs[i], bytes.NewReader(blobs[i])) |
| 970 |
if err != nil { |
| 971 |
t.Fatalf("failed to push test content to src: %d: %v", i, err) |
| 972 |
} |
| 973 |
} |
| 974 |
|
| 975 |
dst = memory.New() |
| 976 |
opts = oras.CopyOptions{} |
| 977 |
targetPlatform = ocispec.Platform{ |
| 978 |
Architecture: arc_1, |
| 979 |
OS: os_1, |
| 980 |
} |
| 981 |
opts.WithTargetPlatform(&targetPlatform) |
| 982 |
|
| 983 |
// test copy with platform filter for the manifest, but the manifest is |
| 984 |
// invalid by having docker mediaType config in the manifest and oci |
| 985 |
// mediaType in the image config. Should return error. |
| 986 |
root = descs[2] |
| 987 |
err = src.Tag(ctx, root, ref) |
| 988 |
if err != nil { |
| 989 |
t.Fatal("fail to tag root node", err) |
| 990 |
} |
| 991 |
|
| 992 |
_, err = oras.Copy(ctx, src, ref, dst, "", opts) |
| 993 |
if wantErr := errdef.ErrUnsupported; !errors.Is(err, wantErr) { |
| 994 |
t.Errorf("Copy() error = %v, wantErr %v", err, wantErr) |
| 995 |
} |
| 996 |
|
| 997 |
// generate test content with null config blob |
| 998 |
blobs = nil |
| 999 |
descs = nil |
| 1000 |
appendBlob(ocispec.MediaTypeImageConfig, []byte("null")) // Blob 0 |
| 1001 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("foo2")) // Blob 1 |
| 1002 |
generateManifest(arc_1, os_1, variant_1, descs[0], descs[1]) // Blob 2 |
| 1003 |
generateIndex(descs[2]) // Blob 3 |
| 1004 |
|
| 1005 |
ctx = context.Background() |
| 1006 |
for i := range blobs { |
| 1007 |
err := src.Push(ctx, descs[i], bytes.NewReader(blobs[i])) |
| 1008 |
if err != nil { |
| 1009 |
t.Fatalf("failed to push test content to src: %d: %v", i, err) |
| 1010 |
} |
| 1011 |
} |
| 1012 |
|
| 1013 |
dst = memory.New() |
| 1014 |
opts = oras.CopyOptions{} |
| 1015 |
targetPlatform = ocispec.Platform{ |
| 1016 |
Architecture: arc_1, |
| 1017 |
OS: os_1, |
| 1018 |
} |
| 1019 |
opts.WithTargetPlatform(&targetPlatform) |
| 1020 |
|
| 1021 |
// test copy with platform filter for the manifest with null config blob |
| 1022 |
// should return not found error |
| 1023 |
root = descs[2] |
| 1024 |
err = src.Tag(ctx, root, ref) |
| 1025 |
if err != nil { |
| 1026 |
t.Fatal("fail to tag root node", err) |
| 1027 |
} |
| 1028 |
|
| 1029 |
_, err = oras.Copy(ctx, src, ref, dst, "", opts) |
| 1030 |
if wantErr := errdef.ErrNotFound; !errors.Is(err, wantErr) { |
| 1031 |
t.Errorf("Copy() error = %v, wantErr %v", err, wantErr) |
| 1032 |
} |
| 1033 |
|
| 1034 |
// generate test content with empty config blob |
| 1035 |
blobs = nil |
| 1036 |
descs = nil |
| 1037 |
appendBlob(ocispec.MediaTypeImageConfig, []byte("")) // Blob 0 |
| 1038 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("foo3")) // Blob 1 |
| 1039 |
generateManifest(arc_1, os_1, variant_1, descs[0], descs[1]) // Blob 2 |
| 1040 |
generateIndex(descs[2]) // Blob 3 |
| 1041 |
|
| 1042 |
ctx = context.Background() |
| 1043 |
for i := range blobs { |
| 1044 |
err := src.Push(ctx, descs[i], bytes.NewReader(blobs[i])) |
| 1045 |
if err != nil { |
| 1046 |
t.Fatalf("failed to push test content to src: %d: %v", i, err) |
| 1047 |
} |
| 1048 |
} |
| 1049 |
|
| 1050 |
dst = memory.New() |
| 1051 |
opts = oras.CopyOptions{} |
| 1052 |
targetPlatform = ocispec.Platform{ |
| 1053 |
Architecture: arc_1, |
| 1054 |
OS: os_1, |
| 1055 |
} |
| 1056 |
opts.WithTargetPlatform(&targetPlatform) |
| 1057 |
|
| 1058 |
// test copy with platform filter for the manifest with empty config blob |
| 1059 |
// should return not found error |
| 1060 |
root = descs[2] |
| 1061 |
err = src.Tag(ctx, root, ref) |
| 1062 |
if err != nil { |
| 1063 |
t.Fatal("fail to tag root node", err) |
| 1064 |
} |
| 1065 |
|
| 1066 |
_, err = oras.Copy(ctx, src, ref, dst, "", opts) |
| 1067 |
if wantErr := errdef.ErrNotFound; !errors.Is(err, wantErr) { |
| 1068 |
t.Errorf("Copy() error = %v, wantErr %v", err, wantErr) |
| 1069 |
} |
| 1070 |
|
| 1071 |
// test copy with no platform filter and nil opts.MapRoot |
| 1072 |
// opts.MapRoot should be nil |
| 1073 |
opts = oras.CopyOptions{} |
| 1074 |
opts.WithTargetPlatform(nil) |
| 1075 |
if opts.MapRoot != nil { |
| 1076 |
t.Fatal("opts.MapRoot not equal to nil when platform is not provided") |
| 1077 |
} |
| 1078 |
|
| 1079 |
// test copy with no platform filter and custom opts.MapRoot |
| 1080 |
// should return ErrNotFound |
| 1081 |
opts = oras.CopyOptions{ |
| 1082 |
MapRoot: func(ctx context.Context, src content.ReadOnlyStorage, root ocispec.Descriptor) (ocispec.Descriptor, error) { |
| 1083 |
if root.MediaType == "test" { |
| 1084 |
return root, nil |
| 1085 |
} else { |
| 1086 |
return ocispec.Descriptor{}, errdef.ErrNotFound |
| 1087 |
} |
| 1088 |
}, |
| 1089 |
CopyGraphOptions: oras.DefaultCopyGraphOptions, |
| 1090 |
} |
| 1091 |
opts.WithTargetPlatform(nil) |
| 1092 |
|
| 1093 |
_, err = oras.Copy(ctx, src, ref, dst, "", opts) |
| 1094 |
if !errors.Is(err, errdef.ErrNotFound) { |
| 1095 |
t.Fatalf("Copy() error = %v, wantErr %v", err, errdef.ErrNotFound) |
| 1096 |
} |
| 1097 |
} |
| 1098 |
|
| 1099 |
func TestCopy_RestoreDuplicates(t *testing.T) { |
| 1100 |
src := memory.New() |
| 1101 |
temp := t.TempDir() |
| 1102 |
dst, err := file.New(temp) |
| 1103 |
if err != nil { |
| 1104 |
t.Fatal("file.New() error =", err) |
| 1105 |
} |
| 1106 |
defer dst.Close() |
| 1107 |
|
| 1108 |
// generate test content |
| 1109 |
var blobs [][]byte |
| 1110 |
var descs []ocispec.Descriptor |
| 1111 |
appendBlob := func(mediaType string, blob []byte, title string) { |
| 1112 |
blobs = append(blobs, blob) |
| 1113 |
desc := ocispec.Descriptor{ |
| 1114 |
MediaType: mediaType, |
| 1115 |
Digest: digest.FromBytes(blob), |
| 1116 |
Size: int64(len(blob)), |
| 1117 |
} |
| 1118 |
if title != "" { |
| 1119 |
desc.Annotations = map[string]string{ |
| 1120 |
ocispec.AnnotationTitle: title, |
| 1121 |
} |
| 1122 |
} |
| 1123 |
descs = append(descs, desc) |
| 1124 |
} |
| 1125 |
generateManifest := func(config ocispec.Descriptor, layers ...ocispec.Descriptor) { |
| 1126 |
manifest := ocispec.Manifest{ |
| 1127 |
Config: config, |
| 1128 |
Layers: layers, |
| 1129 |
} |
| 1130 |
manifestJSON, err := json.Marshal(manifest) |
| 1131 |
if err != nil { |
| 1132 |
t.Fatal(err) |
| 1133 |
} |
| 1134 |
appendBlob(ocispec.MediaTypeImageManifest, manifestJSON, "") |
| 1135 |
} |
| 1136 |
|
| 1137 |
appendBlob(ocispec.MediaTypeImageConfig, []byte("{}"), "") // Blob 0 |
| 1138 |
// 2 blobs with same content |
| 1139 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("hello"), "foo.txt") // Blob 1 |
| 1140 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("hello"), "bar.txt") // Blob 2 |
| 1141 |
generateManifest(descs[0], descs[1:3]...) // Blob 3 |
| 1142 |
|
| 1143 |
ctx := context.Background() |
| 1144 |
for i := range blobs { |
| 1145 |
exists, err := src.Exists(ctx, descs[i]) |
| 1146 |
if err != nil { |
| 1147 |
t.Fatalf("failed to check existence in src: %d: %v", i, err) |
| 1148 |
} |
| 1149 |
if exists { |
| 1150 |
continue |
| 1151 |
} |
| 1152 |
if err := src.Push(ctx, descs[i], bytes.NewReader(blobs[i])); err != nil { |
| 1153 |
t.Fatalf("failed to push test content to src: %d: %v", i, err) |
| 1154 |
} |
| 1155 |
} |
| 1156 |
|
| 1157 |
root := descs[3] |
| 1158 |
ref := "latest" |
| 1159 |
err = src.Tag(ctx, root, ref) |
| 1160 |
if err != nil { |
| 1161 |
t.Fatal("fail to tag root node", err) |
| 1162 |
} |
| 1163 |
|
| 1164 |
// test copy |
| 1165 |
gotDesc, err := oras.Copy(ctx, src, ref, dst, "", oras.CopyOptions{}) |
| 1166 |
if err != nil { |
| 1167 |
t.Fatalf("Copy() error = %v, wantErr %v", err, false) |
| 1168 |
} |
| 1169 |
if !reflect.DeepEqual(gotDesc, root) { |
| 1170 |
t.Errorf("Copy() = %v, want %v", gotDesc, root) |
| 1171 |
} |
| 1172 |
|
| 1173 |
// verify contents |
| 1174 |
for i, desc := range descs { |
| 1175 |
exists, err := dst.Exists(ctx, desc) |
| 1176 |
if err != nil { |
| 1177 |
t.Fatalf("dst.Exists(%d) error = %v", i, err) |
| 1178 |
} |
| 1179 |
if !exists { |
| 1180 |
t.Errorf("dst.Exists(%d) = %v, want %v", i, exists, true) |
| 1181 |
} |
| 1182 |
} |
| 1183 |
|
| 1184 |
// verify tag |
| 1185 |
gotDesc, err = dst.Resolve(ctx, ref) |
| 1186 |
if err != nil { |
| 1187 |
t.Fatal("dst.Resolve() error =", err) |
| 1188 |
} |
| 1189 |
if !reflect.DeepEqual(gotDesc, root) { |
| 1190 |
t.Errorf("dst.Resolve() = %v, want %v", gotDesc, root) |
| 1191 |
} |
| 1192 |
} |
| 1193 |
|
| 1194 |
func TestCopy_DiscardDuplicates(t *testing.T) { |
| 1195 |
src := memory.New() |
| 1196 |
temp := t.TempDir() |
| 1197 |
dst, err := file.New(temp) |
| 1198 |
if err != nil { |
| 1199 |
t.Fatal("file.New() error =", err) |
| 1200 |
} |
| 1201 |
dst.ForceCAS = true |
| 1202 |
defer dst.Close() |
| 1203 |
|
| 1204 |
// generate test content |
| 1205 |
var blobs [][]byte |
| 1206 |
var descs []ocispec.Descriptor |
| 1207 |
appendBlob := func(mediaType string, blob []byte, title string) { |
| 1208 |
blobs = append(blobs, blob) |
| 1209 |
desc := ocispec.Descriptor{ |
| 1210 |
MediaType: mediaType, |
| 1211 |
Digest: digest.FromBytes(blob), |
| 1212 |
Size: int64(len(blob)), |
| 1213 |
} |
| 1214 |
if title != "" { |
| 1215 |
desc.Annotations = map[string]string{ |
| 1216 |
ocispec.AnnotationTitle: title, |
| 1217 |
} |
| 1218 |
} |
| 1219 |
descs = append(descs, desc) |
| 1220 |
} |
| 1221 |
generateManifest := func(config ocispec.Descriptor, layers ...ocispec.Descriptor) { |
| 1222 |
manifest := ocispec.Manifest{ |
| 1223 |
Config: config, |
| 1224 |
Layers: layers, |
| 1225 |
} |
| 1226 |
manifestJSON, err := json.Marshal(manifest) |
| 1227 |
if err != nil { |
| 1228 |
t.Fatal(err) |
| 1229 |
} |
| 1230 |
appendBlob(ocispec.MediaTypeImageManifest, manifestJSON, "") |
| 1231 |
} |
| 1232 |
|
| 1233 |
appendBlob(ocispec.MediaTypeImageConfig, []byte("{}"), "") // Blob 0 |
| 1234 |
// 2 blobs with same content |
| 1235 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("hello"), "foo.txt") // Blob 1 |
| 1236 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("hello"), "bar.txt") // Blob 2 |
| 1237 |
generateManifest(descs[0], descs[1:3]...) // Blob 3 |
| 1238 |
|
| 1239 |
ctx := context.Background() |
| 1240 |
for i := range blobs { |
| 1241 |
exists, err := src.Exists(ctx, descs[i]) |
| 1242 |
if err != nil { |
| 1243 |
t.Fatalf("failed to check existence in src: %d: %v", i, err) |
| 1244 |
} |
| 1245 |
if exists { |
| 1246 |
continue |
| 1247 |
} |
| 1248 |
if err := src.Push(ctx, descs[i], bytes.NewReader(blobs[i])); err != nil { |
| 1249 |
t.Fatalf("failed to push test content to src: %d: %v", i, err) |
| 1250 |
} |
| 1251 |
} |
| 1252 |
|
| 1253 |
root := descs[3] |
| 1254 |
ref := "latest" |
| 1255 |
err = src.Tag(ctx, root, ref) |
| 1256 |
if err != nil { |
| 1257 |
t.Fatal("fail to tag root node", err) |
| 1258 |
} |
| 1259 |
|
| 1260 |
// test copy |
| 1261 |
gotDesc, err := oras.Copy(ctx, src, ref, dst, "", oras.CopyOptions{}) |
| 1262 |
if err != nil { |
| 1263 |
t.Fatalf("Copy() error = %v, wantErr %v", err, false) |
| 1264 |
} |
| 1265 |
if !reflect.DeepEqual(gotDesc, root) { |
| 1266 |
t.Errorf("Copy() = %v, want %v", gotDesc, root) |
| 1267 |
} |
| 1268 |
|
| 1269 |
// verify only one of foo.txt and bar.txt exists |
| 1270 |
fooExists, err := dst.Exists(ctx, descs[1]) |
| 1271 |
if err != nil { |
| 1272 |
t.Fatalf("dst.Exists(foo) error = %v", err) |
| 1273 |
} |
| 1274 |
barExists, err := dst.Exists(ctx, descs[2]) |
| 1275 |
if err != nil { |
| 1276 |
t.Fatalf("dst.Exists(bar) error = %v", err) |
| 1277 |
} |
| 1278 |
if fooExists == barExists { |
| 1279 |
t.Error("Only one of foo.txt and bar.txt should exist") |
| 1280 |
} |
| 1281 |
} |
| 1282 |
|
| 1283 |
func TestCopyGraph_WithOptions(t *testing.T) { |
| 1284 |
src := cas.NewMemory() |
| 1285 |
dst := cas.NewMemory() |
| 1286 |
|
| 1287 |
// generate test content |
| 1288 |
var blobs [][]byte |
| 1289 |
var descs []ocispec.Descriptor |
| 1290 |
appendBlob := func(mediaType string, blob []byte) { |
| 1291 |
blobs = append(blobs, blob) |
| 1292 |
descs = append(descs, ocispec.Descriptor{ |
| 1293 |
MediaType: mediaType, |
| 1294 |
Digest: digest.FromBytes(blob), |
| 1295 |
Size: int64(len(blob)), |
| 1296 |
}) |
| 1297 |
} |
| 1298 |
generateManifest := func(config ocispec.Descriptor, layers ...ocispec.Descriptor) { |
| 1299 |
manifest := ocispec.Manifest{ |
| 1300 |
Config: config, |
| 1301 |
Layers: layers, |
| 1302 |
} |
| 1303 |
manifestJSON, err := json.Marshal(manifest) |
| 1304 |
if err != nil { |
| 1305 |
t.Fatal(err) |
| 1306 |
} |
| 1307 |
appendBlob(ocispec.MediaTypeImageManifest, manifestJSON) |
| 1308 |
} |
| 1309 |
generateIndex := func(manifests ...ocispec.Descriptor) { |
| 1310 |
index := ocispec.Index{ |
| 1311 |
Manifests: manifests, |
| 1312 |
} |
| 1313 |
indexJSON, err := json.Marshal(index) |
| 1314 |
if err != nil { |
| 1315 |
t.Fatal(err) |
| 1316 |
} |
| 1317 |
appendBlob(ocispec.MediaTypeImageIndex, indexJSON) |
| 1318 |
} |
| 1319 |
|
| 1320 |
appendBlob(ocispec.MediaTypeImageConfig, []byte("config")) // Blob 0 |
| 1321 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("foo")) // Blob 1 |
| 1322 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("bar")) // Blob 2 |
| 1323 |
generateManifest(descs[0], descs[1:3]...) // Blob 3 |
| 1324 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("hello")) // Blob 4 |
| 1325 |
generateManifest(descs[0], descs[4]) // Blob 5 |
| 1326 |
generateIndex(descs[3], descs[5]) // Blob 6 |
| 1327 |
|
| 1328 |
ctx := context.Background() |
| 1329 |
for i := range blobs { |
| 1330 |
err := src.Push(ctx, descs[i], bytes.NewReader(blobs[i])) |
| 1331 |
if err != nil { |
| 1332 |
t.Fatalf("failed to push test content to src: %d: %v", i, err) |
| 1333 |
} |
| 1334 |
} |
| 1335 |
|
| 1336 |
// initial copy |
| 1337 |
root := descs[3] |
| 1338 |
opts := oras.DefaultCopyGraphOptions |
| 1339 |
opts.FindSuccessors = func(ctx context.Context, fetcher content.Fetcher, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) { |
| 1340 |
successors, err := content.Successors(ctx, fetcher, desc) |
| 1341 |
if err != nil { |
| 1342 |
return nil, err |
| 1343 |
} |
| 1344 |
// filter media type |
| 1345 |
var filtered []ocispec.Descriptor |
| 1346 |
for _, s := range successors { |
| 1347 |
if s.MediaType != ocispec.MediaTypeImageConfig { |
| 1348 |
filtered = append(filtered, s) |
| 1349 |
} |
| 1350 |
} |
| 1351 |
return filtered, nil |
| 1352 |
} |
| 1353 |
if err := oras.CopyGraph(ctx, src, dst, root, opts); err != nil { |
| 1354 |
t.Fatalf("CopyGraph() error = %v, wantErr %v", err, false) |
| 1355 |
} |
| 1356 |
// verify contents |
| 1357 |
contents := dst.Map() |
| 1358 |
if got, want := len(contents), len(blobs[1:4]); got != want { |
| 1359 |
t.Fatalf("len(dst) = %v, wantErr %v", got, want) |
| 1360 |
} |
| 1361 |
for i := 1; i < 4; i++ { |
| 1362 |
got, err := content.FetchAll(ctx, dst, descs[i]) |
| 1363 |
if err != nil { |
| 1364 |
t.Fatalf("content[%d] error = %v, wantErr %v", i, err, false) |
| 1365 |
} |
| 1366 |
if want := blobs[i]; !bytes.Equal(got, want) { |
| 1367 |
t.Fatalf("content[%d] = %v, want %v", i, got, want) |
| 1368 |
} |
| 1369 |
} |
| 1370 |
|
| 1371 |
// test successor descriptors not obtained from src |
| 1372 |
root = descs[3] |
| 1373 |
opts = oras.DefaultCopyGraphOptions |
| 1374 |
opts.FindSuccessors = func(ctx context.Context, fetcher content.Fetcher, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) { |
| 1375 |
if content.Equal(desc, root) { |
| 1376 |
return descs[1:3], nil |
| 1377 |
} |
| 1378 |
return content.Successors(ctx, fetcher, desc) |
| 1379 |
} |
| 1380 |
if err := oras.CopyGraph(ctx, src, cas.NewMemory(), root, opts); err != nil { |
| 1381 |
t.Fatalf("CopyGraph() error = %v, wantErr %v", err, false) |
| 1382 |
} |
| 1383 |
|
| 1384 |
// test partial copy |
| 1385 |
var preCopyCount int64 |
| 1386 |
var postCopyCount int64 |
| 1387 |
var skippedCount int64 |
| 1388 |
opts = oras.CopyGraphOptions{ |
| 1389 |
PreCopy: func(ctx context.Context, desc ocispec.Descriptor) error { |
| 1390 |
atomic.AddInt64(&preCopyCount, 1) |
| 1391 |
return nil |
| 1392 |
}, |
| 1393 |
PostCopy: func(ctx context.Context, desc ocispec.Descriptor) error { |
| 1394 |
atomic.AddInt64(&postCopyCount, 1) |
| 1395 |
return nil |
| 1396 |
}, |
| 1397 |
OnCopySkipped: func(ctx context.Context, desc ocispec.Descriptor) error { |
| 1398 |
atomic.AddInt64(&skippedCount, 1) |
| 1399 |
return nil |
| 1400 |
}, |
| 1401 |
} |
| 1402 |
root = descs[len(descs)-1] |
| 1403 |
if err := oras.CopyGraph(ctx, src, dst, root, opts); err != nil { |
| 1404 |
t.Fatalf("CopyGraph() error = %v, wantErr %v", err, false) |
| 1405 |
} |
| 1406 |
|
| 1407 |
// verify contents |
| 1408 |
contents = dst.Map() |
| 1409 |
if got, want := len(contents), len(blobs); got != want { |
| 1410 |
t.Errorf("len(dst) = %v, wantErr %v", got, want) |
| 1411 |
} |
| 1412 |
for i := range blobs { |
| 1413 |
got, err := content.FetchAll(ctx, dst, descs[i]) |
| 1414 |
if err != nil { |
| 1415 |
t.Errorf("content[%d] error = %v, wantErr %v", i, err, false) |
| 1416 |
continue |
| 1417 |
} |
| 1418 |
if want := blobs[i]; !bytes.Equal(got, want) { |
| 1419 |
t.Errorf("content[%d] = %v, want %v", i, got, want) |
| 1420 |
} |
| 1421 |
} |
| 1422 |
|
| 1423 |
// verify API counts |
| 1424 |
if got, want := preCopyCount, int64(4); got != want { |
| 1425 |
t.Errorf("count(PreCopy()) = %v, want %v", got, want) |
| 1426 |
} |
| 1427 |
if got, want := postCopyCount, int64(4); got != want { |
| 1428 |
t.Errorf("count(PostCopy()) = %v, want %v", got, want) |
| 1429 |
} |
| 1430 |
if got, want := skippedCount, int64(1); got != want { |
| 1431 |
t.Errorf("count(OnCopySkipped()) = %v, want %v", got, want) |
| 1432 |
} |
| 1433 |
|
| 1434 |
// test CopyGraph with MaxMetadataBytes = 1 |
| 1435 |
root = descs[6] |
| 1436 |
dst = cas.NewMemory() |
| 1437 |
opts = oras.CopyGraphOptions{ |
| 1438 |
MaxMetadataBytes: 1, |
| 1439 |
} |
| 1440 |
if err := oras.CopyGraph(ctx, src, dst, root, opts); !errors.Is(err, errdef.ErrSizeExceedsLimit) { |
| 1441 |
t.Fatalf("CopyGraph() error = %v, wantErr %v", err, errdef.ErrSizeExceedsLimit) |
| 1442 |
} |
| 1443 |
|
| 1444 |
t.Run("SkipNode", func(t *testing.T) { |
| 1445 |
// test CopyGraph with PreCopy = 1 |
| 1446 |
root = descs[6] |
| 1447 |
dst := &countingStorage{storage: cas.NewMemory()} |
| 1448 |
opts = oras.CopyGraphOptions{ |
| 1449 |
PreCopy: func(ctx context.Context, desc ocispec.Descriptor) error { |
| 1450 |
if descs[1].Digest == desc.Digest { |
| 1451 |
// blob 1 is handled by us (really this would be a Mount but ) |
| 1452 |
rc, err := src.Fetch(ctx, desc) |
| 1453 |
if err != nil { |
| 1454 |
t.Fatalf("Failed to fetch: %v", err) |
| 1455 |
} |
| 1456 |
defer rc.Close() |
| 1457 |
err = dst.storage.Push(ctx, desc, rc) // bypass the counters |
| 1458 |
if err != nil { |
| 1459 |
t.Fatalf("Failed to fetch: %v", err) |
| 1460 |
} |
| 1461 |
return oras.SkipNode |
| 1462 |
} |
| 1463 |
return nil |
| 1464 |
}, |
| 1465 |
} |
| 1466 |
if err := oras.CopyGraph(ctx, src, dst, root, opts); err != nil { |
| 1467 |
t.Fatalf("CopyGraph() error = %v", err) |
| 1468 |
} |
| 1469 |
|
| 1470 |
if got, expected := dst.numExists.Load(), int64(7); got != expected { |
| 1471 |
t.Errorf("count(Exists()) = %d, want %d", got, expected) |
| 1472 |
} |
| 1473 |
if got, expected := dst.numFetch.Load(), int64(0); got != expected { |
| 1474 |
t.Errorf("count(Fetch()) = %d, want %d", got, expected) |
| 1475 |
} |
| 1476 |
// 7 (exists) - 1 (skipped) = 6 pushes expected |
| 1477 |
if got, expected := dst.numPush.Load(), int64(6); got != expected { |
| 1478 |
// If we get >=7 then SkipNode did not short circuit the push like it is supposed to do. |
| 1479 |
t.Errorf("count(Push()) = %d, want %d", got, expected) |
| 1480 |
} |
| 1481 |
}) |
| 1482 |
|
| 1483 |
t.Run("MountFrom mounted", func(t *testing.T) { |
| 1484 |
root = descs[6] |
| 1485 |
dst := &countingStorage{storage: cas.NewMemory()} |
| 1486 |
var numMount atomic.Int64 |
| 1487 |
dst.mount = func(ctx context.Context, |
| 1488 |
desc ocispec.Descriptor, |
| 1489 |
fromRepo string, |
| 1490 |
getContent func() (io.ReadCloser, error), |
| 1491 |
) error { |
| 1492 |
numMount.Add(1) |
| 1493 |
if expected := "source"; fromRepo != expected { |
| 1494 |
t.Fatalf("fromRepo = %v, want %v", fromRepo, expected) |
| 1495 |
} |
| 1496 |
rc, err := src.Fetch(ctx, desc) |
| 1497 |
if err != nil { |
| 1498 |
t.Fatalf("Failed to fetch content: %v", err) |
| 1499 |
} |
| 1500 |
defer rc.Close() |
| 1501 |
err = dst.storage.Push(ctx, desc, rc) // bypass the counters |
| 1502 |
if err != nil { |
| 1503 |
t.Fatalf("Failed to push content: %v", err) |
| 1504 |
} |
| 1505 |
return nil |
| 1506 |
} |
| 1507 |
opts = oras.CopyGraphOptions{} |
| 1508 |
var numPreCopy, numPostCopy, numOnMounted, numMountFrom atomic.Int64 |
| 1509 |
opts.PreCopy = func(ctx context.Context, desc ocispec.Descriptor) error { |
| 1510 |
numPreCopy.Add(1) |
| 1511 |
return nil |
| 1512 |
} |
| 1513 |
opts.PostCopy = func(ctx context.Context, desc ocispec.Descriptor) error { |
| 1514 |
numPostCopy.Add(1) |
| 1515 |
return nil |
| 1516 |
} |
| 1517 |
opts.OnMounted = func(ctx context.Context, d ocispec.Descriptor) error { |
| 1518 |
numOnMounted.Add(1) |
| 1519 |
return nil |
| 1520 |
} |
| 1521 |
opts.MountFrom = func(ctx context.Context, desc ocispec.Descriptor) ([]string, error) { |
| 1522 |
numMountFrom.Add(1) |
| 1523 |
return []string{"source"}, nil |
| 1524 |
} |
| 1525 |
if err := oras.CopyGraph(ctx, src, dst, root, opts); err != nil { |
| 1526 |
t.Fatalf("CopyGraph() error = %v", err) |
| 1527 |
} |
| 1528 |
|
| 1529 |
if got, expected := dst.numExists.Load(), int64(7); got != expected { |
| 1530 |
t.Errorf("count(Exists()) = %d, want %d", got, expected) |
| 1531 |
} |
| 1532 |
if got, expected := dst.numFetch.Load(), int64(0); got != expected { |
| 1533 |
t.Errorf("count(Fetch()) = %d, want %d", got, expected) |
| 1534 |
} |
| 1535 |
// 7 (exists) - 1 (skipped) = 6 pushes expected |
| 1536 |
if got, expected := dst.numPush.Load(), int64(3); got != expected { |
| 1537 |
// If we get >=7 then ErrSkipDesc did not short circuit the push like it is supposed to do. |
| 1538 |
t.Errorf("count(Push()) = %d, want %d", got, expected) |
| 1539 |
} |
| 1540 |
if got, expected := numMount.Load(), int64(4); got != expected { |
| 1541 |
t.Errorf("count(Mount()) = %d, want %d", got, expected) |
| 1542 |
} |
| 1543 |
if got, expected := numOnMounted.Load(), int64(4); got != expected { |
| 1544 |
t.Errorf("count(OnMounted()) = %d, want %d", got, expected) |
| 1545 |
} |
| 1546 |
if got, expected := numMountFrom.Load(), int64(4); got != expected { |
| 1547 |
t.Errorf("count(MountFrom()) = %d, want %d", got, expected) |
| 1548 |
} |
| 1549 |
if got, expected := numPreCopy.Load(), int64(3); got != expected { |
| 1550 |
t.Errorf("count(PreCopy()) = %d, want %d", got, expected) |
| 1551 |
} |
| 1552 |
if got, expected := numPostCopy.Load(), int64(3); got != expected { |
| 1553 |
t.Errorf("count(PostCopy()) = %d, want %d", got, expected) |
| 1554 |
} |
| 1555 |
}) |
| 1556 |
|
| 1557 |
t.Run("MountFrom copied", func(t *testing.T) { |
| 1558 |
root = descs[6] |
| 1559 |
dst := &countingStorage{storage: cas.NewMemory()} |
| 1560 |
var numMount atomic.Int64 |
| 1561 |
dst.mount = func(ctx context.Context, |
| 1562 |
desc ocispec.Descriptor, |
| 1563 |
fromRepo string, |
| 1564 |
getContent func() (io.ReadCloser, error), |
| 1565 |
) error { |
| 1566 |
numMount.Add(1) |
| 1567 |
if expected := "source"; fromRepo != expected { |
| 1568 |
t.Fatalf("fromRepo = %v, want %v", fromRepo, expected) |
| 1569 |
} |
| 1570 |
|
| 1571 |
rc, err := getContent() |
| 1572 |
if err != nil { |
| 1573 |
t.Fatalf("Failed to fetch content: %v", err) |
| 1574 |
} |
| 1575 |
defer rc.Close() |
| 1576 |
err = dst.storage.Push(ctx, desc, rc) // bypass the counters |
| 1577 |
if err != nil { |
| 1578 |
t.Fatalf("Failed to push content: %v", err) |
| 1579 |
} |
| 1580 |
return nil |
| 1581 |
} |
| 1582 |
opts = oras.CopyGraphOptions{} |
| 1583 |
var numPreCopy, numPostCopy, numOnMounted, numMountFrom atomic.Int64 |
| 1584 |
opts.PreCopy = func(ctx context.Context, desc ocispec.Descriptor) error { |
| 1585 |
numPreCopy.Add(1) |
| 1586 |
return nil |
| 1587 |
} |
| 1588 |
opts.PostCopy = func(ctx context.Context, desc ocispec.Descriptor) error { |
| 1589 |
numPostCopy.Add(1) |
| 1590 |
return nil |
| 1591 |
} |
| 1592 |
opts.OnMounted = func(ctx context.Context, d ocispec.Descriptor) error { |
| 1593 |
numOnMounted.Add(1) |
| 1594 |
return nil |
| 1595 |
} |
| 1596 |
opts.MountFrom = func(ctx context.Context, desc ocispec.Descriptor) ([]string, error) { |
| 1597 |
numMountFrom.Add(1) |
| 1598 |
return []string{"source"}, nil |
| 1599 |
} |
| 1600 |
if err := oras.CopyGraph(ctx, src, dst, root, opts); err != nil { |
| 1601 |
t.Fatalf("CopyGraph() error = %v", err) |
| 1602 |
} |
| 1603 |
|
| 1604 |
if got, expected := dst.numExists.Load(), int64(7); got != expected { |
| 1605 |
t.Errorf("count(Exists()) = %d, want %d", got, expected) |
| 1606 |
} |
| 1607 |
if got, expected := dst.numFetch.Load(), int64(0); got != expected { |
| 1608 |
t.Errorf("count(Fetch()) = %d, want %d", got, expected) |
| 1609 |
} |
| 1610 |
// 7 (exists) - 1 (skipped) = 6 pushes expected |
| 1611 |
if got, expected := dst.numPush.Load(), int64(3); got != expected { |
| 1612 |
// If we get >=7 then ErrSkipDesc did not short circuit the push like it is supposed to do. |
| 1613 |
t.Errorf("count(Push()) = %d, want %d", got, expected) |
| 1614 |
} |
| 1615 |
if got, expected := numMount.Load(), int64(4); got != expected { |
| 1616 |
t.Errorf("count(Mount()) = %d, want %d", got, expected) |
| 1617 |
} |
| 1618 |
if got, expected := numOnMounted.Load(), int64(0); got != expected { |
| 1619 |
t.Errorf("count(OnMounted()) = %d, want %d", got, expected) |
| 1620 |
} |
| 1621 |
if got, expected := numMountFrom.Load(), int64(4); got != expected { |
| 1622 |
t.Errorf("count(MountFrom()) = %d, want %d", got, expected) |
| 1623 |
} |
| 1624 |
if got, expected := numPreCopy.Load(), int64(7); got != expected { |
| 1625 |
t.Errorf("count(PreCopy()) = %d, want %d", got, expected) |
| 1626 |
} |
| 1627 |
if got, expected := numPostCopy.Load(), int64(7); got != expected { |
| 1628 |
t.Errorf("count(PostCopy()) = %d, want %d", got, expected) |
| 1629 |
} |
| 1630 |
}) |
| 1631 |
|
| 1632 |
t.Run("MountFrom mounted second try", func(t *testing.T) { |
| 1633 |
root = descs[6] |
| 1634 |
dst := &countingStorage{storage: cas.NewMemory()} |
| 1635 |
var numMount atomic.Int64 |
| 1636 |
dst.mount = func(ctx context.Context, |
| 1637 |
desc ocispec.Descriptor, |
| 1638 |
fromRepo string, |
| 1639 |
getContent func() (io.ReadCloser, error), |
| 1640 |
) error { |
| 1641 |
numMount.Add(1) |
| 1642 |
switch fromRepo { |
| 1643 |
case "source": |
| 1644 |
rc, err := src.Fetch(ctx, desc) |
| 1645 |
if err != nil { |
| 1646 |
t.Fatalf("Failed to fetch content: %v", err) |
| 1647 |
} |
| 1648 |
defer rc.Close() |
| 1649 |
err = dst.storage.Push(ctx, desc, rc) // bypass the counters |
| 1650 |
if err != nil { |
| 1651 |
t.Fatalf("Failed to push content: %v", err) |
| 1652 |
} |
| 1653 |
return nil |
| 1654 |
case "missing/the/data": |
| 1655 |
// simulate a registry mount will fail, so it will request the content to start the copy. |
| 1656 |
rc, err := getContent() |
| 1657 |
if err != nil { |
| 1658 |
return fmt.Errorf("getContent failed: %w", err) |
| 1659 |
} |
| 1660 |
defer rc.Close() |
| 1661 |
err = dst.storage.Push(ctx, desc, rc) // bypass the counters |
| 1662 |
if err != nil { |
| 1663 |
t.Fatalf("Failed to push content: %v", err) |
| 1664 |
} |
| 1665 |
return nil |
| 1666 |
default: |
| 1667 |
t.Fatalf("fromRepo = %v, want either %v or %v", fromRepo, "missing/the/data", "source") |
| 1668 |
return nil |
| 1669 |
} |
| 1670 |
} |
| 1671 |
opts = oras.CopyGraphOptions{} |
| 1672 |
var numPreCopy, numPostCopy, numOnMounted, numMountFrom atomic.Int64 |
| 1673 |
opts.PreCopy = func(ctx context.Context, desc ocispec.Descriptor) error { |
| 1674 |
numPreCopy.Add(1) |
| 1675 |
return nil |
| 1676 |
} |
| 1677 |
opts.PostCopy = func(ctx context.Context, desc ocispec.Descriptor) error { |
| 1678 |
numPostCopy.Add(1) |
| 1679 |
return nil |
| 1680 |
} |
| 1681 |
opts.OnMounted = func(ctx context.Context, d ocispec.Descriptor) error { |
| 1682 |
numOnMounted.Add(1) |
| 1683 |
return nil |
| 1684 |
} |
| 1685 |
opts.MountFrom = func(ctx context.Context, desc ocispec.Descriptor) ([]string, error) { |
| 1686 |
numMountFrom.Add(1) |
| 1687 |
return []string{"missing/the/data", "source"}, nil |
| 1688 |
} |
| 1689 |
if err := oras.CopyGraph(ctx, src, dst, root, opts); err != nil { |
| 1690 |
t.Fatalf("CopyGraph() error = %v", err) |
| 1691 |
} |
| 1692 |
|
| 1693 |
if got, expected := dst.numExists.Load(), int64(7); got != expected { |
| 1694 |
t.Errorf("count(Exists()) = %d, want %d", got, expected) |
| 1695 |
} |
| 1696 |
if got, expected := dst.numFetch.Load(), int64(0); got != expected { |
| 1697 |
t.Errorf("count(Fetch()) = %d, want %d", got, expected) |
| 1698 |
} |
| 1699 |
// 7 (exists) - 1 (skipped) = 6 pushes expected |
| 1700 |
if got, expected := dst.numPush.Load(), int64(3); got != expected { |
| 1701 |
// If we get >=7 then ErrSkipDesc did not short circuit the push like it is supposed to do. |
| 1702 |
t.Errorf("count(Push()) = %d, want %d", got, expected) |
| 1703 |
} |
| 1704 |
if got, expected := numMount.Load(), int64(4*2); got != expected { |
| 1705 |
t.Errorf("count(Mount()) = %d, want %d", got, expected) |
| 1706 |
} |
| 1707 |
if got, expected := numOnMounted.Load(), int64(4); got != expected { |
| 1708 |
t.Errorf("count(OnMounted()) = %d, want %d", got, expected) |
| 1709 |
} |
| 1710 |
if got, expected := numMountFrom.Load(), int64(4); got != expected { |
| 1711 |
t.Errorf("count(MountFrom()) = %d, want %d", got, expected) |
| 1712 |
} |
| 1713 |
if got, expected := numPreCopy.Load(), int64(3); got != expected { |
| 1714 |
t.Errorf("count(PreCopy()) = %d, want %d", got, expected) |
| 1715 |
} |
| 1716 |
if got, expected := numPostCopy.Load(), int64(3); got != expected { |
| 1717 |
t.Errorf("count(PostCopy()) = %d, want %d", got, expected) |
| 1718 |
} |
| 1719 |
}) |
| 1720 |
|
| 1721 |
t.Run("MountFrom copied dst not a Mounter", func(t *testing.T) { |
| 1722 |
root = descs[6] |
| 1723 |
dst := cas.NewMemory() |
| 1724 |
opts = oras.CopyGraphOptions{} |
| 1725 |
var numPreCopy, numPostCopy, numOnMounted, numMountFrom atomic.Int64 |
| 1726 |
opts.PreCopy = func(ctx context.Context, desc ocispec.Descriptor) error { |
| 1727 |
numPreCopy.Add(1) |
| 1728 |
return nil |
| 1729 |
} |
| 1730 |
opts.PostCopy = func(ctx context.Context, desc ocispec.Descriptor) error { |
| 1731 |
numPostCopy.Add(1) |
| 1732 |
return nil |
| 1733 |
} |
| 1734 |
opts.OnMounted = func(ctx context.Context, d ocispec.Descriptor) error { |
| 1735 |
numOnMounted.Add(1) |
| 1736 |
return nil |
| 1737 |
} |
| 1738 |
opts.MountFrom = func(ctx context.Context, desc ocispec.Descriptor) ([]string, error) { |
| 1739 |
numMountFrom.Add(1) |
| 1740 |
return []string{"source"}, nil |
| 1741 |
} |
| 1742 |
if err := oras.CopyGraph(ctx, src, dst, root, opts); err != nil { |
| 1743 |
t.Fatalf("CopyGraph() error = %v", err) |
| 1744 |
} |
| 1745 |
|
| 1746 |
if got, expected := numOnMounted.Load(), int64(0); got != expected { |
| 1747 |
t.Errorf("count(OnMounted()) = %d, want %d", got, expected) |
| 1748 |
} |
| 1749 |
if got, expected := numMountFrom.Load(), int64(0); got != expected { |
| 1750 |
t.Errorf("count(MountFrom()) = %d, want %d", got, expected) |
| 1751 |
} |
| 1752 |
if got, expected := numPreCopy.Load(), int64(7); got != expected { |
| 1753 |
t.Errorf("count(PreCopy()) = %d, want %d", got, expected) |
| 1754 |
} |
| 1755 |
if got, expected := numPostCopy.Load(), int64(7); got != expected { |
| 1756 |
t.Errorf("count(PostCopy()) = %d, want %d", got, expected) |
| 1757 |
} |
| 1758 |
}) |
| 1759 |
|
| 1760 |
t.Run("MountFrom empty sourceRepositories", func(t *testing.T) { |
| 1761 |
root = descs[6] |
| 1762 |
dst := &countingStorage{storage: cas.NewMemory()} |
| 1763 |
opts = oras.CopyGraphOptions{} |
| 1764 |
var numMountFrom atomic.Int64 |
| 1765 |
opts.MountFrom = func(ctx context.Context, desc ocispec.Descriptor) ([]string, error) { |
| 1766 |
numMountFrom.Add(1) |
| 1767 |
return nil, nil |
| 1768 |
} |
| 1769 |
if err := oras.CopyGraph(ctx, src, dst, root, opts); err != nil { |
| 1770 |
t.Fatalf("CopyGraph() error = %v", err) |
| 1771 |
} |
| 1772 |
|
| 1773 |
if got, expected := dst.numExists.Load(), int64(7); got != expected { |
| 1774 |
t.Errorf("count(Exists()) = %d, want %d", got, expected) |
| 1775 |
} |
| 1776 |
if got, expected := dst.numFetch.Load(), int64(0); got != expected { |
| 1777 |
t.Errorf("count(Fetch()) = %d, want %d", got, expected) |
| 1778 |
} |
| 1779 |
if got, expected := dst.numPush.Load(), int64(7); got != expected { |
| 1780 |
t.Errorf("count(Push()) = %d, want %d", got, expected) |
| 1781 |
} |
| 1782 |
if got, expected := numMountFrom.Load(), int64(4); got != expected { |
| 1783 |
t.Errorf("count(MountFrom()) = %d, want %d", got, expected) |
| 1784 |
} |
| 1785 |
}) |
| 1786 |
|
| 1787 |
t.Run("MountFrom error", func(t *testing.T) { |
| 1788 |
root = descs[3] |
| 1789 |
dst := &countingStorage{storage: cas.NewMemory()} |
| 1790 |
opts = oras.CopyGraphOptions{ |
| 1791 |
// to make the run result deterministic, we limit concurrency to 1 |
| 1792 |
Concurrency: 1, |
| 1793 |
} |
| 1794 |
var numMountFrom atomic.Int64 |
| 1795 |
e := errors.New("mountFrom error") |
| 1796 |
opts.MountFrom = func(ctx context.Context, desc ocispec.Descriptor) ([]string, error) { |
| 1797 |
numMountFrom.Add(1) |
| 1798 |
return nil, e |
| 1799 |
} |
| 1800 |
if err := oras.CopyGraph(ctx, src, dst, root, opts); !errors.Is(err, e) { |
| 1801 |
t.Fatalf("CopyGraph() error = %v, wantErr %v", err, e) |
| 1802 |
} |
| 1803 |
|
| 1804 |
if got, expected := dst.numExists.Load(), int64(2); got != expected { |
| 1805 |
t.Errorf("count(Exists()) = %d, want %d", got, expected) |
| 1806 |
} |
| 1807 |
if got, expected := dst.numFetch.Load(), int64(0); got != expected { |
| 1808 |
t.Errorf("count(Fetch()) = %d, want %d", got, expected) |
| 1809 |
} |
| 1810 |
if got, expected := dst.numPush.Load(), int64(0); got != expected { |
| 1811 |
t.Errorf("count(Push()) = %d, want %d", got, expected) |
| 1812 |
} |
| 1813 |
if got, expected := numMountFrom.Load(), int64(1); got != expected { |
| 1814 |
t.Errorf("count(MountFrom()) = %d, want %d", got, expected) |
| 1815 |
} |
| 1816 |
}) |
| 1817 |
|
| 1818 |
t.Run("MountFrom OnMounted error", func(t *testing.T) { |
| 1819 |
root = descs[3] |
| 1820 |
dst := &countingStorage{storage: cas.NewMemory()} |
| 1821 |
var numMount atomic.Int64 |
| 1822 |
dst.mount = func(ctx context.Context, |
| 1823 |
desc ocispec.Descriptor, |
| 1824 |
fromRepo string, |
| 1825 |
getContent func() (io.ReadCloser, error), |
| 1826 |
) error { |
| 1827 |
numMount.Add(1) |
| 1828 |
if expected := "source"; fromRepo != expected { |
| 1829 |
t.Fatalf("fromRepo = %v, want %v", fromRepo, expected) |
| 1830 |
} |
| 1831 |
rc, err := src.Fetch(ctx, desc) |
| 1832 |
if err != nil { |
| 1833 |
t.Fatalf("Failed to fetch content: %v", err) |
| 1834 |
} |
| 1835 |
defer rc.Close() |
| 1836 |
err = dst.storage.Push(ctx, desc, rc) // bypass the counters |
| 1837 |
if err != nil { |
| 1838 |
t.Fatalf("Failed to push content: %v", err) |
| 1839 |
} |
| 1840 |
return nil |
| 1841 |
} |
| 1842 |
opts = oras.CopyGraphOptions{ |
| 1843 |
// to make the run result deterministic, we limit concurrency to 1 |
| 1844 |
Concurrency: 1, |
| 1845 |
} |
| 1846 |
var numPreCopy, numPostCopy, numOnMounted, numMountFrom atomic.Int64 |
| 1847 |
opts.PreCopy = func(ctx context.Context, desc ocispec.Descriptor) error { |
| 1848 |
numPreCopy.Add(1) |
| 1849 |
return nil |
| 1850 |
} |
| 1851 |
opts.PostCopy = func(ctx context.Context, desc ocispec.Descriptor) error { |
| 1852 |
numPostCopy.Add(1) |
| 1853 |
return nil |
| 1854 |
} |
| 1855 |
e := errors.New("onMounted error") |
| 1856 |
opts.OnMounted = func(ctx context.Context, d ocispec.Descriptor) error { |
| 1857 |
numOnMounted.Add(1) |
| 1858 |
return e |
| 1859 |
} |
| 1860 |
opts.MountFrom = func(ctx context.Context, desc ocispec.Descriptor) ([]string, error) { |
| 1861 |
numMountFrom.Add(1) |
| 1862 |
return []string{"source"}, nil |
| 1863 |
} |
| 1864 |
if err := oras.CopyGraph(ctx, src, dst, root, opts); !errors.Is(err, e) { |
| 1865 |
t.Fatalf("CopyGraph() error = %v, wantErr %v", err, e) |
| 1866 |
} |
| 1867 |
|
| 1868 |
if got, expected := dst.numExists.Load(), int64(2); got != expected { |
| 1869 |
t.Errorf("count(Exists()) = %d, want %d", got, expected) |
| 1870 |
} |
| 1871 |
if got, expected := dst.numFetch.Load(), int64(0); got != expected { |
| 1872 |
t.Errorf("count(Fetch()) = %d, want %d", got, expected) |
| 1873 |
} |
| 1874 |
if got, expected := dst.numPush.Load(), int64(0); got != expected { |
| 1875 |
t.Errorf("count(Push()) = %d, want %d", got, expected) |
| 1876 |
} |
| 1877 |
if got, expected := numMount.Load(), int64(1); got != expected { |
| 1878 |
t.Errorf("count(Mount()) = %d, want %d", got, expected) |
| 1879 |
} |
| 1880 |
if got, expected := numOnMounted.Load(), int64(1); got != expected { |
| 1881 |
t.Errorf("count(OnMounted()) = %d, want %d", got, expected) |
| 1882 |
} |
| 1883 |
if got, expected := numMountFrom.Load(), int64(1); got != expected { |
| 1884 |
t.Errorf("count(MountFrom()) = %d, want %d", got, expected) |
| 1885 |
} |
| 1886 |
if got, expected := numPreCopy.Load(), int64(0); got != expected { |
| 1887 |
t.Errorf("count(PreCopy()) = %d, want %d", got, expected) |
| 1888 |
} |
| 1889 |
if got, expected := numPostCopy.Load(), int64(0); got != expected { |
| 1890 |
t.Errorf("count(PostCopy()) = %d, want %d", got, expected) |
| 1891 |
} |
| 1892 |
}) |
| 1893 |
} |
| 1894 |
|
| 1895 |
// countingStorage counts the calls to its content.Storage methods |
| 1896 |
type countingStorage struct { |
| 1897 |
storage content.Storage |
| 1898 |
mount mountFunc |
| 1899 |
|
| 1900 |
numExists, numFetch, numPush atomic.Int64 |
| 1901 |
} |
| 1902 |
|
| 1903 |
func (cs *countingStorage) Exists(ctx context.Context, target ocispec.Descriptor) (bool, error) { |
| 1904 |
cs.numExists.Add(1) |
| 1905 |
return cs.storage.Exists(ctx, target) |
| 1906 |
} |
| 1907 |
|
| 1908 |
func (cs *countingStorage) Fetch(ctx context.Context, target ocispec.Descriptor) (io.ReadCloser, error) { |
| 1909 |
cs.numFetch.Add(1) |
| 1910 |
return cs.storage.Fetch(ctx, target) |
| 1911 |
} |
| 1912 |
|
| 1913 |
func (cs *countingStorage) Push(ctx context.Context, target ocispec.Descriptor, r io.Reader) error { |
| 1914 |
cs.numPush.Add(1) |
| 1915 |
return cs.storage.Push(ctx, target, r) |
| 1916 |
} |
| 1917 |
|
| 1918 |
type mountFunc func(context.Context, ocispec.Descriptor, string, func() (io.ReadCloser, error)) error |
| 1919 |
|
| 1920 |
func (cs *countingStorage) Mount(ctx context.Context, |
| 1921 |
desc ocispec.Descriptor, |
| 1922 |
fromRepo string, |
| 1923 |
getContent func() (io.ReadCloser, error), |
| 1924 |
) error { |
| 1925 |
return cs.mount(ctx, desc, fromRepo, getContent) |
| 1926 |
} |
| 1927 |
|
| 1928 |
func TestCopyGraph_WithConcurrencyLimit(t *testing.T) { |
| 1929 |
src := cas.NewMemory() |
| 1930 |
// generate test content |
| 1931 |
var blobs [][]byte |
| 1932 |
var descs []ocispec.Descriptor |
| 1933 |
appendBlob := func(mediaType string, blob []byte) { |
| 1934 |
blobs = append(blobs, blob) |
| 1935 |
descs = append(descs, ocispec.Descriptor{ |
| 1936 |
MediaType: mediaType, |
| 1937 |
Digest: digest.FromBytes(blob), |
| 1938 |
Size: int64(len(blob)), |
| 1939 |
}) |
| 1940 |
} |
| 1941 |
generateManifest := func(config ocispec.Descriptor, layers ...ocispec.Descriptor) { |
| 1942 |
manifest := ocispec.Manifest{ |
| 1943 |
MediaType: ocispec.MediaTypeImageManifest, |
| 1944 |
Config: config, |
| 1945 |
Layers: layers, |
| 1946 |
} |
| 1947 |
manifestJSON, err := json.Marshal(manifest) |
| 1948 |
if err != nil { |
| 1949 |
t.Fatal(err) |
| 1950 |
} |
| 1951 |
appendBlob(manifest.MediaType, manifestJSON) |
| 1952 |
} |
| 1953 |
generateArtifact := func(subject *ocispec.Descriptor, artifactType string, blobs ...ocispec.Descriptor) { |
| 1954 |
manifest := spec.Artifact{ |
| 1955 |
MediaType: spec.MediaTypeArtifactManifest, |
| 1956 |
Subject: subject, |
| 1957 |
Blobs: blobs, |
| 1958 |
ArtifactType: artifactType, |
| 1959 |
} |
| 1960 |
manifestJSON, err := json.Marshal(manifest) |
| 1961 |
if err != nil { |
| 1962 |
t.Fatal(err) |
| 1963 |
} |
| 1964 |
appendBlob(manifest.MediaType, manifestJSON) |
| 1965 |
} |
| 1966 |
generateIndex := func(manifests ...ocispec.Descriptor) { |
| 1967 |
index := ocispec.Index{ |
| 1968 |
MediaType: ocispec.MediaTypeImageIndex, |
| 1969 |
Manifests: manifests, |
| 1970 |
} |
| 1971 |
indexJSON, err := json.Marshal(index) |
| 1972 |
if err != nil { |
| 1973 |
t.Fatal(err) |
| 1974 |
} |
| 1975 |
appendBlob(index.MediaType, indexJSON) |
| 1976 |
} |
| 1977 |
|
| 1978 |
appendBlob(ocispec.MediaTypeImageConfig, []byte("config")) // Blob 0 |
| 1979 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("foo")) // Blob 1 |
| 1980 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("bar")) // Blob 2 |
| 1981 |
generateManifest(descs[0], descs[1:3]...) // Blob 3 |
| 1982 |
generateArtifact(&descs[3], "artifact.1") // Blob 4 |
| 1983 |
generateArtifact(&descs[3], "artifact.2") // Blob 5 |
| 1984 |
generateArtifact(&descs[3], "artifact.3") // Blob 6 |
| 1985 |
generateArtifact(&descs[3], "artifact.4") // Blob 7 |
| 1986 |
generateIndex(descs[3:8]...) // Blob 8 |
| 1987 |
|
| 1988 |
ctx := context.Background() |
| 1989 |
for i := range blobs { |
| 1990 |
err := src.Push(ctx, descs[i], bytes.NewReader(blobs[i])) |
| 1991 |
if err != nil { |
| 1992 |
t.Fatalf("failed to push test content to src: %d: %v", i, err) |
| 1993 |
} |
| 1994 |
} |
| 1995 |
|
| 1996 |
// test different concurrency limit |
| 1997 |
root := descs[len(descs)-1] |
| 1998 |
directSuccessorsNum := 5 |
| 1999 |
opts := oras.DefaultCopyGraphOptions |
| 2000 |
for i := 1; i <= directSuccessorsNum; i++ { |
| 2001 |
dst := cas.NewMemory() |
| 2002 |
opts.Concurrency = i |
| 2003 |
if err := oras.CopyGraph(ctx, src, dst, root, opts); err != nil { |
| 2004 |
t.Fatalf("CopyGraph(concurrency: %d) error = %v, wantErr %v", i, err, false) |
| 2005 |
} |
| 2006 |
|
| 2007 |
// verify contents |
| 2008 |
contents := dst.Map() |
| 2009 |
if got, want := len(contents), len(blobs); got != want { |
| 2010 |
t.Errorf("len(dst) = %v, wantErr %v", got, want) |
| 2011 |
} |
| 2012 |
for i := range blobs { |
| 2013 |
got, err := content.FetchAll(ctx, dst, descs[i]) |
| 2014 |
if err != nil { |
| 2015 |
t.Errorf("content[%d] error = %v, wantErr %v", i, err, false) |
| 2016 |
continue |
| 2017 |
} |
| 2018 |
if want := blobs[i]; !bytes.Equal(got, want) { |
| 2019 |
t.Errorf("content[%d] = %v, want %v", i, got, want) |
| 2020 |
} |
| 2021 |
} |
| 2022 |
} |
| 2023 |
} |
| 2024 |
|
| 2025 |
func TestCopyGraph_ForeignLayers(t *testing.T) { |
| 2026 |
src := cas.NewMemory() |
| 2027 |
dst := cas.NewMemory() |
| 2028 |
|
| 2029 |
// generate test content |
| 2030 |
var blobs [][]byte |
| 2031 |
var descs []ocispec.Descriptor |
| 2032 |
appendBlob := func(mediaType string, blob []byte) { |
| 2033 |
desc := ocispec.Descriptor{ |
| 2034 |
MediaType: mediaType, |
| 2035 |
Digest: digest.FromBytes(blob), |
| 2036 |
Size: int64(len(blob)), |
| 2037 |
} |
| 2038 |
if mediaType == ocispec.MediaTypeImageLayerNonDistributable { |
| 2039 |
desc.URLs = append(desc.URLs, "http://127.0.0.1/dummy") |
| 2040 |
blob = nil |
| 2041 |
} |
| 2042 |
descs = append(descs, desc) |
| 2043 |
blobs = append(blobs, blob) |
| 2044 |
} |
| 2045 |
generateManifest := func(config ocispec.Descriptor, layers ...ocispec.Descriptor) { |
| 2046 |
manifest := ocispec.Manifest{ |
| 2047 |
Config: config, |
| 2048 |
Layers: layers, |
| 2049 |
} |
| 2050 |
manifestJSON, err := json.Marshal(manifest) |
| 2051 |
if err != nil { |
| 2052 |
t.Fatal(err) |
| 2053 |
} |
| 2054 |
appendBlob(ocispec.MediaTypeImageManifest, manifestJSON) |
| 2055 |
} |
| 2056 |
|
| 2057 |
appendBlob(ocispec.MediaTypeImageConfig, []byte("config")) // Blob 0 |
| 2058 |
appendBlob(ocispec.MediaTypeImageLayerNonDistributable, []byte("hello")) // Blob 1 |
| 2059 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("foo")) // Blob 2 |
| 2060 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("bar")) // Blob 3 |
| 2061 |
generateManifest(descs[0], descs[1:4]...) // Blob 4 |
| 2062 |
|
| 2063 |
ctx := context.Background() |
| 2064 |
for i := range blobs { |
| 2065 |
if blobs[i] == nil { |
| 2066 |
continue |
| 2067 |
} |
| 2068 |
err := src.Push(ctx, descs[i], bytes.NewReader(blobs[i])) |
| 2069 |
if err != nil { |
| 2070 |
t.Fatalf("failed to push test content to src: %d: %v", i, err) |
| 2071 |
} |
| 2072 |
} |
| 2073 |
|
| 2074 |
// test copy |
| 2075 |
srcTracker := &storageTracker{Storage: src} |
| 2076 |
dstTracker := &storageTracker{Storage: dst} |
| 2077 |
root := descs[len(descs)-1] |
| 2078 |
if err := oras.CopyGraph(ctx, srcTracker, dstTracker, root, oras.CopyGraphOptions{}); err != nil { |
| 2079 |
t.Fatalf("CopyGraph() error = %v, wantErr %v", err, false) |
| 2080 |
} |
| 2081 |
|
| 2082 |
// verify contents |
| 2083 |
contents := dst.Map() |
| 2084 |
if got, want := len(contents), len(blobs)-1; got != want { |
| 2085 |
t.Errorf("len(dst) = %v, wantErr %v", got, want) |
| 2086 |
} |
| 2087 |
for i := range blobs { |
| 2088 |
if blobs[i] == nil { |
| 2089 |
continue |
| 2090 |
} |
| 2091 |
got, err := content.FetchAll(ctx, dst, descs[i]) |
| 2092 |
if err != nil { |
| 2093 |
t.Errorf("content[%d] error = %v, wantErr %v", i, err, false) |
| 2094 |
continue |
| 2095 |
} |
| 2096 |
if want := blobs[i]; !bytes.Equal(got, want) { |
| 2097 |
t.Errorf("content[%d] = %v, want %v", i, got, want) |
| 2098 |
} |
| 2099 |
} |
| 2100 |
|
| 2101 |
// verify API counts |
| 2102 |
if got, want := srcTracker.fetch, int64(len(blobs)-1); got != want { |
| 2103 |
t.Errorf("count(src.Fetch()) = %v, want %v", got, want) |
| 2104 |
} |
| 2105 |
if got, want := srcTracker.push, int64(0); got != want { |
| 2106 |
t.Errorf("count(src.Push()) = %v, want %v", got, want) |
| 2107 |
} |
| 2108 |
if got, want := srcTracker.exists, int64(0); got != want { |
| 2109 |
t.Errorf("count(src.Exists()) = %v, want %v", got, want) |
| 2110 |
} |
| 2111 |
if got, want := dstTracker.fetch, int64(0); got != want { |
| 2112 |
t.Errorf("count(dst.Fetch()) = %v, want %v", got, want) |
| 2113 |
} |
| 2114 |
if got, want := dstTracker.push, int64(len(blobs)-1); got != want { |
| 2115 |
t.Errorf("count(dst.Push()) = %v, want %v", got, want) |
| 2116 |
} |
| 2117 |
if got, want := dstTracker.exists, int64(len(blobs)-1); got != want { |
| 2118 |
t.Errorf("count(dst.Exists()) = %v, want %v", got, want) |
| 2119 |
} |
| 2120 |
} |
| 2121 |
|
| 2122 |
func TestCopyGraph_ForeignLayers_Mixed(t *testing.T) { |
| 2123 |
src := cas.NewMemory() |
| 2124 |
dst := cas.NewMemory() |
| 2125 |
|
| 2126 |
// generate test content |
| 2127 |
var blobs [][]byte |
| 2128 |
var descs []ocispec.Descriptor |
| 2129 |
appendBlob := func(mediaType string, blob []byte) { |
| 2130 |
desc := ocispec.Descriptor{ |
| 2131 |
MediaType: mediaType, |
| 2132 |
Digest: digest.FromBytes(blob), |
| 2133 |
Size: int64(len(blob)), |
| 2134 |
} |
| 2135 |
if mediaType == ocispec.MediaTypeImageLayerNonDistributable { |
| 2136 |
desc.URLs = append(desc.URLs, "http://127.0.0.1/dummy") |
| 2137 |
blob = nil |
| 2138 |
} |
| 2139 |
descs = append(descs, desc) |
| 2140 |
blobs = append(blobs, blob) |
| 2141 |
} |
| 2142 |
generateManifest := func(config ocispec.Descriptor, layers ...ocispec.Descriptor) { |
| 2143 |
manifest := ocispec.Manifest{ |
| 2144 |
Config: config, |
| 2145 |
Layers: layers, |
| 2146 |
} |
| 2147 |
manifestJSON, err := json.Marshal(manifest) |
| 2148 |
if err != nil { |
| 2149 |
t.Fatal(err) |
| 2150 |
} |
| 2151 |
appendBlob(ocispec.MediaTypeImageManifest, manifestJSON) |
| 2152 |
} |
| 2153 |
|
| 2154 |
appendBlob(ocispec.MediaTypeImageConfig, []byte("config")) // Blob 0 |
| 2155 |
appendBlob(ocispec.MediaTypeImageLayerNonDistributable, []byte("hello")) // Blob 1 |
| 2156 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("hello")) // Blob 2 |
| 2157 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("foo")) // Blob 3 |
| 2158 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("bar")) // Blob 4 |
| 2159 |
generateManifest(descs[0], descs[1:5]...) // Blob 5 |
| 2160 |
|
| 2161 |
ctx := context.Background() |
| 2162 |
for i := range blobs { |
| 2163 |
if blobs[i] == nil { |
| 2164 |
continue |
| 2165 |
} |
| 2166 |
err := src.Push(ctx, descs[i], bytes.NewReader(blobs[i])) |
| 2167 |
if err != nil { |
| 2168 |
t.Fatalf("failed to push test content to src: %d: %v", i, err) |
| 2169 |
} |
| 2170 |
} |
| 2171 |
|
| 2172 |
// test copy |
| 2173 |
srcTracker := &storageTracker{Storage: src} |
| 2174 |
dstTracker := &storageTracker{Storage: dst} |
| 2175 |
root := descs[len(descs)-1] |
| 2176 |
if err := oras.CopyGraph(ctx, srcTracker, dstTracker, root, oras.CopyGraphOptions{ |
| 2177 |
Concurrency: 1, |
| 2178 |
}); err != nil { |
| 2179 |
t.Fatalf("CopyGraph() error = %v, wantErr %v", err, false) |
| 2180 |
} |
| 2181 |
|
| 2182 |
// verify contents |
| 2183 |
contents := dst.Map() |
| 2184 |
if got, want := len(contents), len(blobs)-1; got != want { |
| 2185 |
t.Errorf("len(dst) = %v, wantErr %v", got, want) |
| 2186 |
} |
| 2187 |
for i := range blobs { |
| 2188 |
if blobs[i] == nil { |
| 2189 |
continue |
| 2190 |
} |
| 2191 |
got, err := content.FetchAll(ctx, dst, descs[i]) |
| 2192 |
if err != nil { |
| 2193 |
t.Errorf("content[%d] error = %v, wantErr %v", i, err, false) |
| 2194 |
continue |
| 2195 |
} |
| 2196 |
if want := blobs[i]; !bytes.Equal(got, want) { |
| 2197 |
t.Errorf("content[%d] = %v, want %v", i, got, want) |
| 2198 |
} |
| 2199 |
} |
| 2200 |
|
| 2201 |
// verify API counts |
| 2202 |
if got, want := srcTracker.fetch, int64(len(blobs)-1); got != want { |
| 2203 |
t.Errorf("count(src.Fetch()) = %v, want %v", got, want) |
| 2204 |
} |
| 2205 |
if got, want := srcTracker.push, int64(0); got != want { |
| 2206 |
t.Errorf("count(src.Push()) = %v, want %v", got, want) |
| 2207 |
} |
| 2208 |
if got, want := srcTracker.exists, int64(0); got != want { |
| 2209 |
t.Errorf("count(src.Exists()) = %v, want %v", got, want) |
| 2210 |
} |
| 2211 |
if got, want := dstTracker.fetch, int64(0); got != want { |
| 2212 |
t.Errorf("count(dst.Fetch()) = %v, want %v", got, want) |
| 2213 |
} |
| 2214 |
if got, want := dstTracker.push, int64(len(blobs)-1); got != want { |
| 2215 |
t.Errorf("count(dst.Push()) = %v, want %v", got, want) |
| 2216 |
} |
| 2217 |
if got, want := dstTracker.exists, int64(len(blobs)-1); got != want { |
| 2218 |
t.Errorf("count(dst.Exists()) = %v, want %v", got, want) |
| 2219 |
} |
| 2220 |
} |
| 2221 |
|
| 2222 |
func TestCopy_ReferencePusher(t *testing.T) { |
| 2223 |
ctx := context.Background() |
| 2224 |
src := memory.New() |
| 2225 |
dst := &mockReferencePusher{Target: memory.New()} |
| 2226 |
|
| 2227 |
// generate test content |
| 2228 |
// generate test content |
| 2229 |
var blobs [][]byte |
| 2230 |
var descs []ocispec.Descriptor |
| 2231 |
appendBlob := func(mediaType string, blob []byte) { |
| 2232 |
blobs = append(blobs, blob) |
| 2233 |
descs = append(descs, ocispec.Descriptor{ |
| 2234 |
MediaType: mediaType, |
| 2235 |
Digest: digest.FromBytes(blob), |
| 2236 |
Size: int64(len(blob)), |
| 2237 |
}) |
| 2238 |
} |
| 2239 |
generateManifest := func(config ocispec.Descriptor, layers ...ocispec.Descriptor) { |
| 2240 |
manifest := ocispec.Manifest{ |
| 2241 |
Config: config, |
| 2242 |
Layers: layers, |
| 2243 |
} |
| 2244 |
manifestJSON, err := json.Marshal(manifest) |
| 2245 |
if err != nil { |
| 2246 |
t.Fatal(err) |
| 2247 |
} |
| 2248 |
appendBlob(ocispec.MediaTypeImageManifest, manifestJSON) |
| 2249 |
} |
| 2250 |
|
| 2251 |
appendBlob(ocispec.MediaTypeImageConfig, []byte("config")) // Blob 0 |
| 2252 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("foo")) // Blob 1 |
| 2253 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("bar")) // Blob 2 |
| 2254 |
generateManifest(descs[0], descs[1:3]...) // Blob 3 |
| 2255 |
|
| 2256 |
for i := range blobs { |
| 2257 |
err := src.Push(ctx, descs[i], bytes.NewReader(blobs[i])) |
| 2258 |
if err != nil { |
| 2259 |
t.Fatalf("failed to push test content to src: %d: %v", i, err) |
| 2260 |
} |
| 2261 |
} |
| 2262 |
|
| 2263 |
root := descs[len(descs)-1] |
| 2264 |
tag := "latest" |
| 2265 |
if err := src.Tag(ctx, root, tag); err != nil { |
| 2266 |
t.Fatalf("failed to tag manifest: %v", err) |
| 2267 |
} |
| 2268 |
|
| 2269 |
// test copying to a reference pusher |
| 2270 |
var preCopyCount int64 |
| 2271 |
var postCopyCount int64 |
| 2272 |
opts := oras.CopyOptions{ |
| 2273 |
CopyGraphOptions: oras.CopyGraphOptions{ |
| 2274 |
PreCopy: func(ctx context.Context, desc ocispec.Descriptor) error { |
| 2275 |
atomic.AddInt64(&preCopyCount, 1) |
| 2276 |
return nil |
| 2277 |
}, |
| 2278 |
PostCopy: func(ctx context.Context, desc ocispec.Descriptor) error { |
| 2279 |
atomic.AddInt64(&postCopyCount, 1) |
| 2280 |
return nil |
| 2281 |
}, |
| 2282 |
}, |
| 2283 |
} |
| 2284 |
gotManifestDesc, err := oras.Copy(ctx, src, tag, dst, tag, opts) |
| 2285 |
if err != nil { |
| 2286 |
t.Fatalf("Copy() error = %v, wantErr %v", err, false) |
| 2287 |
} |
| 2288 |
if !reflect.DeepEqual(gotManifestDesc, root) { |
| 2289 |
t.Errorf("Copy() = %v, want %v", gotManifestDesc, root) |
| 2290 |
} |
| 2291 |
|
| 2292 |
// verify contents |
| 2293 |
for i, desc := range descs { |
| 2294 |
exists, err := dst.Exists(ctx, desc) |
| 2295 |
if err != nil { |
| 2296 |
t.Fatalf("dst.Exists(%d) error = %v", i, err) |
| 2297 |
} |
| 2298 |
if !exists { |
| 2299 |
t.Errorf("dst.Exists(%d) = %v, want %v", i, exists, true) |
| 2300 |
} |
| 2301 |
} |
| 2302 |
|
| 2303 |
// verify tag |
| 2304 |
gotDesc, err := dst.Resolve(ctx, tag) |
| 2305 |
if err != nil { |
| 2306 |
t.Fatal("dst.Resolve() error =", err) |
| 2307 |
} |
| 2308 |
if !reflect.DeepEqual(gotDesc, root) { |
| 2309 |
t.Errorf("dst.Resolve() = %v, want %v", gotDesc, root) |
| 2310 |
} |
| 2311 |
|
| 2312 |
// verify API counts |
| 2313 |
if got, want := preCopyCount, int64(4); got != want { |
| 2314 |
t.Errorf("count(PreCopy()) = %v, want %v", got, want) |
| 2315 |
} |
| 2316 |
if got, want := postCopyCount, int64(4); got != want { |
| 2317 |
t.Errorf("count(PostCopy()) = %v, want %v", got, want) |
| 2318 |
} |
| 2319 |
} |
| 2320 |
|
| 2321 |
func TestCopy_CopyError(t *testing.T) { |
| 2322 |
t.Run("src target is nil", func(t *testing.T) { |
| 2323 |
ctx := context.Background() |
| 2324 |
dst := memory.New() |
| 2325 |
_, err := oras.Copy(ctx, nil, "", dst, "", oras.DefaultCopyOptions) |
| 2326 |
if err == nil { |
| 2327 |
t.Fatalf("Copy() error = %v, wantErr %v", err, true) |
| 2328 |
} |
| 2329 |
copyErr, ok := err.(*oras.CopyError) |
| 2330 |
if !ok { |
| 2331 |
t.Fatalf("Copy() error is not a CopyError: %v", err) |
| 2332 |
} |
| 2333 |
if want := oras.CopyErrorOriginSource; copyErr.Origin != want { |
| 2334 |
t.Fatalf("CopyError origin = %v, want %v", copyErr.Origin, want) |
| 2335 |
} |
| 2336 |
}) |
| 2337 |
|
| 2338 |
t.Run("dst target is nil", func(t *testing.T) { |
| 2339 |
ctx := context.Background() |
| 2340 |
src := memory.New() |
| 2341 |
_, err := oras.Copy(ctx, src, "", nil, "", oras.DefaultCopyOptions) |
| 2342 |
if err == nil { |
| 2343 |
t.Errorf("Copy() error = %v, wantErr %v", err, true) |
| 2344 |
} |
| 2345 |
copyErr, ok := err.(*oras.CopyError) |
| 2346 |
if !ok { |
| 2347 |
t.Fatalf("Copy() error is not a CopyError: %v", err) |
| 2348 |
} |
| 2349 |
if want := oras.CopyErrorOriginDestination; copyErr.Origin != want { |
| 2350 |
t.Fatalf("CopyError origin = %v, want %v", copyErr.Origin, want) |
| 2351 |
} |
| 2352 |
}) |
| 2353 |
|
| 2354 |
t.Run("failed to resolve reference", func(t *testing.T) { |
| 2355 |
ctx := context.Background() |
| 2356 |
src := memory.New() |
| 2357 |
dst := memory.New() |
| 2358 |
_, err := oras.Copy(ctx, src, "whatever", dst, "", oras.DefaultCopyOptions) |
| 2359 |
if err == nil { |
| 2360 |
t.Errorf("Copy() error = %v, wantErr %v", err, true) |
| 2361 |
} |
| 2362 |
copyErr, ok := err.(*oras.CopyError) |
| 2363 |
if !ok { |
| 2364 |
t.Fatalf("Copy() error is not a CopyError: %v", err) |
| 2365 |
} |
| 2366 |
if want := oras.CopyErrorOriginSource; copyErr.Origin != want { |
| 2367 |
t.Fatalf("CopyError origin = %v, want %v", copyErr.Origin, want) |
| 2368 |
} |
| 2369 |
}) |
| 2370 |
|
| 2371 |
t.Run("tag error", func(t *testing.T) { |
| 2372 |
ctx := context.Background() |
| 2373 |
src := memory.New() |
| 2374 |
dst := &badTagger{ |
| 2375 |
Target: memory.New(), |
| 2376 |
} |
| 2377 |
srcRef := "test" |
| 2378 |
|
| 2379 |
// prepare test content |
| 2380 |
manifestDesc, err := oras.PackManifest(ctx, src, oras.PackManifestVersion1_1, "application/test", oras.PackManifestOptions{}) |
| 2381 |
if err != nil { |
| 2382 |
t.Fatalf("failed to pack test content: %v", err) |
| 2383 |
} |
| 2384 |
if err := src.Tag(ctx, manifestDesc, srcRef); err != nil { |
| 2385 |
t.Fatalf("failed to tag test content on src: %v", err) |
| 2386 |
} |
| 2387 |
|
| 2388 |
// test copy |
| 2389 |
_, err = oras.Copy(ctx, src, srcRef, dst, "", oras.DefaultCopyOptions) |
| 2390 |
if err == nil { |
| 2391 |
t.Errorf("Copy() error = %v, wantErr %v", err, true) |
| 2392 |
} |
| 2393 |
copyErr, ok := err.(*oras.CopyError) |
| 2394 |
if !ok { |
| 2395 |
t.Fatalf("Copy() error is not a CopyError: %v", err) |
| 2396 |
} |
| 2397 |
if want := oras.CopyErrorOriginDestination; copyErr.Origin != want { |
| 2398 |
t.Errorf("CopyError origin = %v, want %v", copyErr.Origin, want) |
| 2399 |
} |
| 2400 |
if wantErr := errTag; !errors.Is(copyErr.Err, wantErr) { |
| 2401 |
t.Errorf("CopyError error = %v, wantErr %v", copyErr.Err, wantErr) |
| 2402 |
} |
| 2403 |
}) |
| 2404 |
|
| 2405 |
} |
| 2406 |
|
| 2407 |
func TestCopyGraph_CopyError(t *testing.T) { |
| 2408 |
t.Run("src target is nil", func(t *testing.T) { |
| 2409 |
ctx := context.Background() |
| 2410 |
dst := memory.New() |
| 2411 |
err := oras.CopyGraph(ctx, nil, dst, ocispec.Descriptor{}, oras.DefaultCopyGraphOptions) |
| 2412 |
if err == nil { |
| 2413 |
t.Fatalf("CopyGraph() error = %v, wantErr %v", err, true) |
| 2414 |
} |
| 2415 |
copyErr, ok := err.(*oras.CopyError) |
| 2416 |
if !ok { |
| 2417 |
t.Fatalf("CopyGraph() error is not a CopyError: %v", err) |
| 2418 |
} |
| 2419 |
if want := oras.CopyErrorOriginSource; copyErr.Origin != want { |
| 2420 |
t.Fatalf("CopyError origin = %v, want %v", copyErr.Origin, want) |
| 2421 |
} |
| 2422 |
}) |
| 2423 |
|
| 2424 |
t.Run("dst target is nil", func(t *testing.T) { |
| 2425 |
ctx := context.Background() |
| 2426 |
src := memory.New() |
| 2427 |
err := oras.CopyGraph(ctx, src, nil, ocispec.Descriptor{}, oras.DefaultCopyGraphOptions) |
| 2428 |
if err == nil { |
| 2429 |
t.Fatalf("CopyGraph() error = %v, wantErr %v", err, true) |
| 2430 |
} |
| 2431 |
copyErr, ok := err.(*oras.CopyError) |
| 2432 |
if !ok { |
| 2433 |
t.Fatalf("CopyGraph() error is not a CopyError: %v", err) |
| 2434 |
} |
| 2435 |
if want := oras.CopyErrorOriginDestination; copyErr.Origin != want { |
| 2436 |
t.Fatalf("CopyError origin = %v, want %v", copyErr.Origin, want) |
| 2437 |
} |
| 2438 |
}) |
| 2439 |
|
| 2440 |
t.Run("exists error", func(t *testing.T) { |
| 2441 |
ctx := context.Background() |
| 2442 |
src := memory.New() |
| 2443 |
dst := &badExister{ |
| 2444 |
memory.New(), |
| 2445 |
} |
| 2446 |
err := oras.CopyGraph(ctx, src, dst, ocispec.Descriptor{}, oras.DefaultCopyGraphOptions) |
| 2447 |
if err == nil { |
| 2448 |
t.Errorf("CopyGraph() error = %v, wantErr %v", err, true) |
| 2449 |
} |
| 2450 |
copyErr, ok := err.(*oras.CopyError) |
| 2451 |
if !ok { |
| 2452 |
t.Fatalf("CopyGraph() error is not a CopyError: %v", err) |
| 2453 |
} |
| 2454 |
if want := oras.CopyErrorOriginDestination; copyErr.Origin != want { |
| 2455 |
t.Errorf("CopyError origin = %v, want %v", copyErr.Origin, want) |
| 2456 |
} |
| 2457 |
if wantErr := errExists; !errors.Is(copyErr.Err, wantErr) { |
| 2458 |
t.Errorf("CopyGraph() error = %v, wantErr %v", copyErr.Err, wantErr) |
| 2459 |
} |
| 2460 |
}) |
| 2461 |
|
| 2462 |
t.Run("fetch error", func(t *testing.T) { |
| 2463 |
ctx := context.Background() |
| 2464 |
src := &badFetcher{ |
| 2465 |
memory.New(), |
| 2466 |
} |
| 2467 |
dst := memory.New() |
| 2468 |
err := oras.CopyGraph(ctx, src, dst, ocispec.Descriptor{}, oras.DefaultCopyGraphOptions) |
| 2469 |
if err == nil { |
| 2470 |
t.Errorf("CopyGraph() error = %v, wantErr %v", err, true) |
| 2471 |
} |
| 2472 |
copyErr, ok := err.(*oras.CopyError) |
| 2473 |
if !ok { |
| 2474 |
t.Fatalf("CopyGraph() error is not a CopyError: %v", err) |
| 2475 |
} |
| 2476 |
if want := oras.CopyErrorOriginSource; copyErr.Origin != want { |
| 2477 |
t.Errorf("CopyError origin = %v, want %v", copyErr.Origin, want) |
| 2478 |
} |
| 2479 |
if wantErr := errFetch; !errors.Is(copyErr.Err, wantErr) { |
| 2480 |
t.Errorf("CopyGraph() error = %v, wantErr %v", copyErr.Err, wantErr) |
| 2481 |
} |
| 2482 |
}) |
| 2483 |
|
| 2484 |
t.Run("push error", func(t *testing.T) { |
| 2485 |
ctx := context.Background() |
| 2486 |
src := memory.New() |
| 2487 |
dst := &badPusher{ |
| 2488 |
memory.New(), |
| 2489 |
} |
| 2490 |
|
| 2491 |
// prepare test content |
| 2492 |
manifestDesc, err := oras.PackManifest(ctx, src, oras.PackManifestVersion1_1, "application/test", oras.PackManifestOptions{}) |
| 2493 |
if err != nil { |
| 2494 |
t.Fatalf("failed to pack test content: %v", err) |
| 2495 |
} |
| 2496 |
|
| 2497 |
err = oras.CopyGraph(ctx, src, dst, manifestDesc, oras.DefaultCopyGraphOptions) |
| 2498 |
if err == nil { |
| 2499 |
t.Errorf("CopyGraph() error = %v, wantErr %v", err, true) |
| 2500 |
} |
| 2501 |
copyErr, ok := err.(*oras.CopyError) |
| 2502 |
if !ok { |
| 2503 |
t.Fatalf("CopyGraph() error is not a CopyError: %v", err) |
| 2504 |
} |
| 2505 |
if want := oras.CopyErrorOriginDestination; copyErr.Origin != want { |
| 2506 |
t.Errorf("CopyError origin = %v, want %v", copyErr.Origin, want) |
| 2507 |
} |
| 2508 |
if wantErr := errPush; !errors.Is(copyErr.Err, wantErr) { |
| 2509 |
t.Errorf("CopyGraph() error = %v, wantErr %v", copyErr.Err, wantErr) |
| 2510 |
} |
| 2511 |
}) |
| 2512 |
|
| 2513 |
t.Run("mount fetch error", func(t *testing.T) { |
| 2514 |
ctx := context.Background() |
| 2515 |
src := &badFetcher{ |
| 2516 |
memory.New(), |
| 2517 |
} |
| 2518 |
dst := &testMounter{ |
| 2519 |
memory.New(), |
| 2520 |
} |
| 2521 |
|
| 2522 |
opts := oras.CopyGraphOptions{ |
| 2523 |
MountFrom: func(ctx context.Context, desc ocispec.Descriptor) ([]string, error) { |
| 2524 |
return []string{"source"}, nil |
| 2525 |
}, |
| 2526 |
} |
| 2527 |
err := oras.CopyGraph(ctx, src, dst, ocispec.Descriptor{}, opts) |
| 2528 |
if err == nil { |
| 2529 |
t.Errorf("CopyGraph() error = %v, wantErr %v", err, true) |
| 2530 |
} |
| 2531 |
copyErr, ok := err.(*oras.CopyError) |
| 2532 |
if !ok { |
| 2533 |
t.Fatalf("CopyGraph() error is not a CopyError: %v", err) |
| 2534 |
} |
| 2535 |
if want := oras.CopyErrorOriginDestination; copyErr.Origin != want { |
| 2536 |
t.Errorf("CopyError origin = %v, want %v", copyErr.Origin, want) |
| 2537 |
} |
| 2538 |
if wantErr := errFetch; !errors.Is(copyErr.Err, wantErr) { |
| 2539 |
t.Errorf("CopyGraph() error = %v, wantErr %v", copyErr.Err, wantErr) |
| 2540 |
} |
| 2541 |
}) |
| 2542 |
|
| 2543 |
t.Run("mount error", func(t *testing.T) { |
| 2544 |
ctx := context.Background() |
| 2545 |
src := memory.New() |
| 2546 |
dst := &badMounter{ |
| 2547 |
memory.New(), |
| 2548 |
} |
| 2549 |
|
| 2550 |
// prepare test content |
| 2551 |
manifestDesc, err := oras.PackManifest(ctx, src, oras.PackManifestVersion1_1, "application/test", oras.PackManifestOptions{}) |
| 2552 |
if err != nil { |
| 2553 |
t.Fatalf("failed to pack test content: %v", err) |
| 2554 |
} |
| 2555 |
|
| 2556 |
// test copy graph |
| 2557 |
opts := oras.CopyGraphOptions{ |
| 2558 |
MountFrom: func(ctx context.Context, desc ocispec.Descriptor) ([]string, error) { |
| 2559 |
return []string{"source"}, nil |
| 2560 |
}, |
| 2561 |
} |
| 2562 |
err = oras.CopyGraph(ctx, src, dst, manifestDesc, opts) |
| 2563 |
if err == nil { |
| 2564 |
t.Errorf("CopyGraph() error = %v, wantErr %v", err, true) |
| 2565 |
} |
| 2566 |
copyErr, ok := err.(*oras.CopyError) |
| 2567 |
if !ok { |
| 2568 |
t.Fatalf("CopyGraph() error is not a CopyError: %v", err) |
| 2569 |
} |
| 2570 |
if want := oras.CopyErrorOriginDestination; copyErr.Origin != want { |
| 2571 |
t.Errorf("CopyError origin = %v, want %v", copyErr.Origin, want) |
| 2572 |
} |
| 2573 |
if wantErr := errMount; !errors.Is(copyErr.Err, wantErr) { |
| 2574 |
t.Errorf("CopyGraph() error = %v, wantErr %v", copyErr.Err, wantErr) |
| 2575 |
} |
| 2576 |
}) |
| 2577 |
} |
| 2578 |
|
| 2579 |
var ( |
| 2580 |
errExists = errors.New("exists error") |
| 2581 |
errPush = errors.New("push error") |
| 2582 |
errFetch = errors.New("fetch error") |
| 2583 |
errTag = errors.New("tag error") |
| 2584 |
errMount = errors.New("mount error") |
| 2585 |
) |
| 2586 |
|
| 2587 |
type badExister struct { |
| 2588 |
content.Storage |
| 2589 |
} |
| 2590 |
|
| 2591 |
func (be *badExister) Exists(_ context.Context, _ ocispec.Descriptor) (bool, error) { |
| 2592 |
return false, errExists |
| 2593 |
} |
| 2594 |
|
| 2595 |
type badFetcher struct { |
| 2596 |
content.Storage |
| 2597 |
} |
| 2598 |
|
| 2599 |
func (bf *badFetcher) Fetch(_ context.Context, _ ocispec.Descriptor) (io.ReadCloser, error) { |
| 2600 |
return nil, errFetch |
| 2601 |
} |
| 2602 |
|
| 2603 |
type badPusher struct { |
| 2604 |
content.Storage |
| 2605 |
} |
| 2606 |
|
| 2607 |
func (bp *badPusher) Push(_ context.Context, _ ocispec.Descriptor, _ io.Reader) error { |
| 2608 |
return errPush |
| 2609 |
} |
| 2610 |
|
| 2611 |
type badTagger struct { |
| 2612 |
oras.Target |
| 2613 |
} |
| 2614 |
|
| 2615 |
func (bt *badTagger) Tag(_ context.Context, _ ocispec.Descriptor, _ string) error { |
| 2616 |
return errTag |
| 2617 |
} |
| 2618 |
|
| 2619 |
type testMounter struct { |
| 2620 |
oras.Target |
| 2621 |
} |
| 2622 |
|
| 2623 |
func (tm *testMounter) Mount(_ context.Context, _ ocispec.Descriptor, _ string, getContent func() (io.ReadCloser, error)) error { |
| 2624 |
_, err := getContent() |
| 2625 |
if err != nil { |
| 2626 |
return err |
| 2627 |
} |
| 2628 |
return nil |
| 2629 |
} |
| 2630 |
|
| 2631 |
type badMounter struct { |
| 2632 |
oras.Target |
| 2633 |
} |
| 2634 |
|
| 2635 |
func (bm *badMounter) Mount(_ context.Context, _ ocispec.Descriptor, _ string, _ func() (io.ReadCloser, error)) error { |
| 2636 |
return errMount |
| 2637 |
} |