-
Notifications
You must be signed in to change notification settings - Fork 234
/
tests.go
153 lines (136 loc) · 4.16 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
package run
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"os"
"strconv"
"time"
"github.com/rs/xid"
encore "encore.dev"
"encore.dev/runtime/config"
"encr.dev/cli/internal/appfile"
"encr.dev/cli/internal/env"
"encr.dev/cli/internal/version"
"encr.dev/compiler"
"encr.dev/parser"
"encr.dev/pkg/vcs"
)
// Check checks the app for errors.
// It reports a buildDir (if available) when codegenDebug is true.
func (mgr *Manager) Check(ctx context.Context, appRoot, relwd string, codegenDebug bool) (buildDir string, err error) {
vcsRevision := vcs.GetRevision(appRoot)
// TODO: We should check that all secret keys are defined as well.
cfg := &compiler.Config{
Revision: vcsRevision.Revision,
UncommittedChanges: vcsRevision.Uncommitted,
WorkingDir: relwd,
CgoEnabled: true,
EncoreCompilerVersion: fmt.Sprintf("EncoreCLI/%s", version.Version),
EncoreRuntimePath: env.EncoreRuntimePath(),
EncoreGoRoot: env.EncoreGoRoot(),
KeepOutput: codegenDebug,
BuildTags: []string{"encore_local"},
}
result, err := compiler.Build(appRoot, cfg)
if result != nil && result.Dir != "" {
if codegenDebug {
buildDir = result.Dir
} else {
os.RemoveAll(result.Dir)
}
}
return buildDir, err
}
// TestParams groups the parameters for the Test method.
type TestParams struct {
// AppRoot is the application root.
AppRoot string
// AppID is the unique app id, as defined by the manifest.
AppID string
// WorkingDir is the working dir, for formatting
// error messages with relative paths.
WorkingDir string
// DBClusterID is the database cluster id to connect to.
DBClusterID string
// Parse is the parse result for the initial run of the app.
// It must be set.
Parse *parser.Result
// 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) {
appSlug, err := appfile.Slug(params.AppRoot)
if err != nil {
return err
}
var secrets map[string]string
if appSlug != "" {
data, err := mgr.Secret.Get(ctx, appSlug)
if err != nil {
return err
}
secrets = data.Values
}
sqlServer := &config.SQLServer{
Host: "localhost:" + strconv.Itoa(mgr.DBProxyPort),
}
var dbs []*config.SQLDatabase
for _, svc := range params.Parse.Meta.Svcs {
if len(svc.Migrations) > 0 {
dbs = append(dbs, &config.SQLDatabase{
ServerID: 0,
EncoreName: svc.Name,
DatabaseName: svc.Name,
User: "encore",
Password: params.DBClusterID,
})
}
}
runtimeJSON, err := json.Marshal(&config.Runtime{
AppID: "test",
AppSlug: appSlug,
APIBaseURL: fmt.Sprintf("http://localhost:%d", mgr.RuntimePort),
DeployID: fmt.Sprintf("clitest_%s", xid.New()),
DeployedAt: time.Now(),
EnvID: "test",
EnvName: "local",
EnvCloud: string(encore.CloudLocal),
EnvType: string(encore.EnvLocal),
TraceEndpoint: "http://localhost:" + strconv.Itoa(mgr.RuntimePort) + "/trace",
SQLDatabases: dbs,
AuthKeys: []config.EncoreAuthKey{genAuthKey()},
SQLServers: []*config.SQLServer{sqlServer},
})
if err != nil {
return err
}
cfg := &compiler.Config{
Revision: params.Parse.Meta.AppRevision,
UncommittedChanges: params.Parse.Meta.UncommittedChanges,
WorkingDir: params.WorkingDir,
CgoEnabled: true,
EncoreCompilerVersion: fmt.Sprintf("EncoreCLI/%s", version.Version),
EncoreRuntimePath: env.EncoreRuntimePath(),
EncoreGoRoot: env.EncoreGoRoot(),
BuildTags: []string{"encore_local"},
Test: &compiler.TestConfig{
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,
},
}
return compiler.Test(ctx, params.AppRoot, cfg)
}