Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: copy volumes and labels from spec #398

Merged
merged 2 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,19 @@ func MergeImageConfig(dst *DockerImageConfig, src *ImageConfig) error {
dst.User = src.User
}

for k, v := range src.Volumes {
if dst.Volumes == nil {
dst.Volumes = make(map[string]struct{}, len(src.Volumes))
}
dst.Volumes[k] = v
}

for k, v := range src.Labels {
if dst.Labels == nil {
dst.Labels = make(map[string]string, len(src.Labels))
}
dst.Labels[k] = v
}

return nil
}
89 changes: 89 additions & 0 deletions test/azlinux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ package test

import (
"context"
"encoding/json"
"errors"
"fmt"
"path/filepath"
"strings"
"testing"

"github.com/Azure/dalec"
"github.com/Azure/dalec/frontend/azlinux"
"github.com/google/go-cmp/cmp"
"github.com/moby/buildkit/client/llb"
"github.com/moby/buildkit/exporter/containerimage/exptypes"
gwclient "github.com/moby/buildkit/frontend/gateway/client"
moby_buildkit_v1_frontend "github.com/moby/buildkit/frontend/gateway/pb"
"gotest.tools/v3/assert"
Expand Down Expand Up @@ -1253,6 +1257,11 @@ Environment="KUBELET_KUBECONFIG_ARGS=--bootstrap-kubeconfig=/etc/kubernetes/boot
ctx := startTestSpan(baseCtx, t)
testLinuxSymlinkArtifacts(ctx, t, testConfig)
})
t.Run("test image configs", func(t *testing.T) {
t.Parallel()
ctx := startTestSpan(baseCtx, t)
testImageConfig(ctx, t, testConfig.Target.Container)
})
}

func testCustomLinuxWorker(ctx context.Context, t *testing.T, targetCfg targetConfig, workerCfg workerConfig) {
Expand Down Expand Up @@ -1685,3 +1694,83 @@ func testLinuxSymlinkArtifacts(ctx context.Context, t *testing.T, cfg testLinuxC
assert.NilError(t, err)
})
}

func testImageConfig(ctx context.Context, t *testing.T, target string, opts ...srOpt) {
spec := &dalec.Spec{
Name: "test-image-config",
Version: "0.0.1",
Revision: "42",
Description: "Test to make sure image configs are copied over",
License: "MIT",
Image: &dalec.ImageConfig{
Entrypoint: "some-entrypoint",
Cmd: "some-cmd",
Env: []string{
"ENV1=VAL1",
"ENV2=VAL2",
},
Labels: map[string]string{
"label.1": "value1",
"label.2": "value2",
},
Volumes: map[string]struct{}{
"/some/volume": {},
},
WorkingDir: "/some/work/dir",
StopSignal: "SOME-SIG",
User: "some-user",
},
}

envToMap := func(envs []string) map[string]string {
out := make(map[string]string, len(envs))
for _, env := range envs {
k, v, _ := strings.Cut(env, "=")
out[k] = v
}
return out
}

testEnv.RunTest(ctx, t, func(ctx context.Context, gwc gwclient.Client) {
opts = append(opts, withSpec(ctx, t, spec))
opts = append(opts, withBuildTarget(target))
sr := newSolveRequest(opts...)
res := solveT(ctx, t, gwc, sr)

dt, ok := res.Metadata[exptypes.ExporterImageConfigKey]
assert.Assert(t, ok, "missing image config in result metadata")

var img dalec.DockerImageSpec
err := json.Unmarshal(dt, &img)
assert.NilError(t, err)

assert.Check(t, cmp.Equal(strings.Join(img.Config.Entrypoint, " "), spec.Image.Entrypoint))
assert.Check(t, cmp.Equal(strings.Join(img.Config.Cmd, " "), spec.Image.Cmd))

// Envs are merged together with the base image
// So we need to validate that the values we've set are what we expect
// Often there will be at least one other env for `PATH` we won't check
expectEnv := envToMap(spec.Image.Env)
actualEnv := envToMap(img.Config.Env)
for k, v := range expectEnv {
assert.Check(t, cmp.Equal(actualEnv[k], v))
}

// Labels are merged with the base image
// So we need to check that the labels we've set are added
for k, v := range spec.Image.Labels {
assert.Check(t, cmp.Equal(v, img.Config.Labels[k]))
}

// Volumes are merged with the base image
// So we need to check that the volumes we've set are added
for k := range spec.Image.Volumes {
_, ok := img.Config.Volumes[k]
assert.Check(t, ok, k)
}

assert.Check(t, cmp.Equal(img.Config.WorkingDir, spec.Image.WorkingDir))
assert.Check(t, cmp.Equal(img.Config.StopSignal, spec.Image.StopSignal))
assert.Check(t, cmp.Equal(img.Config.User, spec.Image.User))
})
}
7 changes: 7 additions & 0 deletions test/windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,13 @@ echo "$BAR" > bar.txt
solveT(ctx, t, client, req)
})
})

t.Run("test image configs", func(t *testing.T) {
t.Parallel()

ctx := startTestSpan(baseCtx, t)
testImageConfig(ctx, t, buildTarget, withWindowsAmd64)
})
}

func runBuild(ctx context.Context, t *testing.T, gwc gwclient.Client, spec *dalec.Spec, srOpts ...srOpt) {
Expand Down
Loading