-
Notifications
You must be signed in to change notification settings - Fork 726
/
ParametersTests.cs
369 lines (332 loc) · 12.9 KB
/
ParametersTests.cs
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Diagnostics.CodeAnalysis;
using Bicep.Core.Configuration;
using Bicep.Core.Diagnostics;
using Bicep.Core.IntegrationTests.Extensibility;
using Bicep.Core.Syntax;
using Bicep.Core.TypeSystem.Types;
using Bicep.Core.UnitTests;
using Bicep.Core.UnitTests.Assertions;
using Bicep.Core.UnitTests.Utils;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Linq;
namespace Bicep.Core.IntegrationTests
{
[TestClass]
public class ParameterTests
{
private ServiceBuilder ServicesWithResourceTyped => new ServiceBuilder().WithFeatureOverrides(new(TestContext, ResourceTypedParamsAndOutputsEnabled: true));
[NotNull]
public TestContext? TestContext { get; set; }
private ServiceBuilder ServicesWithExtensibility => new ServiceBuilder()
.WithFeatureOverrides(new(TestContext, ExtensibilityEnabled: true, ResourceTypedParamsAndOutputsEnabled: true))
.WithConfigurationPatch(c => c.WithProvidersConfiguration("""
{
"az": "builtin:",
"kubernetes": "builtin:",
"microsoftGraph": "builtin:",
"foo": "builtin:",
"bar": "builtin:"
}
"""))
.WithNamespaceProvider(new TestExtensibilityNamespaceProvider(BicepTestConstants.ResourceTypeProviderFactory));
[TestMethod]
public void Parameter_can_have_resource_type()
{
var result = CompilationHelper.Compile(
ServicesWithResourceTyped, @"
param p resource 'Microsoft.Storage/storageAccounts@2019-06-01'
resource resource 'Microsoft.Storage/storageAccounts@2019-06-01' = {
name: 'test'
location: 'eastus'
kind: 'StorageV2'
sku: {
name: 'Standard_LRS'
}
identity: {
type: 'SystemAssigned'
}
properties:{
accessTier: p.properties.accessTier
}
}
");
result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();
var model = result.Compilation.GetEntrypointSemanticModel();
var parameterSymbol = model.Root.ParameterDeclarations.Should().ContainSingle().Subject;
var typeInfo = model.GetTypeInfo(parameterSymbol.DeclaringSyntax);
typeInfo.Should().BeOfType<ResourceType>().Which.TypeReference.FormatName().Should().BeEquivalentTo("Microsoft.Storage/storageAccounts@2019-06-01");
var reference = model.FindReferences(parameterSymbol).OfType<VariableAccessSyntax>().Should().ContainSingle().Subject;
model.GetDeclaredType(reference).Should().NotBeNull();
model.GetTypeInfo(reference).Should().NotBeNull();
result.Template.Should().HaveValueAtPath("$.resources[0].properties.accessTier", "[reference(parameters('p'), '2019-06-01').accessTier]");
result.Template.Should().HaveValueAtPath("$.parameters.p", new JObject()
{
["type"] = new JValue("string"),
["metadata"] = new JObject()
{
["resourceType"] = new JValue("Microsoft.Storage/storageAccounts@2019-06-01"),
},
});
}
[TestMethod]
public void Parameter_with_resource_type_can_have_decorators()
{
var result = CompilationHelper.Compile(
ServicesWithResourceTyped, @"
@description('cool')
param p resource 'Microsoft.Storage/storageAccounts@2019-06-01'
resource resource 'Microsoft.Storage/storageAccounts@2019-06-01' = {
name: 'test'
location: 'eastus'
kind: 'StorageV2'
sku: {
name: 'Standard_LRS'
}
identity: {
type: 'SystemAssigned'
}
properties:{
accessTier: p.properties.accessTier
}
}
");
result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();
result.Template.Should().HaveValueAtPath("$.parameters.p", new JObject()
{
["type"] = new JValue("string"),
["metadata"] = new JObject()
{
["resourceType"] = new JValue("Microsoft.Storage/storageAccounts@2019-06-01"),
["description"] = new JValue("cool"),
},
});
}
[TestMethod]
public void Parameter_with_resource_type_can_have_properties_evaluated()
{
var result = CompilationHelper.Compile(
ServicesWithResourceTyped, @"
param p resource 'Microsoft.Storage/storageAccounts@2019-06-01'
output id string = p.id
output name string = p.name
output type string = p.type
output apiVersion string = p.apiVersion
output accessTier string = p.properties.accessTier
");
result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();
result.Template.Should().HaveValueAtPath("$.outputs.id", new JObject()
{
["type"] = new JValue("string"),
["value"] = new JValue("[parameters('p')]"),
});
result.Template.Should().HaveValueAtPath("$.outputs.name", new JObject()
{
["type"] = new JValue("string"),
["value"] = new JValue("[last(split(parameters('p'), '/'))]"),
});
result.Template.Should().HaveValueAtPath("$.outputs.type", new JObject()
{
["type"] = new JValue("string"),
["value"] = new JValue("Microsoft.Storage/storageAccounts"),
});
result.Template.Should().HaveValueAtPath("$.outputs.apiVersion", new JObject()
{
["type"] = new JValue("string"),
["value"] = new JValue("2019-06-01"),
});
result.Template.Should().HaveValueAtPath("$.outputs.accessTier", new JObject()
{
["type"] = new JValue("string"),
["value"] = new JValue("[reference(parameters('p'), '2019-06-01').accessTier]"),
});
}
[TestMethod]
public void Parameter_can_have_warnings_for_missing_type()
{
var result = CompilationHelper.Compile(
ServicesWithResourceTyped, @"
param p resource 'Some.Fake/Type@2019-06-01'
output id string = p.id
");
result.ExcludingLinterDiagnostics().Should().HaveDiagnostics(new[]
{
("BCP081", DiagnosticLevel.Warning, "Resource type \"Some.Fake/Type@2019-06-01\" does not have types available."),
});
}
[TestMethod]
public void Parameter_cannot_use_extensibility_resource_type()
{
var result = CompilationHelper.Compile(ServicesWithExtensibility, """
provider bar with {
connectionString: 'asdf'
} as stg
param container resource 'stg:container'
output name string = container.name // silence unused params warning
""");
result.ExcludingLinterDiagnostics().Should().HaveDiagnostics(new[]
{
("BCP227", DiagnosticLevel.Error, "The type \"container\" cannot be used as a parameter or output type. Extensibility types are currently not supported as parameters or outputs."),
("BCP062", DiagnosticLevel.Error, "The referenced declaration with name \"container\" is not valid."),
});
}
[TestMethod]
public void Parameter_with_resource_type_cannot_be_used_as_extension_scope()
{
var result = CompilationHelper.Compile(ServicesWithResourceTyped, @"
param p resource 'Microsoft.Storage/storageAccounts@2019-06-01'
resource resource 'My.Rp/myResource@2020-01-01' = {
scope: p
name: 'resource'
}");
result.ExcludingLinterDiagnostics().Should().HaveDiagnostics(new[]
{
("BCP081", DiagnosticLevel.Warning, "Resource type \"My.Rp/myResource@2020-01-01\" does not have types available."),
("BCP229", DiagnosticLevel.Error, "The parameter \"p\" cannot be used as a resource scope or parent. Resources passed as parameters cannot be used as a scope or parent of a resource."),
});
}
[TestMethod]
public void Parameter_with_resource_type_cannot_be_used_as_parent()
{
var result = CompilationHelper.Compile(ServicesWithResourceTyped, @"
param p resource 'Microsoft.Storage/storageAccounts@2019-06-01'
resource resource 'Microsoft.Storage/storageAccounts/tableServices@2020-06-01' = {
parent: p
name: 'child1'
properties: {
cors: {
corsRules: []
}
}
}");
result.ExcludingLinterDiagnostics().Should().HaveDiagnostics(new[]
{
("BCP081", DiagnosticLevel.Warning, "Resource type \"Microsoft.Storage/storageAccounts/tableServices@2020-06-01\" does not have types available."),
("BCP229", DiagnosticLevel.Error, "The parameter \"p\" cannot be used as a resource scope or parent. Resources passed as parameters cannot be used as a scope or parent of a resource."),
("BCP169", DiagnosticLevel.Error, "Expected resource name to contain 1 \"/\" character(s). The number of name segments must match the number of segments in the resource type."),
});
}
[TestMethod]
public void Parameter_with_string_interpolation()
{
var result = CompilationHelper.CompileParams(
("test.txt", @"Hello $NAME!"),
("parameters.bicepparam", @"
using 'main.bicep'
param foo = 'foo'
param bar = 'bar${foo}bar'
param baz = replace(loadTextContent('test.txt'), '$NAME', 'Anthony')
"),
("main.bicep", @"
param foo string
param bar string
param baz string
output baz string = '${foo}${bar}'
"));
// Exclude the "No using declaration is present in this parameters file" diagnostic
result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();
result.Parameters.Should().DeepEqual(JToken.Parse(@"{
""$schema"": ""https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#"",
""contentVersion"": ""1.0.0.0"",
""parameters"": {
""foo"": {
""value"": ""foo""
},
""bar"": {
""value"": ""barfoobar""
},
""baz"": {
""value"": ""Hello Anthony!""
}
}
}"));
}
[TestMethod]
public void Non_deterministic_functions_are_blocked()
{
var result = CompilationHelper.CompileParams(
("parameters.bicepparam", @"
using 'main.bicep'
param foo = utcNow()
param bar = newGuid()
"),
("main.bicep", @"
param foo string
param bar string
"));
result.ExcludingLinterDiagnostics().Should().HaveDiagnostics(new[] {
("BCP065", DiagnosticLevel.Error, "Function \"utcNow\" is not valid at this location. It can only be used as a parameter default value."),
("BCP065", DiagnosticLevel.Error, "Function \"newGuid\" is not valid at this location. It can only be used as a parameter default value."),
});
}
[TestMethod]
public void Az_functions_are_blocked()
{
var result = CompilationHelper.CompileParams(
("parameters.bicepparam", @"
using 'main.bicep'
param foo = resourceId('Microsoft.Compute/virtualMachines', 'foo')
param bar = deployment()
"),
("main.bicep", @"
param foo string
param bar object
"));
result.ExcludingLinterDiagnostics().Should().HaveDiagnostics(new[] {
("BCP057", DiagnosticLevel.Error, "The name \"resourceId\" does not exist in the current context."),
("BCP057", DiagnosticLevel.Error, "The name \"deployment\" does not exist in the current context."),
});
}
[TestMethod]
public void Parameter_with_complex_functions()
{
var result = CompilationHelper.CompileParams(
("test.txt", @"Hello $NAME!"),
("parameters.bicepparam", @"
using 'main.bicep'
param foo = 'foo/bar/baz'
param bar = [
toLower(foo)
toUpper(foo)
map(split(foo, '/'), v => { segment: v })
replace(loadTextContent('test.txt'), '$NAME', 'Anthony')
]
"),
("main.bicep", @"
param foo string
param bar array
"));
// Exclude the "No using declaration is present in this parameters file" diagnostic
result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();
result.Parameters.Should().DeepEqual(JToken.Parse(@"{
""$schema"": ""https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#"",
""contentVersion"": ""1.0.0.0"",
""parameters"": {
""foo"": {
""value"": ""foo/bar/baz""
},
""bar"": {
""value"": [
""foo/bar/baz"",
""FOO/BAR/BAZ"",
[
{
""segment"": ""foo""
},
{
""segment"": ""bar""
},
{
""segment"": ""baz""
}
],
""Hello Anthony!""
]
}
}
}"));
}
}
}