-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.go
194 lines (167 loc) · 5.25 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
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
package main
import (
"context"
"path/filepath"
"github.com/moby/buildkit/identity"
"github.com/dagger/dagger/ci/util"
"github.com/dagger/dagger/engine/distconsts"
)
type Test struct {
Dagger *Dagger // +private
CacheConfig string // +private
}
func (t *Test) WithCache(config string) *Test {
clone := *t
clone.CacheConfig = config
return &clone
}
// Run all engine tests
func (t *Test) All(
ctx context.Context,
// +optional
race bool,
) error {
return t.test(ctx, race, "", "./...")
}
// Run "important" engine tests
func (t *Test) Important(
ctx context.Context,
// +optional
race bool,
) error {
// These tests give good basic coverage of functionality w/out having to run everything
return t.test(ctx, race, `^(TestModule|TestContainer)`, "./...")
}
// Run custom engine tests
func (t *Test) Custom(
ctx context.Context,
run string,
// +optional
// +default="./..."
pkg string,
// +optional
race bool,
) error {
return t.test(ctx, race, run, pkg)
}
func (t *Test) test(
ctx context.Context,
race bool,
testRegex string,
pkg string,
) error {
cgoEnabledEnv := "0"
args := []string{
"gotestsum",
"--format", "testname",
"--no-color=false",
"--jsonfile=./tests.log",
"--",
// go test flags
"-parallel=16",
"-count=1",
"-timeout=30m",
}
if race {
args = append(args, "-race", "-timeout=1h")
cgoEnabledEnv = "1"
}
if testRegex != "" {
args = append(args, "-run", testRegex)
}
args = append(args, pkg)
cmd, err := t.testCmd(ctx)
if err != nil {
return err
}
_, err = cmd.
WithEnvVariable("CGO_ENABLED", cgoEnabledEnv).
WithExec(args).
WithExec([]string{"gotestsum", "tool", "slowest", "--jsonfile=./tests.log", "--threshold=1s"}).
Sync(ctx)
return err
}
func (t *Test) testCmd(ctx context.Context) (*Container, error) {
engine := t.Dagger.Engine().
WithConfig(`registry."registry:5000"`, `http = true`).
WithConfig(`registry."privateregistry:5000"`, `http = true`).
WithConfig(`registry."docker.io"`, `mirrors = ["mirror.gcr.io"]`).
WithConfig(`grpc`, `address=["unix:https:///var/run/buildkit/buildkitd.sock", "tcp:https://0.0.0.0:1234"]`).
WithArg(`network-name`, `dagger-dev`).
WithArg(`network-cidr`, `10.88.0.0/16`)
devEngine, err := engine.Container(ctx, "")
if err != nil {
return nil, err
}
devBinary, err := t.Dagger.CLI().File(ctx, "")
if err != nil {
return nil, err
}
// This creates an engine.tar container file that can be used by the integration tests.
// In particular, it is used by core/integration/remotecache_test.go to create a
// dev engine that can be used to test remote caching.
// I also load the dagger binary, so that the remote cache tests can use it to
// run dagger queries.
// These are used by core/integration/remotecache_test.go
testEngineUtils := dag.Directory().
WithFile("engine.tar", devEngine.AsTarball()).
WithFile("dagger", devBinary, DirectoryWithFileOpts{
Permissions: 0755,
})
registrySvc := registry()
devEngineSvc := devEngine.
WithServiceBinding("registry", registrySvc).
WithServiceBinding("privateregistry", privateRegistry()).
WithExposedPort(1234, ContainerWithExposedPortOpts{Protocol: Tcp}).
WithMountedCache(distconsts.EngineDefaultStateDir, dag.CacheVolume("dagger-dev-engine-test-state"+identity.NewID())).
WithExec(nil, ContainerWithExecOpts{
InsecureRootCapabilities: true,
}).
AsService()
endpoint, err := devEngineSvc.Endpoint(ctx, ServiceEndpointOpts{Port: 1234, Scheme: "tcp"})
if err != nil {
return nil, err
}
cliBinPath := "/.dagger-cli"
utilDirPath := "/dagger-dev"
tests := util.GoBase(t.Dagger.Source).
WithExec([]string{"go", "install", "gotest.tools/[email protected]"}).
WithMountedDirectory("/app", t.Dagger.Source). // need all the source for extension tests
WithMountedDirectory(utilDirPath, testEngineUtils).
WithEnvVariable("_DAGGER_TESTS_ENGINE_TAR", filepath.Join(utilDirPath, "engine.tar")).
WithWorkdir("/app").
WithServiceBinding("dagger-engine", devEngineSvc).
WithServiceBinding("registry", registrySvc)
if t.CacheConfig != "" {
tests = tests.WithEnvVariable("_EXPERIMENTAL_DAGGER_CACHE_CONFIG", t.CacheConfig)
}
// TODO: should use c.Dagger.installer (but this currently can't connect to services)
tests = tests.
WithMountedFile(cliBinPath, devBinary).
WithEnvVariable("_EXPERIMENTAL_DAGGER_CLI_BIN", cliBinPath).
WithEnvVariable("_EXPERIMENTAL_DAGGER_RUNNER_HOST", endpoint)
if t.Dagger.HostDockerConfig != nil {
// this avoids rate limiting in our ci tests
tests = tests.WithMountedSecret("/root/.docker/config.json", t.Dagger.HostDockerConfig)
}
return tests, nil
}
func registry() *Service {
return dag.Container().
From("registry:2").
WithExposedPort(5000, ContainerWithExposedPortOpts{Protocol: Tcp}).
WithExec(nil).
AsService()
}
func privateRegistry() *Service {
const htpasswd = "john:$2y$05$/iP8ud0Fs8o3NLlElyfVVOp6LesJl3oRLYoc3neArZKWX10OhynSC" //nolint:gosec
return dag.Container().
From("registry:2").
WithNewFile("/auth/htpasswd", ContainerWithNewFileOpts{Contents: htpasswd}).
WithEnvVariable("REGISTRY_AUTH", "htpasswd").
WithEnvVariable("REGISTRY_AUTH_HTPASSWD_REALM", "Registry Realm").
WithEnvVariable("REGISTRY_AUTH_HTPASSWD_PATH", "/auth/htpasswd").
WithExposedPort(5000, ContainerWithExposedPortOpts{Protocol: Tcp}).
WithExec(nil).
AsService()
}