| 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 file |
| 17 |
|
| 18 |
import ( |
| 19 |
"bytes" |
| 20 |
"context" |
| 21 |
"crypto/sha1" |
| 22 |
_ "crypto/sha256" |
| 23 |
"encoding/json" |
| 24 |
"errors" |
| 25 |
"fmt" |
| 26 |
"io" |
| 27 |
"os" |
| 28 |
"path/filepath" |
| 29 |
"reflect" |
| 30 |
"strings" |
| 31 |
"sync/atomic" |
| 32 |
"testing" |
| 33 |
|
| 34 |
"github.com/opencontainers/go-digest" |
| 35 |
ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 36 |
"golang.org/x/sync/errgroup" |
| 37 |
"oras.land/oras-go/v2" |
| 38 |
"oras.land/oras-go/v2/content" |
| 39 |
"oras.land/oras-go/v2/content/memory" |
| 40 |
"oras.land/oras-go/v2/errdef" |
| 41 |
"oras.land/oras-go/v2/internal/cas" |
| 42 |
"oras.land/oras-go/v2/internal/descriptor" |
| 43 |
"oras.land/oras-go/v2/internal/spec" |
| 44 |
) |
| 45 |
|
| 46 |
// storageTracker tracks storage API counts. |
| 47 |
type storageTracker struct { |
| 48 |
content.Storage |
| 49 |
fetch int64 |
| 50 |
push int64 |
| 51 |
exists int64 |
| 52 |
} |
| 53 |
|
| 54 |
func (t *storageTracker) Fetch(ctx context.Context, target ocispec.Descriptor) (io.ReadCloser, error) { |
| 55 |
atomic.AddInt64(&t.fetch, 1) |
| 56 |
return t.Storage.Fetch(ctx, target) |
| 57 |
} |
| 58 |
|
| 59 |
func (t *storageTracker) Push(ctx context.Context, expected ocispec.Descriptor, content io.Reader) error { |
| 60 |
atomic.AddInt64(&t.push, 1) |
| 61 |
return t.Storage.Push(ctx, expected, content) |
| 62 |
} |
| 63 |
|
| 64 |
func (t *storageTracker) Exists(ctx context.Context, target ocispec.Descriptor) (bool, error) { |
| 65 |
atomic.AddInt64(&t.exists, 1) |
| 66 |
return t.Storage.Exists(ctx, target) |
| 67 |
} |
| 68 |
|
| 69 |
type storageMock struct { |
| 70 |
content.Storage |
| 71 |
|
| 72 |
OnFetch func(ctx context.Context, desc ocispec.Descriptor) error |
| 73 |
} |
| 74 |
|
| 75 |
func (m *storageMock) Fetch(ctx context.Context, desc ocispec.Descriptor) (io.ReadCloser, error) { |
| 76 |
if m.OnFetch != nil { |
| 77 |
if err := m.OnFetch(ctx, desc); err != nil { |
| 78 |
return nil, err |
| 79 |
} |
| 80 |
} |
| 81 |
return m.Storage.Fetch(ctx, desc) |
| 82 |
} |
| 83 |
|
| 84 |
func TestStoreInterface(t *testing.T) { |
| 85 |
var store any = &Store{} |
| 86 |
if _, ok := store.(oras.Target); !ok { |
| 87 |
t.Error("&Store{} does not conform oras.Target") |
| 88 |
} |
| 89 |
if _, ok := store.(content.PredecessorFinder); !ok { |
| 90 |
t.Error("&Store{} does not conform content.PredecessorFinder") |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
func TestStore_Success(t *testing.T) { |
| 95 |
tempDir := t.TempDir() |
| 96 |
s, err := New(tempDir) |
| 97 |
if err != nil { |
| 98 |
t.Fatal("Store.New() error =", err) |
| 99 |
} |
| 100 |
defer s.Close() |
| 101 |
ctx := context.Background() |
| 102 |
|
| 103 |
blob := []byte("hello world") |
| 104 |
name := "test.txt" |
| 105 |
mediaType := "test" |
| 106 |
desc := ocispec.Descriptor{ |
| 107 |
MediaType: mediaType, |
| 108 |
Digest: digest.FromBytes(blob), |
| 109 |
Size: int64(len(blob)), |
| 110 |
Annotations: map[string]string{ |
| 111 |
ocispec.AnnotationTitle: name, |
| 112 |
}, |
| 113 |
} |
| 114 |
|
| 115 |
path := filepath.Join(tempDir, name) |
| 116 |
if err := os.WriteFile(path, blob, 0444); err != nil { |
| 117 |
t.Fatal("error calling WriteFile(), error =", err) |
| 118 |
} |
| 119 |
|
| 120 |
// test blob add |
| 121 |
gotDesc, err := s.Add(ctx, name, mediaType, path) |
| 122 |
if err != nil { |
| 123 |
t.Fatal("Store.Add() error =", err) |
| 124 |
} |
| 125 |
if descriptor.FromOCI(gotDesc) != descriptor.FromOCI(desc) { |
| 126 |
t.Fatal("got descriptor mismatch") |
| 127 |
} |
| 128 |
|
| 129 |
// test blob exists |
| 130 |
exists, err := s.Exists(ctx, gotDesc) |
| 131 |
if err != nil { |
| 132 |
t.Fatal("Store.Exists() error =", err) |
| 133 |
} |
| 134 |
if !exists { |
| 135 |
t.Errorf("Store.Exists() = %v, want %v", exists, true) |
| 136 |
} |
| 137 |
|
| 138 |
// test blob fetch |
| 139 |
rc, err := s.Fetch(ctx, gotDesc) |
| 140 |
if err != nil { |
| 141 |
t.Fatal("Store.Fetch() error =", err) |
| 142 |
} |
| 143 |
got, err := io.ReadAll(rc) |
| 144 |
if err != nil { |
| 145 |
t.Fatal("Store.Fetch().Read() error =", err) |
| 146 |
} |
| 147 |
err = rc.Close() |
| 148 |
if err != nil { |
| 149 |
t.Error("Store.Fetch().Close() error =", err) |
| 150 |
} |
| 151 |
if !bytes.Equal(got, blob) { |
| 152 |
t.Errorf("Store.Fetch() = %v, want %v", got, blob) |
| 153 |
} |
| 154 |
|
| 155 |
// test push config |
| 156 |
config := []byte("{}") |
| 157 |
configDesc := ocispec.Descriptor{ |
| 158 |
MediaType: "config", |
| 159 |
Digest: digest.FromBytes(config), |
| 160 |
Size: int64(len(config)), |
| 161 |
Annotations: map[string]string{ |
| 162 |
ocispec.AnnotationTitle: "config.blob", |
| 163 |
}, |
| 164 |
} |
| 165 |
if err := s.Push(ctx, configDesc, bytes.NewReader(config)); err != nil { |
| 166 |
t.Fatal("Store.Push() error =", err) |
| 167 |
} |
| 168 |
|
| 169 |
// test push manifest |
| 170 |
manifest := ocispec.Manifest{ |
| 171 |
MediaType: ocispec.MediaTypeImageManifest, |
| 172 |
Config: configDesc, |
| 173 |
Layers: []ocispec.Descriptor{ |
| 174 |
gotDesc, |
| 175 |
}, |
| 176 |
} |
| 177 |
manifestJSON, err := json.Marshal(manifest) |
| 178 |
if err != nil { |
| 179 |
t.Fatal("json.Marshal() error =", err) |
| 180 |
} |
| 181 |
manifestDesc := ocispec.Descriptor{ |
| 182 |
MediaType: ocispec.MediaTypeImageManifest, |
| 183 |
Digest: digest.FromBytes(manifestJSON), |
| 184 |
Size: int64(len(manifestJSON)), |
| 185 |
} |
| 186 |
if err := s.Push(ctx, manifestDesc, bytes.NewReader(manifestJSON)); err != nil { |
| 187 |
t.Fatal("Store.Push() error =", err) |
| 188 |
} |
| 189 |
|
| 190 |
// test tag |
| 191 |
ref := "foobar" |
| 192 |
if err := s.Tag(ctx, manifestDesc, ref); err != nil { |
| 193 |
t.Fatal("Store.Tag() error =", err) |
| 194 |
} |
| 195 |
|
| 196 |
// test resolve |
| 197 |
gotManifestDesc, err := s.Resolve(ctx, ref) |
| 198 |
if err != nil { |
| 199 |
t.Fatal("Store.Resolve() error =", err) |
| 200 |
} |
| 201 |
if !reflect.DeepEqual(gotManifestDesc, manifestDesc) { |
| 202 |
t.Errorf("Store.Resolve() = %v, want %v", gotManifestDesc, manifestDesc) |
| 203 |
} |
| 204 |
|
| 205 |
// test fetch |
| 206 |
exists, err = s.Exists(ctx, gotManifestDesc) |
| 207 |
if err != nil { |
| 208 |
t.Fatal("Store.Exists() error =", err) |
| 209 |
} |
| 210 |
if !exists { |
| 211 |
t.Errorf("Store.Exists() = %v, want %v", exists, true) |
| 212 |
} |
| 213 |
|
| 214 |
mrc, err := s.Fetch(ctx, gotManifestDesc) |
| 215 |
if err != nil { |
| 216 |
t.Fatal("Store.Fetch() error =", err) |
| 217 |
} |
| 218 |
got, err = io.ReadAll(mrc) |
| 219 |
if err != nil { |
| 220 |
t.Fatal("Store.Fetch().Read() error =", err) |
| 221 |
} |
| 222 |
err = mrc.Close() |
| 223 |
if err != nil { |
| 224 |
t.Error("Store.Fetch().Close() error =", err) |
| 225 |
} |
| 226 |
if !bytes.Equal(got, manifestJSON) { |
| 227 |
t.Errorf("Store.Fetch() = %v, want %v", got, manifestJSON) |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
func TestStore_RelativeRoot_Success(t *testing.T) { |
| 232 |
tempDir, err := filepath.EvalSymlinks(t.TempDir()) |
| 233 |
if err != nil { |
| 234 |
t.Fatal("error calling filepath.EvalSymlinks(), error =", err) |
| 235 |
} |
| 236 |
currDir, err := os.Getwd() |
| 237 |
if err != nil { |
| 238 |
t.Fatal("error calling Getwd(), error=", err) |
| 239 |
} |
| 240 |
t.Chdir(tempDir) |
| 241 |
s, err := New(".") |
| 242 |
if err != nil { |
| 243 |
t.Fatal("Store.New() error =", err) |
| 244 |
} |
| 245 |
defer s.Close() |
| 246 |
if want := tempDir; s.workingDir != want { |
| 247 |
t.Errorf("Store.workingDir = %s, want %s", s.workingDir, want) |
| 248 |
} |
| 249 |
// cd back to allow the temp directory to be removed |
| 250 |
t.Chdir(currDir) |
| 251 |
ctx := context.Background() |
| 252 |
|
| 253 |
blob := []byte("hello world") |
| 254 |
name := "test.txt" |
| 255 |
mediaType := "test" |
| 256 |
desc := ocispec.Descriptor{ |
| 257 |
MediaType: mediaType, |
| 258 |
Digest: digest.FromBytes(blob), |
| 259 |
Size: int64(len(blob)), |
| 260 |
Annotations: map[string]string{ |
| 261 |
ocispec.AnnotationTitle: name, |
| 262 |
}, |
| 263 |
} |
| 264 |
|
| 265 |
path := filepath.Join(tempDir, name) |
| 266 |
if err := os.WriteFile(path, blob, 0444); err != nil { |
| 267 |
t.Fatal("error calling WriteFile(), error =", err) |
| 268 |
} |
| 269 |
|
| 270 |
// test blob add |
| 271 |
gotDesc, err := s.Add(ctx, name, mediaType, path) |
| 272 |
if err != nil { |
| 273 |
t.Fatal("Store.Add() error =", err) |
| 274 |
} |
| 275 |
if descriptor.FromOCI(gotDesc) != descriptor.FromOCI(desc) { |
| 276 |
t.Fatal("got descriptor mismatch") |
| 277 |
} |
| 278 |
|
| 279 |
// test blob exists |
| 280 |
exists, err := s.Exists(ctx, gotDesc) |
| 281 |
if err != nil { |
| 282 |
t.Fatal("Store.Exists() error =", err) |
| 283 |
} |
| 284 |
if !exists { |
| 285 |
t.Errorf("Store.Exists() = %v, want %v", exists, true) |
| 286 |
} |
| 287 |
|
| 288 |
// test blob fetch |
| 289 |
rc, err := s.Fetch(ctx, gotDesc) |
| 290 |
if err != nil { |
| 291 |
t.Fatal("Store.Fetch() error =", err) |
| 292 |
} |
| 293 |
got, err := io.ReadAll(rc) |
| 294 |
if err != nil { |
| 295 |
t.Fatal("Store.Fetch().Read() error =", err) |
| 296 |
} |
| 297 |
err = rc.Close() |
| 298 |
if err != nil { |
| 299 |
t.Error("Store.Fetch().Close() error =", err) |
| 300 |
} |
| 301 |
if !bytes.Equal(got, blob) { |
| 302 |
t.Errorf("Store.Fetch() = %v, want %v", got, blob) |
| 303 |
} |
| 304 |
|
| 305 |
// test push config |
| 306 |
config := []byte("{}") |
| 307 |
configDesc := ocispec.Descriptor{ |
| 308 |
MediaType: "config", |
| 309 |
Digest: digest.FromBytes(config), |
| 310 |
Size: int64(len(config)), |
| 311 |
Annotations: map[string]string{ |
| 312 |
ocispec.AnnotationTitle: "config.blob", |
| 313 |
}, |
| 314 |
} |
| 315 |
if err := s.Push(ctx, configDesc, bytes.NewReader(config)); err != nil { |
| 316 |
t.Fatal("Store.Push() error =", err) |
| 317 |
} |
| 318 |
|
| 319 |
// test push manifest |
| 320 |
manifest := ocispec.Manifest{ |
| 321 |
MediaType: ocispec.MediaTypeImageManifest, |
| 322 |
Config: configDesc, |
| 323 |
Layers: []ocispec.Descriptor{ |
| 324 |
gotDesc, |
| 325 |
}, |
| 326 |
} |
| 327 |
manifestJSON, err := json.Marshal(manifest) |
| 328 |
if err != nil { |
| 329 |
t.Fatal("json.Marshal() error =", err) |
| 330 |
} |
| 331 |
manifestDesc := ocispec.Descriptor{ |
| 332 |
MediaType: ocispec.MediaTypeImageManifest, |
| 333 |
Digest: digest.FromBytes(manifestJSON), |
| 334 |
Size: int64(len(manifestJSON)), |
| 335 |
} |
| 336 |
if err := s.Push(ctx, manifestDesc, bytes.NewReader(manifestJSON)); err != nil { |
| 337 |
t.Fatal("Store.Push() error =", err) |
| 338 |
} |
| 339 |
|
| 340 |
// test tag |
| 341 |
ref := "foobar" |
| 342 |
if err := s.Tag(ctx, manifestDesc, ref); err != nil { |
| 343 |
t.Fatal("Store.Tag() error =", err) |
| 344 |
} |
| 345 |
|
| 346 |
// test resolve |
| 347 |
gotManifestDesc, err := s.Resolve(ctx, ref) |
| 348 |
if err != nil { |
| 349 |
t.Fatal("Store.Resolve() error =", err) |
| 350 |
} |
| 351 |
if !reflect.DeepEqual(gotManifestDesc, manifestDesc) { |
| 352 |
t.Errorf("Store.Resolve() = %v, want %v", gotManifestDesc, manifestDesc) |
| 353 |
} |
| 354 |
|
| 355 |
// test fetch |
| 356 |
exists, err = s.Exists(ctx, gotManifestDesc) |
| 357 |
if err != nil { |
| 358 |
t.Fatal("Store.Exists() error =", err) |
| 359 |
} |
| 360 |
if !exists { |
| 361 |
t.Errorf("Store.Exists() = %v, want %v", exists, true) |
| 362 |
} |
| 363 |
|
| 364 |
mrc, err := s.Fetch(ctx, gotManifestDesc) |
| 365 |
if err != nil { |
| 366 |
t.Fatal("Store.Fetch() error =", err) |
| 367 |
} |
| 368 |
got, err = io.ReadAll(mrc) |
| 369 |
if err != nil { |
| 370 |
t.Fatal("Store.Fetch().Read() error =", err) |
| 371 |
} |
| 372 |
err = mrc.Close() |
| 373 |
if err != nil { |
| 374 |
t.Error("Store.Fetch().Close() error =", err) |
| 375 |
} |
| 376 |
if !bytes.Equal(got, manifestJSON) { |
| 377 |
t.Errorf("Store.Fetch() = %v, want %v", got, manifestJSON) |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
func TestStore_Close(t *testing.T) { |
| 382 |
content := []byte("hello world") |
| 383 |
name := "test.txt" |
| 384 |
mediaType := "test" |
| 385 |
desc := ocispec.Descriptor{ |
| 386 |
MediaType: mediaType, |
| 387 |
Digest: digest.FromBytes(content), |
| 388 |
Size: int64(len(content)), |
| 389 |
Annotations: map[string]string{ |
| 390 |
ocispec.AnnotationTitle: name, |
| 391 |
}, |
| 392 |
} |
| 393 |
ref := "foobar" |
| 394 |
|
| 395 |
tempDir := t.TempDir() |
| 396 |
s, err := New(tempDir) |
| 397 |
if err != nil { |
| 398 |
t.Fatal("Store.New() error =", err) |
| 399 |
} |
| 400 |
ctx := context.Background() |
| 401 |
|
| 402 |
// test push |
| 403 |
err = s.Push(ctx, desc, bytes.NewReader(content)) |
| 404 |
if err != nil { |
| 405 |
t.Fatal("Store.Push() error =", err) |
| 406 |
} |
| 407 |
|
| 408 |
// test exists |
| 409 |
exists, err := s.Exists(ctx, desc) |
| 410 |
if err != nil { |
| 411 |
t.Fatal("Store.Exists() error =", err) |
| 412 |
} |
| 413 |
if !exists { |
| 414 |
t.Errorf("Store.Exists() = %v, want %v", exists, true) |
| 415 |
} |
| 416 |
|
| 417 |
// test fetch |
| 418 |
rc, err := s.Fetch(ctx, desc) |
| 419 |
if err != nil { |
| 420 |
t.Fatal("Store.Fetch() error =", err) |
| 421 |
} |
| 422 |
got, err := io.ReadAll(rc) |
| 423 |
if err != nil { |
| 424 |
t.Fatal("Store.Fetch().Read() error =", err) |
| 425 |
} |
| 426 |
err = rc.Close() |
| 427 |
if err != nil { |
| 428 |
t.Error("Store.Fetch().Close() error =", err) |
| 429 |
} |
| 430 |
if !bytes.Equal(got, content) { |
| 431 |
t.Errorf("Store.Fetch() = %v, want %v", got, content) |
| 432 |
} |
| 433 |
|
| 434 |
// test close |
| 435 |
if err := s.Close(); err != nil { |
| 436 |
t.Error("Store.Close() error =", err) |
| 437 |
} |
| 438 |
// test close twice |
| 439 |
if err := s.Close(); err != nil { |
| 440 |
t.Error("Store.Close() error =", err) |
| 441 |
} |
| 442 |
|
| 443 |
// test add after closed |
| 444 |
if _, err := s.Add(ctx, name, mediaType, ""); !errors.Is(err, ErrStoreClosed) { |
| 445 |
t.Errorf("Store.Add() = %v, want %v", err, ErrStoreClosed) |
| 446 |
} |
| 447 |
|
| 448 |
// test push after closed |
| 449 |
if err = s.Push(ctx, desc, bytes.NewReader(content)); !errors.Is(err, ErrStoreClosed) { |
| 450 |
t.Errorf("Store.Push() = %v, want %v", err, ErrStoreClosed) |
| 451 |
} |
| 452 |
|
| 453 |
// test exists after closed |
| 454 |
if _, err := s.Exists(ctx, desc); !errors.Is(err, ErrStoreClosed) { |
| 455 |
t.Errorf("Store.Exists() = %v, want %v", err, ErrStoreClosed) |
| 456 |
} |
| 457 |
|
| 458 |
// test tag after closed |
| 459 |
if err := s.Tag(ctx, desc, ref); !errors.Is(err, ErrStoreClosed) { |
| 460 |
t.Errorf("Store.Tag() = %v, want %v", err, ErrStoreClosed) |
| 461 |
} |
| 462 |
|
| 463 |
// test resolve after closed |
| 464 |
if _, err := s.Resolve(ctx, ref); !errors.Is(err, ErrStoreClosed) { |
| 465 |
t.Errorf("Store.Resolve() = %v, want %v", err, ErrStoreClosed) |
| 466 |
} |
| 467 |
|
| 468 |
// test fetch after closed |
| 469 |
if _, err := s.Fetch(ctx, desc); !errors.Is(err, ErrStoreClosed) { |
| 470 |
t.Errorf("Store.Fetch() = %v, want %v", err, ErrStoreClosed) |
| 471 |
} |
| 472 |
|
| 473 |
// test Predecessors after closed |
| 474 |
if _, err := s.Predecessors(ctx, desc); !errors.Is(err, ErrStoreClosed) { |
| 475 |
t.Errorf("Store.Predecessors() = %v, want %v", err, ErrStoreClosed) |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
func TestStore_File_Push(t *testing.T) { |
| 480 |
content := []byte("hello world") |
| 481 |
desc := ocispec.Descriptor{ |
| 482 |
MediaType: "test", |
| 483 |
Digest: digest.FromBytes(content), |
| 484 |
Size: int64(len(content)), |
| 485 |
Annotations: map[string]string{ |
| 486 |
ocispec.AnnotationTitle: "test.txt", |
| 487 |
}, |
| 488 |
} |
| 489 |
tempDir := t.TempDir() |
| 490 |
s, err := New(tempDir) |
| 491 |
if err != nil { |
| 492 |
t.Fatal("Store.New() error =", err) |
| 493 |
} |
| 494 |
defer s.Close() |
| 495 |
ctx := context.Background() |
| 496 |
|
| 497 |
// test push |
| 498 |
err = s.Push(ctx, desc, bytes.NewReader(content)) |
| 499 |
if err != nil { |
| 500 |
t.Fatal("Store.Push() error =", err) |
| 501 |
} |
| 502 |
|
| 503 |
// test exists |
| 504 |
exists, err := s.Exists(ctx, desc) |
| 505 |
if err != nil { |
| 506 |
t.Fatal("Store.Exists() error =", err) |
| 507 |
} |
| 508 |
if !exists { |
| 509 |
t.Errorf("Store.Exists() = %v, want %v", exists, true) |
| 510 |
} |
| 511 |
|
| 512 |
// test fetch |
| 513 |
rc, err := s.Fetch(ctx, desc) |
| 514 |
if err != nil { |
| 515 |
t.Fatal("Store.Fetch() error =", err) |
| 516 |
} |
| 517 |
got, err := io.ReadAll(rc) |
| 518 |
if err != nil { |
| 519 |
t.Fatal("Store.Fetch().Read() error =", err) |
| 520 |
} |
| 521 |
err = rc.Close() |
| 522 |
if err != nil { |
| 523 |
t.Error("Store.Fetch().Close() error =", err) |
| 524 |
} |
| 525 |
if !bytes.Equal(got, content) { |
| 526 |
t.Errorf("Store.Fetch() = %v, want %v", got, content) |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
func TestStore_Dir_Push(t *testing.T) { |
| 531 |
tempDir := t.TempDir() |
| 532 |
dirName := "testdir" |
| 533 |
dirPath := filepath.Join(tempDir, dirName) |
| 534 |
if err := os.MkdirAll(dirPath, 0777); err != nil { |
| 535 |
t.Fatal("error calling Mkdir(), error =", err) |
| 536 |
} |
| 537 |
|
| 538 |
content := []byte("hello world") |
| 539 |
fileName := "test.txt" |
| 540 |
if err := os.WriteFile(filepath.Join(dirPath, fileName), content, 0444); err != nil { |
| 541 |
t.Fatal("error calling WriteFile(), error =", err) |
| 542 |
} |
| 543 |
|
| 544 |
s, err := New(tempDir) |
| 545 |
if err != nil { |
| 546 |
t.Fatal("Store.New() error =", err) |
| 547 |
} |
| 548 |
defer s.Close() |
| 549 |
ctx := context.Background() |
| 550 |
|
| 551 |
// test add |
| 552 |
desc, err := s.Add(ctx, dirName, "", dirPath) |
| 553 |
if err != nil { |
| 554 |
t.Fatal("Store.Add() error=", err) |
| 555 |
} |
| 556 |
|
| 557 |
val, ok := s.digestToPath.Load(desc.Digest) |
| 558 |
if !ok { |
| 559 |
t.Fatal("failed to find internal gz") |
| 560 |
} |
| 561 |
tmpPath := val.(string) |
| 562 |
zrc, err := os.Open(tmpPath) |
| 563 |
if err != nil { |
| 564 |
t.Fatal("failed to open internal gz") |
| 565 |
} |
| 566 |
gz, err := io.ReadAll(zrc) |
| 567 |
if err != nil { |
| 568 |
t.Fatal("failed to read internal gz") |
| 569 |
} |
| 570 |
if err := zrc.Close(); err != nil { |
| 571 |
t.Fatal("failed to close internal gz reader") |
| 572 |
} |
| 573 |
|
| 574 |
anotherTempDir := t.TempDir() |
| 575 |
// test with another file store instance to mock push gz |
| 576 |
anotherS, err := New(anotherTempDir) |
| 577 |
if err != nil { |
| 578 |
t.Fatal("Store.New() error =", err) |
| 579 |
} |
| 580 |
defer anotherS.Close() |
| 581 |
|
| 582 |
// test push |
| 583 |
if err := anotherS.Push(ctx, desc, bytes.NewReader(gz)); err != nil { |
| 584 |
t.Fatal("Store.Push() error =", err) |
| 585 |
|
| 586 |
} |
| 587 |
|
| 588 |
// test exists |
| 589 |
exists, err := s.Exists(ctx, desc) |
| 590 |
if err != nil { |
| 591 |
t.Fatal("Store.Exists() error =", err) |
| 592 |
} |
| 593 |
if !exists { |
| 594 |
t.Errorf("Store.Exists() = %v, want %v", exists, true) |
| 595 |
} |
| 596 |
|
| 597 |
// test fetch |
| 598 |
rc, err := s.Fetch(ctx, desc) |
| 599 |
if err != nil { |
| 600 |
t.Fatal("Store.Fetch() error =", err) |
| 601 |
} |
| 602 |
got, err := io.ReadAll(rc) |
| 603 |
if err != nil { |
| 604 |
t.Fatal("Store.Fetch().Read() error =", err) |
| 605 |
} |
| 606 |
err = rc.Close() |
| 607 |
if err != nil { |
| 608 |
t.Error("Store.Fetch().Close() error =", err) |
| 609 |
} |
| 610 |
if !bytes.Equal(got, gz) { |
| 611 |
t.Errorf("Store.Fetch() = %v, want %v", got, gz) |
| 612 |
} |
| 613 |
|
| 614 |
// test file content |
| 615 |
path := filepath.Join(s.workingDir, dirName, fileName) |
| 616 |
fp, err := os.Open(path) |
| 617 |
if err != nil { |
| 618 |
t.Fatalf("failed to open file %s:%v", path, err) |
| 619 |
} |
| 620 |
fc, err := io.ReadAll(fp) |
| 621 |
if err != nil { |
| 622 |
t.Fatalf("failed to read file %s:%v", path, err) |
| 623 |
} |
| 624 |
if err := fp.Close(); err != nil { |
| 625 |
t.Fatalf("failed to close file %s:%v", path, err) |
| 626 |
} |
| 627 |
|
| 628 |
anotherFilePath := filepath.Join(anotherS.workingDir, dirName, fileName) |
| 629 |
anotherFp, err := os.Open(anotherFilePath) |
| 630 |
if err != nil { |
| 631 |
t.Fatalf("failed to open file %s:%v", anotherFilePath, err) |
| 632 |
} |
| 633 |
anotherFc, err := io.ReadAll(anotherFp) |
| 634 |
if err != nil { |
| 635 |
t.Fatalf("failed to read file %s:%v", anotherFilePath, err) |
| 636 |
} |
| 637 |
if err := anotherFp.Close(); err != nil { |
| 638 |
t.Fatalf("failed to close file %s:%v", anotherFilePath, err) |
| 639 |
} |
| 640 |
|
| 641 |
if !bytes.Equal(fc, anotherFc) { |
| 642 |
t.Errorf("file content mismatch") |
| 643 |
} |
| 644 |
} |
| 645 |
|
| 646 |
func TestStore_Dir_Push_SkipUnpack(t *testing.T) { |
| 647 |
// add a file to file store, and obtain its directory as gz |
| 648 |
tempDir := t.TempDir() |
| 649 |
dirName := "testdir" |
| 650 |
dirPath := filepath.Join(tempDir, dirName) |
| 651 |
if err := os.MkdirAll(dirPath, 0777); err != nil { |
| 652 |
t.Fatal("error calling Mkdir(), error =", err) |
| 653 |
} |
| 654 |
content := []byte("hello world") |
| 655 |
fileName := "test.txt" |
| 656 |
if err := os.WriteFile(filepath.Join(dirPath, fileName), content, 0444); err != nil { |
| 657 |
t.Fatal("error calling WriteFile(), error =", err) |
| 658 |
} |
| 659 |
s, err := New(tempDir) |
| 660 |
if err != nil { |
| 661 |
t.Fatal("Store.New() error =", err) |
| 662 |
} |
| 663 |
defer s.Close() |
| 664 |
ctx := context.Background() |
| 665 |
desc, err := s.Add(ctx, dirName, "", dirPath) |
| 666 |
if err != nil { |
| 667 |
t.Fatal("Store.Add() error=", err) |
| 668 |
} |
| 669 |
val, ok := s.digestToPath.Load(desc.Digest) |
| 670 |
if !ok { |
| 671 |
t.Fatal("failed to find internal gz") |
| 672 |
} |
| 673 |
tmpPath := val.(string) |
| 674 |
zrc, err := os.Open(tmpPath) |
| 675 |
if err != nil { |
| 676 |
t.Fatal("failed to open internal gz") |
| 677 |
} |
| 678 |
gz, err := io.ReadAll(zrc) |
| 679 |
if err != nil { |
| 680 |
t.Fatal("failed to read internal gz") |
| 681 |
} |
| 682 |
if err := zrc.Close(); err != nil { |
| 683 |
t.Fatal("failed to close internal gz reader") |
| 684 |
} |
| 685 |
|
| 686 |
// push the gz to another store |
| 687 |
anotherTempDir := t.TempDir() |
| 688 |
anotherS, err := New(anotherTempDir) |
| 689 |
if err != nil { |
| 690 |
t.Fatal("Store.New() error =", err) |
| 691 |
} |
| 692 |
defer anotherS.Close() |
| 693 |
anotherS.SkipUnpack = true |
| 694 |
gzPath := filepath.Join(anotherTempDir, dirName) |
| 695 |
|
| 696 |
// push the gz to the store |
| 697 |
if err := anotherS.Push(ctx, desc, bytes.NewReader(gz)); err != nil { |
| 698 |
t.Fatal("Store.Push() error =", err) |
| 699 |
} |
| 700 |
pushedFile, err := os.Open(gzPath) |
| 701 |
if err != nil { |
| 702 |
t.Fatal("failed to open internal gz") |
| 703 |
} |
| 704 |
defer pushedFile.Close() |
| 705 |
pushedContent, err := io.ReadAll(pushedFile) |
| 706 |
if err != nil { |
| 707 |
t.Fatal(err) |
| 708 |
} |
| 709 |
|
| 710 |
// check that the pushedContent is equal to the original gz, i.e. it is |
| 711 |
// not unpacked |
| 712 |
if !bytes.Equal(gz, pushedContent) { |
| 713 |
t.Errorf("file content mismatch") |
| 714 |
} |
| 715 |
} |
| 716 |
|
| 717 |
func TestStore_Push_NoName(t *testing.T) { |
| 718 |
content := []byte("hello world") |
| 719 |
desc := ocispec.Descriptor{ |
| 720 |
MediaType: "test", |
| 721 |
Digest: digest.FromBytes(content), |
| 722 |
Size: int64(len(content)), |
| 723 |
} |
| 724 |
|
| 725 |
tempDir := t.TempDir() |
| 726 |
s, err := New(tempDir) |
| 727 |
if err != nil { |
| 728 |
t.Fatal("Store.New() error =", err) |
| 729 |
} |
| 730 |
defer s.Close() |
| 731 |
ctx := context.Background() |
| 732 |
|
| 733 |
// test push |
| 734 |
err = s.Push(ctx, desc, bytes.NewReader(content)) |
| 735 |
if err != nil { |
| 736 |
t.Fatal("Store.Push() error =", err) |
| 737 |
} |
| 738 |
|
| 739 |
// test exists |
| 740 |
exists, err := s.Exists(ctx, desc) |
| 741 |
if err != nil { |
| 742 |
t.Fatal("Store.Exists() error =", err) |
| 743 |
} |
| 744 |
if !exists { |
| 745 |
t.Errorf("Store.Exists() = %v, want %v", exists, true) |
| 746 |
} |
| 747 |
|
| 748 |
// test fetch |
| 749 |
rc, err := s.Fetch(ctx, desc) |
| 750 |
if err != nil { |
| 751 |
t.Fatal("Store.Fetch() error =", err) |
| 752 |
} |
| 753 |
got, err := io.ReadAll(rc) |
| 754 |
if err != nil { |
| 755 |
t.Fatal("Store.Fetch().Read() error =", err) |
| 756 |
} |
| 757 |
err = rc.Close() |
| 758 |
if err != nil { |
| 759 |
t.Error("Store.Fetch().Close() error =", err) |
| 760 |
} |
| 761 |
if !bytes.Equal(got, content) { |
| 762 |
t.Errorf("Store.Fetch() = %v, want %v", got, content) |
| 763 |
} |
| 764 |
} |
| 765 |
|
| 766 |
func TestStore_Push_NoName_ExceedLimit(t *testing.T) { |
| 767 |
blob := []byte("hello world") |
| 768 |
desc := ocispec.Descriptor{ |
| 769 |
MediaType: "test", |
| 770 |
Digest: digest.FromBytes(blob), |
| 771 |
Size: int64(len(blob)), |
| 772 |
} |
| 773 |
|
| 774 |
tempDir := t.TempDir() |
| 775 |
s, err := NewWithFallbackLimit(tempDir, 1) |
| 776 |
if err != nil { |
| 777 |
t.Fatal("Store.NewWithFallbackLimit() error =", err) |
| 778 |
} |
| 779 |
defer s.Close() |
| 780 |
ctx := context.Background() |
| 781 |
|
| 782 |
// test push |
| 783 |
err = s.Push(ctx, desc, bytes.NewReader(blob)) |
| 784 |
if !errors.Is(err, errdef.ErrSizeExceedsLimit) { |
| 785 |
t.Errorf("Store.Push() error = %v, want %v", err, errdef.ErrSizeExceedsLimit) |
| 786 |
} |
| 787 |
} |
| 788 |
|
| 789 |
func TestStore_Push_NoName_SizeNotMatch(t *testing.T) { |
| 790 |
blob := []byte("hello world") |
| 791 |
desc := ocispec.Descriptor{ |
| 792 |
MediaType: "test", |
| 793 |
Digest: digest.FromBytes(blob), |
| 794 |
Size: 1, |
| 795 |
} |
| 796 |
|
| 797 |
tempDir := t.TempDir() |
| 798 |
s, err := NewWithFallbackLimit(tempDir, 1) |
| 799 |
if err != nil { |
| 800 |
t.Fatal("Store.NewWithFallbackLimit() error =", err) |
| 801 |
} |
| 802 |
defer s.Close() |
| 803 |
ctx := context.Background() |
| 804 |
|
| 805 |
// test push |
| 806 |
err = s.Push(ctx, desc, bytes.NewReader(blob)) |
| 807 |
if err == nil { |
| 808 |
t.Errorf("Store.Push() error = nil, want: error") |
| 809 |
} |
| 810 |
} |
| 811 |
|
| 812 |
func TestStore_File_NotFound(t *testing.T) { |
| 813 |
content := []byte("hello world") |
| 814 |
desc := ocispec.Descriptor{ |
| 815 |
MediaType: "test", |
| 816 |
Digest: digest.FromBytes(content), |
| 817 |
Size: int64(len(content)), |
| 818 |
Annotations: map[string]string{ |
| 819 |
ocispec.AnnotationTitle: "test.txt", |
| 820 |
}, |
| 821 |
} |
| 822 |
|
| 823 |
tempDir := t.TempDir() |
| 824 |
s, err := New(tempDir) |
| 825 |
if err != nil { |
| 826 |
t.Fatal("Store.New() error =", err) |
| 827 |
} |
| 828 |
defer s.Close() |
| 829 |
ctx := context.Background() |
| 830 |
|
| 831 |
exists, err := s.Exists(ctx, desc) |
| 832 |
if err != nil { |
| 833 |
t.Error("Store.Exists() error =", err) |
| 834 |
} |
| 835 |
if exists { |
| 836 |
t.Errorf("Store.Exists() = %v, want %v", exists, false) |
| 837 |
} |
| 838 |
|
| 839 |
_, err = s.Fetch(ctx, desc) |
| 840 |
if !errors.Is(err, errdef.ErrNotFound) { |
| 841 |
t.Errorf("Store.Fetch() error = %v, want %v", err, errdef.ErrNotFound) |
| 842 |
} |
| 843 |
} |
| 844 |
|
| 845 |
func TestStore_File_ContentBadPush(t *testing.T) { |
| 846 |
content := []byte("hello world") |
| 847 |
desc := ocispec.Descriptor{ |
| 848 |
MediaType: "test", |
| 849 |
Digest: digest.FromBytes(content), |
| 850 |
Size: int64(len(content)), |
| 851 |
Annotations: map[string]string{ |
| 852 |
ocispec.AnnotationTitle: "test.txt", |
| 853 |
}, |
| 854 |
} |
| 855 |
|
| 856 |
tempDir := t.TempDir() |
| 857 |
s, err := New(tempDir) |
| 858 |
if err != nil { |
| 859 |
t.Fatal("Store.New() error =", err) |
| 860 |
} |
| 861 |
defer s.Close() |
| 862 |
ctx := context.Background() |
| 863 |
|
| 864 |
err = s.Push(ctx, desc, strings.NewReader("foobar")) |
| 865 |
if err == nil { |
| 866 |
t.Errorf("Store.Push() error = %v, wantErr %v", err, true) |
| 867 |
} |
| 868 |
} |
| 869 |
|
| 870 |
func TestStore_File_Add(t *testing.T) { |
| 871 |
content := []byte("hello world") |
| 872 |
name := "test.txt" |
| 873 |
mediaType := "test" |
| 874 |
desc := ocispec.Descriptor{ |
| 875 |
MediaType: mediaType, |
| 876 |
Digest: digest.FromBytes(content), |
| 877 |
Size: int64(len(content)), |
| 878 |
Annotations: map[string]string{ |
| 879 |
ocispec.AnnotationTitle: name, |
| 880 |
}, |
| 881 |
} |
| 882 |
|
| 883 |
tempDir := t.TempDir() |
| 884 |
path := filepath.Join(tempDir, name) |
| 885 |
if err := os.WriteFile(path, content, 0444); err != nil { |
| 886 |
t.Fatal("error calling WriteFile(), error =", err) |
| 887 |
} |
| 888 |
|
| 889 |
s, err := New(tempDir) |
| 890 |
if err != nil { |
| 891 |
t.Fatal("Store.New() error =", err) |
| 892 |
} |
| 893 |
defer s.Close() |
| 894 |
ctx := context.Background() |
| 895 |
|
| 896 |
// test add |
| 897 |
gotDesc, err := s.Add(ctx, name, mediaType, path) |
| 898 |
if err != nil { |
| 899 |
t.Fatal("Store.Add() error =", err) |
| 900 |
} |
| 901 |
if descriptor.FromOCI(gotDesc) != descriptor.FromOCI(desc) { |
| 902 |
t.Fatal("got descriptor mismatch") |
| 903 |
} |
| 904 |
|
| 905 |
// test exists |
| 906 |
exists, err := s.Exists(ctx, gotDesc) |
| 907 |
if err != nil { |
| 908 |
t.Fatal("Store.Exists() error =", err) |
| 909 |
} |
| 910 |
if !exists { |
| 911 |
t.Errorf("Store.Exists() = %v, want %v", exists, true) |
| 912 |
} |
| 913 |
|
| 914 |
// test fetch |
| 915 |
rc, err := s.Fetch(ctx, gotDesc) |
| 916 |
if err != nil { |
| 917 |
t.Fatal("Store.Fetch() error =", err) |
| 918 |
} |
| 919 |
got, err := io.ReadAll(rc) |
| 920 |
if err != nil { |
| 921 |
t.Fatal("Store.Fetch().Read() error =", err) |
| 922 |
} |
| 923 |
err = rc.Close() |
| 924 |
if err != nil { |
| 925 |
t.Error("Store.Fetch().Close() error =", err) |
| 926 |
} |
| 927 |
if !bytes.Equal(got, content) { |
| 928 |
t.Errorf("Store.Fetch() = %v, want %v", got, content) |
| 929 |
} |
| 930 |
} |
| 931 |
|
| 932 |
func TestStore_Dir_Add(t *testing.T) { |
| 933 |
tempDir := t.TempDir() |
| 934 |
dirName := "testdir" |
| 935 |
dirPath := filepath.Join(tempDir, dirName) |
| 936 |
if err := os.MkdirAll(dirPath, 0777); err != nil { |
| 937 |
t.Fatal("error calling Mkdir(), error =", err) |
| 938 |
} |
| 939 |
|
| 940 |
content := []byte("hello world") |
| 941 |
if err := os.WriteFile(filepath.Join(dirPath, "test.txt"), content, 0444); err != nil { |
| 942 |
t.Fatal("error calling WriteFile(), error =", err) |
| 943 |
} |
| 944 |
|
| 945 |
s, err := New(tempDir) |
| 946 |
if err != nil { |
| 947 |
t.Fatal("Store.New() error =", err) |
| 948 |
} |
| 949 |
defer s.Close() |
| 950 |
ctx := context.Background() |
| 951 |
|
| 952 |
// test add |
| 953 |
gotDesc, err := s.Add(ctx, dirName, "", dirPath) |
| 954 |
if err != nil { |
| 955 |
t.Fatal("Store.Add() error=", err) |
| 956 |
} |
| 957 |
|
| 958 |
// test exists |
| 959 |
exists, err := s.Exists(ctx, gotDesc) |
| 960 |
if err != nil { |
| 961 |
t.Fatal("Store.Exists() error =", err) |
| 962 |
} |
| 963 |
if !exists { |
| 964 |
t.Errorf("Store.Exists() = %v, want %v", exists, true) |
| 965 |
} |
| 966 |
|
| 967 |
val, ok := s.digestToPath.Load(gotDesc.Digest) |
| 968 |
if !ok { |
| 969 |
t.Fatal("failed to find internal gz") |
| 970 |
} |
| 971 |
tmpPath := val.(string) |
| 972 |
zrc, err := os.Open(tmpPath) |
| 973 |
if err != nil { |
| 974 |
t.Fatal("failed to open internal gz") |
| 975 |
} |
| 976 |
gotgz, err := io.ReadAll(zrc) |
| 977 |
if err != nil { |
| 978 |
t.Fatal("failed to read internal gz") |
| 979 |
} |
| 980 |
|
| 981 |
// test fetch |
| 982 |
rc, err := s.Fetch(ctx, gotDesc) |
| 983 |
if err != nil { |
| 984 |
t.Fatal("Store.Fetch() error =", err) |
| 985 |
} |
| 986 |
got, err := io.ReadAll(rc) |
| 987 |
if err != nil { |
| 988 |
t.Fatal("Store.Fetch().Read() error =", err) |
| 989 |
} |
| 990 |
err = rc.Close() |
| 991 |
if err != nil { |
| 992 |
t.Error("Store.Fetch().Close() error =", err) |
| 993 |
} |
| 994 |
if !bytes.Equal(got, gotgz) { |
| 995 |
t.Errorf("Store.Fetch() = %v, want %v", got, gotgz) |
| 996 |
} |
| 997 |
} |
| 998 |
func TestStore_File_SameContent_DuplicateName(t *testing.T) { |
| 999 |
content := []byte("hello world") |
| 1000 |
name := "test.txt" |
| 1001 |
mediaType := "test" |
| 1002 |
desc := ocispec.Descriptor{ |
| 1003 |
MediaType: mediaType, |
| 1004 |
Digest: digest.FromBytes(content), |
| 1005 |
Size: int64(len(content)), |
| 1006 |
Annotations: map[string]string{ |
| 1007 |
ocispec.AnnotationTitle: name, |
| 1008 |
}, |
| 1009 |
} |
| 1010 |
|
| 1011 |
tempDir := t.TempDir() |
| 1012 |
path := filepath.Join(tempDir, name) |
| 1013 |
if err := os.WriteFile(path, content, 0444); err != nil { |
| 1014 |
t.Fatal("error calling WriteFile(), error =", err) |
| 1015 |
} |
| 1016 |
|
| 1017 |
s, err := New(tempDir) |
| 1018 |
if err != nil { |
| 1019 |
t.Fatal("Store.New() error =", err) |
| 1020 |
} |
| 1021 |
defer s.Close() |
| 1022 |
ctx := context.Background() |
| 1023 |
|
| 1024 |
// test add |
| 1025 |
gotDesc, err := s.Add(ctx, name, mediaType, path) |
| 1026 |
if err != nil { |
| 1027 |
t.Fatal("Store.Add() error =", err) |
| 1028 |
} |
| 1029 |
if descriptor.FromOCI(gotDesc) != descriptor.FromOCI(desc) { |
| 1030 |
t.Fatal("got descriptor mismatch") |
| 1031 |
} |
| 1032 |
|
| 1033 |
// test exists |
| 1034 |
exists, err := s.Exists(ctx, gotDesc) |
| 1035 |
if err != nil { |
| 1036 |
t.Fatal("Store.Exists() error =", err) |
| 1037 |
} |
| 1038 |
if !exists { |
| 1039 |
t.Errorf("Store.Exists() = %v, want %v", exists, true) |
| 1040 |
} |
| 1041 |
|
| 1042 |
// test fetch |
| 1043 |
rc, err := s.Fetch(ctx, gotDesc) |
| 1044 |
if err != nil { |
| 1045 |
t.Fatal("Store.Fetch() error =", err) |
| 1046 |
} |
| 1047 |
got, err := io.ReadAll(rc) |
| 1048 |
if err != nil { |
| 1049 |
t.Fatal("Store.Fetch().Read() error =", err) |
| 1050 |
} |
| 1051 |
err = rc.Close() |
| 1052 |
if err != nil { |
| 1053 |
t.Error("Store.Fetch().Close() error =", err) |
| 1054 |
} |
| 1055 |
if !bytes.Equal(got, content) { |
| 1056 |
t.Errorf("Store.Fetch() = %v, want %v", got, content) |
| 1057 |
} |
| 1058 |
|
| 1059 |
// test duplicate name |
| 1060 |
if _, err := s.Add(ctx, name, mediaType, path); !errors.Is(err, ErrDuplicateName) { |
| 1061 |
t.Errorf("Store.Add() = %v, want %v", err, ErrDuplicateName) |
| 1062 |
} |
| 1063 |
} |
| 1064 |
|
| 1065 |
func TestStore_File_DifferentContent_DuplicateName(t *testing.T) { |
| 1066 |
content_1 := []byte("hello world") |
| 1067 |
content_2 := []byte("goodbye world") |
| 1068 |
|
| 1069 |
name_1 := "test_1.txt" |
| 1070 |
name_2 := "test_2.txt" |
| 1071 |
|
| 1072 |
mediaType_1 := "test" |
| 1073 |
mediaType_2 := "test_2" |
| 1074 |
desc_1 := ocispec.Descriptor{ |
| 1075 |
MediaType: mediaType_1, |
| 1076 |
Digest: digest.FromBytes(content_1), |
| 1077 |
Size: int64(len(content_1)), |
| 1078 |
Annotations: map[string]string{ |
| 1079 |
ocispec.AnnotationTitle: name_1, |
| 1080 |
}, |
| 1081 |
} |
| 1082 |
|
| 1083 |
tempDir := t.TempDir() |
| 1084 |
path_1 := filepath.Join(tempDir, name_1) |
| 1085 |
if err := os.WriteFile(path_1, content_1, 0444); err != nil { |
| 1086 |
t.Fatal("error calling WriteFile(), error =", err) |
| 1087 |
} |
| 1088 |
|
| 1089 |
s, err := New(tempDir) |
| 1090 |
if err != nil { |
| 1091 |
t.Fatal("Store.New() error =", err) |
| 1092 |
} |
| 1093 |
defer s.Close() |
| 1094 |
ctx := context.Background() |
| 1095 |
|
| 1096 |
// test add |
| 1097 |
gotDesc, err := s.Add(ctx, name_1, mediaType_1, path_1) |
| 1098 |
if err != nil { |
| 1099 |
t.Fatal("Store.Add() error =", err) |
| 1100 |
} |
| 1101 |
if descriptor.FromOCI(gotDesc) != descriptor.FromOCI(desc_1) { |
| 1102 |
t.Fatal("got descriptor mismatch") |
| 1103 |
} |
| 1104 |
|
| 1105 |
// test exists |
| 1106 |
exists, err := s.Exists(ctx, gotDesc) |
| 1107 |
if err != nil { |
| 1108 |
t.Fatal("Store.Exists() error =", err) |
| 1109 |
} |
| 1110 |
if !exists { |
| 1111 |
t.Errorf("Store.Exists() = %v, want %v", exists, true) |
| 1112 |
} |
| 1113 |
|
| 1114 |
// test fetch |
| 1115 |
rc, err := s.Fetch(ctx, gotDesc) |
| 1116 |
if err != nil { |
| 1117 |
t.Fatal("Store.Fetch() error =", err) |
| 1118 |
} |
| 1119 |
got, err := io.ReadAll(rc) |
| 1120 |
if err != nil { |
| 1121 |
t.Fatal("Store.Fetch().Read() error =", err) |
| 1122 |
} |
| 1123 |
err = rc.Close() |
| 1124 |
if err != nil { |
| 1125 |
t.Error("Store.Fetch().Close() error =", err) |
| 1126 |
} |
| 1127 |
if !bytes.Equal(got, content_1) { |
| 1128 |
t.Errorf("Store.Fetch() = %v, want %v", got, content_1) |
| 1129 |
} |
| 1130 |
|
| 1131 |
// test add duplicate name |
| 1132 |
path_2 := filepath.Join(tempDir, name_2) |
| 1133 |
if err := os.WriteFile(path_2, content_2, 0444); err != nil { |
| 1134 |
t.Fatal("error calling WriteFile(), error =", err) |
| 1135 |
} |
| 1136 |
|
| 1137 |
if _, err := s.Add(ctx, name_1, mediaType_2, path_2); !errors.Is(err, ErrDuplicateName) { |
| 1138 |
t.Errorf("Store.Add() = %v, want %v", err, ErrDuplicateName) |
| 1139 |
} |
| 1140 |
} |
| 1141 |
|
| 1142 |
func TestStore_File_Add_MissingName(t *testing.T) { |
| 1143 |
content := []byte("hello world") |
| 1144 |
name := "test.txt" |
| 1145 |
mediaType := "test" |
| 1146 |
|
| 1147 |
tempDir := t.TempDir() |
| 1148 |
path := filepath.Join(tempDir, name) |
| 1149 |
if err := os.WriteFile(path, content, 0444); err != nil { |
| 1150 |
t.Fatal("error calling WriteFile(), error =", err) |
| 1151 |
} |
| 1152 |
|
| 1153 |
s, err := New(tempDir) |
| 1154 |
if err != nil { |
| 1155 |
t.Fatal("Store.New() error =", err) |
| 1156 |
} |
| 1157 |
defer s.Close() |
| 1158 |
ctx := context.Background() |
| 1159 |
|
| 1160 |
// test add with empty name |
| 1161 |
_, err = s.Add(ctx, "", mediaType, path) |
| 1162 |
if !errors.Is(err, ErrMissingName) { |
| 1163 |
t.Errorf("Store.Add() error = %v, want %v", err, ErrMissingName) |
| 1164 |
} |
| 1165 |
} |
| 1166 |
|
| 1167 |
func TestStore_File_Add_SameContent(t *testing.T) { |
| 1168 |
mediaType := "test" |
| 1169 |
content := []byte("hello world") |
| 1170 |
|
| 1171 |
name_1 := "test_1.txt" |
| 1172 |
desc_1 := ocispec.Descriptor{ |
| 1173 |
MediaType: mediaType, |
| 1174 |
Digest: digest.FromBytes(content), |
| 1175 |
Size: int64(len(content)), |
| 1176 |
Annotations: map[string]string{ |
| 1177 |
ocispec.AnnotationTitle: name_1, |
| 1178 |
}, |
| 1179 |
} |
| 1180 |
|
| 1181 |
name_2 := "test_2.txt" |
| 1182 |
desc_2 := ocispec.Descriptor{ |
| 1183 |
MediaType: mediaType, |
| 1184 |
Digest: digest.FromBytes(content), |
| 1185 |
Size: int64(len(content)), |
| 1186 |
Annotations: map[string]string{ |
| 1187 |
ocispec.AnnotationTitle: name_2, |
| 1188 |
}, |
| 1189 |
} |
| 1190 |
|
| 1191 |
tempDir := t.TempDir() |
| 1192 |
path_1 := filepath.Join(tempDir, name_1) |
| 1193 |
if err := os.WriteFile(path_1, content, 0444); err != nil { |
| 1194 |
t.Fatal("error calling WriteFile(), error =", err) |
| 1195 |
} |
| 1196 |
path_2 := filepath.Join(tempDir, name_2) |
| 1197 |
if err := os.WriteFile(path_2, content, 0444); err != nil { |
| 1198 |
t.Fatal("error calling WriteFile(), error =", err) |
| 1199 |
} |
| 1200 |
|
| 1201 |
s, err := New(tempDir) |
| 1202 |
if err != nil { |
| 1203 |
t.Fatal("Store.New() error =", err) |
| 1204 |
} |
| 1205 |
defer s.Close() |
| 1206 |
ctx := context.Background() |
| 1207 |
|
| 1208 |
// test add |
| 1209 |
gotDesc_1, err := s.Add(ctx, name_1, mediaType, path_1) |
| 1210 |
if err != nil { |
| 1211 |
t.Fatal("Store.Add() error =", err) |
| 1212 |
} |
| 1213 |
if descriptor.FromOCI(gotDesc_1) != descriptor.FromOCI(desc_1) { |
| 1214 |
t.Fatal("got descriptor mismatch") |
| 1215 |
} |
| 1216 |
|
| 1217 |
gotDesc_2, err := s.Add(ctx, name_2, mediaType, path_2) |
| 1218 |
if err != nil { |
| 1219 |
t.Fatal("Store.Add() error =", err) |
| 1220 |
} |
| 1221 |
if descriptor.FromOCI(gotDesc_2) != descriptor.FromOCI(desc_2) { |
| 1222 |
t.Fatal("got descriptor mismatch") |
| 1223 |
} |
| 1224 |
|
| 1225 |
// test exists |
| 1226 |
exists, err := s.Exists(ctx, gotDesc_1) |
| 1227 |
if err != nil { |
| 1228 |
t.Fatal("Store.Exists() error =", err) |
| 1229 |
} |
| 1230 |
if !exists { |
| 1231 |
t.Errorf("Store.Exists() = %v, want %v", exists, true) |
| 1232 |
} |
| 1233 |
|
| 1234 |
exists, err = s.Exists(ctx, gotDesc_2) |
| 1235 |
if err != nil { |
| 1236 |
t.Fatal("Store.Exists() error =", err) |
| 1237 |
} |
| 1238 |
if !exists { |
| 1239 |
t.Errorf("Store.Exists() = %v, want %v", exists, true) |
| 1240 |
} |
| 1241 |
|
| 1242 |
// test fetch |
| 1243 |
rc_1, err := s.Fetch(ctx, gotDesc_1) |
| 1244 |
if err != nil { |
| 1245 |
t.Fatal("Store.Fetch() error =", err) |
| 1246 |
} |
| 1247 |
got_1, err := io.ReadAll(rc_1) |
| 1248 |
if err != nil { |
| 1249 |
t.Fatal("Store.Fetch().Read() error =", err) |
| 1250 |
} |
| 1251 |
err = rc_1.Close() |
| 1252 |
if err != nil { |
| 1253 |
t.Error("Store.Fetch().Close() error =", err) |
| 1254 |
} |
| 1255 |
if !bytes.Equal(got_1, content) { |
| 1256 |
t.Errorf("Store.Fetch() = %v, want %v", got_1, content) |
| 1257 |
} |
| 1258 |
|
| 1259 |
rc_2, err := s.Fetch(ctx, gotDesc_2) |
| 1260 |
if err != nil { |
| 1261 |
t.Fatal("Store.Fetch() error =", err) |
| 1262 |
} |
| 1263 |
got_2, err := io.ReadAll(rc_2) |
| 1264 |
if err != nil { |
| 1265 |
t.Fatal("Store.Fetch().Read() error =", err) |
| 1266 |
} |
| 1267 |
err = rc_2.Close() |
| 1268 |
if err != nil { |
| 1269 |
t.Error("Store.Fetch().Close() error =", err) |
| 1270 |
} |
| 1271 |
if !bytes.Equal(got_2, content) { |
| 1272 |
t.Errorf("Store.Fetch() = %v, want %v", got_2, content) |
| 1273 |
} |
| 1274 |
} |
| 1275 |
|
| 1276 |
func TestStore_File_Push_SameContent(t *testing.T) { |
| 1277 |
mediaType := "test" |
| 1278 |
content := []byte("hello world") |
| 1279 |
|
| 1280 |
name_1 := "test_1.txt" |
| 1281 |
desc_1 := ocispec.Descriptor{ |
| 1282 |
MediaType: mediaType, |
| 1283 |
Digest: digest.FromBytes(content), |
| 1284 |
Size: int64(len(content)), |
| 1285 |
Annotations: map[string]string{ |
| 1286 |
ocispec.AnnotationTitle: name_1, |
| 1287 |
}, |
| 1288 |
} |
| 1289 |
|
| 1290 |
name_2 := "test_2.txt" |
| 1291 |
desc_2 := ocispec.Descriptor{ |
| 1292 |
MediaType: mediaType, |
| 1293 |
Digest: digest.FromBytes(content), |
| 1294 |
Size: int64(len(content)), |
| 1295 |
Annotations: map[string]string{ |
| 1296 |
ocispec.AnnotationTitle: name_2, |
| 1297 |
}, |
| 1298 |
} |
| 1299 |
|
| 1300 |
tempDir := t.TempDir() |
| 1301 |
s, err := New(tempDir) |
| 1302 |
if err != nil { |
| 1303 |
t.Fatal("Store.New() error =", err) |
| 1304 |
} |
| 1305 |
defer s.Close() |
| 1306 |
ctx := context.Background() |
| 1307 |
|
| 1308 |
// test push |
| 1309 |
if err := s.Push(ctx, desc_1, bytes.NewReader(content)); err != nil { |
| 1310 |
t.Fatal("Store.Push() error =", err) |
| 1311 |
} |
| 1312 |
if err := s.Push(ctx, desc_2, bytes.NewReader(content)); err != nil { |
| 1313 |
t.Fatal("Store.Push() error =", err) |
| 1314 |
} |
| 1315 |
|
| 1316 |
// test exists |
| 1317 |
exists, err := s.Exists(ctx, desc_1) |
| 1318 |
if err != nil { |
| 1319 |
t.Fatal("Store.Exists() error =", err) |
| 1320 |
} |
| 1321 |
if !exists { |
| 1322 |
t.Errorf("Store.Exists() = %v, want %v", exists, true) |
| 1323 |
} |
| 1324 |
|
| 1325 |
exists, err = s.Exists(ctx, desc_2) |
| 1326 |
if err != nil { |
| 1327 |
t.Fatal("Store.Exists() error =", err) |
| 1328 |
} |
| 1329 |
if !exists { |
| 1330 |
t.Errorf("Store.Exists() = %v, want %v", exists, true) |
| 1331 |
} |
| 1332 |
|
| 1333 |
// test fetch |
| 1334 |
rc_1, err := s.Fetch(ctx, desc_1) |
| 1335 |
if err != nil { |
| 1336 |
t.Fatal("Store.Fetch() error =", err) |
| 1337 |
} |
| 1338 |
got_1, err := io.ReadAll(rc_1) |
| 1339 |
if err != nil { |
| 1340 |
t.Fatal("Store.Fetch().Read() error =", err) |
| 1341 |
} |
| 1342 |
err = rc_1.Close() |
| 1343 |
if err != nil { |
| 1344 |
t.Error("Store.Fetch().Close() error =", err) |
| 1345 |
} |
| 1346 |
if !bytes.Equal(got_1, content) { |
| 1347 |
t.Errorf("Store.Fetch() = %v, want %v", got_1, content) |
| 1348 |
} |
| 1349 |
|
| 1350 |
rc_2, err := s.Fetch(ctx, desc_2) |
| 1351 |
if err != nil { |
| 1352 |
t.Fatal("Store.Fetch() error =", err) |
| 1353 |
} |
| 1354 |
got_2, err := io.ReadAll(rc_2) |
| 1355 |
if err != nil { |
| 1356 |
t.Fatal("Store.Fetch().Read() error =", err) |
| 1357 |
} |
| 1358 |
err = rc_2.Close() |
| 1359 |
if err != nil { |
| 1360 |
t.Error("Store.Fetch().Close() error =", err) |
| 1361 |
} |
| 1362 |
if !bytes.Equal(got_2, content) { |
| 1363 |
t.Errorf("Store.Fetch() = %v, want %v", got_2, content) |
| 1364 |
} |
| 1365 |
} |
| 1366 |
|
| 1367 |
func TestStore_File_Push_DuplicateName(t *testing.T) { |
| 1368 |
mediaType := "test" |
| 1369 |
name := "test.txt" |
| 1370 |
content_1 := []byte("hello world") |
| 1371 |
content_2 := []byte("goodbye world") |
| 1372 |
desc_1 := ocispec.Descriptor{ |
| 1373 |
MediaType: mediaType, |
| 1374 |
Digest: digest.FromBytes(content_1), |
| 1375 |
Size: int64(len(content_1)), |
| 1376 |
Annotations: map[string]string{ |
| 1377 |
ocispec.AnnotationTitle: name, |
| 1378 |
}, |
| 1379 |
} |
| 1380 |
desc_2 := ocispec.Descriptor{ |
| 1381 |
MediaType: mediaType, |
| 1382 |
Digest: digest.FromBytes(content_2), |
| 1383 |
Size: int64(len(content_2)), |
| 1384 |
Annotations: map[string]string{ |
| 1385 |
ocispec.AnnotationTitle: name, |
| 1386 |
}, |
| 1387 |
} |
| 1388 |
|
| 1389 |
tempDir := t.TempDir() |
| 1390 |
s, err := New(tempDir) |
| 1391 |
if err != nil { |
| 1392 |
t.Fatal("Store.New() error =", err) |
| 1393 |
} |
| 1394 |
defer s.Close() |
| 1395 |
ctx := context.Background() |
| 1396 |
|
| 1397 |
// test push |
| 1398 |
err = s.Push(ctx, desc_1, bytes.NewReader(content_1)) |
| 1399 |
if err != nil { |
| 1400 |
t.Fatal("Store.Push() error =", err) |
| 1401 |
} |
| 1402 |
|
| 1403 |
// test exists |
| 1404 |
exists, err := s.Exists(ctx, desc_1) |
| 1405 |
if err != nil { |
| 1406 |
t.Fatal("Store.Exists() error =", err) |
| 1407 |
} |
| 1408 |
if !exists { |
| 1409 |
t.Errorf("Store.Exists() = %v, want %v", exists, true) |
| 1410 |
} |
| 1411 |
|
| 1412 |
// test fetch |
| 1413 |
rc, err := s.Fetch(ctx, desc_1) |
| 1414 |
if err != nil { |
| 1415 |
t.Fatal("Store.Fetch() error =", err) |
| 1416 |
} |
| 1417 |
got, err := io.ReadAll(rc) |
| 1418 |
if err != nil { |
| 1419 |
t.Fatal("Store.Fetch().Read() error =", err) |
| 1420 |
} |
| 1421 |
err = rc.Close() |
| 1422 |
if err != nil { |
| 1423 |
t.Error("Store.Fetch().Close() error =", err) |
| 1424 |
} |
| 1425 |
if !bytes.Equal(got, content_1) { |
| 1426 |
t.Errorf("Store.Fetch() = %v, want %v", got, content_1) |
| 1427 |
} |
| 1428 |
|
| 1429 |
// test push with duplicate name |
| 1430 |
err = s.Push(ctx, desc_2, bytes.NewBuffer(content_2)) |
| 1431 |
if !errors.Is(err, ErrDuplicateName) { |
| 1432 |
t.Errorf("Store.Push() error = %v, want %v", err, ErrDuplicateName) |
| 1433 |
} |
| 1434 |
} |
| 1435 |
|
| 1436 |
func TestStore_File_Push_ForceCAS(t *testing.T) { |
| 1437 |
mediaType := "test" |
| 1438 |
content := []byte("hello world") |
| 1439 |
desc1 := ocispec.Descriptor{ |
| 1440 |
MediaType: mediaType, |
| 1441 |
Digest: digest.FromBytes(content), |
| 1442 |
Size: int64(len(content)), |
| 1443 |
Annotations: map[string]string{ |
| 1444 |
ocispec.AnnotationTitle: "blob1", |
| 1445 |
}, |
| 1446 |
} |
| 1447 |
desc2 := ocispec.Descriptor{ |
| 1448 |
MediaType: mediaType, |
| 1449 |
Digest: digest.FromBytes(content), |
| 1450 |
Size: int64(len(content)), |
| 1451 |
Annotations: map[string]string{ |
| 1452 |
ocispec.AnnotationTitle: "blob2", |
| 1453 |
}, |
| 1454 |
} |
| 1455 |
config := []byte("{}") |
| 1456 |
configDesc := ocispec.Descriptor{ |
| 1457 |
MediaType: ocispec.MediaTypeImageConfig, |
| 1458 |
Digest: digest.FromBytes(config), |
| 1459 |
Size: int64(len(config)), |
| 1460 |
} |
| 1461 |
manifest := ocispec.Manifest{ |
| 1462 |
MediaType: ocispec.MediaTypeImageManifest, |
| 1463 |
Config: configDesc, |
| 1464 |
Layers: []ocispec.Descriptor{desc1, desc2}, |
| 1465 |
} |
| 1466 |
manifestJSON, err := json.Marshal(manifest) |
| 1467 |
if err != nil { |
| 1468 |
t.Fatal("json.Marshal() error =", err) |
| 1469 |
} |
| 1470 |
manifestDesc := ocispec.Descriptor{ |
| 1471 |
MediaType: ocispec.MediaTypeImageManifest, |
| 1472 |
Digest: digest.FromBytes(manifestJSON), |
| 1473 |
Size: int64(len(manifestJSON)), |
| 1474 |
} |
| 1475 |
tempDir := t.TempDir() |
| 1476 |
s, err := New(tempDir) |
| 1477 |
if err != nil { |
| 1478 |
t.Fatal("Store.New() error =", err) |
| 1479 |
} |
| 1480 |
s.ForceCAS = true |
| 1481 |
defer s.Close() |
| 1482 |
ctx := context.Background() |
| 1483 |
|
| 1484 |
// push blob1 |
| 1485 |
if err := s.Push(ctx, desc1, bytes.NewReader(content)); err != nil { |
| 1486 |
t.Fatal("Store.Push() error =", err) |
| 1487 |
} |
| 1488 |
// push manifest |
| 1489 |
if err := s.Push(ctx, manifestDesc, bytes.NewReader(manifestJSON)); err != nil { |
| 1490 |
t.Fatal("Store.Push() error =", err) |
| 1491 |
} |
| 1492 |
|
| 1493 |
// verify blob2 not exists |
| 1494 |
exists, err := s.Exists(ctx, desc2) |
| 1495 |
if err != nil { |
| 1496 |
t.Fatal("Store.Exists() error =", err) |
| 1497 |
} |
| 1498 |
if exists { |
| 1499 |
t.Error("Blob2 is restored") |
| 1500 |
} |
| 1501 |
} |
| 1502 |
|
| 1503 |
func TestStore_File_Push_RestoreDuplicates(t *testing.T) { |
| 1504 |
mediaType := "test" |
| 1505 |
content := []byte("hello world") |
| 1506 |
desc1 := ocispec.Descriptor{ |
| 1507 |
MediaType: mediaType, |
| 1508 |
Digest: digest.FromBytes(content), |
| 1509 |
Size: int64(len(content)), |
| 1510 |
Annotations: map[string]string{ |
| 1511 |
ocispec.AnnotationTitle: "blob1", |
| 1512 |
}, |
| 1513 |
} |
| 1514 |
desc2 := ocispec.Descriptor{ |
| 1515 |
MediaType: mediaType, |
| 1516 |
Digest: digest.FromBytes(content), |
| 1517 |
Size: int64(len(content)), |
| 1518 |
Annotations: map[string]string{ |
| 1519 |
ocispec.AnnotationTitle: "blob2", |
| 1520 |
}, |
| 1521 |
} |
| 1522 |
config := []byte("{}") |
| 1523 |
configDesc := ocispec.Descriptor{ |
| 1524 |
MediaType: ocispec.MediaTypeImageConfig, |
| 1525 |
Digest: digest.FromBytes(config), |
| 1526 |
Size: int64(len(config)), |
| 1527 |
} |
| 1528 |
manifest := ocispec.Manifest{ |
| 1529 |
MediaType: ocispec.MediaTypeImageManifest, |
| 1530 |
Config: configDesc, |
| 1531 |
Layers: []ocispec.Descriptor{desc1, desc2}, |
| 1532 |
} |
| 1533 |
manifestJSON, err := json.Marshal(manifest) |
| 1534 |
if err != nil { |
| 1535 |
t.Fatal("json.Marshal() error =", err) |
| 1536 |
} |
| 1537 |
manifestDesc := ocispec.Descriptor{ |
| 1538 |
MediaType: ocispec.MediaTypeImageManifest, |
| 1539 |
Digest: digest.FromBytes(manifestJSON), |
| 1540 |
Size: int64(len(manifestJSON)), |
| 1541 |
} |
| 1542 |
tempDir := t.TempDir() |
| 1543 |
s, err := New(tempDir) |
| 1544 |
if err != nil { |
| 1545 |
t.Fatal("Store.New() error =", err) |
| 1546 |
} |
| 1547 |
defer s.Close() |
| 1548 |
ctx := context.Background() |
| 1549 |
|
| 1550 |
// push blob1 |
| 1551 |
if err := s.Push(ctx, desc1, bytes.NewReader(content)); err != nil { |
| 1552 |
t.Fatal("Store.Push() error =", err) |
| 1553 |
} |
| 1554 |
// push manifest |
| 1555 |
if err := s.Push(ctx, manifestDesc, bytes.NewReader(manifestJSON)); err != nil { |
| 1556 |
t.Fatal("Store.Push() error =", err) |
| 1557 |
} |
| 1558 |
|
| 1559 |
// verify blob2 is restored |
| 1560 |
exists, err := s.Exists(ctx, desc2) |
| 1561 |
if err != nil { |
| 1562 |
t.Fatal("Store.Exists() error =", err) |
| 1563 |
} |
| 1564 |
if !exists { |
| 1565 |
t.Error("Blob2 is not restored") |
| 1566 |
} |
| 1567 |
rc, err := s.Fetch(ctx, desc2) |
| 1568 |
if err != nil { |
| 1569 |
t.Fatal("Store.Fetch() error =", err) |
| 1570 |
} |
| 1571 |
got, err := io.ReadAll(rc) |
| 1572 |
if err != nil { |
| 1573 |
t.Fatal("Store.Fetch().Read() error =", err) |
| 1574 |
} |
| 1575 |
err = rc.Close() |
| 1576 |
if err != nil { |
| 1577 |
t.Error("Store.Fetch().Close() error =", err) |
| 1578 |
} |
| 1579 |
if !bytes.Equal(got, content) { |
| 1580 |
t.Errorf("Store.Fetch() = %v, want %v", got, content) |
| 1581 |
} |
| 1582 |
} |
| 1583 |
|
| 1584 |
func TestStore_File_Push_RestoreDuplicates_NotFound(t *testing.T) { |
| 1585 |
mediaType := "test" |
| 1586 |
content := []byte("hello world") |
| 1587 |
desc := ocispec.Descriptor{ |
| 1588 |
MediaType: mediaType, |
| 1589 |
Digest: digest.FromBytes(content), |
| 1590 |
Size: int64(len(content)), |
| 1591 |
Annotations: map[string]string{ |
| 1592 |
ocispec.AnnotationTitle: "blob1", |
| 1593 |
}, |
| 1594 |
} |
| 1595 |
config := []byte("{}") |
| 1596 |
configDesc := ocispec.Descriptor{ |
| 1597 |
MediaType: ocispec.MediaTypeImageConfig, |
| 1598 |
Digest: digest.FromBytes(config), |
| 1599 |
Size: int64(len(config)), |
| 1600 |
} |
| 1601 |
manifest := ocispec.Manifest{ |
| 1602 |
MediaType: ocispec.MediaTypeImageManifest, |
| 1603 |
Config: configDesc, |
| 1604 |
Layers: []ocispec.Descriptor{desc}, |
| 1605 |
} |
| 1606 |
manifestJSON, err := json.Marshal(manifest) |
| 1607 |
if err != nil { |
| 1608 |
t.Fatal("json.Marshal() error =", err) |
| 1609 |
} |
| 1610 |
manifestDesc := ocispec.Descriptor{ |
| 1611 |
MediaType: ocispec.MediaTypeImageManifest, |
| 1612 |
Digest: digest.FromBytes(manifestJSON), |
| 1613 |
Size: int64(len(manifestJSON)), |
| 1614 |
} |
| 1615 |
tempDir := t.TempDir() |
| 1616 |
s, err := New(tempDir) |
| 1617 |
if err != nil { |
| 1618 |
t.Fatal("Store.New() error =", err) |
| 1619 |
} |
| 1620 |
defer s.Close() |
| 1621 |
ctx := context.Background() |
| 1622 |
|
| 1623 |
// push manifest before blob is fine |
| 1624 |
if err := s.Push(ctx, manifestDesc, bytes.NewReader(manifestJSON)); err != nil { |
| 1625 |
t.Error("Store.Push(): error = ", err) |
| 1626 |
} |
| 1627 |
} |
| 1628 |
|
| 1629 |
func TestStore_File_Push_RestoreDuplicates_DuplicateName(t *testing.T) { |
| 1630 |
mediaType := "test" |
| 1631 |
content := []byte("hello world") |
| 1632 |
blobDesc := ocispec.Descriptor{ |
| 1633 |
MediaType: mediaType, |
| 1634 |
Digest: digest.FromBytes(content), |
| 1635 |
Size: int64(len(content)), |
| 1636 |
Annotations: map[string]string{ |
| 1637 |
ocispec.AnnotationTitle: "blob", |
| 1638 |
}, |
| 1639 |
} |
| 1640 |
config := []byte("{}") |
| 1641 |
configDesc := ocispec.Descriptor{ |
| 1642 |
MediaType: ocispec.MediaTypeImageConfig, |
| 1643 |
Digest: digest.FromBytes(config), |
| 1644 |
Size: int64(len(config)), |
| 1645 |
} |
| 1646 |
manifest := ocispec.Manifest{ |
| 1647 |
MediaType: ocispec.MediaTypeImageManifest, |
| 1648 |
Config: configDesc, |
| 1649 |
Layers: []ocispec.Descriptor{blobDesc}, |
| 1650 |
} |
| 1651 |
manifestJSON, err := json.Marshal(manifest) |
| 1652 |
if err != nil { |
| 1653 |
t.Fatal("json.Marshal() error =", err) |
| 1654 |
} |
| 1655 |
manifestDesc := ocispec.Descriptor{ |
| 1656 |
MediaType: ocispec.MediaTypeImageManifest, |
| 1657 |
Digest: digest.FromBytes(manifestJSON), |
| 1658 |
Size: int64(len(manifestJSON)), |
| 1659 |
} |
| 1660 |
tempDir := t.TempDir() |
| 1661 |
fallbackMock := &storageMock{ |
| 1662 |
Storage: cas.NewMemory(), |
| 1663 |
} |
| 1664 |
s, err := NewWithFallbackStorage(tempDir, fallbackMock) |
| 1665 |
if err != nil { |
| 1666 |
t.Fatal("NewWithFallbackStorage() error =", err) |
| 1667 |
} |
| 1668 |
defer s.Close() |
| 1669 |
ctx := context.Background() |
| 1670 |
|
| 1671 |
// push blob as unnamed |
| 1672 |
if err := fallbackMock.Push(ctx, blobDesc, bytes.NewReader(content)); err != nil { |
| 1673 |
t.Fatal("Store.Push() error =", err) |
| 1674 |
} |
| 1675 |
// push manifest |
| 1676 |
fallbackMock.OnFetch = func(ctx context.Context, desc ocispec.Descriptor) error { |
| 1677 |
if desc.Digest == blobDesc.Digest { |
| 1678 |
// push blob before being restored by manifest put to simulate |
| 1679 |
// concurrent pushing for race condition |
| 1680 |
if err := s.Push(ctx, blobDesc, bytes.NewReader(content)); err != nil { |
| 1681 |
t.Fatal("Store.Push() error =", err) |
| 1682 |
} |
| 1683 |
} |
| 1684 |
return nil |
| 1685 |
} |
| 1686 |
if err := s.Push(ctx, manifestDesc, bytes.NewReader(manifestJSON)); err != nil { |
| 1687 |
t.Fatal("Store.Push() error =", err) |
| 1688 |
} |
| 1689 |
|
| 1690 |
// verify blob is restored |
| 1691 |
got, err := os.ReadFile(filepath.Join(tempDir, "blob")) |
| 1692 |
if err != nil { |
| 1693 |
t.Fatal("os.ReadFile() error =", err) |
| 1694 |
} |
| 1695 |
if !bytes.Equal(got, content) { |
| 1696 |
t.Errorf("os.ReadFile() = %v, want %v", got, content) |
| 1697 |
} |
| 1698 |
} |
| 1699 |
|
| 1700 |
func TestStore_File_Push_RestoreDuplicates_Failure(t *testing.T) { |
| 1701 |
mediaType := "test" |
| 1702 |
content := []byte("hello world") |
| 1703 |
blobDesc := ocispec.Descriptor{ |
| 1704 |
MediaType: mediaType, |
| 1705 |
Digest: digest.FromBytes(content), |
| 1706 |
Size: int64(len(content)), |
| 1707 |
Annotations: map[string]string{ |
| 1708 |
ocispec.AnnotationTitle: "blob", |
| 1709 |
}, |
| 1710 |
} |
| 1711 |
config := []byte("{}") |
| 1712 |
configDesc := ocispec.Descriptor{ |
| 1713 |
MediaType: ocispec.MediaTypeImageConfig, |
| 1714 |
Digest: digest.FromBytes(config), |
| 1715 |
Size: int64(len(config)), |
| 1716 |
} |
| 1717 |
manifest := ocispec.Manifest{ |
| 1718 |
MediaType: ocispec.MediaTypeImageManifest, |
| 1719 |
Config: configDesc, |
| 1720 |
Layers: []ocispec.Descriptor{blobDesc}, |
| 1721 |
} |
| 1722 |
manifestJSON, err := json.Marshal(manifest) |
| 1723 |
if err != nil { |
| 1724 |
t.Fatal("json.Marshal() error =", err) |
| 1725 |
} |
| 1726 |
manifestDesc := ocispec.Descriptor{ |
| 1727 |
MediaType: ocispec.MediaTypeImageManifest, |
| 1728 |
Digest: digest.FromBytes(manifestJSON), |
| 1729 |
Size: int64(len(manifestJSON)), |
| 1730 |
} |
| 1731 |
tempDir := t.TempDir() |
| 1732 |
fallbackMock := &storageMock{ |
| 1733 |
Storage: cas.NewMemory(), |
| 1734 |
} |
| 1735 |
s, err := NewWithFallbackStorage(tempDir, fallbackMock) |
| 1736 |
if err != nil { |
| 1737 |
t.Fatal("NewWithFallbackStorage() error =", err) |
| 1738 |
} |
| 1739 |
defer s.Close() |
| 1740 |
ctx := context.Background() |
| 1741 |
|
| 1742 |
// push manifest |
| 1743 |
wantErr := errors.New("restoreDuplicates: fetch error") |
| 1744 |
fallbackMock.OnFetch = func(ctx context.Context, desc ocispec.Descriptor) error { |
| 1745 |
if desc.Digest == blobDesc.Digest { |
| 1746 |
return wantErr |
| 1747 |
} |
| 1748 |
return nil |
| 1749 |
} |
| 1750 |
if err := s.Push(ctx, manifestDesc, bytes.NewReader(manifestJSON)); !errors.Is(err, wantErr) { |
| 1751 |
t.Fatalf("Store.Push() error = %v, wantErr %v", err, wantErr) |
| 1752 |
} |
| 1753 |
} |
| 1754 |
|
| 1755 |
func TestStore_File_Fetch_SameDigest_NoName(t *testing.T) { |
| 1756 |
mediaType := "test" |
| 1757 |
content := []byte("hello world") |
| 1758 |
|
| 1759 |
name_1 := "test_1.txt" |
| 1760 |
desc_1 := ocispec.Descriptor{ |
| 1761 |
MediaType: mediaType, |
| 1762 |
Digest: digest.FromBytes(content), |
| 1763 |
Size: int64(len(content)), |
| 1764 |
Annotations: map[string]string{ |
| 1765 |
ocispec.AnnotationTitle: name_1, |
| 1766 |
}, |
| 1767 |
} |
| 1768 |
|
| 1769 |
desc_2 := ocispec.Descriptor{ |
| 1770 |
MediaType: mediaType, |
| 1771 |
Digest: digest.FromBytes(content), |
| 1772 |
Size: int64(len(content)), |
| 1773 |
} |
| 1774 |
|
| 1775 |
tempDir := t.TempDir() |
| 1776 |
s, err := New(tempDir) |
| 1777 |
if err != nil { |
| 1778 |
t.Fatal("Store.New() error =", err) |
| 1779 |
} |
| 1780 |
defer s.Close() |
| 1781 |
ctx := context.Background() |
| 1782 |
|
| 1783 |
// test push |
| 1784 |
if err := s.Push(ctx, desc_1, bytes.NewReader(content)); err != nil { |
| 1785 |
t.Fatal("Store.Push() error =", err) |
| 1786 |
} |
| 1787 |
if err := s.Push(ctx, desc_2, bytes.NewReader(content)); err != nil { |
| 1788 |
t.Fatal("Store.Push() error =", err) |
| 1789 |
} |
| 1790 |
|
| 1791 |
// test exists |
| 1792 |
exists, err := s.Exists(ctx, desc_1) |
| 1793 |
if err != nil { |
| 1794 |
t.Fatal("Store.Exists() error =", err) |
| 1795 |
} |
| 1796 |
if !exists { |
| 1797 |
t.Errorf("Store.Exists() = %v, want %v", exists, true) |
| 1798 |
} |
| 1799 |
|
| 1800 |
exists, err = s.Exists(ctx, desc_2) |
| 1801 |
if err != nil { |
| 1802 |
t.Fatal("Store.Exists() error =", err) |
| 1803 |
} |
| 1804 |
if !exists { |
| 1805 |
t.Errorf("Store.Exists() = %v, want %v", exists, true) |
| 1806 |
} |
| 1807 |
|
| 1808 |
// test fetch |
| 1809 |
rc_1, err := s.Fetch(ctx, desc_1) |
| 1810 |
if err != nil { |
| 1811 |
t.Fatal("Store.Fetch() error =", err) |
| 1812 |
} |
| 1813 |
got_1, err := io.ReadAll(rc_1) |
| 1814 |
if err != nil { |
| 1815 |
t.Fatal("Store.Fetch().Read() error =", err) |
| 1816 |
} |
| 1817 |
err = rc_1.Close() |
| 1818 |
if err != nil { |
| 1819 |
t.Error("Store.Fetch().Close() error =", err) |
| 1820 |
} |
| 1821 |
if !bytes.Equal(got_1, content) { |
| 1822 |
t.Errorf("Store.Fetch() = %v, want %v", got_1, content) |
| 1823 |
} |
| 1824 |
|
| 1825 |
rc_2, err := s.Fetch(ctx, desc_2) |
| 1826 |
if err != nil { |
| 1827 |
t.Fatal("Store.Fetch() error =", err) |
| 1828 |
} |
| 1829 |
got_2, err := io.ReadAll(rc_2) |
| 1830 |
if err != nil { |
| 1831 |
t.Fatal("Store.Fetch().Read() error =", err) |
| 1832 |
} |
| 1833 |
err = rc_2.Close() |
| 1834 |
if err != nil { |
| 1835 |
t.Error("Store.Fetch().Close() error =", err) |
| 1836 |
} |
| 1837 |
if !bytes.Equal(got_2, content) { |
| 1838 |
t.Errorf("Store.Fetch() = %v, want %v", got_2, content) |
| 1839 |
} |
| 1840 |
} |
| 1841 |
|
| 1842 |
func TestStore_File_Fetch_SameDigest_DifferentName(t *testing.T) { |
| 1843 |
mediaType := "test" |
| 1844 |
content := []byte("hello world") |
| 1845 |
|
| 1846 |
name_1 := "test_1.txt" |
| 1847 |
desc_1 := ocispec.Descriptor{ |
| 1848 |
MediaType: mediaType, |
| 1849 |
Digest: digest.FromBytes(content), |
| 1850 |
Size: int64(len(content)), |
| 1851 |
Annotations: map[string]string{ |
| 1852 |
ocispec.AnnotationTitle: name_1, |
| 1853 |
}, |
| 1854 |
} |
| 1855 |
|
| 1856 |
name_2 := "test_2.txt" |
| 1857 |
desc_2 := ocispec.Descriptor{ |
| 1858 |
MediaType: mediaType, |
| 1859 |
Digest: digest.FromBytes(content), |
| 1860 |
Size: int64(len(content)), |
| 1861 |
Annotations: map[string]string{ |
| 1862 |
ocispec.AnnotationTitle: name_2, |
| 1863 |
}, |
| 1864 |
} |
| 1865 |
|
| 1866 |
tempDir := t.TempDir() |
| 1867 |
s, err := New(tempDir) |
| 1868 |
if err != nil { |
| 1869 |
t.Fatal("Store.New() error =", err) |
| 1870 |
} |
| 1871 |
defer s.Close() |
| 1872 |
ctx := context.Background() |
| 1873 |
|
| 1874 |
// test desc_1 |
| 1875 |
if err := s.Push(ctx, desc_1, bytes.NewReader(content)); err != nil { |
| 1876 |
t.Fatal("Store.Push() error =", err) |
| 1877 |
} |
| 1878 |
|
| 1879 |
exists, err := s.Exists(ctx, desc_1) |
| 1880 |
if err != nil { |
| 1881 |
t.Fatal("Store.Exists() error =", err) |
| 1882 |
} |
| 1883 |
if !exists { |
| 1884 |
t.Errorf("Store.Exists() = %v, want %v", exists, true) |
| 1885 |
} |
| 1886 |
|
| 1887 |
rc_1, err := s.Fetch(ctx, desc_1) |
| 1888 |
if err != nil { |
| 1889 |
t.Fatal("Store.Fetch() error =", err) |
| 1890 |
} |
| 1891 |
got_1, err := io.ReadAll(rc_1) |
| 1892 |
if err != nil { |
| 1893 |
t.Fatal("Store.Fetch().Read() error =", err) |
| 1894 |
} |
| 1895 |
err = rc_1.Close() |
| 1896 |
if err != nil { |
| 1897 |
t.Error("Store.Fetch().Close() error =", err) |
| 1898 |
} |
| 1899 |
if !bytes.Equal(got_1, content) { |
| 1900 |
t.Errorf("Store.Fetch() = %v, want %v", got_1, content) |
| 1901 |
} |
| 1902 |
|
| 1903 |
// test desc_2 |
| 1904 |
exists, err = s.Exists(ctx, desc_2) |
| 1905 |
if err != nil { |
| 1906 |
t.Fatal("Store.Exists() error =", err) |
| 1907 |
} |
| 1908 |
if exists { |
| 1909 |
t.Errorf("Store.Exists() = %v, want %v", exists, false) |
| 1910 |
} |
| 1911 |
|
| 1912 |
_, err = s.Fetch(ctx, desc_2) |
| 1913 |
if !errors.Is(err, errdef.ErrNotFound) { |
| 1914 |
t.Errorf("Store.Fetch() error = %v, want %v", err, errdef.ErrNotFound) |
| 1915 |
} |
| 1916 |
} |
| 1917 |
|
| 1918 |
func TestStore_File_Push_Overwrite(t *testing.T) { |
| 1919 |
mediaType := "test" |
| 1920 |
name := "test.txt" |
| 1921 |
old_content := []byte("hello world") |
| 1922 |
new_content := []byte("goodbye world") |
| 1923 |
desc := ocispec.Descriptor{ |
| 1924 |
MediaType: mediaType, |
| 1925 |
Digest: digest.FromBytes(new_content), |
| 1926 |
Size: int64(len(new_content)), |
| 1927 |
Annotations: map[string]string{ |
| 1928 |
ocispec.AnnotationTitle: name, |
| 1929 |
}, |
| 1930 |
} |
| 1931 |
|
| 1932 |
tempDir := t.TempDir() |
| 1933 |
path := filepath.Join(tempDir, name) |
| 1934 |
if err := os.WriteFile(path, old_content, 0666); err != nil { |
| 1935 |
t.Fatal("error calling WriteFile(), error =", err) |
| 1936 |
} |
| 1937 |
|
| 1938 |
s, err := New(tempDir) |
| 1939 |
if err != nil { |
| 1940 |
t.Fatal("Store.New() error =", err) |
| 1941 |
} |
| 1942 |
defer s.Close() |
| 1943 |
ctx := context.Background() |
| 1944 |
|
| 1945 |
// test push |
| 1946 |
err = s.Push(ctx, desc, bytes.NewReader(new_content)) |
| 1947 |
if err != nil { |
| 1948 |
t.Fatal("Store.Push() error =", err) |
| 1949 |
} |
| 1950 |
|
| 1951 |
// test exists |
| 1952 |
exists, err := s.Exists(ctx, desc) |
| 1953 |
if err != nil { |
| 1954 |
t.Fatal("Store.Exists() error =", err) |
| 1955 |
} |
| 1956 |
if !exists { |
| 1957 |
t.Errorf("Store.Exists() = %v, want %v", exists, true) |
| 1958 |
} |
| 1959 |
|
| 1960 |
// test fetch |
| 1961 |
rc, err := s.Fetch(ctx, desc) |
| 1962 |
if err != nil { |
| 1963 |
t.Fatal("Store.Fetch() error =", err) |
| 1964 |
} |
| 1965 |
got, err := io.ReadAll(rc) |
| 1966 |
if err != nil { |
| 1967 |
t.Fatal("Store.Fetch().Read() error =", err) |
| 1968 |
} |
| 1969 |
err = rc.Close() |
| 1970 |
if err != nil { |
| 1971 |
t.Error("Store.Fetch().Close() error =", err) |
| 1972 |
} |
| 1973 |
if !bytes.Equal(got, new_content) { |
| 1974 |
t.Errorf("Store.Fetch() = %v, want %v", got, new_content) |
| 1975 |
} |
| 1976 |
|
| 1977 |
} |
| 1978 |
|
| 1979 |
func TestStore_File_Push_DisableOverwrite(t *testing.T) { |
| 1980 |
content := []byte("hello world") |
| 1981 |
name := "test.txt" |
| 1982 |
desc := ocispec.Descriptor{ |
| 1983 |
MediaType: "test", |
| 1984 |
Digest: digest.FromBytes(content), |
| 1985 |
Size: int64(len(content)), |
| 1986 |
Annotations: map[string]string{ |
| 1987 |
ocispec.AnnotationTitle: name, |
| 1988 |
}, |
| 1989 |
} |
| 1990 |
|
| 1991 |
tempDir := t.TempDir() |
| 1992 |
path := filepath.Join(tempDir, name) |
| 1993 |
if err := os.WriteFile(path, content, 0444); err != nil { |
| 1994 |
t.Fatal("error calling WriteFile(), error =", err) |
| 1995 |
} |
| 1996 |
|
| 1997 |
s, err := New(tempDir) |
| 1998 |
if err != nil { |
| 1999 |
t.Fatal("Store.New() error =", err) |
| 2000 |
} |
| 2001 |
defer s.Close() |
| 2002 |
s.DisableOverwrite = true |
| 2003 |
|
| 2004 |
ctx := context.Background() |
| 2005 |
err = s.Push(ctx, desc, bytes.NewReader(content)) |
| 2006 |
if !errors.Is(err, ErrOverwriteDisallowed) { |
| 2007 |
t.Errorf("Store.Push() error = %v, want %v", err, ErrOverwriteDisallowed) |
| 2008 |
} |
| 2009 |
} |
| 2010 |
|
| 2011 |
func TestStore_File_Push_IgnoreNoName(t *testing.T) { |
| 2012 |
config := []byte("{}") |
| 2013 |
configDesc := ocispec.Descriptor{ |
| 2014 |
MediaType: "config", |
| 2015 |
Digest: digest.FromBytes(config), |
| 2016 |
Size: int64(len(config)), |
| 2017 |
} |
| 2018 |
manifest := ocispec.Manifest{ |
| 2019 |
MediaType: ocispec.MediaTypeImageManifest, |
| 2020 |
Config: configDesc, |
| 2021 |
Layers: []ocispec.Descriptor{}, |
| 2022 |
} |
| 2023 |
manifestJSON, err := json.Marshal(manifest) |
| 2024 |
if err != nil { |
| 2025 |
t.Fatal("json.Marshal() error =", err) |
| 2026 |
} |
| 2027 |
manifestDesc := ocispec.Descriptor{ |
| 2028 |
MediaType: ocispec.MediaTypeImageManifest, |
| 2029 |
Digest: digest.FromBytes(manifestJSON), |
| 2030 |
Size: int64(len(manifestJSON)), |
| 2031 |
} |
| 2032 |
tempDir := t.TempDir() |
| 2033 |
s, err := New(tempDir) |
| 2034 |
if err != nil { |
| 2035 |
t.Fatal("Store.New() error =", err) |
| 2036 |
} |
| 2037 |
defer s.Close() |
| 2038 |
s.IgnoreNoName = true |
| 2039 |
|
| 2040 |
// push an OCI manifest |
| 2041 |
ctx := context.Background() |
| 2042 |
err = s.Push(ctx, manifestDesc, bytes.NewReader(manifestJSON)) |
| 2043 |
if err != nil { |
| 2044 |
t.Fatal("Store.Push() error = ", err) |
| 2045 |
} |
| 2046 |
|
| 2047 |
// verify the manifest is not saved |
| 2048 |
exists, err := s.Exists(ctx, manifestDesc) |
| 2049 |
if err != nil { |
| 2050 |
t.Fatal("Store.Exists() error =", err) |
| 2051 |
} |
| 2052 |
if exists { |
| 2053 |
t.Errorf("Unnamed manifest is saved in file store") |
| 2054 |
} |
| 2055 |
// verify the manifest is not indexed |
| 2056 |
predecessors, err := s.Predecessors(ctx, configDesc) |
| 2057 |
if err != nil { |
| 2058 |
t.Fatal("Store.Predecessors() error = ", err) |
| 2059 |
} |
| 2060 |
if len(predecessors) != 0 { |
| 2061 |
t.Errorf("Unnamed manifest is indexed in file store") |
| 2062 |
} |
| 2063 |
} |
| 2064 |
|
| 2065 |
func TestStore_File_Push_DisallowPathTraversal(t *testing.T) { |
| 2066 |
content := []byte("hello world") |
| 2067 |
name := "../test.txt" |
| 2068 |
desc := ocispec.Descriptor{ |
| 2069 |
MediaType: "test", |
| 2070 |
Digest: digest.FromBytes(content), |
| 2071 |
Size: int64(len(content)), |
| 2072 |
Annotations: map[string]string{ |
| 2073 |
ocispec.AnnotationTitle: name, |
| 2074 |
}, |
| 2075 |
} |
| 2076 |
|
| 2077 |
tempDir := t.TempDir() |
| 2078 |
s, err := New(tempDir) |
| 2079 |
if err != nil { |
| 2080 |
t.Fatal("Store.New() error =", err) |
| 2081 |
} |
| 2082 |
defer s.Close() |
| 2083 |
|
| 2084 |
ctx := context.Background() |
| 2085 |
err = s.Push(ctx, desc, bytes.NewReader(content)) |
| 2086 |
if !errors.Is(err, ErrPathTraversalDisallowed) { |
| 2087 |
t.Errorf("Store.Push() error = %v, want %v", err, ErrPathTraversalDisallowed) |
| 2088 |
} |
| 2089 |
} |
| 2090 |
|
| 2091 |
func TestStore_Dir_Push_DisallowPathTraversal(t *testing.T) { |
| 2092 |
tempDir := t.TempDir() |
| 2093 |
dirName := "../testdir" |
| 2094 |
dirPath := filepath.Join(tempDir, dirName) |
| 2095 |
if err := os.MkdirAll(dirPath, 0777); err != nil { |
| 2096 |
t.Fatal("error calling Mkdir(), error =", err) |
| 2097 |
} |
| 2098 |
|
| 2099 |
content := []byte("hello world") |
| 2100 |
fileName := "test.txt" |
| 2101 |
if err := os.WriteFile(filepath.Join(dirPath, fileName), content, 0444); err != nil { |
| 2102 |
t.Fatal("error calling WriteFile(), error =", err) |
| 2103 |
} |
| 2104 |
|
| 2105 |
s, err := New(tempDir) |
| 2106 |
if err != nil { |
| 2107 |
t.Fatal("Store.New() error =", err) |
| 2108 |
} |
| 2109 |
defer s.Close() |
| 2110 |
ctx := context.Background() |
| 2111 |
|
| 2112 |
// test add |
| 2113 |
desc, err := s.Add(ctx, dirName, "", dirPath) |
| 2114 |
if err != nil { |
| 2115 |
t.Fatal("Store.Add() error=", err) |
| 2116 |
} |
| 2117 |
|
| 2118 |
val, ok := s.digestToPath.Load(desc.Digest) |
| 2119 |
if !ok { |
| 2120 |
t.Fatal("failed to find internal gz") |
| 2121 |
} |
| 2122 |
tmpPath := val.(string) |
| 2123 |
zrc, err := os.Open(tmpPath) |
| 2124 |
if err != nil { |
| 2125 |
t.Fatal("failed to open internal gz") |
| 2126 |
} |
| 2127 |
gz, err := io.ReadAll(zrc) |
| 2128 |
if err != nil { |
| 2129 |
t.Fatal("failed to read internal gz") |
| 2130 |
} |
| 2131 |
if err := zrc.Close(); err != nil { |
| 2132 |
t.Fatal("failed to close internal gz reader") |
| 2133 |
} |
| 2134 |
|
| 2135 |
anotherTempDir := t.TempDir() |
| 2136 |
// test with another file store instance to mock push gz |
| 2137 |
anotherS, err := New(anotherTempDir) |
| 2138 |
if err != nil { |
| 2139 |
t.Fatal("Store.New() error =", err) |
| 2140 |
} |
| 2141 |
defer anotherS.Close() |
| 2142 |
|
| 2143 |
// test push |
| 2144 |
err = anotherS.Push(ctx, desc, bytes.NewReader(gz)) |
| 2145 |
if !errors.Is(err, ErrPathTraversalDisallowed) { |
| 2146 |
t.Errorf("Store.Push() error = %v, want %v", err, ErrPathTraversalDisallowed) |
| 2147 |
} |
| 2148 |
} |
| 2149 |
|
| 2150 |
func TestStore_File_Push_PathTraversal(t *testing.T) { |
| 2151 |
content := []byte("hello world") |
| 2152 |
name := "../test.txt" |
| 2153 |
desc := ocispec.Descriptor{ |
| 2154 |
MediaType: "test", |
| 2155 |
Digest: digest.FromBytes(content), |
| 2156 |
Size: int64(len(content)), |
| 2157 |
Annotations: map[string]string{ |
| 2158 |
ocispec.AnnotationTitle: name, |
| 2159 |
}, |
| 2160 |
} |
| 2161 |
|
| 2162 |
tempDir := t.TempDir() |
| 2163 |
subTempDir, err := os.MkdirTemp(tempDir, "oras_filestore_*") |
| 2164 |
if err != nil { |
| 2165 |
t.Fatal("error creating temp dir, error =", err) |
| 2166 |
} |
| 2167 |
|
| 2168 |
s, err := New(subTempDir) |
| 2169 |
if err != nil { |
| 2170 |
t.Fatal("Store.New() error =", err) |
| 2171 |
} |
| 2172 |
defer s.Close() |
| 2173 |
s.AllowPathTraversalOnWrite = true |
| 2174 |
|
| 2175 |
ctx := context.Background() |
| 2176 |
// test push |
| 2177 |
err = s.Push(ctx, desc, bytes.NewReader(content)) |
| 2178 |
if err != nil { |
| 2179 |
t.Fatal("Store.Push() error =", err) |
| 2180 |
} |
| 2181 |
|
| 2182 |
// test exists |
| 2183 |
exists, err := s.Exists(ctx, desc) |
| 2184 |
if err != nil { |
| 2185 |
t.Fatal("Store.Exists() error =", err) |
| 2186 |
} |
| 2187 |
if !exists { |
| 2188 |
t.Errorf("Store.Exists() = %v, want %v", exists, true) |
| 2189 |
} |
| 2190 |
|
| 2191 |
// test fetch |
| 2192 |
rc, err := s.Fetch(ctx, desc) |
| 2193 |
if err != nil { |
| 2194 |
t.Fatal("Store.Fetch() error =", err) |
| 2195 |
} |
| 2196 |
got, err := io.ReadAll(rc) |
| 2197 |
if err != nil { |
| 2198 |
t.Fatal("Store.Fetch().Read() error =", err) |
| 2199 |
} |
| 2200 |
err = rc.Close() |
| 2201 |
if err != nil { |
| 2202 |
t.Error("Store.Fetch().Close() error =", err) |
| 2203 |
} |
| 2204 |
if !bytes.Equal(got, content) { |
| 2205 |
t.Errorf("Store.Fetch() = %v, want %v", got, content) |
| 2206 |
} |
| 2207 |
} |
| 2208 |
|
| 2209 |
func TestStore_File_Push_Concurrent(t *testing.T) { |
| 2210 |
content := []byte("hello world") |
| 2211 |
desc := ocispec.Descriptor{ |
| 2212 |
MediaType: "test", |
| 2213 |
Digest: digest.FromBytes(content), |
| 2214 |
Size: int64(len(content)), |
| 2215 |
Annotations: map[string]string{ |
| 2216 |
ocispec.AnnotationTitle: "test.txt", |
| 2217 |
}, |
| 2218 |
} |
| 2219 |
|
| 2220 |
tempDir := t.TempDir() |
| 2221 |
s, err := New(tempDir) |
| 2222 |
if err != nil { |
| 2223 |
t.Fatal("Store.New() error =", err) |
| 2224 |
} |
| 2225 |
defer s.Close() |
| 2226 |
ctx := context.Background() |
| 2227 |
|
| 2228 |
concurrency := 64 |
| 2229 |
eg, egCtx := errgroup.WithContext(ctx) |
| 2230 |
for i := range concurrency { |
| 2231 |
eg.Go(func(i int) func() error { |
| 2232 |
return func() error { |
| 2233 |
if err := s.Push(egCtx, desc, bytes.NewReader(content)); err != nil { |
| 2234 |
if errors.Is(err, ErrDuplicateName) { |
| 2235 |
return nil |
| 2236 |
} |
| 2237 |
return fmt.Errorf("failed to push content: %v", err) |
| 2238 |
} |
| 2239 |
return nil |
| 2240 |
} |
| 2241 |
}(i)) |
| 2242 |
} |
| 2243 |
|
| 2244 |
if err := eg.Wait(); err != nil { |
| 2245 |
t.Fatal(err) |
| 2246 |
} |
| 2247 |
|
| 2248 |
exists, err := s.Exists(ctx, desc) |
| 2249 |
if err != nil { |
| 2250 |
t.Fatal("Store.Exists() error =", err) |
| 2251 |
} |
| 2252 |
if !exists { |
| 2253 |
t.Errorf("Store.Exists() = %v, want %v", exists, true) |
| 2254 |
} |
| 2255 |
|
| 2256 |
rc, err := s.Fetch(ctx, desc) |
| 2257 |
if err != nil { |
| 2258 |
t.Fatal("Store.Fetch() error =", err) |
| 2259 |
} |
| 2260 |
got, err := io.ReadAll(rc) |
| 2261 |
if err != nil { |
| 2262 |
t.Fatal("Store.Fetch().Read() error =", err) |
| 2263 |
} |
| 2264 |
err = rc.Close() |
| 2265 |
if err != nil { |
| 2266 |
t.Error("Store.Fetch().Close() error =", err) |
| 2267 |
} |
| 2268 |
if !bytes.Equal(got, content) { |
| 2269 |
t.Errorf("Store.Fetch() = %v, want %v", got, content) |
| 2270 |
} |
| 2271 |
} |
| 2272 |
|
| 2273 |
func TestStore_File_Fetch_Concurrent(t *testing.T) { |
| 2274 |
content := []byte("hello world") |
| 2275 |
desc := ocispec.Descriptor{ |
| 2276 |
MediaType: "test", |
| 2277 |
Digest: digest.FromBytes(content), |
| 2278 |
Size: int64(len(content)), |
| 2279 |
Annotations: map[string]string{ |
| 2280 |
ocispec.AnnotationTitle: "test.txt", |
| 2281 |
}, |
| 2282 |
} |
| 2283 |
|
| 2284 |
tempDir := t.TempDir() |
| 2285 |
s, err := New(tempDir) |
| 2286 |
if err != nil { |
| 2287 |
t.Fatal("Store.New() error =", err) |
| 2288 |
} |
| 2289 |
defer s.Close() |
| 2290 |
ctx := context.Background() |
| 2291 |
|
| 2292 |
if err := s.Push(ctx, desc, bytes.NewReader(content)); err != nil { |
| 2293 |
t.Fatal("Store.Push() error =", err) |
| 2294 |
} |
| 2295 |
|
| 2296 |
concurrency := 64 |
| 2297 |
eg, egCtx := errgroup.WithContext(ctx) |
| 2298 |
|
| 2299 |
for i := range concurrency { |
| 2300 |
eg.Go(func(i int) func() error { |
| 2301 |
return func() error { |
| 2302 |
rc, err := s.Fetch(egCtx, desc) |
| 2303 |
if err != nil { |
| 2304 |
return fmt.Errorf("failed to fetch content: %v", err) |
| 2305 |
} |
| 2306 |
got, err := io.ReadAll(rc) |
| 2307 |
if err != nil { |
| 2308 |
t.Fatal("Store.Fetch().Read() error =", err) |
| 2309 |
} |
| 2310 |
err = rc.Close() |
| 2311 |
if err != nil { |
| 2312 |
t.Error("Store.Fetch().Close() error =", err) |
| 2313 |
} |
| 2314 |
if !bytes.Equal(got, content) { |
| 2315 |
t.Errorf("Store.Fetch() = %v, want %v", got, content) |
| 2316 |
} |
| 2317 |
return nil |
| 2318 |
} |
| 2319 |
}(i)) |
| 2320 |
} |
| 2321 |
|
| 2322 |
if err := eg.Wait(); err != nil { |
| 2323 |
t.Fatal(err) |
| 2324 |
} |
| 2325 |
} |
| 2326 |
|
| 2327 |
func TestStore_TagNotFound(t *testing.T) { |
| 2328 |
ref := "foobar" |
| 2329 |
|
| 2330 |
tempDir := t.TempDir() |
| 2331 |
s, err := New(tempDir) |
| 2332 |
if err != nil { |
| 2333 |
t.Fatal("Store.New() error =", err) |
| 2334 |
} |
| 2335 |
defer s.Close() |
| 2336 |
ctx := context.Background() |
| 2337 |
|
| 2338 |
_, err = s.Resolve(ctx, ref) |
| 2339 |
if !errors.Is(err, errdef.ErrNotFound) { |
| 2340 |
t.Errorf("Store.Resolve() error = %v, want %v", err, errdef.ErrNotFound) |
| 2341 |
} |
| 2342 |
} |
| 2343 |
|
| 2344 |
func TestStore_TagUnknownContent(t *testing.T) { |
| 2345 |
content := []byte(`{"layers":[]}`) |
| 2346 |
desc := ocispec.Descriptor{ |
| 2347 |
MediaType: ocispec.MediaTypeImageManifest, |
| 2348 |
Digest: digest.FromBytes(content), |
| 2349 |
Size: int64(len(content)), |
| 2350 |
} |
| 2351 |
ref := "foobar" |
| 2352 |
|
| 2353 |
tempDir := t.TempDir() |
| 2354 |
s, err := New(tempDir) |
| 2355 |
if err != nil { |
| 2356 |
t.Fatal("Store.New() error =", err) |
| 2357 |
} |
| 2358 |
defer s.Close() |
| 2359 |
ctx := context.Background() |
| 2360 |
|
| 2361 |
err = s.Tag(ctx, desc, ref) |
| 2362 |
if !errors.Is(err, errdef.ErrNotFound) { |
| 2363 |
t.Errorf("Store.Resolve() error = %v, want %v", err, errdef.ErrNotFound) |
| 2364 |
} |
| 2365 |
} |
| 2366 |
|
| 2367 |
func TestStore_RepeatTag(t *testing.T) { |
| 2368 |
tempDir := t.TempDir() |
| 2369 |
s, err := New(tempDir) |
| 2370 |
if err != nil { |
| 2371 |
t.Fatal("Store.New() error =", err) |
| 2372 |
} |
| 2373 |
defer s.Close() |
| 2374 |
ctx := context.Background() |
| 2375 |
|
| 2376 |
generate := func(content []byte) ocispec.Descriptor { |
| 2377 |
dgst := digest.FromBytes(content) |
| 2378 |
desc := ocispec.Descriptor{ |
| 2379 |
MediaType: "test", |
| 2380 |
Digest: dgst, |
| 2381 |
Size: int64(len(content)), |
| 2382 |
Annotations: map[string]string{ |
| 2383 |
ocispec.AnnotationTitle: dgst.Encoded() + ".blob", |
| 2384 |
}, |
| 2385 |
} |
| 2386 |
return desc |
| 2387 |
} |
| 2388 |
ref := "foobar" |
| 2389 |
|
| 2390 |
// initial tag |
| 2391 |
content := []byte("hello world") |
| 2392 |
desc := generate(content) |
| 2393 |
err = s.Push(ctx, desc, bytes.NewReader(content)) |
| 2394 |
if err != nil { |
| 2395 |
t.Fatal("Store.Push() error =", err) |
| 2396 |
} |
| 2397 |
|
| 2398 |
err = s.Tag(ctx, desc, ref) |
| 2399 |
if err != nil { |
| 2400 |
t.Fatal("Store.Tag() error =", err) |
| 2401 |
} |
| 2402 |
|
| 2403 |
gotDesc, err := s.Resolve(ctx, ref) |
| 2404 |
if err != nil { |
| 2405 |
t.Fatal("Store.Resolve() error =", err) |
| 2406 |
} |
| 2407 |
if !reflect.DeepEqual(gotDesc, desc) { |
| 2408 |
t.Errorf("Store.Resolve() = %v, want %v", gotDesc, desc) |
| 2409 |
} |
| 2410 |
|
| 2411 |
// repeat tag |
| 2412 |
content = []byte("foo") |
| 2413 |
desc = generate(content) |
| 2414 |
err = s.Push(ctx, desc, bytes.NewReader(content)) |
| 2415 |
if err != nil { |
| 2416 |
t.Fatal("Store.Push() error =", err) |
| 2417 |
} |
| 2418 |
|
| 2419 |
err = s.Tag(ctx, desc, ref) |
| 2420 |
if err != nil { |
| 2421 |
t.Fatal("Store.Tag() error =", err) |
| 2422 |
} |
| 2423 |
|
| 2424 |
gotDesc, err = s.Resolve(ctx, ref) |
| 2425 |
if err != nil { |
| 2426 |
t.Fatal("Store.Resolve() error =", err) |
| 2427 |
} |
| 2428 |
if !reflect.DeepEqual(gotDesc, desc) { |
| 2429 |
t.Errorf("Store.Resolve() = %v, want %v", gotDesc, desc) |
| 2430 |
} |
| 2431 |
|
| 2432 |
// repeat tag |
| 2433 |
content = []byte("bar") |
| 2434 |
desc = generate(content) |
| 2435 |
err = s.Push(ctx, desc, bytes.NewReader(content)) |
| 2436 |
if err != nil { |
| 2437 |
t.Fatal("Store.Push() error =", err) |
| 2438 |
} |
| 2439 |
|
| 2440 |
err = s.Tag(ctx, desc, ref) |
| 2441 |
if err != nil { |
| 2442 |
t.Fatal("Store.Tag() error =", err) |
| 2443 |
} |
| 2444 |
|
| 2445 |
gotDesc, err = s.Resolve(ctx, ref) |
| 2446 |
if err != nil { |
| 2447 |
t.Fatal("Store.Resolve() error =", err) |
| 2448 |
} |
| 2449 |
if !reflect.DeepEqual(gotDesc, desc) { |
| 2450 |
t.Errorf("Store.Resolve() = %v, want %v", gotDesc, desc) |
| 2451 |
} |
| 2452 |
} |
| 2453 |
|
| 2454 |
func TestStore_Predecessors(t *testing.T) { |
| 2455 |
tempDir := t.TempDir() |
| 2456 |
s, err := New(tempDir) |
| 2457 |
if err != nil { |
| 2458 |
t.Fatal("Store.New() error =", err) |
| 2459 |
} |
| 2460 |
defer s.Close() |
| 2461 |
ctx := context.Background() |
| 2462 |
|
| 2463 |
// generate test content |
| 2464 |
var blobs [][]byte |
| 2465 |
var descs []ocispec.Descriptor |
| 2466 |
appendBlob := func(mediaType string, blob []byte) { |
| 2467 |
blobs = append(blobs, blob) |
| 2468 |
dgst := digest.FromBytes(blob) |
| 2469 |
descs = append(descs, ocispec.Descriptor{ |
| 2470 |
MediaType: mediaType, |
| 2471 |
Digest: dgst, |
| 2472 |
Size: int64(len(blob)), |
| 2473 |
Annotations: map[string]string{ |
| 2474 |
ocispec.AnnotationTitle: dgst.Encoded() + ".blob", |
| 2475 |
}, |
| 2476 |
}) |
| 2477 |
} |
| 2478 |
generateManifest := func(config ocispec.Descriptor, layers ...ocispec.Descriptor) { |
| 2479 |
manifest := ocispec.Manifest{ |
| 2480 |
Config: config, |
| 2481 |
Layers: layers, |
| 2482 |
} |
| 2483 |
manifestJSON, err := json.Marshal(manifest) |
| 2484 |
if err != nil { |
| 2485 |
t.Fatal(err) |
| 2486 |
} |
| 2487 |
appendBlob(ocispec.MediaTypeImageManifest, manifestJSON) |
| 2488 |
} |
| 2489 |
generateIndex := func(manifests ...ocispec.Descriptor) { |
| 2490 |
index := ocispec.Index{ |
| 2491 |
Manifests: manifests, |
| 2492 |
} |
| 2493 |
indexJSON, err := json.Marshal(index) |
| 2494 |
if err != nil { |
| 2495 |
t.Fatal(err) |
| 2496 |
} |
| 2497 |
appendBlob(ocispec.MediaTypeImageIndex, indexJSON) |
| 2498 |
} |
| 2499 |
generateArtifactManifest := func(subject ocispec.Descriptor, blobs ...ocispec.Descriptor) { |
| 2500 |
var manifest spec.Artifact |
| 2501 |
manifest.Subject = &subject |
| 2502 |
manifest.Blobs = append(manifest.Blobs, blobs...) |
| 2503 |
manifestJSON, err := json.Marshal(manifest) |
| 2504 |
if err != nil { |
| 2505 |
t.Fatal(err) |
| 2506 |
} |
| 2507 |
appendBlob(spec.MediaTypeArtifactManifest, manifestJSON) |
| 2508 |
} |
| 2509 |
|
| 2510 |
appendBlob(ocispec.MediaTypeImageConfig, []byte("config")) // Blob 0 |
| 2511 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("foo")) // Blob 1 |
| 2512 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("bar")) // Blob 2 |
| 2513 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("hello")) // Blob 3 |
| 2514 |
generateManifest(descs[0], descs[1:3]...) // Blob 4 |
| 2515 |
generateManifest(descs[0], descs[3]) // Blob 5 |
| 2516 |
generateManifest(descs[0], descs[1:4]...) // Blob 6 |
| 2517 |
generateIndex(descs[4:6]...) // Blob 7 |
| 2518 |
generateIndex(descs[6]) // Blob 8 |
| 2519 |
generateIndex() // Blob 9 |
| 2520 |
generateIndex(descs[7:10]...) // Blob 10 |
| 2521 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("sig_1")) // Blob 11 |
| 2522 |
generateArtifactManifest(descs[6], descs[11]) // Blob 12 |
| 2523 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("sig_2")) // Blob 13 |
| 2524 |
generateArtifactManifest(descs[10], descs[13]) // Blob 14 |
| 2525 |
|
| 2526 |
eg, egCtx := errgroup.WithContext(ctx) |
| 2527 |
for i := range blobs { |
| 2528 |
eg.Go(func(i int) func() error { |
| 2529 |
return func() error { |
| 2530 |
err := s.Push(egCtx, descs[i], bytes.NewReader(blobs[i])) |
| 2531 |
if err != nil { |
| 2532 |
return fmt.Errorf("failed to push test content to src: %d: %v", i, err) |
| 2533 |
} |
| 2534 |
return nil |
| 2535 |
} |
| 2536 |
}(i)) |
| 2537 |
} |
| 2538 |
if err := eg.Wait(); err != nil { |
| 2539 |
t.Fatal(err) |
| 2540 |
} |
| 2541 |
|
| 2542 |
// verify predecessors |
| 2543 |
wants := [][]ocispec.Descriptor{ |
| 2544 |
descs[4:7], // Blob 0 |
| 2545 |
{descs[4], descs[6]}, // Blob 1 |
| 2546 |
{descs[4], descs[6]}, // Blob 2 |
| 2547 |
{descs[5], descs[6]}, // Blob 3 |
| 2548 |
{descs[7]}, // Blob 4 |
| 2549 |
{descs[7]}, // Blob 5 |
| 2550 |
{descs[8], descs[12]}, // Blob 6 |
| 2551 |
{descs[10]}, // Blob 7 |
| 2552 |
{descs[10]}, // Blob 8 |
| 2553 |
{descs[10]}, // Blob 9 |
| 2554 |
{descs[14]}, // Blob 10 |
| 2555 |
{descs[12]}, // Blob 11 |
| 2556 |
nil, // Blob 12 |
| 2557 |
{descs[14]}, // Blob 13 |
| 2558 |
nil, // Blob 14 |
| 2559 |
} |
| 2560 |
for i, want := range wants { |
| 2561 |
predecessors, err := s.Predecessors(ctx, descs[i]) |
| 2562 |
if err != nil { |
| 2563 |
t.Errorf("Store.Predecessors(%d) error = %v", i, err) |
| 2564 |
} |
| 2565 |
if !equalDescriptorSet(predecessors, want) { |
| 2566 |
t.Errorf("Store.Predecessors(%d) = %v, want %v", i, predecessors, want) |
| 2567 |
} |
| 2568 |
} |
| 2569 |
} |
| 2570 |
|
| 2571 |
func TestCopy_File_MemoryToFile_FullCopy(t *testing.T) { |
| 2572 |
src := memory.New() |
| 2573 |
|
| 2574 |
tempDir := t.TempDir() |
| 2575 |
dst, err := New(tempDir) |
| 2576 |
if err != nil { |
| 2577 |
t.Fatal("Store.New() error =", err) |
| 2578 |
} |
| 2579 |
defer dst.Close() |
| 2580 |
|
| 2581 |
// generate test content |
| 2582 |
var blobs [][]byte |
| 2583 |
var descs []ocispec.Descriptor |
| 2584 |
appendBlob := func(mediaType string, blob []byte) { |
| 2585 |
blobs = append(blobs, blob) |
| 2586 |
dgst := digest.FromBytes(blob) |
| 2587 |
descs = append(descs, ocispec.Descriptor{ |
| 2588 |
MediaType: mediaType, |
| 2589 |
Digest: dgst, |
| 2590 |
Size: int64(len(blob)), |
| 2591 |
Annotations: map[string]string{ |
| 2592 |
ocispec.AnnotationTitle: dgst.Encoded() + ".blob", |
| 2593 |
}, |
| 2594 |
}) |
| 2595 |
} |
| 2596 |
generateManifest := func(config ocispec.Descriptor, layers ...ocispec.Descriptor) { |
| 2597 |
manifest := ocispec.Manifest{ |
| 2598 |
Config: config, |
| 2599 |
Layers: layers, |
| 2600 |
} |
| 2601 |
manifestJSON, err := json.Marshal(manifest) |
| 2602 |
if err != nil { |
| 2603 |
t.Fatal(err) |
| 2604 |
} |
| 2605 |
appendBlob(ocispec.MediaTypeImageManifest, manifestJSON) |
| 2606 |
} |
| 2607 |
|
| 2608 |
appendBlob(ocispec.MediaTypeImageConfig, []byte("config")) // Blob 0 |
| 2609 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("foo")) // Blob 1 |
| 2610 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("bar")) // Blob 2 |
| 2611 |
generateManifest(descs[0], descs[1:3]...) // Blob 3 |
| 2612 |
|
| 2613 |
ctx := context.Background() |
| 2614 |
for i := range blobs { |
| 2615 |
err := src.Push(ctx, descs[i], bytes.NewReader(blobs[i])) |
| 2616 |
if err != nil { |
| 2617 |
t.Fatalf("failed to push test content to src: %d: %v", i, err) |
| 2618 |
} |
| 2619 |
} |
| 2620 |
|
| 2621 |
root := descs[3] |
| 2622 |
ref := "foobar" |
| 2623 |
err = src.Tag(ctx, root, ref) |
| 2624 |
if err != nil { |
| 2625 |
t.Fatal("fail to tag root node", err) |
| 2626 |
} |
| 2627 |
|
| 2628 |
// test copy |
| 2629 |
gotDesc, err := oras.Copy(ctx, src, ref, dst, "", oras.CopyOptions{}) |
| 2630 |
if err != nil { |
| 2631 |
t.Fatalf("Copy() error = %v, wantErr %v", err, false) |
| 2632 |
} |
| 2633 |
if !reflect.DeepEqual(gotDesc, root) { |
| 2634 |
t.Errorf("Copy() = %v, want %v", gotDesc, root) |
| 2635 |
} |
| 2636 |
|
| 2637 |
// verify contents |
| 2638 |
for i, desc := range descs { |
| 2639 |
exists, err := dst.Exists(ctx, desc) |
| 2640 |
if err != nil { |
| 2641 |
t.Fatalf("dst.Exists(%d) error = %v", i, err) |
| 2642 |
} |
| 2643 |
if !exists { |
| 2644 |
t.Errorf("dst.Exists(%d) = %v, want %v", i, exists, true) |
| 2645 |
} |
| 2646 |
} |
| 2647 |
|
| 2648 |
// verify tag |
| 2649 |
gotDesc, err = dst.Resolve(ctx, ref) |
| 2650 |
if err != nil { |
| 2651 |
t.Fatal("dst.Resolve() error =", err) |
| 2652 |
} |
| 2653 |
if !reflect.DeepEqual(gotDesc, root) { |
| 2654 |
t.Errorf("dst.Resolve() = %v, want %v", gotDesc, root) |
| 2655 |
} |
| 2656 |
} |
| 2657 |
|
| 2658 |
func TestCopyGraph_MemoryToFile_FullCopy(t *testing.T) { |
| 2659 |
src := memory.New() |
| 2660 |
|
| 2661 |
tempDir := t.TempDir() |
| 2662 |
dst, err := New(tempDir) |
| 2663 |
if err != nil { |
| 2664 |
t.Fatal("Store.New() error =", err) |
| 2665 |
} |
| 2666 |
defer dst.Close() |
| 2667 |
|
| 2668 |
// generate test content |
| 2669 |
var blobs [][]byte |
| 2670 |
var descs []ocispec.Descriptor |
| 2671 |
appendBlob := func(mediaType string, blob []byte) { |
| 2672 |
blobs = append(blobs, blob) |
| 2673 |
dgst := digest.FromBytes(blob) |
| 2674 |
descs = append(descs, ocispec.Descriptor{ |
| 2675 |
MediaType: mediaType, |
| 2676 |
Digest: dgst, |
| 2677 |
Size: int64(len(blob)), |
| 2678 |
Annotations: map[string]string{ |
| 2679 |
ocispec.AnnotationTitle: dgst.Encoded() + ".blob", |
| 2680 |
}, |
| 2681 |
}) |
| 2682 |
} |
| 2683 |
generateManifest := func(config ocispec.Descriptor, layers ...ocispec.Descriptor) { |
| 2684 |
manifest := ocispec.Manifest{ |
| 2685 |
Config: config, |
| 2686 |
Layers: layers, |
| 2687 |
} |
| 2688 |
manifestJSON, err := json.Marshal(manifest) |
| 2689 |
if err != nil { |
| 2690 |
t.Fatal(err) |
| 2691 |
} |
| 2692 |
appendBlob(ocispec.MediaTypeImageManifest, manifestJSON) |
| 2693 |
} |
| 2694 |
generateIndex := func(manifests ...ocispec.Descriptor) { |
| 2695 |
index := ocispec.Index{ |
| 2696 |
Manifests: manifests, |
| 2697 |
} |
| 2698 |
indexJSON, err := json.Marshal(index) |
| 2699 |
if err != nil { |
| 2700 |
t.Fatal(err) |
| 2701 |
} |
| 2702 |
appendBlob(ocispec.MediaTypeImageIndex, indexJSON) |
| 2703 |
} |
| 2704 |
|
| 2705 |
appendBlob(ocispec.MediaTypeImageConfig, []byte("config")) // Blob 0 |
| 2706 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("foo")) // Blob 1 |
| 2707 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("bar")) // Blob 2 |
| 2708 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("hello")) // Blob 3 |
| 2709 |
generateManifest(descs[0], descs[1:3]...) // Blob 4 |
| 2710 |
generateManifest(descs[0], descs[3]) // Blob 5 |
| 2711 |
generateManifest(descs[0], descs[1:4]...) // Blob 6 |
| 2712 |
generateIndex(descs[4:6]...) // Blob 7 |
| 2713 |
generateIndex(descs[6]) // Blob 8 |
| 2714 |
generateIndex() // Blob 9 |
| 2715 |
generateIndex(descs[7:10]...) // Blob 10 |
| 2716 |
|
| 2717 |
ctx := context.Background() |
| 2718 |
for i := range blobs { |
| 2719 |
err := src.Push(ctx, descs[i], bytes.NewReader(blobs[i])) |
| 2720 |
if err != nil { |
| 2721 |
t.Fatalf("failed to push test content to src: %d: %v", i, err) |
| 2722 |
} |
| 2723 |
} |
| 2724 |
|
| 2725 |
// test copy |
| 2726 |
srcTracker := &storageTracker{Storage: src} |
| 2727 |
dstTracker := &storageTracker{Storage: dst} |
| 2728 |
root := descs[len(descs)-1] |
| 2729 |
if err := oras.CopyGraph(ctx, srcTracker, dstTracker, root, oras.CopyGraphOptions{}); err != nil { |
| 2730 |
t.Fatalf("CopyGraph() error = %v, wantErr %v", err, false) |
| 2731 |
} |
| 2732 |
|
| 2733 |
// verify contents |
| 2734 |
for i := range blobs { |
| 2735 |
got, err := content.FetchAll(ctx, dst, descs[i]) |
| 2736 |
if err != nil { |
| 2737 |
t.Errorf("content[%d] error = %v, wantErr %v", i, err, false) |
| 2738 |
continue |
| 2739 |
} |
| 2740 |
if want := blobs[i]; !bytes.Equal(got, want) { |
| 2741 |
t.Errorf("content[%d] = %v, want %v", i, got, want) |
| 2742 |
} |
| 2743 |
} |
| 2744 |
|
| 2745 |
// verify API counts |
| 2746 |
if got, want := srcTracker.fetch, int64(len(blobs)); got != want { |
| 2747 |
t.Errorf("count(src.Fetch()) = %v, want %v", got, want) |
| 2748 |
} |
| 2749 |
if got, want := srcTracker.push, int64(0); got != want { |
| 2750 |
t.Errorf("count(src.Push()) = %v, want %v", got, want) |
| 2751 |
} |
| 2752 |
if got, want := srcTracker.exists, int64(0); got != want { |
| 2753 |
t.Errorf("count(src.Exists()) = %v, want %v", got, want) |
| 2754 |
} |
| 2755 |
if got, want := dstTracker.fetch, int64(0); got != want { |
| 2756 |
t.Errorf("count(dst.Fetch()) = %v, want %v", got, want) |
| 2757 |
} |
| 2758 |
if got, want := dstTracker.push, int64(len(blobs)); got != want { |
| 2759 |
t.Errorf("count(dst.Push()) = %v, want %v", got, want) |
| 2760 |
} |
| 2761 |
if got, want := dstTracker.exists, int64(len(blobs)); got != want { |
| 2762 |
t.Errorf("count(dst.Exists()) = %v, want %v", got, want) |
| 2763 |
} |
| 2764 |
} |
| 2765 |
|
| 2766 |
func TestCopyGraph_MemoryToFile_PartialCopy(t *testing.T) { |
| 2767 |
src := memory.New() |
| 2768 |
|
| 2769 |
tempDir := t.TempDir() |
| 2770 |
dst, err := New(tempDir) |
| 2771 |
if err != nil { |
| 2772 |
t.Fatal("Store.New() error =", err) |
| 2773 |
} |
| 2774 |
defer dst.Close() |
| 2775 |
|
| 2776 |
// generate test content |
| 2777 |
var blobs [][]byte |
| 2778 |
var descs []ocispec.Descriptor |
| 2779 |
appendBlob := func(mediaType string, blob []byte) { |
| 2780 |
blobs = append(blobs, blob) |
| 2781 |
dgst := digest.FromBytes(blob) |
| 2782 |
descs = append(descs, ocispec.Descriptor{ |
| 2783 |
MediaType: mediaType, |
| 2784 |
Digest: dgst, |
| 2785 |
Size: int64(len(blob)), |
| 2786 |
Annotations: map[string]string{ |
| 2787 |
ocispec.AnnotationTitle: dgst.Encoded() + ".blob", |
| 2788 |
}, |
| 2789 |
}) |
| 2790 |
} |
| 2791 |
generateManifest := func(config ocispec.Descriptor, layers ...ocispec.Descriptor) { |
| 2792 |
manifest := ocispec.Manifest{ |
| 2793 |
Config: config, |
| 2794 |
Layers: layers, |
| 2795 |
} |
| 2796 |
manifestJSON, err := json.Marshal(manifest) |
| 2797 |
if err != nil { |
| 2798 |
t.Fatal(err) |
| 2799 |
} |
| 2800 |
appendBlob(ocispec.MediaTypeImageManifest, manifestJSON) |
| 2801 |
} |
| 2802 |
generateIndex := func(manifests ...ocispec.Descriptor) { |
| 2803 |
index := ocispec.Index{ |
| 2804 |
Manifests: manifests, |
| 2805 |
} |
| 2806 |
indexJSON, err := json.Marshal(index) |
| 2807 |
if err != nil { |
| 2808 |
t.Fatal(err) |
| 2809 |
} |
| 2810 |
appendBlob(ocispec.MediaTypeImageIndex, indexJSON) |
| 2811 |
} |
| 2812 |
|
| 2813 |
appendBlob(ocispec.MediaTypeImageConfig, []byte("config")) // Blob 0 |
| 2814 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("foo")) // Blob 1 |
| 2815 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("bar")) // Blob 2 |
| 2816 |
generateManifest(descs[0], descs[1:3]...) // Blob 3 |
| 2817 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("hello")) // Blob 4 |
| 2818 |
generateManifest(descs[0], descs[4]) // Blob 5 |
| 2819 |
generateIndex(descs[3], descs[5]) // Blob 6 |
| 2820 |
|
| 2821 |
ctx := context.Background() |
| 2822 |
for i := range blobs { |
| 2823 |
err := src.Push(ctx, descs[i], bytes.NewReader(blobs[i])) |
| 2824 |
if err != nil { |
| 2825 |
t.Fatalf("failed to push test content to src: %d: %v", i, err) |
| 2826 |
} |
| 2827 |
} |
| 2828 |
|
| 2829 |
// initial copy |
| 2830 |
root := descs[3] |
| 2831 |
if err := oras.CopyGraph(ctx, src, dst, root, oras.CopyGraphOptions{}); err != nil { |
| 2832 |
t.Fatalf("CopyGraph() error = %v, wantErr %v", err, false) |
| 2833 |
} |
| 2834 |
// verify contents |
| 2835 |
for i := range blobs[:4] { |
| 2836 |
got, err := content.FetchAll(ctx, dst, descs[i]) |
| 2837 |
if err != nil { |
| 2838 |
t.Fatalf("content[%d] error = %v, wantErr %v", i, err, false) |
| 2839 |
} |
| 2840 |
if want := blobs[i]; !bytes.Equal(got, want) { |
| 2841 |
t.Fatalf("content[%d] = %v, want %v", i, got, want) |
| 2842 |
} |
| 2843 |
} |
| 2844 |
|
| 2845 |
// test copy |
| 2846 |
srcTracker := &storageTracker{Storage: src} |
| 2847 |
dstTracker := &storageTracker{Storage: dst} |
| 2848 |
root = descs[len(descs)-1] |
| 2849 |
if err := oras.CopyGraph(ctx, srcTracker, dstTracker, root, oras.CopyGraphOptions{}); err != nil { |
| 2850 |
t.Fatalf("CopyGraph() error = %v, wantErr %v", err, false) |
| 2851 |
} |
| 2852 |
|
| 2853 |
// verify contents |
| 2854 |
for i := range blobs { |
| 2855 |
got, err := content.FetchAll(ctx, dst, descs[i]) |
| 2856 |
if err != nil { |
| 2857 |
t.Errorf("content[%d] error = %v, wantErr %v", i, err, false) |
| 2858 |
continue |
| 2859 |
} |
| 2860 |
if want := blobs[i]; !bytes.Equal(got, want) { |
| 2861 |
t.Errorf("content[%d] = %v, want %v", i, got, want) |
| 2862 |
} |
| 2863 |
} |
| 2864 |
|
| 2865 |
// verify API counts |
| 2866 |
if got, want := srcTracker.fetch, int64(3); got != want { |
| 2867 |
t.Errorf("count(src.Fetch()) = %v, want %v", got, want) |
| 2868 |
} |
| 2869 |
if got, want := srcTracker.push, int64(0); got != want { |
| 2870 |
t.Errorf("count(src.Push()) = %v, want %v", got, want) |
| 2871 |
} |
| 2872 |
if got, want := srcTracker.exists, int64(0); got != want { |
| 2873 |
t.Errorf("count(src.Exists()) = %v, want %v", got, want) |
| 2874 |
} |
| 2875 |
if got, want := dstTracker.fetch, int64(0); got != want { |
| 2876 |
t.Errorf("count(dst.Fetch()) = %v, want %v", got, want) |
| 2877 |
} |
| 2878 |
if got, want := dstTracker.push, int64(3); got != want { |
| 2879 |
t.Errorf("count(dst.Push()) = %v, want %v", got, want) |
| 2880 |
} |
| 2881 |
if got, want := dstTracker.exists, int64(5); got != want { |
| 2882 |
t.Errorf("count(dst.Exists()) = %v, want %v", got, want) |
| 2883 |
} |
| 2884 |
} |
| 2885 |
|
| 2886 |
func TestCopy_File_FileToMemory_FullCopy(t *testing.T) { |
| 2887 |
tempDir := t.TempDir() |
| 2888 |
src, err := New(tempDir) |
| 2889 |
if err != nil { |
| 2890 |
t.Fatal("Store.New() error =", err) |
| 2891 |
} |
| 2892 |
defer src.Close() |
| 2893 |
|
| 2894 |
dst := memory.New() |
| 2895 |
|
| 2896 |
// generate test content |
| 2897 |
var blobs [][]byte |
| 2898 |
var descs []ocispec.Descriptor |
| 2899 |
appendBlob := func(mediaType string, blob []byte) { |
| 2900 |
blobs = append(blobs, blob) |
| 2901 |
dgst := digest.FromBytes(blob) |
| 2902 |
descs = append(descs, ocispec.Descriptor{ |
| 2903 |
MediaType: mediaType, |
| 2904 |
Digest: dgst, |
| 2905 |
Size: int64(len(blob)), |
| 2906 |
Annotations: map[string]string{ |
| 2907 |
ocispec.AnnotationTitle: dgst.Encoded() + ".blob", |
| 2908 |
}, |
| 2909 |
}) |
| 2910 |
} |
| 2911 |
generateManifest := func(config ocispec.Descriptor, layers ...ocispec.Descriptor) { |
| 2912 |
manifest := ocispec.Manifest{ |
| 2913 |
Config: config, |
| 2914 |
Layers: layers, |
| 2915 |
} |
| 2916 |
manifestJSON, err := json.Marshal(manifest) |
| 2917 |
if err != nil { |
| 2918 |
t.Fatal(err) |
| 2919 |
} |
| 2920 |
appendBlob(ocispec.MediaTypeImageManifest, manifestJSON) |
| 2921 |
} |
| 2922 |
|
| 2923 |
appendBlob(ocispec.MediaTypeImageConfig, []byte("config")) // Blob 0 |
| 2924 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("foo")) // Blob 1 |
| 2925 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("bar")) // Blob 2 |
| 2926 |
generateManifest(descs[0], descs[1:3]...) // Blob 3 |
| 2927 |
|
| 2928 |
ctx := context.Background() |
| 2929 |
for i := range blobs { |
| 2930 |
err := src.Push(ctx, descs[i], bytes.NewReader(blobs[i])) |
| 2931 |
if err != nil { |
| 2932 |
t.Fatalf("failed to push test content to src: %d: %v", i, err) |
| 2933 |
} |
| 2934 |
} |
| 2935 |
|
| 2936 |
root := descs[3] |
| 2937 |
ref := "foobar" |
| 2938 |
err = src.Tag(ctx, root, ref) |
| 2939 |
if err != nil { |
| 2940 |
t.Fatal("fail to tag root node", err) |
| 2941 |
} |
| 2942 |
|
| 2943 |
// test copy |
| 2944 |
gotDesc, err := oras.Copy(ctx, src, ref, dst, "", oras.CopyOptions{}) |
| 2945 |
if err != nil { |
| 2946 |
t.Fatalf("Copy() error = %v, wantErr %v", err, false) |
| 2947 |
} |
| 2948 |
if !reflect.DeepEqual(gotDesc, root) { |
| 2949 |
t.Errorf("Copy() = %v, want %v", gotDesc, root) |
| 2950 |
} |
| 2951 |
|
| 2952 |
// verify contents |
| 2953 |
for i, desc := range descs { |
| 2954 |
exists, err := dst.Exists(ctx, desc) |
| 2955 |
if err != nil { |
| 2956 |
t.Fatalf("dst.Exists(%d) error = %v", i, err) |
| 2957 |
} |
| 2958 |
if !exists { |
| 2959 |
t.Errorf("dst.Exists(%d) = %v, want %v", i, exists, true) |
| 2960 |
} |
| 2961 |
} |
| 2962 |
|
| 2963 |
// verify tag |
| 2964 |
gotDesc, err = dst.Resolve(ctx, ref) |
| 2965 |
if err != nil { |
| 2966 |
t.Fatal("dst.Resolve() error =", err) |
| 2967 |
} |
| 2968 |
if !reflect.DeepEqual(gotDesc, root) { |
| 2969 |
t.Errorf("dst.Resolve() = %v, want %v", gotDesc, root) |
| 2970 |
} |
| 2971 |
} |
| 2972 |
|
| 2973 |
func TestCopyGraph_FileToMemory_FullCopy(t *testing.T) { |
| 2974 |
tempDir := t.TempDir() |
| 2975 |
src, err := New(tempDir) |
| 2976 |
if err != nil { |
| 2977 |
t.Fatal("Store.New() error =", err) |
| 2978 |
} |
| 2979 |
defer src.Close() |
| 2980 |
|
| 2981 |
dst := memory.New() |
| 2982 |
|
| 2983 |
// generate test content |
| 2984 |
var blobs [][]byte |
| 2985 |
var descs []ocispec.Descriptor |
| 2986 |
appendBlob := func(mediaType string, blob []byte) { |
| 2987 |
blobs = append(blobs, blob) |
| 2988 |
dgst := digest.FromBytes(blob) |
| 2989 |
descs = append(descs, ocispec.Descriptor{ |
| 2990 |
MediaType: mediaType, |
| 2991 |
Digest: dgst, |
| 2992 |
Size: int64(len(blob)), |
| 2993 |
Annotations: map[string]string{ |
| 2994 |
ocispec.AnnotationTitle: dgst.Encoded() + ".blob", |
| 2995 |
}, |
| 2996 |
}) |
| 2997 |
} |
| 2998 |
generateManifest := func(config ocispec.Descriptor, layers ...ocispec.Descriptor) { |
| 2999 |
manifest := ocispec.Manifest{ |
| 3000 |
Config: config, |
| 3001 |
Layers: layers, |
| 3002 |
} |
| 3003 |
manifestJSON, err := json.Marshal(manifest) |
| 3004 |
if err != nil { |
| 3005 |
t.Fatal(err) |
| 3006 |
} |
| 3007 |
appendBlob(ocispec.MediaTypeImageManifest, manifestJSON) |
| 3008 |
} |
| 3009 |
generateIndex := func(manifests ...ocispec.Descriptor) { |
| 3010 |
index := ocispec.Index{ |
| 3011 |
Manifests: manifests, |
| 3012 |
} |
| 3013 |
indexJSON, err := json.Marshal(index) |
| 3014 |
if err != nil { |
| 3015 |
t.Fatal(err) |
| 3016 |
} |
| 3017 |
appendBlob(ocispec.MediaTypeImageIndex, indexJSON) |
| 3018 |
} |
| 3019 |
|
| 3020 |
appendBlob(ocispec.MediaTypeImageConfig, []byte("config")) // Blob 0 |
| 3021 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("foo")) // Blob 1 |
| 3022 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("bar")) // Blob 2 |
| 3023 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("hello")) // Blob 3 |
| 3024 |
generateManifest(descs[0], descs[1:3]...) // Blob 4 |
| 3025 |
generateManifest(descs[0], descs[3]) // Blob 5 |
| 3026 |
generateManifest(descs[0], descs[1:4]...) // Blob 6 |
| 3027 |
generateIndex(descs[4:6]...) // Blob 7 |
| 3028 |
generateIndex(descs[6]) // Blob 8 |
| 3029 |
generateIndex() // Blob 9 |
| 3030 |
generateIndex(descs[7:10]...) // Blob 10 |
| 3031 |
|
| 3032 |
ctx := context.Background() |
| 3033 |
for i := range blobs { |
| 3034 |
err := src.Push(ctx, descs[i], bytes.NewReader(blobs[i])) |
| 3035 |
if err != nil { |
| 3036 |
t.Fatalf("failed to push test content to src: %d: %v", i, err) |
| 3037 |
} |
| 3038 |
} |
| 3039 |
|
| 3040 |
// test copy |
| 3041 |
srcTracker := &storageTracker{Storage: src} |
| 3042 |
dstTracker := &storageTracker{Storage: dst} |
| 3043 |
root := descs[len(descs)-1] |
| 3044 |
if err := oras.CopyGraph(ctx, srcTracker, dstTracker, root, oras.CopyGraphOptions{}); err != nil { |
| 3045 |
t.Fatalf("CopyGraph() error = %v, wantErr %v", err, false) |
| 3046 |
} |
| 3047 |
|
| 3048 |
// verify contents |
| 3049 |
for i := range blobs { |
| 3050 |
got, err := content.FetchAll(ctx, dst, descs[i]) |
| 3051 |
if err != nil { |
| 3052 |
t.Errorf("content[%d] error = %v, wantErr %v", i, err, false) |
| 3053 |
continue |
| 3054 |
} |
| 3055 |
if want := blobs[i]; !bytes.Equal(got, want) { |
| 3056 |
t.Errorf("content[%d] = %v, want %v", i, got, want) |
| 3057 |
} |
| 3058 |
} |
| 3059 |
|
| 3060 |
// verify API counts |
| 3061 |
if got, want := srcTracker.fetch, int64(len(blobs)); got != want { |
| 3062 |
t.Errorf("count(src.Fetch()) = %v, want %v", got, want) |
| 3063 |
} |
| 3064 |
if got, want := srcTracker.push, int64(0); got != want { |
| 3065 |
t.Errorf("count(src.Push()) = %v, want %v", got, want) |
| 3066 |
} |
| 3067 |
if got, want := srcTracker.exists, int64(0); got != want { |
| 3068 |
t.Errorf("count(src.Exists()) = %v, want %v", got, want) |
| 3069 |
} |
| 3070 |
if got, want := dstTracker.fetch, int64(0); got != want { |
| 3071 |
t.Errorf("count(dst.Fetch()) = %v, want %v", got, want) |
| 3072 |
} |
| 3073 |
if got, want := dstTracker.push, int64(len(blobs)); got != want { |
| 3074 |
t.Errorf("count(dst.Push()) = %v, want %v", got, want) |
| 3075 |
} |
| 3076 |
if got, want := dstTracker.exists, int64(len(blobs)); got != want { |
| 3077 |
t.Errorf("count(dst.Exists()) = %v, want %v", got, want) |
| 3078 |
} |
| 3079 |
} |
| 3080 |
|
| 3081 |
func TestCopyGraph_FileToMemory_PartialCopy(t *testing.T) { |
| 3082 |
tempDir := t.TempDir() |
| 3083 |
src, err := New(tempDir) |
| 3084 |
if err != nil { |
| 3085 |
t.Fatal("Store.New() error =", err) |
| 3086 |
} |
| 3087 |
defer src.Close() |
| 3088 |
|
| 3089 |
dst := memory.New() |
| 3090 |
|
| 3091 |
// generate test content |
| 3092 |
var blobs [][]byte |
| 3093 |
var descs []ocispec.Descriptor |
| 3094 |
appendBlob := func(mediaType string, blob []byte) { |
| 3095 |
blobs = append(blobs, blob) |
| 3096 |
dgst := digest.FromBytes(blob) |
| 3097 |
descs = append(descs, ocispec.Descriptor{ |
| 3098 |
MediaType: mediaType, |
| 3099 |
Digest: dgst, |
| 3100 |
Size: int64(len(blob)), |
| 3101 |
Annotations: map[string]string{ |
| 3102 |
ocispec.AnnotationTitle: dgst.Encoded() + ".blob", |
| 3103 |
}, |
| 3104 |
}) |
| 3105 |
} |
| 3106 |
generateManifest := func(config ocispec.Descriptor, layers ...ocispec.Descriptor) { |
| 3107 |
manifest := ocispec.Manifest{ |
| 3108 |
Config: config, |
| 3109 |
Layers: layers, |
| 3110 |
} |
| 3111 |
manifestJSON, err := json.Marshal(manifest) |
| 3112 |
if err != nil { |
| 3113 |
t.Fatal(err) |
| 3114 |
} |
| 3115 |
appendBlob(ocispec.MediaTypeImageManifest, manifestJSON) |
| 3116 |
} |
| 3117 |
generateIndex := func(manifests ...ocispec.Descriptor) { |
| 3118 |
index := ocispec.Index{ |
| 3119 |
Manifests: manifests, |
| 3120 |
} |
| 3121 |
indexJSON, err := json.Marshal(index) |
| 3122 |
if err != nil { |
| 3123 |
t.Fatal(err) |
| 3124 |
} |
| 3125 |
appendBlob(ocispec.MediaTypeImageIndex, indexJSON) |
| 3126 |
} |
| 3127 |
|
| 3128 |
appendBlob(ocispec.MediaTypeImageConfig, []byte("config")) // Blob 0 |
| 3129 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("foo")) // Blob 1 |
| 3130 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("bar")) // Blob 2 |
| 3131 |
generateManifest(descs[0], descs[1:3]...) // Blob 3 |
| 3132 |
appendBlob(ocispec.MediaTypeImageLayer, []byte("hello")) // Blob 4 |
| 3133 |
generateManifest(descs[0], descs[4]) // Blob 5 |
| 3134 |
generateIndex(descs[3], descs[5]) // Blob 6 |
| 3135 |
|
| 3136 |
ctx := context.Background() |
| 3137 |
for i := range blobs { |
| 3138 |
err := src.Push(ctx, descs[i], bytes.NewReader(blobs[i])) |
| 3139 |
if err != nil { |
| 3140 |
t.Fatalf("failed to push test content to src: %d: %v", i, err) |
| 3141 |
} |
| 3142 |
} |
| 3143 |
|
| 3144 |
// initial copy |
| 3145 |
root := descs[3] |
| 3146 |
if err := oras.CopyGraph(ctx, src, dst, root, oras.CopyGraphOptions{}); err != nil { |
| 3147 |
t.Fatalf("CopyGraph() error = %v, wantErr %v", err, false) |
| 3148 |
} |
| 3149 |
// verify contents |
| 3150 |
for i := range blobs[:4] { |
| 3151 |
got, err := content.FetchAll(ctx, dst, descs[i]) |
| 3152 |
if err != nil { |
| 3153 |
t.Fatalf("content[%d] error = %v, wantErr %v", i, err, false) |
| 3154 |
} |
| 3155 |
if want := blobs[i]; !bytes.Equal(got, want) { |
| 3156 |
t.Fatalf("content[%d] = %v, want %v", i, got, want) |
| 3157 |
} |
| 3158 |
} |
| 3159 |
|
| 3160 |
// test copy |
| 3161 |
srcTracker := &storageTracker{Storage: src} |
| 3162 |
dstTracker := &storageTracker{Storage: dst} |
| 3163 |
root = descs[len(descs)-1] |
| 3164 |
if err := oras.CopyGraph(ctx, srcTracker, dstTracker, root, oras.CopyGraphOptions{}); err != nil { |
| 3165 |
t.Fatalf("CopyGraph() error = %v, wantErr %v", err, false) |
| 3166 |
} |
| 3167 |
|
| 3168 |
// verify contents |
| 3169 |
for i := range blobs { |
| 3170 |
got, err := content.FetchAll(ctx, dst, descs[i]) |
| 3171 |
if err != nil { |
| 3172 |
t.Errorf("content[%d] error = %v, wantErr %v", i, err, false) |
| 3173 |
continue |
| 3174 |
} |
| 3175 |
if want := blobs[i]; !bytes.Equal(got, want) { |
| 3176 |
t.Errorf("content[%d] = %v, want %v", i, got, want) |
| 3177 |
} |
| 3178 |
} |
| 3179 |
|
| 3180 |
// verify API counts |
| 3181 |
if got, want := srcTracker.fetch, int64(3); got != want { |
| 3182 |
t.Errorf("count(src.Fetch()) = %v, want %v", got, want) |
| 3183 |
} |
| 3184 |
if got, want := srcTracker.push, int64(0); got != want { |
| 3185 |
t.Errorf("count(src.Push()) = %v, want %v", got, want) |
| 3186 |
} |
| 3187 |
if got, want := srcTracker.exists, int64(0); got != want { |
| 3188 |
t.Errorf("count(src.Exists()) = %v, want %v", got, want) |
| 3189 |
} |
| 3190 |
if got, want := dstTracker.fetch, int64(0); got != want { |
| 3191 |
t.Errorf("count(dst.Fetch()) = %v, want %v", got, want) |
| 3192 |
} |
| 3193 |
if got, want := dstTracker.push, int64(3); got != want { |
| 3194 |
t.Errorf("count(dst.Push()) = %v, want %v", got, want) |
| 3195 |
} |
| 3196 |
if got, want := dstTracker.exists, int64(5); got != want { |
| 3197 |
t.Errorf("count(dst.Exists()) = %v, want %v", got, want) |
| 3198 |
} |
| 3199 |
} |
| 3200 |
|
| 3201 |
func TestStore_resolveWritePath_PathTraversal(t *testing.T) { |
| 3202 |
tempDir := t.TempDir() |
| 3203 |
|
| 3204 |
tests := []struct { |
| 3205 |
name string |
| 3206 |
workingDir string |
| 3207 |
allowPathTraversal bool |
| 3208 |
input string |
| 3209 |
want string |
| 3210 |
wantErr error |
| 3211 |
}{ |
| 3212 |
{ |
| 3213 |
name: "good relative path with path traversal disallowed", |
| 3214 |
workingDir: tempDir, |
| 3215 |
allowPathTraversal: false, |
| 3216 |
input: "test.txt", |
| 3217 |
want: filepath.Join(tempDir, "test.txt"), |
| 3218 |
wantErr: nil, |
| 3219 |
}, |
| 3220 |
{ |
| 3221 |
name: "good absolute path with path traversal disallowed", |
| 3222 |
workingDir: tempDir, |
| 3223 |
allowPathTraversal: false, |
| 3224 |
input: filepath.Join(tempDir, "test.txt"), |
| 3225 |
want: filepath.Join(tempDir, "test.txt"), |
| 3226 |
wantErr: nil, |
| 3227 |
}, |
| 3228 |
{ |
| 3229 |
name: "bad absolute path with path traversal disallowed", |
| 3230 |
workingDir: tempDir, |
| 3231 |
allowPathTraversal: false, |
| 3232 |
input: filepath.Clean(filepath.Join(tempDir, "../test.txt")), |
| 3233 |
want: "", |
| 3234 |
wantErr: ErrPathTraversalDisallowed, |
| 3235 |
}, |
| 3236 |
{ |
| 3237 |
name: "bad absolute path with path traversal allowed", |
| 3238 |
workingDir: tempDir, |
| 3239 |
allowPathTraversal: true, |
| 3240 |
input: filepath.Clean(filepath.Join(tempDir, "../test.txt")), |
| 3241 |
want: filepath.Clean(filepath.Join(tempDir, "../test.txt")), |
| 3242 |
wantErr: nil, |
| 3243 |
}, |
| 3244 |
{ |
| 3245 |
name: "bad relative path with path traversal disallowed", |
| 3246 |
workingDir: tempDir, |
| 3247 |
allowPathTraversal: false, |
| 3248 |
input: "../test.txt", |
| 3249 |
want: "", |
| 3250 |
wantErr: ErrPathTraversalDisallowed, |
| 3251 |
}, |
| 3252 |
{ |
| 3253 |
name: "bad relative path with path traversal allowed", |
| 3254 |
workingDir: tempDir, |
| 3255 |
allowPathTraversal: true, |
| 3256 |
input: "../test.txt", |
| 3257 |
want: filepath.Clean(filepath.Join(tempDir, "../test.txt")), |
| 3258 |
wantErr: nil, |
| 3259 |
}, |
| 3260 |
{ |
| 3261 |
name: "bad relative directory path with path traversal disallowed", |
| 3262 |
workingDir: tempDir, |
| 3263 |
allowPathTraversal: false, |
| 3264 |
input: "..", |
| 3265 |
want: "", |
| 3266 |
wantErr: ErrPathTraversalDisallowed, |
| 3267 |
}, |
| 3268 |
{ |
| 3269 |
name: "bad relative directory path with path traversal allowed", |
| 3270 |
workingDir: tempDir, |
| 3271 |
allowPathTraversal: true, |
| 3272 |
input: "..", |
| 3273 |
want: filepath.Clean(filepath.Join(tempDir, "..")), |
| 3274 |
wantErr: nil, |
| 3275 |
}, |
| 3276 |
} |
| 3277 |
|
| 3278 |
for _, tt := range tests { |
| 3279 |
t.Run(tt.name, func(t *testing.T) { |
| 3280 |
s, err := New(tt.workingDir) |
| 3281 |
if err != nil { |
| 3282 |
t.Fatalf("failed to create store: %v", err) |
| 3283 |
} |
| 3284 |
defer s.Close() |
| 3285 |
|
| 3286 |
s.AllowPathTraversalOnWrite = tt.allowPathTraversal |
| 3287 |
got, err := s.resolveWritePath(tt.input) |
| 3288 |
if !errors.Is(err, tt.wantErr) { |
| 3289 |
t.Fatalf("resolveWritePath() error = %v, wantErr %v", err, tt.wantErr) |
| 3290 |
} |
| 3291 |
if got != tt.want { |
| 3292 |
t.Errorf("resolveWritePath() = %v, want %v", got, tt.want) |
| 3293 |
} |
| 3294 |
}) |
| 3295 |
} |
| 3296 |
} |
| 3297 |
|
| 3298 |
func TestStore_resolveWritePath_SymlinkTraversal(t *testing.T) { |
| 3299 |
// GHSA-8xwf-rjm4-xvhv: resolveWritePath used a lexical filepath.Rel check |
| 3300 |
// that passed for names like "out/secret.txt" even when "out" is a symlink |
| 3301 |
// pointing outside workingDir. |
| 3302 |
t.Run("symlink component in workingDir escaping write boundary is blocked", func(t *testing.T) { |
| 3303 |
tempDir := t.TempDir() |
| 3304 |
outside := filepath.Join(tempDir, "outside") |
| 3305 |
if err := os.MkdirAll(outside, 0755); err != nil { |
| 3306 |
t.Fatal(err) |
| 3307 |
} |
| 3308 |
workingDir := filepath.Join(tempDir, "store") |
| 3309 |
if err := os.MkdirAll(workingDir, 0755); err != nil { |
| 3310 |
t.Fatal(err) |
| 3311 |
} |
| 3312 |
// "store/out" is a symlink to a directory outside workingDir. |
| 3313 |
if err := os.Symlink(outside, filepath.Join(workingDir, "out")); err != nil { |
| 3314 |
t.Skip("symlinks not available:", err) |
| 3315 |
} |
| 3316 |
|
| 3317 |
s, err := New(workingDir) |
| 3318 |
if err != nil { |
| 3319 |
t.Fatal(err) |
| 3320 |
} |
| 3321 |
defer s.Close() |
| 3322 |
|
| 3323 |
// "out/secret.txt" passes the lexical check but follows the symlink |
| 3324 |
// to outside/secret.txt, escaping workingDir. |
| 3325 |
_, err = s.resolveWritePath("out/secret.txt") |
| 3326 |
if !errors.Is(err, ErrPathTraversalDisallowed) { |
| 3327 |
t.Errorf("resolveWritePath() error = %v, want %v", err, ErrPathTraversalDisallowed) |
| 3328 |
} |
| 3329 |
}) |
| 3330 |
|
| 3331 |
t.Run("regular subdirectory write within workingDir still works", func(t *testing.T) { |
| 3332 |
tempDir := t.TempDir() |
| 3333 |
workingDir := filepath.Join(tempDir, "store") |
| 3334 |
subDir := filepath.Join(workingDir, "sub") |
| 3335 |
if err := os.MkdirAll(subDir, 0755); err != nil { |
| 3336 |
t.Fatal(err) |
| 3337 |
} |
| 3338 |
|
| 3339 |
s, err := New(workingDir) |
| 3340 |
if err != nil { |
| 3341 |
t.Fatal(err) |
| 3342 |
} |
| 3343 |
defer s.Close() |
| 3344 |
|
| 3345 |
got, err := s.resolveWritePath("sub/file.txt") |
| 3346 |
if err != nil { |
| 3347 |
t.Fatalf("resolveWritePath() unexpected error: %v", err) |
| 3348 |
} |
| 3349 |
if want := filepath.Join(workingDir, "sub/file.txt"); got != want { |
| 3350 |
t.Errorf("resolveWritePath() = %v, want %v", got, want) |
| 3351 |
} |
| 3352 |
}) |
| 3353 |
} |
| 3354 |
|
| 3355 |
func TestStore_resolveWritePath_Overwrite(t *testing.T) { |
| 3356 |
t.Run("Target file already exists", func(t *testing.T) { |
| 3357 |
tempDir := t.TempDir() |
| 3358 |
|
| 3359 |
s, err := New(tempDir) |
| 3360 |
if err != nil { |
| 3361 |
t.Fatalf("failed to create store: %v", err) |
| 3362 |
} |
| 3363 |
defer s.Close() |
| 3364 |
s.DisableOverwrite = true |
| 3365 |
|
| 3366 |
existingFile := filepath.Join(tempDir, "test.txt") |
| 3367 |
if err := os.WriteFile(existingFile, []byte("content"), 0444); err != nil { |
| 3368 |
t.Fatalf("failed to create existing file: %v", err) |
| 3369 |
} |
| 3370 |
if _, err := s.resolveWritePath("test.txt"); !errors.Is(err, ErrOverwriteDisallowed) { |
| 3371 |
t.Errorf("resolveWritePath() error = %v, wantErr %v", err, ErrOverwriteDisallowed) |
| 3372 |
} |
| 3373 |
}) |
| 3374 |
|
| 3375 |
t.Run("Target file does not exist", func(t *testing.T) { |
| 3376 |
tempDir := t.TempDir() |
| 3377 |
|
| 3378 |
s, err := New(tempDir) |
| 3379 |
if err != nil { |
| 3380 |
t.Fatalf("failed to create store: %v", err) |
| 3381 |
} |
| 3382 |
defer s.Close() |
| 3383 |
s.DisableOverwrite = true |
| 3384 |
|
| 3385 |
got, err := s.resolveWritePath("test.txt") |
| 3386 |
if err != nil { |
| 3387 |
t.Fatalf("resolveWritePath() error = %v", err) |
| 3388 |
} |
| 3389 |
if want := filepath.Join(tempDir, "test.txt"); got != want { |
| 3390 |
t.Errorf("resolveWritePath() = %v, want %v", got, want) |
| 3391 |
} |
| 3392 |
}) |
| 3393 |
|
| 3394 |
t.Run("Invalid path", func(t *testing.T) { |
| 3395 |
tempDir := t.TempDir() |
| 3396 |
|
| 3397 |
s, err := New(tempDir) |
| 3398 |
if err != nil { |
| 3399 |
t.Fatalf("failed to create store: %v", err) |
| 3400 |
} |
| 3401 |
defer s.Close() |
| 3402 |
s.AllowPathTraversalOnWrite = true |
| 3403 |
s.DisableOverwrite = true |
| 3404 |
|
| 3405 |
if _, err := s.resolveWritePath("\x00invalid:path/test.txt"); err == nil { |
| 3406 |
t.Error("resolveWritePath() error = nil, wantErr = true") |
| 3407 |
} |
| 3408 |
}) |
| 3409 |
} |
| 3410 |
|
| 3411 |
func TestStore_BadDigest(t *testing.T) { |
| 3412 |
data := []byte("hello world") |
| 3413 |
ref := "foobar" |
| 3414 |
|
| 3415 |
t.Run("invalid digest", func(t *testing.T) { |
| 3416 |
desc := ocispec.Descriptor{ |
| 3417 |
MediaType: "application/test", |
| 3418 |
Size: int64(len(data)), |
| 3419 |
Digest: "invalid-digest", |
| 3420 |
} |
| 3421 |
|
| 3422 |
s, err := New(t.TempDir()) |
| 3423 |
if err != nil { |
| 3424 |
t.Fatal("Store.New() error =", err) |
| 3425 |
} |
| 3426 |
ctx := context.Background() |
| 3427 |
if err := s.Push(ctx, desc, bytes.NewReader(data)); !errors.Is(err, digest.ErrDigestInvalidFormat) { |
| 3428 |
t.Errorf("Store.Push() error = %v, wantErr %v", err, digest.ErrDigestInvalidFormat) |
| 3429 |
|
| 3430 |
} |
| 3431 |
|
| 3432 |
if err := s.Tag(ctx, desc, ref); !errors.Is(err, errdef.ErrNotFound) { |
| 3433 |
t.Errorf("Store.Tag() error = %v, wantErr %v", err, errdef.ErrNotFound) |
| 3434 |
} |
| 3435 |
|
| 3436 |
if _, err := s.Exists(ctx, desc); err != nil { |
| 3437 |
t.Errorf("Store.Exists() error = %v, wantErr %v", err, nil) |
| 3438 |
} |
| 3439 |
|
| 3440 |
if _, err := s.Fetch(ctx, desc); !errors.Is(err, errdef.ErrNotFound) { |
| 3441 |
t.Errorf("Store.Fetch() error = %v, wantErr %v", err, errdef.ErrNotFound) |
| 3442 |
} |
| 3443 |
|
| 3444 |
if _, err := s.Predecessors(ctx, desc); err != nil { |
| 3445 |
t.Errorf("Store.Predecessors() error = %v, wantErr %v", err, nil) |
| 3446 |
} |
| 3447 |
}) |
| 3448 |
|
| 3449 |
t.Run("unsupported digest (sha1)", func(t *testing.T) { |
| 3450 |
h := sha1.New() |
| 3451 |
h.Write(data) |
| 3452 |
desc := ocispec.Descriptor{ |
| 3453 |
MediaType: "application/test", |
| 3454 |
Size: int64(len(data)), |
| 3455 |
Digest: digest.NewDigestFromBytes("sha1", h.Sum(nil)), |
| 3456 |
} |
| 3457 |
|
| 3458 |
s, err := New(t.TempDir()) |
| 3459 |
if err != nil { |
| 3460 |
t.Fatal("Store.New() error =", err) |
| 3461 |
} |
| 3462 |
ctx := context.Background() |
| 3463 |
if err := s.Push(ctx, desc, bytes.NewReader(data)); !errors.Is(err, digest.ErrDigestUnsupported) { |
| 3464 |
t.Errorf("Store.Push() error = %v, wantErr %v", err, digest.ErrDigestUnsupported) |
| 3465 |
|
| 3466 |
} |
| 3467 |
|
| 3468 |
if err := s.Tag(ctx, desc, ref); !errors.Is(err, errdef.ErrNotFound) { |
| 3469 |
t.Errorf("Store.Tag() error = %v, wantErr %v", err, errdef.ErrNotFound) |
| 3470 |
} |
| 3471 |
|
| 3472 |
if _, err := s.Exists(ctx, desc); err != nil { |
| 3473 |
t.Errorf("Store.Exists() error = %v, wantErr %v", err, nil) |
| 3474 |
} |
| 3475 |
|
| 3476 |
if _, err := s.Fetch(ctx, desc); !errors.Is(err, errdef.ErrNotFound) { |
| 3477 |
t.Errorf("Store.Fetch() error = %v, wantErr %v", err, errdef.ErrNotFound) |
| 3478 |
} |
| 3479 |
|
| 3480 |
if _, err := s.Predecessors(ctx, desc); err != nil { |
| 3481 |
t.Errorf("Store.Predecessors() error = %v, wantErr %v", err, nil) |
| 3482 |
} |
| 3483 |
}) |
| 3484 |
} |
| 3485 |
|
| 3486 |
func equalDescriptorSet(actual []ocispec.Descriptor, expected []ocispec.Descriptor) bool { |
| 3487 |
if len(actual) != len(expected) { |
| 3488 |
return false |
| 3489 |
} |
| 3490 |
contains := func(node ocispec.Descriptor) bool { |
| 3491 |
for _, candidate := range actual { |
| 3492 |
if reflect.DeepEqual(candidate, node) { |
| 3493 |
return true |
| 3494 |
} |
| 3495 |
} |
| 3496 |
return false |
| 3497 |
} |
| 3498 |
for _, node := range expected { |
| 3499 |
if !contains(node) { |
| 3500 |
return false |
| 3501 |
} |
| 3502 |
} |
| 3503 |
return true |
| 3504 |
} |