-
Notifications
You must be signed in to change notification settings - Fork 883
Expand file tree
/
Copy pathtest.go
More file actions
325 lines (295 loc) · 8.74 KB
/
test.go
File metadata and controls
325 lines (295 loc) · 8.74 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package main
import (
"context"
"crypto/rand"
"fmt"
"path/filepath"
"dagger/engine-dev/internal/dagger"
"github.com/dagger/dagger/engine/distconsts"
)
// List all core engine tests
func (dev *EngineDev) Tests(ctx context.Context) (string, error) {
return dag.Go(dagger.GoOpts{Source: dev.Source}).Tests(ctx)
}
// Run core engine tests
// +cache="session"
func (dev *EngineDev) Test(
ctx context.Context,
// Only run these tests
// +optional
run string,
// Skip these tests
// +optional
skip string,
// +optional
// +default="./..."
pkg string,
// Abort test run on first failure
// +optional
failfast bool,
// How many tests to run in parallel - defaults to the number of CPUs
// +optional
parallel int,
// How long before timing out the test run
// +optional
timeout string,
// +optional
race bool,
// +default=1
// +optional
count int,
// +optional
envFile *dagger.Secret,
// Enable verbose output
// +optional
testVerbose bool,
// Update golden files
// +optional
update bool,
// Enable the given ebpf progs in the engine during tests
// +optional
ebpfProgs []string,
) error {
// FIXME: use the damn standard Go toolchain
ctr, _, err := dev.testContainer(ctx, ebpfProgs)
if err != nil {
return err
}
_, err = dev.test(ctr, &testOpts{
runTestRegex: run,
skipTestRegex: skip,
pkg: pkg,
failfast: failfast,
parallel: parallel,
timeout: timeout,
race: race,
count: count,
envs: envFile,
testVerbose: testVerbose,
update: update,
},
).Sync(ctx)
return err
}
// Run telemetry tests
// +cache="session"
func (dev *EngineDev) TestTelemetry(
ctx context.Context,
// Only run these tests
// +optional
run string,
// Skip these tests
// +optional
skip string,
// +optional
update bool,
// +optional
failfast bool,
// +optional
parallel int,
// +optional
timeout string,
// +optional
race bool,
// +default=1
count int,
// +optional
envFile *dagger.Secret,
// +optional
testVerbose bool,
// Enable the given ebpf progs in the engine during tests
// +optional
ebpfProgs []string,
) (*dagger.Changeset, error) {
ctr, _, err := dev.testContainer(ctx, ebpfProgs)
if err != nil {
return nil, err
}
ran, err := dev.test(ctr, &testOpts{
runTestRegex: run,
skipTestRegex: skip,
pkg: "./dagql/idtui/",
failfast: failfast,
parallel: parallel,
timeout: timeout,
race: race,
count: count,
update: update,
envs: envFile,
testVerbose: testVerbose,
},
).Sync(ctx)
if err != nil {
return nil, err
}
return ran.Directory(".").Changes(ctr.Directory(".")), nil
}
type testOpts struct {
runTestRegex string
skipTestRegex string
pkg string
failfast bool
parallel int
timeout string
race bool
count int
update bool
envs *dagger.Secret
testVerbose bool
bench bool
}
func (dev *EngineDev) test(
// The test container to run the tests in
container *dagger.Container,
// Various test options
// FIXME merge this into chainable functions instead
opts *testOpts,
) *dagger.Container {
if opts.envs != nil {
container = container.WithMountedSecret("/dagger.env", opts.envs)
}
cgoEnabledEnv := "0"
args := []string{
"otelgotest",
}
// allow verbose
if opts.testVerbose {
args = append(args, "-v")
}
// All following are go test flags
if opts.failfast {
args = append(args, "-failfast")
}
// Go will default parallel to number of CPUs, so only pass if set
if opts.parallel != 0 {
args = append(args, fmt.Sprintf("-parallel=%d", opts.parallel))
}
// Default timeout to 30m
// No test suite should take more than 30 minutes to run
if opts.timeout == "" {
opts.timeout = "30m"
}
args = append(args, fmt.Sprintf("-timeout=%s", opts.timeout))
if opts.race {
args = append(args, "-race")
cgoEnabledEnv = "1"
}
// when bench is true, disable normal tests and select benchmarks based on runTestRegex instead
if opts.bench {
if opts.runTestRegex == "" {
opts.runTestRegex = "."
}
args = append(args, "-bench", opts.runTestRegex, "-run", "^$")
args = append(args, fmt.Sprintf("-benchtime=%dx", opts.count))
} else {
// Disable test caching, since these are integration tests
args = append(args, fmt.Sprintf("-count=%d", opts.count))
if opts.runTestRegex != "" {
args = append(args, "-run", opts.runTestRegex)
}
}
if opts.skipTestRegex != "" {
args = append(args, "-skip", opts.skipTestRegex)
}
args = append(args, opts.pkg)
if opts.update {
args = append(args, "-update")
}
return container.
WithEnvVariable("CGO_ENABLED", cgoEnabledEnv).
WithExec(args)
}
// Build an ephemeral test environment ready to run core engine tests
// Also return the URL of a pprof debug endpoint, to dump profiling data from the tested engine
// (FIXME: do this more cleanly, and reuse the standard Go toolchain)
func (dev *EngineDev) testContainer(ctx context.Context, ebpfProgs []string) (*dagger.Container, string, error) {
devEngine, err := dev.
WithEBPFProgs(ebpfProgs).
WithEngineConfig(`registry."registry:5000"`, `http = true`).
WithEngineConfig(`registry."privateregistry:5000"`, `http = true`).
WithEngineConfig(`registry."docker.io"`, `mirrors = ["mirror.gcr.io"]`).
Container(
ctx,
"", // platform
false, // gpuSupport
"", // version
)
if err != nil {
return nil, "", err
}
// TODO: mitigation for https://github.com/dagger/dagger/issues/8031
// during our test suite
devEngine = devEngine.
WithEnvVariable("_DAGGER_ENGINE_SYSTEMENV_GODEBUG", "goindex=0")
devBinary := dag.DaggerCli().Binary()
// This creates an engine.tar container file that can be used by the integration tests.
// In particular, it is used by core/integration/remotecache_test.go to create a
// dev engine that can be used to test remote caching.
// I also load the dagger binary, so that the remote cache tests can use it to
// run dagger queries.
// These are used by core/integration/remotecache_test.go
testEngineUtils := dag.Directory().
WithFile("engine.tar", devEngine.AsTarball()).
WithFile("dagger", devBinary, dagger.DirectoryWithFileOpts{
Permissions: 0755,
})
engineRunVol := dag.CacheVolume("dagger-dev-engine-test-varrun" + rand.Text())
registrySvc := registry()
devEngineSvc := devEngine.
WithServiceBinding("registry", registrySvc).
WithServiceBinding("privateregistry", privateRegistry()).
WithExposedPort(1234, dagger.ContainerWithExposedPortOpts{Protocol: dagger.NetworkProtocolTcp}).
WithMountedCache(distconsts.EngineDefaultStateDir, dag.CacheVolume("dagger-dev-engine-test-state"+rand.Text())).
WithMountedCache("/run", engineRunVol).
AsService(dagger.ContainerAsServiceOpts{
Args: []string{
"--addr", "unix:///run/dagger-engine.sock",
"--addr", "tcp://0.0.0.0:1234",
"--network-name", "dagger-dev",
"--network-cidr", "10.88.0.0/16",
"--debugaddr", "0.0.0.0:6060",
},
UseEntrypoint: true,
InsecureRootCapabilities: true,
})
// manually starting service to ensure it's not reaped between benchmark prewarm & run
// FIXME: just persist the dev engine into a field of the object... cleaner
devEngineSvc, err = devEngineSvc.Start(ctx)
if err != nil {
return nil, "", err
}
debugEndpoint, err := devEngineSvc.Endpoint(ctx, dagger.ServiceEndpointOpts{Port: 6060, Scheme: "http"})
if err != nil {
return nil, "", err
}
utilDirPath := "/dagger-dev"
tests := dag.Go(dagger.GoOpts{Source: dev.Source}).Env().
WithExec([]string{"go", "install", "github.com/dagger/otel-go/cmd/otelgotest"}).
WithMountedDirectory(utilDirPath, testEngineUtils).
WithEnvVariable("_DAGGER_TESTS_ENGINE_TAR", filepath.Join(utilDirPath, "engine.tar")).
WithServiceBinding("daggerengine", devEngineSvc).
WithMountedCache("/run", engineRunVol).
WithServiceBinding("registry", registrySvc)
tests, err = dev.InstallClient(ctx, tests, devEngineSvc, "")
if err != nil {
return nil, "", err
}
return tests, debugEndpoint, nil
}
func registry() *dagger.Service {
return dag.Container().
From("registry:2").
WithExposedPort(5000, dagger.ContainerWithExposedPortOpts{Protocol: dagger.NetworkProtocolTcp}).
AsService(dagger.ContainerAsServiceOpts{UseEntrypoint: true})
}
func privateRegistry() *dagger.Service {
const htpasswd = "john:$2y$05$/iP8ud0Fs8o3NLlElyfVVOp6LesJl3oRLYoc3neArZKWX10OhynSC" //nolint:gosec
return dag.Container().
From("registry:2").
WithNewFile("/auth/htpasswd", htpasswd).
WithEnvVariable("REGISTRY_AUTH", "htpasswd").
WithEnvVariable("REGISTRY_AUTH_HTPASSWD_REALM", "Registry Realm").
WithEnvVariable("REGISTRY_AUTH_HTPASSWD_PATH", "/auth/htpasswd").
WithExposedPort(5000, dagger.ContainerWithExposedPortOpts{Protocol: dagger.NetworkProtocolTcp}).
AsService(dagger.ContainerAsServiceOpts{UseEntrypoint: true})
}