Skip to content

Commit

Permalink
Fix buildctl metadata test
Browse files Browse the repository at this point in the history
  • Loading branch information
a-palchikov committed Mar 30, 2022
1 parent 4e92c6d commit d26ef62
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ RUN curl -Ls https://github.com/containernetworking/plugins/releases/download/$C

FROM buildkit-base AS integration-tests-base
ENV BUILDKIT_INTEGRATION_ROOTLESS_IDPAIR="1000:1000"
RUN apk add --no-cache shadow shadow-uidmap sudo vim iptables fuse dumb-init \
RUN apk add --no-cache shadow shadow-uidmap sudo vim iptables fuse \
&& useradd --create-home --home-dir /home/user --uid 1000 -s /bin/sh user \
&& echo "XDG_RUNTIME_DIR=/run/user/1000; export XDG_RUNTIME_DIR" >> /home/user/.profile \
&& mkdir -m 0700 -p /run/user/1000 \
Expand Down
1 change: 1 addition & 0 deletions client/exporters.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

const (
// TODO: keep these in sync with the corresponding exporter Name constants
ExporterImage = "image"
ExporterLocal = "local"
ExporterTar = "tar"
Expand Down
18 changes: 11 additions & 7 deletions cmd/buildctl/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/containerd/containerd/namespaces"
"github.com/containerd/continuity/fs/fstest"
"github.com/moby/buildkit/client/llb"
"github.com/moby/buildkit/exporter/containerimage"
"github.com/moby/buildkit/exporter/containerimage/exptypes"
"github.com/moby/buildkit/util/testutil/integration"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
Expand Down Expand Up @@ -142,20 +143,23 @@ func testBuildMetadataFile(t *testing.T, sb integration.Sandbox) {
metadataBytes, err := os.ReadFile(metadataFile)
require.NoError(t, err)

var metadata map[string]interface{}
var metadata map[string]map[string]interface{}
err = json.Unmarshal(metadataBytes, &metadata)
require.NoError(t, err)

require.Contains(t, metadata, "image.name")
require.Equal(t, imageName, metadata["image.name"])
require.Contains(t, metadata, containerimage.Name)
exporterMetadata := metadata[containerimage.Name]

require.Contains(t, metadata, exptypes.ExporterImageDigestKey)
digest := metadata[exptypes.ExporterImageDigestKey]
require.Contains(t, exporterMetadata, "image.name")
require.Equal(t, imageName, exporterMetadata["image.name"])

require.Contains(t, exporterMetadata, exptypes.ExporterImageDigestKey)
digest := exporterMetadata[exptypes.ExporterImageDigestKey]
require.NotEmpty(t, digest)

require.Contains(t, metadata, exptypes.ExporterImageDescriptorKey)
require.Contains(t, exporterMetadata, exptypes.ExporterImageDescriptorKey)
var desc *ocispecs.Descriptor
dtdesc, err := json.Marshal(metadata[exptypes.ExporterImageDescriptorKey])
dtdesc, err := json.Marshal(exporterMetadata[exptypes.ExporterImageDescriptorKey])
require.NoError(t, err)
err = json.Unmarshal(dtdesc, &desc)
require.NoError(t, err)
Expand Down
6 changes: 6 additions & 0 deletions exporter/containerimage/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import (
)

const (
Name = "image"

keyImageName = "name"
keyPush = "push"
keyPushByDigest = "push-by-digest"
Expand Down Expand Up @@ -242,6 +244,10 @@ func (e *imageExporterInstance) Name() string {
return "exporting to image"
}

func (e *imageExporterInstance) ID() string {
return Name
}

func (e *imageExporterInstance) Config() exporter.Config {
return exporter.Config{
Compression: e.compression(),
Expand Down
1 change: 1 addition & 0 deletions exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Exporter interface {

type ExporterInstance interface {
Name() string
ID() string
Config() Config
Export(ctx context.Context, src Source, sessionID string) (map[string]string, error)
}
Expand Down
6 changes: 6 additions & 0 deletions exporter/local/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"golang.org/x/time/rate"
)

const Name = "local"

type Opt struct {
SessionManager *session.Manager
}
Expand All @@ -45,6 +47,10 @@ func (e *localExporterInstance) Name() string {
return "exporting to client"
}

func (e *localExporterInstance) ID() string {
return Name
}

func (e *localExporter) Config() exporter.Config {
return exporter.Config{}
}
Expand Down
4 changes: 4 additions & 0 deletions exporter/oci/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ func (e *imageExporterInstance) Name() string {
return "exporting to oci image format"
}

func (e *imageExporterInstance) ID() string {
return string(e.opt.Variant)
}

func (e *imageExporterInstance) Config() exporter.Config {
return exporter.Config{
Compression: e.compression(),
Expand Down
6 changes: 6 additions & 0 deletions exporter/tar/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
)

const (
Name = "tar"

// preferNondistLayersKey is an exporter option which can be used to mark a layer as non-distributable if the layer reference was
// already found to use a non-distributable media type.
// When this option is not set, the exporter will change the media type of the layer to a distributable one.
Expand Down Expand Up @@ -64,6 +66,10 @@ func (e *localExporterInstance) Name() string {
return "exporting to client"
}

func (e *localExporterInstance) ID() string {
return Name
}

func (e *localExporterInstance) Config() exporter.Config {
return exporter.Config{}
}
Expand Down
2 changes: 1 addition & 1 deletion hack/test
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ if ! docker container inspect "$cacheVolume" >/dev/null 2>/dev/null; then
fi

if [ "$TEST_INTEGRATION" == 1 ]; then
cid=$(docker create --rm -v /tmp $coverageVol --volumes-from=$cacheVolume -e TEST_DOCKERD -e SKIP_INTEGRATION_TESTS ${BUILDKIT_INTEGRATION_SNAPSHOTTER:+"-eBUILDKIT_INTEGRATION_SNAPSHOTTER"} -e BUILDKIT_REGISTRY_MIRROR_DIR=/root/.cache/registry --privileged $iid dumb-init go test $coverageFlags ${TESTFLAGS:--v} ${TESTPKGS:-./...})
cid=$(docker create --rm -v /tmp $coverageVol --volumes-from=$cacheVolume -e TEST_DOCKERD -e SKIP_INTEGRATION_TESTS ${BUILDKIT_INTEGRATION_SNAPSHOTTER:+"-eBUILDKIT_INTEGRATION_SNAPSHOTTER"} -e BUILDKIT_REGISTRY_MIRROR_DIR=/root/.cache/registry --privileged $iid go test $coverageFlags ${TESTFLAGS:--v} ${TESTPKGS:-./...})
if [ "$TEST_DOCKERD" = "1" ]; then
docker cp "$TEST_DOCKERD_BINARY" $cid:/usr/bin/dockerd
fi
Expand Down
5 changes: 2 additions & 3 deletions solver/llbsolver/solver.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func (s *Solver) Solve(ctx context.Context, id string, sessionID string, req fro
inp.Metadata = make(map[string][]byte)
}
var cr solver.CachedResult
crMap := map[string]solver.CachedResult{}
var crMap = map[string]solver.CachedResult{}
if res := res.Ref; res != nil {
r, err := res.Result(ctx)
if err != nil {
Expand Down Expand Up @@ -262,7 +262,7 @@ func (s *Solver) Solve(ctx context.Context, id string, sessionID string, req fro
return err
}
if resp != nil {
exportersResponse[exp.Name()] = resp
exportersResponse[exp.ID()] = resp
}
return nil
}); err != nil {
Expand Down Expand Up @@ -401,7 +401,6 @@ func defaultResolver(wc *worker.Controller) ResolveWorkerFunc {
return wc.GetDefault()
}
}

func allWorkers(wc *worker.Controller) func(func(w worker.Worker) error) error {
return func(f func(worker.Worker) error) error {
all, err := wc.List()
Expand Down

0 comments on commit d26ef62

Please sign in to comment.