-
Notifications
You must be signed in to change notification settings - Fork 8
/
run.go
315 lines (255 loc) · 9.33 KB
/
run.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
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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package testing
import (
"context"
"errors"
"fmt"
"io"
"sync"
"time"
"github.com/morikuni/aec"
"golang.org/x/exp/slices"
"google.golang.org/grpc/status"
"namespacelabs.dev/foundation/internal/compute"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/console/common"
"namespacelabs.dev/foundation/internal/executor"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/planning/deploy"
"namespacelabs.dev/foundation/internal/runtime"
fnsync "namespacelabs.dev/foundation/internal/sync"
"namespacelabs.dev/foundation/orchestration"
"namespacelabs.dev/foundation/schema"
runtimepb "namespacelabs.dev/foundation/schema/runtime"
"namespacelabs.dev/foundation/schema/storage"
"namespacelabs.dev/foundation/std/cfg"
"namespacelabs.dev/foundation/std/tasks"
)
const TestRunAction = "test.run"
var errTestFailed = errors.New("test failed")
type testRun struct {
SealedContext cfg.Context // Doesn't affect the output.
Cluster runtime.Cluster // Target, doesn't affect the output.
TestRef *schema.PackageRef
Driver runtime.DeployableSpec
Stack *schema.Stack
ServersUnderTest []string // Package names.
Plan compute.Computable[*deploy.Plan]
OutputProgress bool
compute.LocalScoped[*storage.TestResultBundle]
}
var _ compute.Computable[*storage.TestResultBundle] = &testRun{}
func (test *testRun) Action() *tasks.ActionEvent {
return tasks.Action("test").Arg("name", test.TestRef.Name).Arg("package_name", test.TestRef.AsPackageName())
}
func (test *testRun) Inputs() *compute.In {
in := compute.Inputs().
Str("testName", test.TestRef.Name).
Stringer("testPkg", test.TestRef.AsPackageName()).
Proto("workspace", test.SealedContext.Workspace().Proto()).
Proto("env", test.SealedContext.Environment()).
Indigestible("driver", test.Driver).
Proto("stack", test.Stack).
Strs("focus", test.ServersUnderTest).
Computable("plan", test.Plan)
return in
}
func (test *testRun) Compute(ctx context.Context, r compute.Resolved) (*storage.TestResultBundle, error) {
// The actual test run is wrapped in another action, so we can apply policies to it (e.g. constrain how many tests are deployed in parallel).
return tasks.Return(ctx, tasks.Action(TestRunAction), func(ctx context.Context) (*storage.TestResultBundle, error) {
return test.compute(ctx, r)
})
}
func (test *testRun) compute(ctx context.Context, r compute.Resolved) (*storage.TestResultBundle, error) {
p := compute.MustGetDepValue(r, test.Plan, "plan")
env := test.SealedContext
cluster, err := test.Cluster.Bind(ctx, test.SealedContext)
if err != nil {
return nil, err
}
defer func() {
if !test.SealedContext.Environment().Ephemeral {
// skip cleanup for non-ephemeral environments (e.g. to allow manual inspection of the resources)
return
}
if _, err := cluster.DeleteRecursively(ctx, false); err != nil {
fmt.Fprintln(console.Errors(ctx), "Failed to cleanup: ", err)
}
}()
deployPlan := deploy.Serialize(env.Workspace().Proto(), env.Environment(), test.Stack, p, test.ServersUnderTest)
out := console.TypedOutput(ctx, "test", common.CatOutputUs)
fmt.Fprintf(out, "%s: Test %s\n", test.TestRef.Canonical(), aec.LightBlackF.Apply("RUNNING"))
var waitErr error
if err := orchestration.Deploy(ctx, env, cluster, deployPlan, true, test.OutputProgress); err != nil {
waitErr = fnerrors.AttachLocation(test.TestRef.AsPackageName(), err)
}
var testLogBuf *fnsync.ByteBuffer
if waitErr == nil {
// All servers deployed. Lets start wait for the test driver.
localCtx, cancelAll := context.WithCancel(ctx)
defer cancelAll()
ex := executor.Newf(localCtx, "testing.run(%s)", test.TestRef.Canonical())
var extraOutput []io.Writer
if test.OutputProgress {
extraOutput = append(extraOutput, console.Output(ctx, "testlog"))
}
var testLog io.Writer
testLog, testLogBuf = makeLog(extraOutput...)
ex.Go(func(ctx context.Context) error {
defer cancelAll() // When the test is done, cancel logging.
containers, err := cluster.WaitForTermination(ctx, test.Driver)
if err != nil {
return err
}
if len(containers) != 1 {
return fnerrors.InternalError("expected test driver to yield exactly one container, got %d", len(containers))
}
for _, container := range containers {
if err := cluster.Cluster().FetchLogsTo(ctx, container.Reference, runtime.FetchLogsOpts{}, runtime.WriteToWriter(testLog)); err != nil {
if errors.Is(err, context.Canceled) {
return err
}
fmt.Fprintf(console.Errors(ctx), "%s: failed to fetch test log: %v\n", test.TestRef.Canonical(), err)
}
}
for _, container := range containers {
// XXX consolidate these two.
var e1 runtime.ErrContainerExitStatus
var e2 runtime.ErrContainerFailed
if errors.As(container.TerminationError, &e1) && e1.ExitCode > 0 {
return errTestFailed
} else if errors.As(err, &e2) {
return errTestFailed
} else if container.TerminationError != nil {
return container.TerminationError
}
}
return nil
})
waitErr = ex.Wait()
}
testResults := &storage.TestResult{}
if waitErr == nil {
testResults.Success = true
} else if errors.Is(waitErr, errTestFailed) {
testResults.Success = false
} else {
testResults.Success = false
st := status.Convert(waitErr)
testResults.ErrorCode = int32(st.Code())
testResults.ErrorMessage = st.Message()
}
if test.OutputProgress {
fmt.Fprintf(out, "\nCollecting post-execution server logs...\n\n")
}
// Limit how much time we spend on collecting logs out of the test environment.
collectionCtx, collectionDone := context.WithTimeout(ctx, 60*time.Second)
defer collectionDone()
bundle, err := collectLogs(collectionCtx, env, cluster, test.TestRef, test.Stack, test.ServersUnderTest, test.OutputProgress)
if err != nil {
return nil, err
}
bundle.DeployPlan = deployPlan
bundle.ComputedConfigurations = p.Computed
// Clear the hints, no point storing those.
bundle.DeployPlan.Hints = nil
bundle.Result = testResults
if testLogBuf != nil {
bundle.TestLog = &storage.TestResultBundle_InlineLog{
PackageName: test.TestRef.PackageName,
Output: testLogBuf.Seal().Bytes(),
}
}
return bundle, nil
}
func collectLogs(ctx context.Context, env cfg.Context, rt runtime.ClusterNamespace, testRef *schema.PackageRef, stack *schema.Stack, focus []string, printLogs bool) (*storage.TestResultBundle, error) {
ex := executor.New(ctx, "test.collect-logs")
type serverLog struct {
PackageName string
ContainerName string
ContainerKind runtimepb.ContainerKind
Buffer *fnsync.ByteBuffer
}
var serverLogs []serverLog
var mu sync.Mutex // Protects concurrent access to serverLogs.
out := console.Output(ctx, "test.collect-logs")
for _, entry := range stack.Entry {
srv := entry.Server // Close on srv.
ex.Go(func(ctx context.Context) error {
// It should be possible to resolve a container fairly quickly. Make
// sure we don't get stuck here waiting forever.
resolveCtx, resolveDone := context.WithTimeout(ctx, 10*time.Second)
defer resolveDone()
containers, err := rt.ResolveContainers(resolveCtx, srv)
if err != nil {
fmt.Fprintf(out, "%s: failed to resolve containers: %s: %v\n", testRef.Canonical(), srv.PackageName, err)
return nil
}
for _, ctr := range containers {
ctr := ctr // Close on ctr.
var extraOutput []io.Writer
if printLogs && slices.Contains(focus, srv.PackageName) {
name := srv.Name
if len(containers) > 0 {
name = ctr.HumanReference
}
extraOutput = append(extraOutput, console.Output(ctx, name))
}
w, log := makeLog(extraOutput...)
mu.Lock()
serverLogs = append(serverLogs, serverLog{
PackageName: srv.PackageName,
ContainerName: ctr.HumanReference,
ContainerKind: ctr.Kind,
Buffer: log,
})
mu.Unlock()
ex.Go(func(ctx context.Context) error {
err := rt.Cluster().FetchLogsTo(ctx, ctr, runtime.FetchLogsOpts{}, runtime.WriteToWriterWithTimestamps(w))
if errors.Is(err, context.Canceled) {
return nil
}
if err != nil {
fmt.Fprintf(out, "%s: failed to fetch logs: %s: %v\n", testRef.Canonical(), srv.PackageName, err)
}
return nil
})
}
return nil
})
}
var diagnostics *storage.EnvironmentDiagnostics
ex.Go(func(ctx context.Context) error {
var err error
diagnostics, err = rt.FetchEnvironmentDiagnostics(ctx)
if err != nil {
fmt.Fprintf(console.Warnings(ctx), "Failed to retrieve environment diagnostics: %v\n", err)
}
return nil
})
if err := ex.Wait(); err != nil {
return nil, err
}
bundle := &storage.TestResultBundle{
EnvDiagnostics: diagnostics,
}
for _, entry := range serverLogs {
bundle.ServerLog = append(bundle.ServerLog, &storage.TestResultBundle_InlineLog{
PackageName: entry.PackageName,
ContainerName: entry.ContainerName,
ContainerKind: entry.ContainerKind,
Output: entry.Buffer.Seal().Bytes(),
})
}
return bundle, nil
}
func makeLog(otherWriters ...io.Writer) (io.Writer, *fnsync.ByteBuffer) {
buf := fnsync.NewByteBuffer()
if len(otherWriters) == 0 {
return buf, buf
}
w := io.MultiWriter(append(otherWriters, buf)...)
return w, buf
}