/
tests.go
132 lines (114 loc) · 3.19 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
package run
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"runtime"
"encr.dev/cli/daemon/apps"
"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/vcs"
)
// TestParams groups the parameters for the Test method.
type TestParams struct {
// App is the app to test.
App *apps.Instance
// WorkingDir is the working dir, for formatting
// error messages with relative paths.
WorkingDir string
// Secrets are the secrets to use.
Secrets *secret.LoadResult
// Args are the arguments to pass to "go test".
Args []string
// Environ are the environment variables to set when running the tests,
// in the same format as os.Environ().
Environ []string
// 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
}
secretData, err := params.Secrets.Get(ctx, expSet)
if err != nil {
return err
}
secrets := secretData.Values
bld := builderimpl.Resolve(expSet)
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: false,
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 err
}
rm := infra.NewResourceManager(params.App, mgr.ClusterMgr, nil, true)
apiBaseURL := fmt.Sprintf("http://localhost:%d", mgr.RuntimePort)
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 err
}
runtimeCfg, err := mgr.generateConfig(generateConfigParams{
App: params.App,
RM: rm,
Meta: parse.Meta,
ForTests: true,
AuthKey: genAuthKey(),
APIBaseURL: apiBaseURL,
ConfigAppID: "test",
ConfigEnvID: "test",
})
if err != nil {
return err
}
runtimeJSON, _ := json.Marshal(runtimeCfg)
return bld.Test(ctx, builder.TestParams{
Compile: builder.CompileParams{
Build: buildInfo,
App: params.App,
Parse: parse,
OpTracker: nil,
Experiments: expSet,
WorkingDir: params.WorkingDir,
CueMeta: &cueutil.Meta{
APIBaseURL: apiBaseURL,
EnvName: "local",
EnvType: cueutil.EnvType_Test,
CloudType: cueutil.CloudType_Local,
},
},
Env: append(params.Environ,
"ENCORE_RUNTIME_CONFIG="+base64.RawURLEncoding.EncodeToString(runtimeJSON),
"ENCORE_APP_SECRETS="+encodeSecretsEnv(secrets),
),
Args: params.Args,
Stdout: params.Stdout,
Stderr: params.Stderr,
})
}