-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathinclude_test.go
More file actions
290 lines (263 loc) · 8.69 KB
/
Copy pathinclude_test.go
File metadata and controls
290 lines (263 loc) · 8.69 KB
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
282
283
284
285
286
287
288
289
290
/*
Copyright 2020 The Compose Specification Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package loader
import (
"context"
"fmt"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/compose-spec/compose-go/v2/types"
"gotest.tools/v3/assert"
)
func TestLoadIncludeExtendsCombined(t *testing.T) {
_, err := LoadWithContext(context.TODO(), types.ConfigDetails{
WorkingDir: "testdata/combined",
ConfigFiles: []types.ConfigFile{
{
Filename: "testdata/combined/compose.yaml",
},
},
}, withProjectName("test-load-combined", true))
assert.NilError(t, err)
}
func TestLoadWithMultipleInclude(t *testing.T) {
// include same service twice should not trigger an error
details := buildConfigDetails(`
name: 'test-multi-include'
include:
- path: ./testdata/subdir/compose-test-extends-imported.yaml
env_file: ./testdata/subdir/extra.env
- path: ./testdata/compose-include.yaml
services:
foo:
image: busybox
depends_on:
- imported
`, map[string]string{"SOURCE": "override"})
p, err := LoadWithContext(context.TODO(), details, func(options *Options) {
options.SkipNormalization = true
options.ResolvePaths = true
})
assert.NilError(t, err)
imported, err := p.GetService("imported")
assert.NilError(t, err)
assert.Equal(t, imported.ContainerName, "override")
}
func TestLoadWithMultipleIncludeConflict(t *testing.T) {
// include 2 different services with same name should trigger an error
details := buildConfigDetails(`
name: 'test-multi-include'
include:
- path: ./testdata/subdir/compose-test-extends-imported.yaml
env_file: ./testdata/subdir/extra.env
- path: ./testdata/compose-include.yaml
env_file: ./testdata/subdir/extra.env
services:
bar:
image: busybox
environment: !override
- ZOT=QIX
`, map[string]string{"SOURCE": "override"})
p, err := LoadWithContext(context.TODO(), details, func(options *Options) {
options.SkipNormalization = true
options.ResolvePaths = true
})
assert.NilError(t, err)
assert.DeepEqual(t, p.Services["bar"], types.ServiceConfig{
Name: "bar",
Image: "busybox",
Environment: types.MappingWithEquals{
"ZOT": strPtr("QIX"),
},
})
}
func TestIncludeRelative(t *testing.T) {
wd, err := filepath.Abs(filepath.Join("testdata", "include"))
assert.NilError(t, err)
p, err := LoadWithContext(context.TODO(), types.ConfigDetails{
ConfigFiles: []types.ConfigFile{
{
Filename: filepath.Join("testdata", "include", "compose.yaml"),
},
},
WorkingDir: wd,
}, func(options *Options) {
options.projectName = "test-include-relative"
options.ResolvePaths = false
})
assert.NilError(t, err)
included := p.Services["included"]
assert.Equal(t, included.Build.Context, ".")
assert.Equal(t, included.Volumes[0].Source, ".")
}
func TestLoadWithIncludeEnv(t *testing.T) {
fileName := "compose.yml"
tmpdir := t.TempDir()
// file in root
yaml := `
include:
- path:
- ./module/compose.yml
env_file:
- ./custom.env
services:
a:
image: alpine
environment:
- VAR_NAME`
createFile(t, tmpdir, `VAR_NAME=value`, "custom.env")
path := createFile(t, tmpdir, yaml, fileName)
// file in /module
yaml = `
services:
b:
image: alpine
environment:
- VAR_NAME
c:
image: alpine
environment:
- VAR_NAME`
createFileSubDir(t, tmpdir, "module", yaml, fileName)
p, err := LoadWithContext(context.TODO(), types.ConfigDetails{
WorkingDir: tmpdir,
ConfigFiles: []types.ConfigFile{{
Filename: path,
}},
Environment: nil,
}, func(options *Options) {
options.SkipNormalization = true
options.ResolvePaths = true
options.SetProjectName("project", true)
})
assert.NilError(t, err)
a := p.Services["a"]
// make sure VAR_NAME is only accessible in include context
assert.Check(t, a.Environment["VAR_NAME"] == nil, "VAR_NAME should not be defined in environment")
b := p.Services["b"]
assert.Check(t, b.Environment["VAR_NAME"] != nil, "VAR_NAME is not defined in environment")
assert.Equal(t, *b.Environment["VAR_NAME"], "value")
c := p.Services["c"]
assert.Check(t, c.Environment["VAR_NAME"] != nil, "VAR_NAME is not defined in environment")
assert.Equal(t, *c.Environment["VAR_NAME"], "value")
}
func TestIncludeWithProjectDirectory(t *testing.T) {
var envs map[string]string
if runtime.GOOS == "windows" {
envs = map[string]string{"COMPOSE_CONVERT_WINDOWS_PATHS": "1"}
}
p, err := LoadWithContext(context.TODO(), types.ConfigDetails{
WorkingDir: "testdata/include",
Environment: envs,
ConfigFiles: []types.ConfigFile{
{
Filename: "testdata/include/project-directory.yaml",
},
},
}, withProjectName("test-load-project-directory", true))
assert.NilError(t, err)
assert.Equal(t, filepath.ToSlash(p.Services["service"].Build.Context), "testdata/subdir")
assert.Equal(t, filepath.ToSlash(p.Services["service"].Volumes[0].Source), "testdata/subdir/compose-test-extends-imported.yaml")
assert.Equal(t, filepath.ToSlash(p.Services["service"].EnvFiles[0].Path), "testdata/subdir/extra.env")
}
func TestNestedIncludeAndExtends(t *testing.T) {
fileName := "compose.yml"
yaml := `
include:
- project_directory: .
path: dir/included.yaml
`
tmpdir := t.TempDir()
path := createFile(t, tmpdir, yaml, fileName)
yaml = `
services:
included:
extends:
file: dir/extended.yaml
service: extended
`
createFileSubDir(t, tmpdir, "dir", yaml, "included.yaml")
yaml = `
services:
extended:
image: alpine
`
createFile(t, filepath.Join(tmpdir, "dir"), yaml, "extended.yaml")
p, err := LoadWithContext(context.TODO(), types.ConfigDetails{
WorkingDir: tmpdir,
ConfigFiles: []types.ConfigFile{{
Filename: path,
}},
Environment: nil,
}, func(options *Options) {
options.SkipNormalization = true
options.ResolvePaths = true
options.SetProjectName("project", true)
})
assert.NilError(t, err)
assert.Equal(t, p.Services["included"].Image, "alpine")
}
func createFile(t *testing.T, rootDir, content, fileName string) string {
path := filepath.Join(rootDir, fileName)
assert.NilError(t, os.WriteFile(path, []byte(content), 0o600))
return path
}
func createFileSubDir(t *testing.T, rootDir, subDir, content, fileName string) {
subDirPath := filepath.Join(rootDir, subDir)
assert.NilError(t, os.Mkdir(subDirPath, 0o700))
path := filepath.Join(subDirPath, fileName)
assert.NilError(t, os.WriteFile(path, []byte(content), 0o600))
}
// TestIncludeDiamondDedup builds a deep "diamond" include graph where every
// level includes the next level twice. Without include memoization the leaf is
// loaded 2^depth times (exponential); the cache loads each distinct file once.
// A depth that is trivial when deduplicated (and astronomically large when not)
// makes this both a correctness and a non-flaky performance regression test.
func TestIncludeDiamondDedup(t *testing.T) {
dir := t.TempDir()
const depth = 24 // 2^24 ~= 16.7M leaf loads without dedup
for i := 0; i < depth; i++ {
content := fmt.Sprintf("include:\n - path: ./level%d.yaml\n - path: ./level%d.yaml\n", i+1, i+1)
assert.NilError(t, os.WriteFile(filepath.Join(dir, fmt.Sprintf("level%d.yaml", i)), []byte(content), 0o600))
}
leaf := "services:\n leaf:\n image: busybox\n"
assert.NilError(t, os.WriteFile(filepath.Join(dir, fmt.Sprintf("level%d.yaml", depth)), []byte(leaf), 0o600))
p, err := LoadWithContext(context.TODO(), types.ConfigDetails{
WorkingDir: dir,
ConfigFiles: []types.ConfigFile{{Filename: filepath.Join(dir, "level0.yaml")}},
}, withProjectName("diamond", true))
assert.NilError(t, err)
_, err = p.GetService("leaf")
assert.NilError(t, err)
}
func BenchmarkIncludeDiamond(b *testing.B) {
dir := b.TempDir()
const depth = 16
for i := 0; i < depth; i++ {
content := fmt.Sprintf("include:\n - path: ./level%d.yaml\n - path: ./level%d.yaml\n", i+1, i+1)
_ = os.WriteFile(filepath.Join(dir, fmt.Sprintf("level%d.yaml", i)), []byte(content), 0o600)
}
_ = os.WriteFile(filepath.Join(dir, fmt.Sprintf("level%d.yaml", depth)), []byte("services:\n leaf:\n image: busybox\n"), 0o600)
b.ResetTimer()
for n := 0; n < b.N; n++ {
_, err := LoadWithContext(context.TODO(), types.ConfigDetails{
WorkingDir: dir,
ConfigFiles: []types.ConfigFile{{Filename: filepath.Join(dir, "level0.yaml")}},
}, withProjectName("diamond", true))
if err != nil {
b.Fatal(err)
}
}
}