| 1 |
/* |
| 2 |
Copyright The containerd Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package zstdchunked |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"testing" |
| 22 |
|
| 23 |
"runtime/debug" |
| 24 |
|
| 25 |
"github.com/containerd/containerd/images" |
| 26 |
"github.com/containerd/containerd/images/converter" |
| 27 |
"github.com/containerd/containerd/platforms" |
| 28 |
"github.com/containerd/stargz-snapshotter/estargz" |
| 29 |
"github.com/containerd/stargz-snapshotter/estargz/zstdchunked" |
| 30 |
"github.com/containerd/stargz-snapshotter/util/testutil" |
| 31 |
ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 32 |
) |
| 33 |
|
| 34 |
// TestLayerConvertFunc tests zstd:chunked conversion. |
| 35 |
// TestLayerConvertFunc is a pure unit test that does not need the daemon to be running. |
| 36 |
func TestLayerConvertFunc(t *testing.T) { |
| 37 |
ctx := context.Background() |
| 38 |
desc, cs, err := testutil.EnsureHello(ctx) |
| 39 |
if err != nil { |
| 40 |
t.Fatal(err) |
| 41 |
} |
| 42 |
|
| 43 |
lcf := LayerConvertFunc(estargz.WithPrioritizedFiles([]string{"hello"})) |
| 44 |
docker2oci := true |
| 45 |
platformMC := platforms.DefaultStrict() |
| 46 |
cf := converter.DefaultIndexConvertFunc(lcf, docker2oci, platformMC) |
| 47 |
|
| 48 |
newDesc, err := cf(ctx, cs, *desc) |
| 49 |
if err != nil { |
| 50 |
t.Log(string(debug.Stack())) |
| 51 |
t.Fatal(err) |
| 52 |
} |
| 53 |
|
| 54 |
metadata := make(map[string]string) |
| 55 |
mt := make(map[string]struct{}) |
| 56 |
handler := func(hCtx context.Context, hDesc ocispec.Descriptor) ([]ocispec.Descriptor, error) { |
| 57 |
mt[hDesc.MediaType] = struct{}{} |
| 58 |
for k, v := range hDesc.Annotations { |
| 59 |
if k == estargz.TOCJSONDigestAnnotation || |
| 60 |
k == zstdchunked.ManifestChecksumAnnotation || |
| 61 |
k == zstdchunked.ManifestPositionAnnotation { |
| 62 |
metadata[k] = v |
| 63 |
} |
| 64 |
} |
| 65 |
return nil, nil |
| 66 |
} |
| 67 |
handlers := images.Handlers( |
| 68 |
images.ChildrenHandler(cs), |
| 69 |
images.HandlerFunc(handler), |
| 70 |
) |
| 71 |
if err := images.Walk(ctx, handlers, *newDesc); err != nil { |
| 72 |
t.Fatal(err) |
| 73 |
} |
| 74 |
|
| 75 |
if _, ok := mt[ocispec.MediaTypeImageLayerZstd]; !ok { |
| 76 |
t.Errorf("mediatype %q is not created", ocispec.MediaTypeImageLayerZstd) |
| 77 |
} |
| 78 |
if _, ok := metadata[estargz.TOCJSONDigestAnnotation]; !ok { |
| 79 |
t.Errorf("%q is not set", estargz.TOCJSONDigestAnnotation) |
| 80 |
} |
| 81 |
if _, ok := metadata[zstdchunked.ManifestChecksumAnnotation]; !ok { |
| 82 |
t.Errorf("%q is not set", zstdchunked.ManifestChecksumAnnotation) |
| 83 |
} |
| 84 |
if _, ok := metadata[zstdchunked.ManifestPositionAnnotation]; !ok { |
| 85 |
t.Errorf("%q is not set", zstdchunked.ManifestPositionAnnotation) |
| 86 |
} |
| 87 |
} |