/
tests.go
215 lines (186 loc) · 5.58 KB
/
tests.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
package run
import (
"context"
"fmt"
"io"
"runtime"
"github.com/cockroachdb/errors"
"github.com/rs/xid"
"encore.dev/appruntime/exported/experiments"
"encr.dev/cli/daemon/apps"
"encr.dev/cli/daemon/namespace"
"encr.dev/cli/daemon/run/infra"
"encr.dev/cli/daemon/secret"
"encr.dev/internal/optracker"
"encr.dev/pkg/builder"
"encr.dev/pkg/builder/builderimpl"
"encr.dev/pkg/cueutil"
"encr.dev/pkg/fns"
"encr.dev/pkg/option"
"encr.dev/pkg/paths"
"encr.dev/pkg/vcs"
runtimev1 "encr.dev/proto/encore/runtime/v1"
)
// TestParams groups the parameters for the Test method.
type TestParams struct {
*TestSpecParams
// Stdout and Stderr are where "go test" output should be written.
Stdout, Stderr io.Writer
}
// Test runs the tests.
func (mgr *Manager) Test(ctx context.Context, params TestParams) (err error) {
expSet, err := params.App.Experiments(params.Environ)
if err != nil {
return err
}
bld := builderimpl.Resolve(expSet)
defer fns.CloseIgnore(bld)
spec, err := mgr.testSpec(ctx, bld, expSet, params.TestSpecParams)
if err != nil {
return err
}
workingDir := paths.RootedFSPath(params.App.Root(), params.WorkingDir)
return bld.RunTests(ctx, builder.RunTestsParams{
Spec: spec,
WorkingDir: workingDir,
Stdout: params.Stdout,
Stderr: params.Stderr,
})
}
// TestSpecParams are the parameters for computing a test spec.
type TestSpecParams struct {
// App is the app to test.
App *apps.Instance
// NS is the namespace to use.
NS *namespace.Namespace
// Secrets are the secrets to use.
Secrets *secret.LoadResult
// Args are the arguments to pass to the test command.
Args []string
// WorkingDir is the working dir, for formatting
// error messages with relative paths.
WorkingDir string
// Environ are the environment variables to set when running the tests,
// in the same format as os.Environ().
Environ []string
// CodegenDebug, if true, specifies to keep the output
// around for codegen debugging purposes.
CodegenDebug bool
}
type TestSpecResponse struct {
Command string
Args []string
Environ []string
}
// TestSpec returns how to run the tests.
func (mgr *Manager) TestSpec(ctx context.Context, params TestSpecParams) (*TestSpecResponse, error) {
expSet, err := params.App.Experiments(params.Environ)
if err != nil {
return nil, err
}
bld := builderimpl.Resolve(expSet)
defer fns.CloseIgnore(bld)
spec, err := mgr.testSpec(ctx, bld, expSet, ¶ms)
if err != nil {
return nil, err
}
return &TestSpecResponse{
Command: spec.Command,
Args: spec.Args,
Environ: spec.Environ,
}, nil
}
// testSpec returns how to run the tests.
func (mgr *Manager) testSpec(ctx context.Context, bld builder.Impl, expSet *experiments.Set, params *TestSpecParams) (*builder.TestSpecResult, error) {
secretData, err := params.Secrets.Get(ctx, expSet)
if err != nil {
return nil, err
}
secrets := secretData.Values
vcsRevision := vcs.GetRevision(params.App.Root())
buildInfo := builder.BuildInfo{
BuildTags: builder.LocalBuildTags,
CgoEnabled: true,
StaticLink: false,
Debug: false,
GOOS: runtime.GOOS,
GOARCH: runtime.GOARCH,
KeepOutput: params.CodegenDebug,
Revision: vcsRevision.Revision,
UncommittedChanges: vcsRevision.Uncommitted,
}
parse, err := bld.Parse(ctx, builder.ParseParams{
Build: buildInfo,
App: params.App,
Experiments: expSet,
WorkingDir: params.WorkingDir,
ParseTests: true,
})
if err != nil {
return nil, err
}
if err := params.App.CacheMetadata(parse.Meta); err != nil {
return nil, errors.Wrap(err, "cache metadata")
}
rm := infra.NewResourceManager(params.App, mgr.ClusterMgr, params.NS, nil, mgr.DBProxyPort, true)
jobs := optracker.NewAsyncBuildJobs(ctx, params.App.PlatformOrLocalID(), nil)
rm.StartRequiredServices(jobs, parse.Meta)
// Note: jobs.Wait must be called before generateConfig.
if err := jobs.Wait(); err != nil {
return nil, err
}
gateways := make(map[string]GatewayConfig)
gatewayBaseURL := fmt.Sprintf("http://localhost:%d", mgr.RuntimePort)
for _, gw := range parse.Meta.Gateways {
gateways[gw.EncoreName] = GatewayConfig{
BaseURL: gatewayBaseURL,
Hostnames: []string{"localhost"},
}
}
cfg, err := bld.ServiceConfigs(ctx, builder.ServiceConfigsParams{
Parse: parse,
CueMeta: &cueutil.Meta{
APIBaseURL: gatewayBaseURL,
EnvName: "local",
EnvType: cueutil.EnvType_Test,
CloudType: cueutil.CloudType_Local,
},
})
if err != nil {
return nil, err
}
authKey := genAuthKey()
configGen := &RuntimeConfigGenerator{
app: params.App,
infraManager: rm,
md: parse.Meta,
AppID: option.Some("test"),
EnvID: option.Some("test"),
TraceEndpoint: option.Some(fmt.Sprintf("http://localhost:%d/trace", mgr.RuntimePort)),
AuthKey: authKey,
Gateways: gateways,
DefinedSecrets: secrets,
SvcConfigs: cfg.Configs,
EnvName: option.Some("test"),
EnvType: option.Some(runtimev1.Environment_TYPE_TEST),
DeployID: option.Some(fmt.Sprintf("clitest_%s", xid.New().String())),
IncludeMetaEnv: bld.NeedsMeta(),
}
env, err := configGen.ForTests(bld.UseNewRuntimeConfig())
if err != nil {
return nil, err
}
env = append(env, encodeServiceConfigs(cfg.Configs)...)
return bld.TestSpec(ctx, builder.TestSpecParams{
Compile: builder.CompileParams{
Build: buildInfo,
App: params.App,
Parse: parse,
OpTracker: nil,
Experiments: expSet,
WorkingDir: params.WorkingDir,
},
Env: append(params.Environ, env...),
Args: params.Args,
})
}