-
Notifications
You must be signed in to change notification settings - Fork 3
/
client_test.go
281 lines (221 loc) · 6.07 KB
/
client_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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package dagger
import (
"context"
"errors"
"io"
"testing"
"github.com/stretchr/testify/require"
)
func TestDirectory(t *testing.T) {
t.Parallel()
ctx := context.Background()
c, err := Connect(ctx)
require.NoError(t, err)
defer c.Close()
dir := c.Directory()
contents, err := dir.
WithNewFile("/hello.txt", "world").
File("/hello.txt").
Contents(ctx)
require.NoError(t, err)
require.Equal(t, "world", contents)
}
func TestGit(t *testing.T) {
t.Parallel()
ctx := context.Background()
c, err := Connect(ctx)
require.NoError(t, err)
defer c.Close()
tree := c.Git("github.com/dagger/dagger").
Branch("main").
Tree()
files, err := tree.Entries(ctx)
require.NoError(t, err)
require.Contains(t, files, "README.md")
readmeFile := tree.File("README.md")
readme, err := readmeFile.Contents(ctx)
require.NoError(t, err)
require.NotEmpty(t, readme)
require.Contains(t, readme, "Dagger")
readmeID, err := readmeFile.ID(ctx)
require.NoError(t, err)
otherReadme, err := c.LoadFileFromID(readmeID).Contents(ctx)
require.NoError(t, err)
require.Equal(t, readme, otherReadme)
}
func TestContainer(t *testing.T) {
t.Parallel()
ctx := context.Background()
c, err := Connect(ctx)
require.NoError(t, err)
defer c.Close()
alpine := c.
Container().
From("alpine:3.16.2")
contents, err := alpine.
File("/etc/alpine-release").
Contents(ctx)
require.NoError(t, err)
require.Equal(t, "3.16.2\n", contents)
stdout, err := alpine.
WithExec([]string{"cat", "/etc/alpine-release"}).
Stdout(ctx)
require.NoError(t, err)
require.Equal(t, "3.16.2\n", stdout)
// Ensure we can grab the container ID back and re-run the same query
id, err := alpine.ID(ctx)
require.NoError(t, err)
contents, err = c.
LoadContainerFromID(id).
File("/etc/alpine-release").
Contents(ctx)
require.NoError(t, err)
require.Equal(t, "3.16.2\n", contents)
}
// TODO: fix this test, it's actually broken, the result is an empty string
// We could use a buffer, however the regexp want need to be updated, the
// display of Dagger has change since.
func TestConnectOption(t *testing.T) {
t.Skip("test broken with io.Pipe and empty string on the standard output." +
"We need to update the test with new output and use a buffer to catch" +
"output.")
t.Parallel()
ctx := context.Background()
r, w := io.Pipe()
c, err := Connect(ctx, WithLogOutput(w))
require.NoError(t, err)
_, err = c.
Container().
From("alpine:3.16.1").
File("/etc/alpine-release").
Contents(ctx)
require.NoError(t, err)
err = c.Close()
w.Close()
require.NoError(t, err)
wants := []string{
"#1 resolve image config for docker.io/library/alpine:3.16.1",
"#1 DONE [0-9.]+s",
"#2 docker-image://docker.io/library/alpine:3.16.1",
"#2 resolve docker.io/library/alpine:3.16.1 [0-9.]+s done",
"#2 (DONE [0-9.]+s|CACHED)",
}
logOutput, err := io.ReadAll(r)
require.NoError(t, err)
// Empty
// fmt.Println(string(logOutput))
for _, want := range wants {
// NOTE: the string is empty
// This pass the test
// require.Regexp(t, "", want)
require.Regexp(t, string(logOutput), want)
}
}
func TestContainerWith(t *testing.T) {
t.Parallel()
ctx := context.Background()
env := func(c *Container) *Container {
return c.WithEnvVariable("FOO", "bar")
}
secret := func(token string, client *Client) WithContainerFunc {
return func(c *Container) *Container {
return c.WithSecretVariable("TOKEN", client.SetSecret("TOKEN", token))
}
}
c, err := Connect(ctx)
require.NoError(t, err)
defer c.Close()
_, err = c.
Container().
From("alpine:3.16.2").
With(env).
With(secret("baz", c)).
WithExec([]string{"sh", "-c", "test $FOO = bar && test $TOKEN = baz"}).
Sync(ctx)
require.NoError(t, err)
}
func TestList(t *testing.T) {
t.Parallel()
ctx := context.Background()
c, err := Connect(ctx)
require.NoError(t, err)
defer c.Close()
envs, err := c.
Container().
From("alpine:3.16.2").
WithEnvVariable("FOO", "BAR").
WithEnvVariable("BAR", "BAZ").
EnvVariables(ctx)
require.NoError(t, err)
envName, err := envs[1].Name(ctx)
require.NoError(t, err)
envValue, err := envs[1].Value(ctx)
require.NoError(t, err)
require.Equal(t, "FOO", envName)
require.Equal(t, "BAR", envValue)
envName, err = envs[2].Name(ctx)
require.NoError(t, err)
envValue, err = envs[2].Value(ctx)
require.NoError(t, err)
require.Equal(t, "BAR", envName)
require.Equal(t, "BAZ", envValue)
}
func TestExecError(t *testing.T) {
t.Parallel()
ctx := context.Background()
c, err := Connect(ctx)
require.NoError(t, err)
defer c.Close()
t.Run("get output and exit code", func(t *testing.T) {
outMsg := "STDOUT HERE"
errMsg := "STDERR HERE"
args := []string{"sh", "-c", "cat /testout; cat /testerr >&2; exit 127"}
_, err = c.
Container().
From("alpine:3.16.2").
// don't put these in the command args so it stays out of the
// error message
WithDirectory("/", c.Directory().
WithNewFile("testout", outMsg).
WithNewFile("testerr", errMsg),
).
WithExec(args).
Sync(ctx)
var exErr *ExecError
require.ErrorAs(t, err, &exErr)
require.Equal(t, 127, exErr.ExitCode)
require.Equal(t, args, exErr.Cmd)
require.Equal(t, outMsg, exErr.Stdout)
require.Equal(t, errMsg, exErr.Stderr)
require.Contains(t, exErr.Error(), outMsg)
require.Contains(t, exErr.Error(), errMsg)
require.NotContains(t, exErr.Message(), outMsg)
require.NotContains(t, exErr.Message(), errMsg)
if _, ok := err.(*ExecError); !ok {
t.Fatal("unable to cast error type, check potential wrapping")
}
})
t.Run("no output", func(t *testing.T) {
_, err = c.
Container().
From("alpine:3.16.2").
WithExec([]string{"false"}).
Sync(ctx)
var exErr *ExecError
require.ErrorAs(t, err, &exErr)
require.ErrorContains(t, exErr, "did not complete successfully: exit code: 1")
require.Equal(t, "", exErr.Stdout)
require.Equal(t, "", exErr.Stderr)
})
t.Run("not an exec error", func(t *testing.T) {
_, err = c.
Container().
From("invalid!").
WithExec([]string{"false"}).
Sync(ctx)
var exErr *ExecError
if errors.As(err, &exErr) {
t.Fatal("unexpected ExecError")
}
})
}