-
Notifications
You must be signed in to change notification settings - Fork 267
/
config.ts
245 lines (218 loc) · 8.08 KB
/
config.ts
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
/*
* Copyright (C) 2018-2024 Garden Technologies, Inc. <info@garden.io>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import { resolve } from "path"
import { expect } from "chai"
import cloneDeep from "fast-copy"
import { omit } from "lodash-es"
import type { TestGarden } from "../../../../../helpers.js"
import { expectError, getDataDir, makeTestGarden, withDefaultGlobalOpts } from "../../../../../helpers.js"
import type { PluginContext } from "../../../../../../src/plugin-context.js"
import { dedent } from "../../../../../../src/util/string.js"
import type { ModuleConfig } from "../../../../../../src/config/module.js"
import { apply } from "json-merge-patch"
import { getHelmTestGarden } from "./common.js"
import { defaultHelmTimeout } from "../../../../../../src/plugins/kubernetes/helm/module-config.js"
import stripAnsi from "strip-ansi"
import {
DEFAULT_BUILD_TIMEOUT_SEC,
DEFAULT_DEPLOY_TIMEOUT_SEC,
GardenApiVersion,
} from "../../../../../../src/constants.js"
import { ValidateCommand } from "../../../../../../src/commands/validate.js"
describe("configureHelmModule", () => {
let garden: TestGarden
let ctx: PluginContext
let moduleConfigs: { [key: string]: ModuleConfig }
before(async () => {
garden = await getHelmTestGarden()
const provider = await garden.resolveProvider(garden.log, "local-kubernetes")
ctx = await garden.getPluginContext({ provider, templateContext: undefined, events: undefined })
await garden.resolveModules({ log: garden.log })
moduleConfigs = cloneDeep((<any>garden).moduleConfigs)
})
afterEach(() => {
garden["moduleConfigs"] = cloneDeep(moduleConfigs)
})
function patchModuleConfig(name: string, patch: any) {
apply((<any>garden).moduleConfigs[name], patch)
}
it("should validate a Helm module", async () => {
const module = await garden.resolveModule("api-module")
const graph = await garden.getConfigGraph({ log: garden.log, emit: false })
const imageModule = graph.getModule("api-image")
const imageVersion = imageModule.version.versionString
const spec = {
atomicInstall: true,
build: {
dependencies: [],
timeout: DEFAULT_BUILD_TIMEOUT_SEC,
},
chartPath: ".",
sync: {
paths: [
{
mode: "two-way",
defaultDirectoryMode: 0o755,
defaultFileMode: 0o644,
source: ".",
target: "/app",
},
],
},
dependencies: [],
releaseName: "api-module-release",
serviceResource: {
kind: "Deployment",
containerModule: "api-image",
},
skipDeploy: false,
tasks: [],
tests: [],
timeout: defaultHelmTimeout,
values: {
image: {
tag: imageVersion,
},
ingress: {
enabled: true,
paths: ["/api-module/"],
hosts: ["api.local.demo.garden"],
},
},
valueFiles: [],
}
expect(module._config).to.eql({
apiVersion: GardenApiVersion.v0,
kind: "Module",
allowPublish: true,
build: {
dependencies: [],
timeout: DEFAULT_BUILD_TIMEOUT_SEC,
},
local: false,
configPath: resolve(ctx.projectRoot, "api", "garden.yml"),
description: "The API backend for the voting UI",
disabled: false,
generateFiles: undefined,
include: ["*", "charts/**/*", "templates/**/*"],
inputs: {},
exclude: undefined,
name: "api-module",
path: resolve(ctx.projectRoot, "api"),
repositoryUrl: undefined,
buildConfig: omit(spec, ["atomicInstall", "serviceResource", "skipDeploy", "tasks", "tests"]),
serviceConfigs: [
{
name: "api-module",
dependencies: [],
disabled: false,
sourceModuleName: "api-image",
timeout: DEFAULT_DEPLOY_TIMEOUT_SEC,
spec,
},
],
spec,
testConfigs: [],
type: "helm",
taskConfigs: [],
variables: {},
varfile: undefined,
})
})
it("should not set default includes if include has already been explicitly set", async () => {
patchModuleConfig("api-module", { include: ["foo"] })
const configInclude = await garden.resolveModule("api-module")
expect(configInclude.include).to.eql(["foo"])
})
it("should not set default includes if exclude has already been explicitly set", async () => {
patchModuleConfig("api-module", { exclude: ["bar"] })
const configExclude = await garden.resolveModule("api-module")
expect(configExclude.include).to.be.undefined
})
it("should set include to default if module does not have local chart sources", async () => {
// So that Chart.yaml isn't found
patchModuleConfig("api-module", { spec: { chartPath: "invalid-path" } })
const config = await garden.resolveModule("api-module")
expect(config.include).to.eql(["invalid-path/*.yaml", "invalid-path/*.yml"])
})
it("should not return a serviceConfig if skipDeploy=true", async () => {
patchModuleConfig("api-module", { spec: { skipDeploy: true } })
const config = await garden.resolveModule("api-module")
expect(config.serviceConfigs).to.eql([])
})
it("should add the module specified under 'base' as a build dependency", async () => {
patchModuleConfig("postgres", { spec: { base: "api-module" } })
const config = await garden.resolveModule("postgres")
expect(config.build.dependencies).to.eql([{ name: "api-module", copy: [{ source: "*", target: "." }] }])
})
it("should add copy spec to build dependency if it's already a dependency", async () => {
patchModuleConfig("postgres", {
build: { dependencies: [{ name: "api-module", copy: [] }] },
spec: { base: "api-module" },
})
const config = await garden.resolveModule("postgres")
expect(config.build.dependencies).to.eql([{ name: "api-module", copy: [{ source: "*", target: "." }] }])
})
it("should add module specified under tasks[].resource.containerModule as a build dependency", async () => {
patchModuleConfig("api-module", {
spec: {
tasks: [
{
name: "my-task",
resource: { kind: "Deployment", containerModule: "postgres" },
},
],
},
})
const config = await garden.resolveModule("api-module")
expect(config.build.dependencies).to.eql([
{ name: "postgres", copy: [] },
{ name: "api-image", copy: [] },
])
})
it("should add module specified under tests[].resource.containerModule as a build dependency", async () => {
patchModuleConfig("api-module", {
spec: {
tests: [
{
name: "my-task",
resource: { kind: "Deployment", containerModule: "postgres" },
},
],
},
})
const config = await garden.resolveModule("api-module")
expect(config.build.dependencies).to.eql([
{ name: "postgres", copy: [] },
{ name: "api-image", copy: [] },
])
})
// TODO: this doesn't seem to work and I don't want to dive in
it.skip("should throw if chart both contains sources and specifies base", async () => {
patchModuleConfig("api-module", { spec: { base: "artifacts" } })
await expectError(
() => garden.resolveModule("api-module"),
(err) =>
expect(stripAnsi(err.message)).to.equal(dedent`
Failed resolving one or more modules:
api: Helm module 'api' both contains sources and specifies a base module. Since Helm charts cannot currently be merged, please either remove the sources or the \`base\` reference in your module config.
`)
)
})
it("should pass validation with a chart name but no version specified", async () => {
const projectRoot = getDataDir("test-projects", "helm-name-version-regression")
const g = await makeTestGarden(projectRoot)
const command = new ValidateCommand()
await command.action({
garden: g,
log: g.log,
args: {},
opts: withDefaultGlobalOpts({ resolve: undefined }),
})
})
})