Skip to content

Commit e8be354

Browse files
committed
fix
1 parent a5821f9 commit e8be354

4 files changed

Lines changed: 72 additions & 23 deletions

File tree

docs/examples/go-md2man.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# syntax=local/dalec/frontend:latest
12
# syntax=ghcr.io/project-dalec/dalec/frontend:latest
23

34
name: go-md2man

frontend/test_runner.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ import (
55
"strconv"
66

77
"github.com/moby/buildkit/client/llb"
8+
"github.com/moby/buildkit/frontend/dockerui"
89
gwclient "github.com/moby/buildkit/frontend/gateway/client"
910
"github.com/pkg/errors"
1011
"github.com/project-dalec/dalec"
1112
"github.com/project-dalec/dalec/internal/testrunner"
1213
)
1314

15+
const IgnoreCacheTestsKey = "dalec.tests"
16+
1417
// RunTests runs the tests defined in the spec against the given target container.
1518
func RunTests(ctx context.Context, client gwclient.Client, sOpt dalec.SourceOpts, spec *dalec.Spec, withTestDeps llb.StateOption, target string, opts ...llb.ConstraintsOpt) llb.StateOption {
1619
return func(in llb.State) llb.State {
@@ -25,6 +28,15 @@ func RunTests(ctx context.Context, client gwclient.Client, sOpt dalec.SourceOpts
2528
}
2629
}
2730

31+
dc, err := dockerui.NewClient(client)
32+
if err != nil {
33+
return dalec.ErrorState(in, errors.Wrap(err, "failed to create docker client for test runner"))
34+
}
35+
36+
if dc.IsNoCache(IgnoreCacheTestsKey) {
37+
opts = append(opts, llb.IgnoreCache)
38+
}
39+
2840
tests := spec.Tests
2941

3042
t, ok := spec.Targets[target]

internal/testrunner/runner.go

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -162,17 +162,41 @@ func asConstraints(opts ...ValidationOpt) llb.ConstraintsOpt {
162162
})
163163
}
164164

165-
// For each llb.StateOption, set the state option on the original state
165+
// mergeStateOptions merges multiple llb.StateOptions into a single llb.StateOption.
166+
//
167+
// Each option is applied independently to the same input state.
168+
// Conceptually:
169+
//
170+
// 1. Start from origState
171+
// 2. For each option:
172+
// - create a temporary branch: diff(origState, stateN)
173+
// 3. Merge all resulting branches back together
174+
//
175+
// No option sees the effects of any other option.
176+
//
166177
// Example:
167-
// origState:
168-
// |-apply(opt) -> state1
169-
// |-apply(opt) -> state2
170-
// |-apply(opt) -> state3
171178
//
172-
// In the above example, each of the new states (state1,state2,state3) are direct decendants
179+
// input
180+
// |
181+
// +-------+-------+
182+
// | | |
183+
// apply apply apply
184+
// opt1 opt2 opt3
185+
// | | |
186+
// diff1 diff2 diff3
187+
// | | |
188+
// +-------+-------+
189+
// |
190+
// merge(input, diff1, diff2, diff3)
191+
// |
192+
// output
193+
//
194+
// All intermediate states (state1, state2, state3) are direct descendants
173195
// of origState.
174-
// If you are expecting states to apply in order (origState -> state1 -> state2 -> state3),
175-
// this is not the function you are looking for, see [withOptions] instead.
196+
//
197+
// If you expect options to apply sequentially
198+
// (origState -> state1 -> state2 -> state3),
199+
// This is NOT the function you want.
176200
func mergeStateOptions(stateOpts []llb.StateOption, opts ...ValidationOpt) llb.StateOption {
177201
return func(in llb.State) llb.State {
178202
if len(stateOpts) == 0 {
@@ -185,22 +209,12 @@ func mergeStateOptions(stateOpts []llb.StateOption, opts ...ValidationOpt) llb.S
185209
}
186210

187211
states := make([]llb.State, 0, len(opts))
212+
states = append(states, in)
188213
for _, o := range stateOpts {
189214
states = append(states, in.With(o))
190215
}
191-
return dalec.MergeAtPath(in, states, "/", asConstraints(opts...))
192-
}
193-
}
194-
195-
// withOptions applies multiple llb.StateOption to a state in order
196-
func withOptions(opts []llb.StateOption) llb.StateOption {
197-
return func(in llb.State) llb.State {
198-
out := in
199216

200-
for _, opt := range opts {
201-
out = out.With(opt)
202-
}
203-
return out
217+
return dalec.MergeAtPath(in, states, "/", asConstraints(opts...))
204218
}
205219
}
206220

test/linux_target_test.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3199,7 +3199,7 @@ func testLinuxPackageTestsFail(ctx context.Context, t *testing.T, cfg testLinuxC
31993199
spec.Tests = []*dalec.TestSpec{tc.test}
32003200

32013201
testEnv.RunTest(ctx, t, func(ctx context.Context, client gwclient.Client) {
3202-
sr := newSolveRequest(withSpec(ctx, t, spec), withBuildTarget(target))
3202+
sr := newSolveRequest(withSpec(ctx, t, spec), withBuildTarget(target), withIgnoreCache(frontend.IgnoreCacheTestsKey))
32033203
_, err := client.Solve(ctx, sr)
32043204
assert.Assert(t, err != nil)
32053205

@@ -3243,6 +3243,10 @@ func testLinuxPackageTestsFail(ctx context.Context, t *testing.T, cfg testLinuxC
32433243
t.Parallel()
32443244
ctx := startTestSpan(baseCtx, t)
32453245

3246+
equalCheck := func(v string) dalec.CheckOutput {
3247+
return dalec.CheckOutput{Equals: v}
3248+
}
3249+
32463250
spec := &dalec.Spec{
32473251
Name: "test-package-tests",
32483252
Version: "0.0.1",
@@ -3295,6 +3299,24 @@ func testLinuxPackageTestsFail(ctx context.Context, t *testing.T, cfg testLinuxC
32953299
"/some_symlink3": {LinkTarget: "/not-a-real-file", NoFollow: true},
32963300
},
32973301
},
3302+
{
3303+
Name: "Test multiple commands with no fs changes",
3304+
Steps: []dalec.TestStep{
3305+
{Command: "/bin/sh -ec 'echo command one'"},
3306+
{Command: "/bin/sh -ec 'echo command two'"},
3307+
{Command: "/bin/sh -ec 'echo command three'"},
3308+
{Command: "/bin/sh -ec 'echo command four'"},
3309+
},
3310+
},
3311+
{
3312+
Name: "Test multiple commands with stdio checks",
3313+
Steps: []dalec.TestStep{
3314+
{Command: "/bin/sh -ec 'echo command one'", Stdout: equalCheck("command one\n")},
3315+
{Command: "/bin/sh -ec 'echo command two'"},
3316+
{Command: "/bin/sh -ec 'echo command three'", Stdout: equalCheck("command three\n")},
3317+
{Command: "/bin/sh -ec 'echo command four'"},
3318+
},
3319+
},
32983320
{
32993321
Name: "Test that test mounts work",
33003322
Files: map[string]dalec.FileCheckOutput{
@@ -3370,7 +3392,7 @@ func testLinuxPackageTestsFail(ctx context.Context, t *testing.T, cfg testLinuxC
33703392
t.Parallel()
33713393
ctx = startTestSpan(baseCtx, t)
33723394
testEnv.RunTest(ctx, t, func(ctx context.Context, client gwclient.Client) {
3373-
sr := newSolveRequest(withSpec(ctx, t, spec), withBuildTarget(cfg.Target.Package))
3395+
sr := newSolveRequest(withSpec(ctx, t, spec), withBuildTarget(cfg.Target.Package), withIgnoreCache(frontend.IgnoreCacheTestsKey))
33743396
res := solveT(ctx, t, client, sr)
33753397
_, err := res.SingleRef()
33763398
assert.NilError(t, err)
@@ -3381,7 +3403,7 @@ func testLinuxPackageTestsFail(ctx context.Context, t *testing.T, cfg testLinuxC
33813403
t.Parallel()
33823404
ctx := startTestSpan(baseCtx, t)
33833405
testEnv.RunTest(ctx, t, func(ctx context.Context, client gwclient.Client) {
3384-
sr := newSolveRequest(withSpec(ctx, t, spec), withBuildTarget(cfg.Target.Container))
3406+
sr := newSolveRequest(withSpec(ctx, t, spec), withBuildTarget(cfg.Target.Container), withIgnoreCache(frontend.IgnoreCacheTestsKey))
33853407
res := solveT(ctx, t, client, sr)
33863408
_, err := res.SingleRef()
33873409
assert.NilError(t, err)

0 commit comments

Comments
 (0)