| 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 estargz |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"testing" |
| 22 |
|
| 23 |
"github.com/containerd/containerd/images" |
| 24 |
"github.com/containerd/containerd/images/converter" |
| 25 |
"github.com/containerd/containerd/platforms" |
| 26 |
"github.com/containerd/stargz-snapshotter/estargz" |
| 27 |
"github.com/containerd/stargz-snapshotter/util/testutil" |
| 28 |
ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 29 |
) |
| 30 |
|
| 31 |
// TestLayerConvertFunc tests eStargz conversion. |
| 32 |
// TestLayerConvertFunc is a pure unit test that does not need the daemon to be running. |
| 33 |
func TestLayerConvertFunc(t *testing.T) { |
| 34 |
ctx := context.Background() |
| 35 |
desc, cs, err := testutil.EnsureHello(ctx) |
| 36 |
if err != nil { |
| 37 |
t.Fatal(err) |
| 38 |
} |
| 39 |
|
| 40 |
lcf := LayerConvertFunc(estargz.WithPrioritizedFiles([]string{"hello"})) |
| 41 |
docker2oci := true |
| 42 |
platformMC := platforms.DefaultStrict() |
| 43 |
cf := converter.DefaultIndexConvertFunc(lcf, docker2oci, platformMC) |
| 44 |
|
| 45 |
newDesc, err := cf(ctx, cs, *desc) |
| 46 |
if err != nil { |
| 47 |
t.Fatal(err) |
| 48 |
} |
| 49 |
|
| 50 |
var tocDigests []string |
| 51 |
handler := func(hCtx context.Context, hDesc ocispec.Descriptor) ([]ocispec.Descriptor, error) { |
| 52 |
if hDesc.Annotations != nil { |
| 53 |
if x, ok := hDesc.Annotations[estargz.TOCJSONDigestAnnotation]; ok && len(x) > 0 { |
| 54 |
tocDigests = append(tocDigests, x) |
| 55 |
} |
| 56 |
} |
| 57 |
return nil, nil |
| 58 |
} |
| 59 |
handlers := images.Handlers( |
| 60 |
images.ChildrenHandler(cs), |
| 61 |
images.HandlerFunc(handler), |
| 62 |
) |
| 63 |
if err := images.Walk(ctx, handlers, *newDesc); err != nil { |
| 64 |
t.Fatal(err) |
| 65 |
} |
| 66 |
|
| 67 |
if len(tocDigests) == 0 { |
| 68 |
t.Fatal("no eStargz layer was created") |
| 69 |
} |
| 70 |
} |