Skip to content

Commit

Permalink
Actually use test depdenencies
Browse files Browse the repository at this point in the history
We were never installing test dependencies before.
Installing test deps requires some careful handling to not modify
containers too much and as such should be used only when neccessary.

Test dependencies do not alter the output container, only the container
under test, and only when there are commands that need to be run in the
container since the built-in test handlers do not require any extra
tooling.

Signed-off-by: Brian Goff <[email protected]>
  • Loading branch information
cpuguy83 committed May 9, 2024
1 parent ad5dfaf commit db22914
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 20 deletions.
2 changes: 1 addition & 1 deletion docs/spec.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@
"type": "string"
},
"type": "array",
"description": "Test lists any extra packages required for running tests"
"description": "Test lists any extra packages required for running tests\nThese packages are only installed for tests which have steps that require\nrunning a command in the built container.\nSee [TestSpec] for more information."
}
},
"additionalProperties": false,
Expand Down
22 changes: 20 additions & 2 deletions frontend/mariner2/handle_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ func handleContainer(ctx context.Context, client gwclient.Client) (*gwclient.Res
return nil, nil, fmt.Errorf("error creating rpm: %w", err)
}

st, err := specToContainerLLB(spec, targetKey, getWorkerImage(client, pg), rpmDir, sOpt, pg)
worker := getWorkerImage(client, pg)
st, err := specToContainerLLB(spec, targetKey, worker, rpmDir, sOpt, pg)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -60,7 +61,7 @@ func handleContainer(ctx context.Context, client gwclient.Client) (*gwclient.Res
return nil, nil, err
}

if err := frontend.RunTests(ctx, client, spec, ref, targetKey); err != nil {
if err := frontend.RunTests(ctx, client, spec, ref, installTestDeps(worker, spec, targetKey, pg), targetKey); err != nil {
return nil, nil, err
}

Expand Down Expand Up @@ -191,3 +192,20 @@ type runOptionFunc func(*llb.ExecInfo)
func (f runOptionFunc) SetRunOption(ei *llb.ExecInfo) {
f(ei)
}

func installTestDeps(worker llb.State, spec *dalec.Spec, targetKey string, opts ...llb.ConstraintsOpt) llb.StateOption {
return func(in llb.State) llb.State {
deps := spec.GetTestDeps(targetKey)
if len(deps) == 0 {
return in
}
opts = append(opts, dalec.ProgressGroup("Install test deps"))

const workPath = "/tmp/rootfs"
return worker.Run(
installPackgesRunOpts(workPath, deps, true),
tdnfCacheMountWithPrefix(workPath),
dalec.WithConstraints(opts...),
).AddMount(workPath, in)
}
}
21 changes: 13 additions & 8 deletions frontend/mariner2/handle_rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,21 @@ func installBuildDeps(spec *dalec.Spec, targetKey string, opts ...llb.Constraint
if len(deps) == 0 {
return in
}

opts = append(opts, dalec.ProgressGroup("Install build deps"))
return in.
Run(
shArgs(fmt.Sprintf("tdnf install --releasever=2.0 -y %s", strings.Join(deps, " "))),
defaultTdnfCacheMount(),
dalec.WithConstraints(opts...),
).
Root()
return in.Run(
installPackgesRunOpts("/", deps, false),
defaultTdnfCacheMount(),
dalec.WithConstraints(opts...),
).Root()
}
}

func installPackgesRunOpts(root string, packages []string, cleanup bool) llb.RunOption {
cmd := fmt.Sprintf("set -e; tdnf install --installroot=%q --releasever=2.0 -y %s", root, strings.Join(packages, " "))
if cleanup {
cmd += fmt.Sprintf("; rm -rf %q", filepath.Join(root, "/var/lib/rpm"))
}
return shArgs(cmd)
}

func specToRpmLLB(spec *dalec.Spec, sOpt dalec.SourceOpts, targetKey string, opts ...llb.ConstraintsOpt) (llb.State, error) {
Expand Down
9 changes: 6 additions & 3 deletions frontend/test_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

// Run tests runs the tests defined in the spec against the given target container.
func RunTests(ctx context.Context, client gwclient.Client, spec *dalec.Spec, ref gwclient.Reference, target string) error {
func RunTests(ctx context.Context, client gwclient.Client, spec *dalec.Spec, ref gwclient.Reference, withTestDeps llb.StateOption, target string) error {
tests := spec.Tests

t, ok := spec.Targets[target]
Expand Down Expand Up @@ -45,6 +45,8 @@ func RunTests(ctx context.Context, client gwclient.Client, spec *dalec.Spec, ref
stdios map[int]llb.State
}

ctrWithDeps := ctr.With(withTestDeps)

runs := make([]testPair, 0, len(tests))
for _, test := range tests {
base := ctr
Expand All @@ -67,6 +69,7 @@ func RunTests(ctx context.Context, client gwclient.Client, spec *dalec.Spec, ref

opts = append(opts, pg)
if len(test.Steps) > 0 {
exec := ctrWithDeps
var worker llb.State
var needsStdioMount bool
ios := map[int]llb.State{}
Expand Down Expand Up @@ -117,9 +120,9 @@ func RunTests(ctx context.Context, client gwclient.Client, spec *dalec.Spec, ref

var est llb.ExecState
if i == 0 {
est = base.Run(stepOpts...)
est = exec.Run(stepOpts...)
} else {
est = worker.Run(stepOpts...)
est = exec.Run(stepOpts...)
}
if needsStdioMount {
ioSt = est.AddMount(filepath.Join("/tmp", id), ioSt)
Expand Down
22 changes: 17 additions & 5 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dalec
import (
"encoding/json"
"path"
"slices"
"sort"
"sync/atomic"

Expand Down Expand Up @@ -294,14 +295,25 @@ func (s *Spec) GetBuildDeps(targetKey string) []string {
}
}

var out []string
for p := range deps.Build {
out = append(out, p)
return SortMapKeys(deps.Build)
}

func (s *Spec) GetTestDeps(targetKey string) []string {
var deps *PackageDependencies
if t, ok := s.Targets[targetKey]; ok {
deps = t.Dependencies
}

sort.Strings(out)
return out
if deps == nil {
deps = s.Dependencies
if deps == nil {
return nil
}
}

out := slices.Clone(deps.Test)
slices.Sort(out)
return out
}

func (s *Spec) GetSymlinks(target string) map[string]SymlinkTarget {
Expand Down
3 changes: 3 additions & 0 deletions spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,9 @@ type PackageDependencies struct {
Recommends map[string][]string `yaml:"recommends,omitempty" json:"recommends,omitempty"`

// Test lists any extra packages required for running tests
// These packages are only installed for tests which have steps that require
// running a command in the built container.
// See [TestSpec] for more information.
Test []string `yaml:"test,omitempty" json:"test,omitempty"`
}

Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/moby-runc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ targets: # Distro specific build requirements
pkgconfig:
tar:
runtime:
/bin/sh:
libseccomp:
- ">= 2.3"
test:
- /bin/sh
tests:
- name: mariner rpm manifest files
files:
Expand Down

0 comments on commit db22914

Please sign in to comment.