| 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 oci |
| 17 |
|
| 18 |
import ( |
| 19 |
"bytes" |
| 20 |
"context" |
| 21 |
"errors" |
| 22 |
"fmt" |
| 23 |
"io" |
| 24 |
"os" |
| 25 |
"path/filepath" |
| 26 |
"strings" |
| 27 |
"testing" |
| 28 |
|
| 29 |
"github.com/opencontainers/go-digest" |
| 30 |
ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 31 |
"golang.org/x/sync/errgroup" |
| 32 |
"oras.land/oras-go/v2/errdef" |
| 33 |
) |
| 34 |
|
| 35 |
func TestStorage_Success(t *testing.T) { |
| 36 |
content := []byte("hello world") |
| 37 |
desc := ocispec.Descriptor{ |
| 38 |
MediaType: "test", |
| 39 |
Digest: digest.FromBytes(content), |
| 40 |
Size: int64(len(content)), |
| 41 |
} |
| 42 |
|
| 43 |
tempDir := t.TempDir() |
| 44 |
s, err := NewStorage(tempDir) |
| 45 |
if err != nil { |
| 46 |
t.Fatal("New() error =", err) |
| 47 |
} |
| 48 |
ctx := context.Background() |
| 49 |
|
| 50 |
// test push |
| 51 |
err = s.Push(ctx, desc, bytes.NewReader(content)) |
| 52 |
if err != nil { |
| 53 |
t.Fatal("Storage.Push() error =", err) |
| 54 |
} |
| 55 |
|
| 56 |
// test fetch |
| 57 |
exists, err := s.Exists(ctx, desc) |
| 58 |
if err != nil { |
| 59 |
t.Fatal("Storage.Exists() error =", err) |
| 60 |
} |
| 61 |
if !exists { |
| 62 |
t.Errorf("Storage.Exists() = %v, want %v", exists, true) |
| 63 |
} |
| 64 |
|
| 65 |
rc, err := s.Fetch(ctx, desc) |
| 66 |
if err != nil { |
| 67 |
t.Fatal("Storage.Fetch() error =", err) |
| 68 |
} |
| 69 |
got, err := io.ReadAll(rc) |
| 70 |
if err != nil { |
| 71 |
t.Fatal("Storage.Fetch().Read() error =", err) |
| 72 |
} |
| 73 |
err = rc.Close() |
| 74 |
if err != nil { |
| 75 |
t.Error("Storage.Fetch().Close() error =", err) |
| 76 |
} |
| 77 |
if !bytes.Equal(got, content) { |
| 78 |
t.Errorf("Storage.Fetch() = %v, want %v", got, content) |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
func TestStorage_RelativeRoot_Success(t *testing.T) { |
| 83 |
content := []byte("hello world") |
| 84 |
desc := ocispec.Descriptor{ |
| 85 |
MediaType: "test", |
| 86 |
Digest: digest.FromBytes(content), |
| 87 |
Size: int64(len(content)), |
| 88 |
} |
| 89 |
|
| 90 |
tempDir, err := filepath.EvalSymlinks(t.TempDir()) |
| 91 |
if err != nil { |
| 92 |
t.Fatal("error calling filepath.EvalSymlinks(), error =", err) |
| 93 |
} |
| 94 |
currDir, err := os.Getwd() |
| 95 |
if err != nil { |
| 96 |
t.Fatal("error calling Getwd(), error=", err) |
| 97 |
} |
| 98 |
t.Chdir(tempDir) |
| 99 |
s, err := NewStorage(".") |
| 100 |
if err != nil { |
| 101 |
t.Fatal("New() error =", err) |
| 102 |
} |
| 103 |
if want := tempDir; s.root != want { |
| 104 |
t.Errorf("Storage.root = %s, want %s", s.root, want) |
| 105 |
} |
| 106 |
// cd back to allow the temp directory to be removed |
| 107 |
t.Chdir(currDir) |
| 108 |
ctx := context.Background() |
| 109 |
|
| 110 |
// test push |
| 111 |
err = s.Push(ctx, desc, bytes.NewReader(content)) |
| 112 |
if err != nil { |
| 113 |
t.Fatal("Storage.Push() error =", err) |
| 114 |
} |
| 115 |
|
| 116 |
// test fetch |
| 117 |
exists, err := s.Exists(ctx, desc) |
| 118 |
if err != nil { |
| 119 |
t.Fatal("Storage.Exists() error =", err) |
| 120 |
} |
| 121 |
if !exists { |
| 122 |
t.Errorf("Storage.Exists() = %v, want %v", exists, true) |
| 123 |
} |
| 124 |
|
| 125 |
rc, err := s.Fetch(ctx, desc) |
| 126 |
if err != nil { |
| 127 |
t.Fatal("Storage.Fetch() error =", err) |
| 128 |
} |
| 129 |
got, err := io.ReadAll(rc) |
| 130 |
if err != nil { |
| 131 |
t.Fatal("Storage.Fetch().Read() error =", err) |
| 132 |
} |
| 133 |
err = rc.Close() |
| 134 |
if err != nil { |
| 135 |
t.Error("Storage.Fetch().Close() error =", err) |
| 136 |
} |
| 137 |
if !bytes.Equal(got, content) { |
| 138 |
t.Errorf("Storage.Fetch() = %v, want %v", got, content) |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
func TestStorage_NotFound(t *testing.T) { |
| 143 |
content := []byte("hello world") |
| 144 |
desc := ocispec.Descriptor{ |
| 145 |
MediaType: "test", |
| 146 |
Digest: digest.FromBytes(content), |
| 147 |
Size: int64(len(content)), |
| 148 |
} |
| 149 |
|
| 150 |
tempDir := t.TempDir() |
| 151 |
s, err := NewStorage(tempDir) |
| 152 |
if err != nil { |
| 153 |
t.Fatal("New() error =", err) |
| 154 |
} |
| 155 |
ctx := context.Background() |
| 156 |
|
| 157 |
exists, err := s.Exists(ctx, desc) |
| 158 |
if err != nil { |
| 159 |
t.Error("Storage.Exists() error =", err) |
| 160 |
} |
| 161 |
if exists { |
| 162 |
t.Errorf("Storage.Exists() = %v, want %v", exists, false) |
| 163 |
} |
| 164 |
|
| 165 |
_, err = s.Fetch(ctx, desc) |
| 166 |
if !errors.Is(err, errdef.ErrNotFound) { |
| 167 |
t.Errorf("Storage.Fetch() error = %v, want %v", err, errdef.ErrNotFound) |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
func TestStorage_AlreadyExists(t *testing.T) { |
| 172 |
content := []byte("hello world") |
| 173 |
desc := ocispec.Descriptor{ |
| 174 |
MediaType: "test", |
| 175 |
Digest: digest.FromBytes(content), |
| 176 |
Size: int64(len(content)), |
| 177 |
} |
| 178 |
|
| 179 |
tempDir := t.TempDir() |
| 180 |
s, err := NewStorage(tempDir) |
| 181 |
if err != nil { |
| 182 |
t.Fatal("New() error =", err) |
| 183 |
} |
| 184 |
ctx := context.Background() |
| 185 |
|
| 186 |
err = s.Push(ctx, desc, bytes.NewReader(content)) |
| 187 |
if err != nil { |
| 188 |
t.Fatal("Storage.Push() error =", err) |
| 189 |
} |
| 190 |
|
| 191 |
err = s.Push(ctx, desc, bytes.NewReader(content)) |
| 192 |
if !errors.Is(err, errdef.ErrAlreadyExists) { |
| 193 |
t.Errorf("Storage.Push() error = %v, want %v", err, errdef.ErrAlreadyExists) |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
func TestStorage_BadPush(t *testing.T) { |
| 198 |
content := []byte("hello world") |
| 199 |
desc := ocispec.Descriptor{ |
| 200 |
MediaType: "test", |
| 201 |
Digest: digest.FromBytes(content), |
| 202 |
Size: int64(len(content)), |
| 203 |
} |
| 204 |
|
| 205 |
tempDir := t.TempDir() |
| 206 |
s, err := NewStorage(tempDir) |
| 207 |
if err != nil { |
| 208 |
t.Fatal("New() error =", err) |
| 209 |
} |
| 210 |
ctx := context.Background() |
| 211 |
|
| 212 |
err = s.Push(ctx, desc, strings.NewReader("foobar")) |
| 213 |
if err == nil { |
| 214 |
t.Errorf("Storage.Push() error = %v, wantErr %v", err, true) |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
func TestStorage_Push_Concurrent(t *testing.T) { |
| 219 |
content := []byte("hello world") |
| 220 |
desc := ocispec.Descriptor{ |
| 221 |
MediaType: "test", |
| 222 |
Digest: digest.FromBytes(content), |
| 223 |
Size: int64(len(content)), |
| 224 |
} |
| 225 |
|
| 226 |
tempDir := t.TempDir() |
| 227 |
s, err := NewStorage(tempDir) |
| 228 |
if err != nil { |
| 229 |
t.Fatal("New() error =", err) |
| 230 |
} |
| 231 |
ctx := context.Background() |
| 232 |
|
| 233 |
concurrency := 64 |
| 234 |
eg, egCtx := errgroup.WithContext(ctx) |
| 235 |
for i := range concurrency { |
| 236 |
eg.Go(func(i int) func() error { |
| 237 |
return func() error { |
| 238 |
if err := s.Push(egCtx, desc, bytes.NewReader(content)); err != nil { |
| 239 |
if errors.Is(err, errdef.ErrAlreadyExists) { |
| 240 |
return nil |
| 241 |
} |
| 242 |
return fmt.Errorf("failed to push content: %v", err) |
| 243 |
} |
| 244 |
return nil |
| 245 |
} |
| 246 |
}(i)) |
| 247 |
} |
| 248 |
|
| 249 |
if err := eg.Wait(); err != nil { |
| 250 |
t.Fatal(err) |
| 251 |
} |
| 252 |
|
| 253 |
exists, err := s.Exists(ctx, desc) |
| 254 |
if err != nil { |
| 255 |
t.Fatal("Storage.Exists() error =", err) |
| 256 |
} |
| 257 |
if !exists { |
| 258 |
t.Errorf("Storage.Exists() = %v, want %v", exists, true) |
| 259 |
} |
| 260 |
|
| 261 |
rc, err := s.Fetch(ctx, desc) |
| 262 |
if err != nil { |
| 263 |
t.Fatal("Storage.Fetch() error =", err) |
| 264 |
} |
| 265 |
got, err := io.ReadAll(rc) |
| 266 |
if err != nil { |
| 267 |
t.Fatal("Storage.Fetch().Read() error =", err) |
| 268 |
} |
| 269 |
err = rc.Close() |
| 270 |
if err != nil { |
| 271 |
t.Error("Storage.Fetch().Close() error =", err) |
| 272 |
} |
| 273 |
if !bytes.Equal(got, content) { |
| 274 |
t.Errorf("Storage.Fetch() = %v, want %v", got, content) |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
func TestStorage_Fetch_ExistingBlobs(t *testing.T) { |
| 279 |
content := []byte("hello world") |
| 280 |
dgst := digest.FromBytes(content) |
| 281 |
desc := ocispec.Descriptor{ |
| 282 |
MediaType: "test", |
| 283 |
Digest: dgst, |
| 284 |
Size: int64(len(content)), |
| 285 |
} |
| 286 |
|
| 287 |
tempDir := t.TempDir() |
| 288 |
path := filepath.Join(tempDir, "blobs", dgst.Algorithm().String(), dgst.Encoded()) |
| 289 |
if err := os.MkdirAll(filepath.Dir(path), 0777); err != nil { |
| 290 |
t.Fatal("error calling Mkdir(), error =", err) |
| 291 |
} |
| 292 |
if err := os.WriteFile(path, content, 0444); err != nil { |
| 293 |
t.Fatal("error calling WriteFile(), error =", err) |
| 294 |
} |
| 295 |
|
| 296 |
s, err := NewStorage(tempDir) |
| 297 |
if err != nil { |
| 298 |
t.Fatal("New() error =", err) |
| 299 |
} |
| 300 |
ctx := context.Background() |
| 301 |
|
| 302 |
exists, err := s.Exists(ctx, desc) |
| 303 |
if err != nil { |
| 304 |
t.Fatal("Storage.Exists() error =", err) |
| 305 |
} |
| 306 |
if !exists { |
| 307 |
t.Errorf("Storage.Exists() = %v, want %v", exists, true) |
| 308 |
} |
| 309 |
|
| 310 |
rc, err := s.Fetch(ctx, desc) |
| 311 |
if err != nil { |
| 312 |
t.Fatal("Storage.Fetch() error =", err) |
| 313 |
} |
| 314 |
got, err := io.ReadAll(rc) |
| 315 |
if err != nil { |
| 316 |
t.Fatal("Storage.Fetch().Read() error =", err) |
| 317 |
} |
| 318 |
err = rc.Close() |
| 319 |
if err != nil { |
| 320 |
t.Error("Storage.Fetch().Close() error =", err) |
| 321 |
} |
| 322 |
if !bytes.Equal(got, content) { |
| 323 |
t.Errorf("Storage.Fetch() = %v, want %v", got, content) |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
func TestStorage_Fetch_Concurrent(t *testing.T) { |
| 328 |
content := []byte("hello world") |
| 329 |
desc := ocispec.Descriptor{ |
| 330 |
MediaType: "test", |
| 331 |
Digest: digest.FromBytes(content), |
| 332 |
Size: int64(len(content)), |
| 333 |
} |
| 334 |
|
| 335 |
tempDir := t.TempDir() |
| 336 |
s, err := NewStorage(tempDir) |
| 337 |
if err != nil { |
| 338 |
t.Fatal("New() error =", err) |
| 339 |
} |
| 340 |
ctx := context.Background() |
| 341 |
|
| 342 |
if err := s.Push(ctx, desc, bytes.NewReader(content)); err != nil { |
| 343 |
t.Fatal("Storage.Push() error =", err) |
| 344 |
} |
| 345 |
|
| 346 |
concurrency := 64 |
| 347 |
eg, egCtx := errgroup.WithContext(ctx) |
| 348 |
|
| 349 |
for i := range concurrency { |
| 350 |
eg.Go(func(i int) func() error { |
| 351 |
return func() error { |
| 352 |
rc, err := s.Fetch(egCtx, desc) |
| 353 |
if err != nil { |
| 354 |
return fmt.Errorf("failed to fetch content: %v", err) |
| 355 |
} |
| 356 |
got, err := io.ReadAll(rc) |
| 357 |
if err != nil { |
| 358 |
t.Fatal("Storage.Fetch().Read() error =", err) |
| 359 |
} |
| 360 |
err = rc.Close() |
| 361 |
if err != nil { |
| 362 |
t.Error("Storage.Fetch().Close() error =", err) |
| 363 |
} |
| 364 |
if !bytes.Equal(got, content) { |
| 365 |
t.Errorf("Storage.Fetch() = %v, want %v", got, content) |
| 366 |
} |
| 367 |
return nil |
| 368 |
} |
| 369 |
}(i)) |
| 370 |
} |
| 371 |
|
| 372 |
if err := eg.Wait(); err != nil { |
| 373 |
t.Fatal(err) |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
func TestStorage_Delete(t *testing.T) { |
| 378 |
content := []byte("test delete") |
| 379 |
desc := ocispec.Descriptor{ |
| 380 |
MediaType: "test", |
| 381 |
Digest: digest.FromBytes(content), |
| 382 |
Size: int64(len(content)), |
| 383 |
} |
| 384 |
tempDir := t.TempDir() |
| 385 |
s, err := NewStorage(tempDir) |
| 386 |
if err != nil { |
| 387 |
t.Fatal("New() error =", err) |
| 388 |
} |
| 389 |
ctx := context.Background() |
| 390 |
if err := s.Push(ctx, desc, bytes.NewReader(content)); err != nil { |
| 391 |
t.Fatal("Storage.Push() error =", err) |
| 392 |
} |
| 393 |
exists, err := s.Exists(ctx, desc) |
| 394 |
if err != nil { |
| 395 |
t.Fatal("Storage.Exists() error =", err) |
| 396 |
} |
| 397 |
if !exists { |
| 398 |
t.Errorf("Storage.Exists() = %v, want %v", exists, true) |
| 399 |
} |
| 400 |
err = s.Delete(ctx, desc) |
| 401 |
if err != nil { |
| 402 |
t.Fatal("Storage.Delete() error =", err) |
| 403 |
} |
| 404 |
exists, err = s.Exists(ctx, desc) |
| 405 |
if err != nil { |
| 406 |
t.Fatal("Storage.Exists() error =", err) |
| 407 |
} |
| 408 |
if exists { |
| 409 |
t.Errorf("Storage.Exists() = %v, want %v", exists, false) |
| 410 |
} |
| 411 |
err = s.Delete(ctx, desc) |
| 412 |
if !errors.Is(err, errdef.ErrNotFound) { |
| 413 |
t.Fatalf("got error = %v, want %v", err, errdef.ErrNotFound) |
| 414 |
} |
| 415 |
} |