-
Notifications
You must be signed in to change notification settings - Fork 95
/
test.go
160 lines (140 loc) · 5.88 KB
/
test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright 2023 Chainguard, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cli
import (
"context"
"errors"
"fmt"
"log"
apko_types "chainguard.dev/apko/pkg/build/types"
"chainguard.dev/melange/pkg/build"
"github.com/spf13/cobra"
"go.opentelemetry.io/otel"
"golang.org/x/sync/errgroup"
)
func Test() *cobra.Command {
var workspaceDir string
var sourceDir string
var cacheDir string
var cacheSource string
var apkCacheDir string
var guestDir string
var archstrs []string
var pipelineDirs []string
var extraKeys []string
var extraRepos []string
var overlayBinSh string
var testOption []string
var logPolicy []string
var debug bool
var debugRunner bool
var runner string
cmd := &cobra.Command{
Use: "test",
Short: "Test a package with a YAML configuration file",
Long: `Test a package from a YAML configuration file containing a test pipeline.`,
Example: ` melange test <test.yaml> [package-name]`,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
archs := apko_types.ParseArchitectures(archstrs)
options := []build.TestOption{
build.WithTestWorkspaceDir(workspaceDir),
build.WithTestCacheDir(cacheDir),
build.WithTestCacheSource(cacheSource),
build.WithTestPackageCacheDir(apkCacheDir),
build.WithTestGuestDir(guestDir),
build.WithTestExtraKeys(extraKeys),
build.WithTestExtraRepos(extraRepos),
build.WithTestBinShOverlay(overlayBinSh),
build.WithTestRunner(runner),
build.WithTestDebug(debug),
build.WithTestDebugRunner(debugRunner),
}
if len(args) > 0 {
options = append(options, build.WithTestConfig(args[0]))
}
if len(args) > 1 {
options = append(options, build.WithTestPackage(args[1]))
}
if sourceDir != "" {
options = append(options, build.WithTestSourceDir(sourceDir))
}
for i := range pipelineDirs {
options = append(options, build.WithTestPipelineDir(pipelineDirs[i]))
}
options = append(options, build.WithTestPipelineDir(BuiltinPipelineDir))
return TestCmd(cmd.Context(), archs, options...)
},
}
cmd.Flags().StringVar(&workspaceDir, "workspace-dir", "", "directory used for the workspace at /home/build")
cmd.Flags().StringSliceVar(&pipelineDirs, "pipeline-dirs", []string{}, "directories used to extend defined built-in pipelines")
cmd.Flags().StringVar(&sourceDir, "source-dir", "", "directory used for included sources")
cmd.Flags().StringVar(&cacheDir, "cache-dir", "", "directory used for cached inputs")
cmd.Flags().StringVar(&cacheSource, "cache-source", "", "directory or bucket used for preloading the cache")
cmd.Flags().StringVar(&apkCacheDir, "apk-cache-dir", "", "directory used for cached apk packages (default is system-defined cache directory)")
cmd.Flags().StringVar(&guestDir, "guest-dir", "", "directory used for the build environment guest")
cmd.Flags().StringVar(&overlayBinSh, "overlay-binsh", "", "use specified file as /bin/sh overlay in build environment")
cmd.Flags().StringSliceVar(&archstrs, "arch", nil, "architectures to build for (e.g., x86_64,ppc64le,arm64) -- default is all, unless specified in config")
cmd.Flags().StringSliceVar(&testOption, "test-option", []string{}, "build options to enable")
cmd.Flags().StringSliceVar(&logPolicy, "log-policy", []string{"builtin:stderr"}, "logging policy to use")
cmd.Flags().StringVar(&runner, "runner", string(build.GetDefaultRunner()), fmt.Sprintf("which runner to use to enable running commands, default is based on your platform. Options are %q", build.GetAllRunners()))
cmd.Flags().StringSliceVarP(&extraKeys, "keyring-append", "k", []string{}, "path to extra keys to include in the build environment keyring")
cmd.Flags().BoolVar(&debug, "debug", false, "enables debug logging of test pipelines (sets -x for steps)")
cmd.Flags().BoolVar(&debugRunner, "debug-runner", false, "when enabled, the builder pod will persist after the build succeeds or fails")
cmd.Flags().StringSliceVarP(&extraRepos, "repository-append", "r", []string{}, "path to extra repositories to include in the build environment")
return cmd
}
func TestCmd(ctx context.Context, archs []apko_types.Architecture, baseOpts ...build.TestOption) error {
ctx, span := otel.Tracer("melange").Start(ctx, "TestCmd")
defer span.End()
if len(archs) == 0 {
archs = apko_types.AllArchs
}
// Set up the test contexts before running them. This avoids various
// race conditions and the possibility that a context may be garbage
// collected before it is actually run.
//
// Yes, this happens. Really.
// https://github.com/distroless/nginx/runs/7219233843?check_suite_focus=true
bcs := []*build.Test{}
for _, arch := range archs {
opts := []build.TestOption{build.WithTestArch(arch)}
opts = append(opts, baseOpts...)
bc, err := build.NewTest(ctx, opts...)
if errors.Is(err, build.ErrSkipThisArch) {
log.Printf("skipping arch %s", arch)
continue
} else if err != nil {
return err
}
bcs = append(bcs, bc)
}
if len(bcs) == 0 {
log.Printf("WARNING: target-architecture and --arch do not overlap, nothing to test")
return nil
}
var errg errgroup.Group
for _, bc := range bcs {
bc := bc
errg.Go(func() error {
if err := bc.TestPackage(ctx); err != nil {
log.Printf("ERROR: failed to test package. the test environment has been preserved:")
bc.SummarizePaths()
return fmt.Errorf("failed to build package: %w", err)
}
return nil
})
}
return errg.Wait()
}