File: client_nydus_test.go

package info (click to toggle)
docker.io 26.1.5%2Bdfsg1-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 68,644 kB
  • sloc: sh: 5,748; makefile: 912; ansic: 664; asm: 228; python: 162
file content (140 lines) | stat: -rw-r--r-- 4,126 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//go:build nydus
// +build nydus

package client

import (
	"fmt"
	"testing"

	"github.com/containerd/containerd/images"
	"github.com/containerd/containerd/namespaces"
	"github.com/containerd/nydus-snapshotter/pkg/converter"
	"github.com/moby/buildkit/client/llb"
	"github.com/moby/buildkit/identity"
	"github.com/moby/buildkit/util/compression"
	"github.com/moby/buildkit/util/testutil/integration"
	"github.com/moby/buildkit/util/testutil/workers"
	ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
	"github.com/pkg/errors"
	"github.com/stretchr/testify/require"
)

func init() {
	allTests = append(
		allTests,
		testBuildExportNydusWithHybrid,
	)
}

func testBuildExportNydusWithHybrid(t *testing.T, sb integration.Sandbox) {
	workers.CheckFeatureCompat(t, sb, workers.FeatureDirectPush)
	requiresLinux(t)

	cdAddress := sb.ContainerdAddress()
	if cdAddress == "" {
		t.Skip("test requires containerd worker")
	}

	client, err := newContainerd(cdAddress)
	require.NoError(t, err)
	defer client.Close()
	registry, err := sb.NewRegistry()
	if errors.Is(err, integration.ErrRequirements) {
		t.Skip(err.Error())
	}
	require.NoError(t, err)

	var (
		imageService = client.ImageService()
		contentStore = client.ContentStore()
		ctx          = namespaces.WithNamespace(sb.Context(), "buildkit")
	)

	c, err := New(sb.Context(), sb.Address())
	require.NoError(t, err)
	defer c.Close()

	buildNydus := func(file string) {
		orgImage := "docker.io/library/alpine:latest"
		baseDef := llb.Image(orgImage).Run(llb.Args([]string{"/bin/touch", "/" + file}))
		def, err := baseDef.Marshal(sb.Context())
		require.NoError(t, err)

		target := registry + "/nydus/alpine:" + identity.NewID()
		_, err = c.Solve(sb.Context(), def, SolveOpt{
			Exports: []ExportEntry{
				{
					Type: ExporterImage,
					Attrs: map[string]string{
						"name":              target,
						"push":              "true",
						"compression":       "nydus",
						"oci-mediatypes":    "true",
						"force-compression": "true",
					},
				},
			},
		}, nil)
		require.NoError(t, err)

		img, err := imageService.Get(ctx, target)
		require.NoError(t, err)

		manifest, err := images.Manifest(ctx, contentStore, img.Target, nil)
		require.NoError(t, err)

		require.Equal(t, len(manifest.Layers), 3)
		require.Equal(t, "true", manifest.Layers[0].Annotations[converter.LayerAnnotationNydusBlob])
		require.Equal(t, "true", manifest.Layers[1].Annotations[converter.LayerAnnotationNydusBlob])
		require.Equal(t, "true", manifest.Layers[2].Annotations[converter.LayerAnnotationNydusBootstrap])
	}

	buildOther := func(file string, compType compression.Type) {
		orgImage := "docker.io/library/alpine:latest"
		baseDef := llb.Image(orgImage).Run(llb.Args([]string{"/bin/touch", "/" + file}))
		def, err := baseDef.Marshal(sb.Context())
		require.NoError(t, err)

		mediaTypes := map[compression.Type]string{
			compression.Gzip: ocispecs.MediaTypeImageLayerGzip,
			compression.Zstd: ocispecs.MediaTypeImageLayer + "+zstd",
		}
		target := fmt.Sprintf("%s/%s/alpine:%s", registry, compType, identity.NewID())
		_, err = c.Solve(sb.Context(), def, SolveOpt{
			Exports: []ExportEntry{
				{
					Type: ExporterImage,
					Attrs: map[string]string{
						"name":              target,
						"push":              "true",
						"compression":       compType.String(),
						"oci-mediatypes":    "true",
						"force-compression": "true",
					},
				},
			},
		}, nil)
		require.NoError(t, err)

		img, err := imageService.Get(ctx, target)
		require.NoError(t, err)

		manifest, err := images.Manifest(ctx, contentStore, img.Target, nil)
		require.NoError(t, err)

		require.Equal(t, 2, len(manifest.Layers))
		require.Equal(t, mediaTypes[compType], manifest.Layers[0].MediaType)
		require.Equal(t, mediaTypes[compType], manifest.Layers[1].MediaType)
	}

	// Make sure that the nydus compression layer is not mixed with other
	// types of compression layers in an image.
	buildNydus("foo")
	buildOther("foo", compression.Gzip)
	buildOther("foo", compression.Zstd)

	buildOther("bar", compression.Gzip)
	buildOther("bar", compression.Zstd)
	buildNydus("bar")
}