forked from encoredev/encore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.go
148 lines (129 loc) · 3.49 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
package daemon
import (
"context"
"fmt"
"runtime/debug"
"github.com/cockroachdb/errors"
"github.com/rs/zerolog/log"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"encr.dev/cli/daemon/run"
"encr.dev/pkg/builder"
"encr.dev/pkg/fns"
daemonpb "encr.dev/proto/encore/daemon"
)
// Test runs tests.
func (s *Server) Test(req *daemonpb.TestRequest, stream daemonpb.Daemon_TestServer) error {
ctx := stream.Context()
slog := &streamLog{stream: stream, buffered: false}
stderr := slog.Stderr(false)
sendErr := func(err error) {
stderr.Write([]byte(err.Error() + "\n"))
streamExit(stream, 1)
}
ctx, tracer, err := s.beginTracing(ctx, req.AppRoot, req.WorkingDir, req.TraceFile)
if err != nil {
sendErr(err)
return nil
}
defer tracer.Close()
app, err := s.apps.Track(req.AppRoot)
if err != nil {
sendErr(err)
return nil
}
ns, err := s.namespaceOrActive(ctx, app, nil /* tests don't support different namespaces */)
if err != nil {
sendErr(err)
return nil
}
secrets := s.sm.Load(app)
testCtx, cancel := context.WithCancel(ctx)
defer cancel()
testResults := make(chan error, 1)
go func() {
defer func() {
if recovered := recover(); recovered != nil {
var err error
switch recovered := recovered.(type) {
case error:
err = recovered
default:
err = fmt.Errorf("%+v", recovered)
}
stack := debug.Stack()
log.Err(err).Msgf("panic during test run:\n%s", stack)
testResults <- fmt.Errorf("panic occured within Encore during test run: %v\n%s\n", recovered, stack)
}
}()
tp := run.TestParams{
TestSpecParams: &run.TestSpecParams{
App: app,
NS: ns,
WorkingDir: req.WorkingDir,
Environ: req.Environ,
Args: req.Args,
Secrets: secrets,
CodegenDebug: req.CodegenDebug,
},
Stdout: slog.Stdout(false),
Stderr: slog.Stderr(false),
}
testResults <- s.mgr.Test(testCtx, tp)
}()
if err := <-testResults; err != nil {
sendErr(err)
} else {
streamExit(stream, 0)
}
return nil
}
// TestSpec runs tests.
func (s *Server) TestSpec(ctx context.Context, req *daemonpb.TestSpecRequest) (resp *daemonpb.TestSpecResponse, err error) {
ctx, tracer, err := s.beginTracing(ctx, req.AppRoot, req.WorkingDir, nil)
if err != nil {
return nil, errors.Wrap(err, "unable to begin tracing")
}
defer fns.CloseIgnore(tracer)
app, err := s.apps.Track(req.AppRoot)
if err != nil {
return nil, errors.Wrap(err, "unable to track app")
}
ns, err := s.namespaceOrActive(ctx, app, nil /* tests don't support different namespaces */)
if err != nil {
return nil, errors.Wrap(err, "unable to get namespace")
}
secrets := s.sm.Load(app)
defer func() {
if recovered := recover(); recovered != nil {
var panicErr error
switch recovered := recovered.(type) {
case error:
panicErr = recovered
default:
panicErr = fmt.Errorf("%+v", recovered)
}
stack := debug.Stack()
log.Err(panicErr).Msgf("panic during test run:\n%s", stack)
err = fmt.Errorf("panic during test run: %v", panicErr)
}
}()
spec, err := s.mgr.TestSpec(ctx, run.TestSpecParams{
App: app,
NS: ns,
WorkingDir: req.WorkingDir,
Environ: req.Environ,
Args: req.Args,
Secrets: secrets,
})
if errors.Is(err, builder.ErrNoTests) {
return nil, status.Error(codes.NotFound, "no tests defined")
} else if err != nil {
return nil, err
}
return &daemonpb.TestSpecResponse{
Command: spec.Command,
Args: spec.Args,
Environ: spec.Environ,
}, nil
}