Skip to content

Commit fc6afbe

Browse files
committed
feat: implement new kres config for building integration tests
With the new steps it's now possible to generate `go test -c` build along with the multiarch image. Signed-off-by: Artem Chernyshev <artem.chernyshev@talos-systems.com>
1 parent b711b5e commit fc6afbe

6 files changed

Lines changed: 88 additions & 4 deletions

File tree

internal/project/auto/builder.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ func (builder *builder) build() error {
8484
detect: builder.DetectHelm,
8585
build: builder.BuildHelm,
8686
},
87+
{
88+
detect: builder.DetectIntegrationTests,
89+
build: builder.BuildIntegrationTests,
90+
},
8791
{ // custom should be the last in the list, so that step could be hooked up to the build
8892
detect: builder.DetectCustom,
8993
build: builder.BuildCustom,

internal/project/auto/config.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,16 @@ type Helm struct {
4545
ChartDir string `yaml:"chartDir"`
4646
Enabled bool `yaml:"enabled"`
4747
}
48+
49+
// IntegrationTests defines integration tests builder to be generated.
50+
type IntegrationTests struct {
51+
Tests []IntegrationTestConfig
52+
}
53+
54+
// IntegrationTestConfig defines the integration tests build configuration.
55+
type IntegrationTestConfig struct {
56+
Outputs map[string]map[string]string `yaml:"outputs"`
57+
Name string `yaml:"name"`
58+
Path string `yaml:"path"`
59+
EnableDockerImage bool `yaml:"enableDockerImage"`
60+
}

internal/project/auto/golang.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ func (builder *builder) BuildGolang() error {
274274
return err
275275
}
276276

277-
build := golang.NewBuild(builder.meta, cmd.Name, cmd.Path)
277+
build := golang.NewBuild(builder.meta, cmd.Name, cmd.Path, "go build")
278278
build.AddInput(toolchain)
279279
builder.targets = append(builder.targets, build)
280280

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
5+
package auto
6+
7+
import (
8+
"github.com/siderolabs/gen/maps"
9+
10+
"github.com/siderolabs/kres/internal/project/common"
11+
"github.com/siderolabs/kres/internal/project/golang"
12+
)
13+
14+
// DetectIntegrationTests checks if the Go project has any integration tests.
15+
func (builder *builder) DetectIntegrationTests() (bool, error) {
16+
var integrationTests IntegrationTests
17+
18+
if err := builder.meta.Config.Load(&integrationTests); err != nil {
19+
return false, err
20+
}
21+
22+
return len(integrationTests.Tests) > 0, nil
23+
}
24+
25+
// BuildIntegrationTests builds Go integration tests.
26+
func (builder *builder) BuildIntegrationTests() error {
27+
var integrationTests IntegrationTests
28+
29+
if err := builder.meta.Config.Load(&integrationTests); err != nil {
30+
return err
31+
}
32+
33+
for _, spec := range integrationTests.Tests {
34+
build := golang.NewBuild(builder.meta, spec.Name, spec.Path, "go test -c -covermode=atomic")
35+
36+
build.Outputs = maps.Map(spec.Outputs, func(k string, m map[string]string) (string, golang.CompileConfig) {
37+
return k, golang.CompileConfig(m)
38+
})
39+
40+
build.BuildFlags = append(build.BuildFlags, "-tags integration,sidero.debug")
41+
42+
builder.targets = append(builder.targets, build)
43+
44+
if spec.EnableDockerImage {
45+
image := common.NewImage(
46+
builder.meta, spec.Name,
47+
)
48+
49+
image.AddInput(build)
50+
51+
builder.targets = append(builder.targets, image)
52+
}
53+
}
54+
55+
return nil
56+
}

internal/project/custom/custom.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ type Step struct {
4747
Src string `yaml:"src"`
4848
Dst string `yaml:"dst"`
4949
} `yaml:"copy"`
50-
Arg string `yaml:"arg"`
50+
Arg string `yaml:"arg"`
51+
Entrypoint []string `yaml:"entrypoint"`
5152
} `yaml:"steps"`
5253
} `yaml:"stages"`
5354

@@ -170,6 +171,14 @@ func (step *Step) CompileDockerfile(output *dockerfile.Output) error {
170171
}
171172

172173
s.Step(copyStep)
174+
case stageStep.Entrypoint != nil:
175+
var args []string
176+
177+
if len(stageStep.Entrypoint) > 1 {
178+
args = stageStep.Entrypoint[1:]
179+
}
180+
181+
s.Step(dockerstep.Entrypoint(stageStep.Entrypoint[0], args...))
173182
}
174183
}
175184
}

internal/project/golang/build.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type Build struct {
3232
meta *meta.Options
3333
sourcePath string
3434
entrypoint string
35+
command string
3536
artifacts []artifact
3637
configs []CompileConfig
3738
}
@@ -55,11 +56,12 @@ func (c CompileConfig) set(script *step.RunStep) {
5556
}
5657

5758
// NewBuild initializes Build.
58-
func NewBuild(meta *meta.Options, name, sourcePath string) *Build {
59+
func NewBuild(meta *meta.Options, name, sourcePath, buildCommand string) *Build {
5960
return &Build{
6061
BaseNode: dag.NewBaseNode(name),
6162
meta: meta,
6263
sourcePath: sourcePath,
64+
command: buildCommand,
6365
}
6466
}
6567

@@ -98,7 +100,7 @@ func (build *Build) CompileDockerfile(output *dockerfile.Output) error {
98100
buildFlags = " " + strings.Join(build.BuildFlags, " ")
99101
}
100102

101-
script := step.Script(fmt.Sprintf(`go build%s -ldflags "%s" -o /%s`, buildFlags, ldflags, name)).
103+
script := step.Script(fmt.Sprintf(`%s%s -ldflags "%s" -o /%s`, build.command, buildFlags, ldflags, name)).
102104
MountCache(filepath.Join(build.meta.CachePath, "go-build"), build.meta.GitHubRepository).
103105
MountCache(filepath.Join(build.meta.GoPath, "pkg"), build.meta.GitHubRepository)
104106

0 commit comments

Comments
 (0)