Skip to content

Commit

Permalink
test: add test case for multi-platform scratch
Browse files Browse the repository at this point in the history
Signed-off-by: Justin Chadwell <[email protected]>
  • Loading branch information
jedevc committed Jan 8, 2024
1 parent 7c0f37b commit fc55dfb
Showing 1 changed file with 99 additions and 25 deletions.
124 changes: 99 additions & 25 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2266,49 +2266,123 @@ func testBuildExportScratch(t *testing.T, sb integration.Sandbox) {
require.NoError(t, err)
defer c.Close()

st := llb.Scratch()
def, err := st.Marshal(sb.Context())
require.NoError(t, err)

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

makeFrontend := func(ps []string) func(ctx context.Context, c gateway.Client) (*gateway.Result, error) {
return func(ctx context.Context, c gateway.Client) (*gateway.Result, error) {
st := llb.Scratch()
def, err := st.Marshal(sb.Context())
require.NoError(t, err)

r, err := c.Solve(ctx, gateway.SolveRequest{
Definition: def.ToPB(),
})
if err != nil {
return nil, err
}

ref, err := r.SingleRef()
if err != nil {
return nil, err
}

res := gateway.NewResult()
if ps == nil {
res.SetRef(ref)
} else {
for _, p := range ps {
res.AddRef(p, ref)
}

expPlatforms := &exptypes.Platforms{
Platforms: make([]exptypes.Platform, len(ps)),
}
for i, pk := range ps {
p := platforms.MustParse(pk)

var img ocispecs.Image
img.Platform = p
config, err := json.Marshal(img)
if err != nil {
return nil, errors.Wrapf(err, "failed to marshal image config")
}
res.AddMeta(fmt.Sprintf("%s/%s", exptypes.ExporterImageConfigKey, pk), config)

expPlatforms.Platforms[i] = exptypes.Platform{
ID: pk,
Platform: p,
}
}
dt, err := json.Marshal(expPlatforms)
if err != nil {
return nil, err
}
res.AddMeta(exptypes.ExporterPlatformsKey, dt)
}

return res, nil
}
}

target := registry + "/buildkit/build/exporter:withnocompressed"

_, err = c.Solve(sb.Context(), def, SolveOpt{
_, err = c.Build(sb.Context(), SolveOpt{
Exports: []ExportEntry{
{
Type: ExporterImage,
Attrs: map[string]string{
"name": target,
"push": "true",
"unpack": "true",
"compression": "uncompressed",
"name": target,
"push": "true",
"unpack": "true",
"compression": "uncompressed",
"attest:provenance": "mode=max",
},
},
},
}, nil)
}, "", makeFrontend(nil), nil)
require.NoError(t, err)

ctx := namespaces.WithNamespace(sb.Context(), "buildkit")
cdAddress := sb.ContainerdAddress()
var client *containerd.Client
if cdAddress != "" {
client, err = newContainerd(cdAddress)
require.NoError(t, err)
defer client.Close()
targetMulti := registry + "/buildkit/build/exporter-multi:withnocompressed"
_, err = c.Build(sb.Context(), SolveOpt{
Exports: []ExportEntry{
{
Type: ExporterImage,
Attrs: map[string]string{
"name": targetMulti,
"push": "true",
"unpack": "true",
"compression": "uncompressed",
"attest:provenance": "mode=max",
},
},
},
}, "", makeFrontend([]string{"linux/amd64", "linux/arm64"}), nil)
require.NoError(t, err)

img, err := client.GetImage(ctx, target)
require.NoError(t, err)
mfst, err := images.Manifest(ctx, client.ContentStore(), img.Target(), nil)
require.NoError(t, err)
require.Equal(t, 0, len(mfst.Layers))
err = client.ImageService().Delete(ctx, target, images.SynchronousDelete())
require.NoError(t, err)
}
desc, provider, err := contentutil.ProviderFromRef(target)
require.NoError(t, err)
imgs, err := testutil.ReadImages(sb.Context(), provider, desc)
require.NoError(t, err)
require.Len(t, imgs.Images, 1)
img := imgs.Find(platforms.DefaultString())
require.Empty(t, img.Layers)
require.Equal(t, platforms.DefaultSpec(), img.Img.Platform)

desc, provider, err = contentutil.ProviderFromRef(targetMulti)
require.NoError(t, err)
imgs, err = testutil.ReadImages(sb.Context(), provider, desc)
require.NoError(t, err)
require.Len(t, imgs.Images, 2)
img = imgs.Find("linux/amd64")
require.Empty(t, img.Layers)
require.Equal(t, "linux/amd64", platforms.Format(img.Img.Platform))
img = imgs.Find("linux/arm64")
require.Empty(t, img.Layers)
require.Equal(t, "linux/arm64", platforms.Format(img.Img.Platform))
}

func testBuildHTTPSource(t *testing.T, sb integration.Sandbox) {
Expand Down

0 comments on commit fc55dfb

Please sign in to comment.