| 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 content |
| 17 |
|
| 18 |
import ( |
| 19 |
"bytes" |
| 20 |
"context" |
| 21 |
"errors" |
| 22 |
"io" |
| 23 |
"testing" |
| 24 |
|
| 25 |
"github.com/opencontainers/go-digest" |
| 26 |
ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 27 |
) |
| 28 |
|
| 29 |
func TestFetcherFunc_Fetch(t *testing.T) { |
| 30 |
data := []byte("test content") |
| 31 |
desc := ocispec.Descriptor{ |
| 32 |
MediaType: "test", |
| 33 |
Digest: digest.FromBytes(data), |
| 34 |
Size: int64(len(data)), |
| 35 |
} |
| 36 |
|
| 37 |
fetcherFunc := FetcherFunc(func(ctx context.Context, target ocispec.Descriptor) (io.ReadCloser, error) { |
| 38 |
if target.Digest != desc.Digest { |
| 39 |
return nil, errors.New("content not found") |
| 40 |
} |
| 41 |
return io.NopCloser(bytes.NewReader(data)), nil |
| 42 |
}) |
| 43 |
|
| 44 |
ctx := context.Background() |
| 45 |
rc, err := fetcherFunc.Fetch(ctx, desc) |
| 46 |
if err != nil { |
| 47 |
t.Fatalf("FetcherFunc.Fetch() error = %v", err) |
| 48 |
} |
| 49 |
defer rc.Close() |
| 50 |
|
| 51 |
got, err := io.ReadAll(rc) |
| 52 |
if err != nil { |
| 53 |
t.Fatalf("FetcherFunc.Fetch().Read() error = %v", err) |
| 54 |
} |
| 55 |
if !bytes.Equal(got, data) { |
| 56 |
t.Errorf("FetcherFunc.Fetch() = %v, want %v", got, data) |
| 57 |
} |
| 58 |
} |