-
Notifications
You must be signed in to change notification settings - Fork 726
/
ScenarioTests.cs
5918 lines (5187 loc) · 189 KB
/
ScenarioTests.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Security.Cryptography;
using Bicep.Core.Configuration;
using Bicep.Core.Diagnostics;
using Bicep.Core.Resources;
using Bicep.Core.TypeSystem;
using Bicep.Core.TypeSystem.Types;
using Bicep.Core.UnitTests;
using Bicep.Core.UnitTests.Assertions;
using Bicep.Core.UnitTests.Features;
using Bicep.Core.UnitTests.Utils;
using FluentAssertions;
using FluentAssertions.Execution;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Linq;
namespace Bicep.Core.IntegrationTests;
[TestClass]
public class ScenarioTests
{
private static ServiceBuilder Services => new();
[NotNull] public TestContext? TestContext { get; set; }
[TestMethod]
// https://github.com/azure/bicep/issues/3636
public void Test_Issue3636()
{
var lineCount = 100; // increase this number to 10,000 for more intense test
// use this crypto random number gen to avoid CI warning
int generateRandomInt(int minVal = 0, int maxVal = 50)
{
var rnd = new byte[4];
RandomNumberGenerator.Fill(rnd);
var i = Math.Abs(BitConverter.ToInt32(rnd, 0));
return Convert.ToInt32(i % (maxVal - minVal + 1) + minVal);
}
string randomString()
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return new string(Enumerable.Repeat(chars, generateRandomInt())
.Select(s => s[generateRandomInt(0, s.Length - 1)]).ToArray());
}
var file = "param adminuser string\nvar adminstring = 'xyx ${adminuser} 123'\n";
file += "output values object = {\n";
for (var i = 0; i < lineCount; i++)
{
file += $" testa{i}: '{randomString()} ${{adminuser}} {randomString()}'\n";
file += $" testb{i}: '{randomString()} ${{adminstring}} {randomString()}'\n";
}
file += "}\n";
// not a true test for existing diagnostics
// this is a trigger to allow timing within the
// linter rules - timing must be readded to
// initially added to time NoHardcodedEnvironmentUrlsRule
CompilationHelper.Compile(file).Should().NotHaveAnyDiagnostics();
}
[TestMethod]
// https://github.com/azure/bicep/issues/746
public void Test_Issue746()
{
var result = CompilationHelper.Compile(@"
var l = l
param l
");
result.Template.Should().NotHaveValue();
result.ExcludingLinterDiagnostics().Should().HaveDiagnostics(new[]
{
("BCP028", DiagnosticLevel.Error, "Identifier \"l\" is declared multiple times. Remove or rename the duplicates."),
("BCP079", DiagnosticLevel.Error, "This expression is referencing its own declaration, which is not allowed."),
("BCP028", DiagnosticLevel.Error, "Identifier \"l\" is declared multiple times. Remove or rename the duplicates."),
("BCP279", DiagnosticLevel.Error, "Expected a type at this location. Please specify a valid type expression or one of the following types: \"array\", \"bool\", \"int\", \"object\", \"string\"."),
});
}
[TestMethod]
// https://github.com/azure/bicep/issues/801
public void Test_Issue801()
{
var result = CompilationHelper.Compile(
("main.bicep", @"
targetScope = 'subscription'
resource rg 'Microsoft.Resources/resourceGroups@2020-06-01' = {
location: 'eastus'
name: 'vnet-rg'
}
module vnet './vnet.bicep' = {
scope: resourceGroup('vnet-rg')
name: 'network-module'
params: {
location: 'eastus'
name: 'myVnet'
}
dependsOn: [
rg
]
}
output vnetid string = vnet.outputs.vnetId
output vnetstate string = vnet.outputs.vnetstate
"),
("vnet.bicep", @"
param location string
param name string
resource vnet 'Microsoft.Network/virtualNetworks@2020-06-01' = {
location: location
name: name
properties:{
addressSpace: {
addressPrefixes: [
'10.0.0.0/20'
]
}
subnets: [
{
name: 'snet-apim'
properties: {
addressPrefix: '10.0.0.0/24'
}
}
]
}
}
output vnetId string = vnet.id
output vnetstate string = vnet.properties.provisioningState
"));
result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();
// ensure we're generating the correct expression with 'subscriptionResourceId', and using the correct name for the module
result.Template.Should().HaveValueAtPath("$.outputs['vnetid'].value", "[reference(extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', subscription().subscriptionId, 'vnet-rg'), 'Microsoft.Resources/deployments', 'network-module'), '2022-09-01').outputs.vnetId.value]");
result.Template.Should().HaveValueAtPath("$.outputs['vnetstate'].value", "[reference(extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', subscription().subscriptionId, 'vnet-rg'), 'Microsoft.Resources/deployments', 'network-module'), '2022-09-01').outputs.vnetstate.value]");
}
[TestMethod]
// https://github.com/azure/bicep/issues/982
public void Test_Issue982()
{
var result = CompilationHelper.Compile(@"
param functionApp object
param serverFarmId string
#disable-next-line outputs-should-not-contain-secrets
output config object = list(format('{0}/config/appsettings', functionAppResource.id), functionAppResource.apiVersion)
resource functionAppResource 'Microsoft.Web/sites@2020-06-01' = {
name: functionApp.name
location: resourceGroup().location
kind: 'functionApp'
properties: {
httpsOnly: true
serverFarmId: serverFarmId
}
}
");
result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();
result.Template.Should().HaveValueAtPath("$.outputs['config'].value", "[list(format('{0}/config/appsettings', resourceId('Microsoft.Web/sites', parameters('functionApp').name)), '2020-06-01')]");
}
[TestMethod]
// https://github.com/azure/bicep/issues/1093
public void Test_Issue1093()
{
var result = CompilationHelper.Compile(
("main.bicep", @"
targetScope = 'managementGroup'
module bicep3rg 'resourceGroup.bicep' = {
name: 'rg30'
params: {
rgName: 'bicep3-rg'
}
scope: subscription('DEV1')
}
module bicep4rg 'resourceGroup.bicep' = {
name: 'rg31'
params: {
rgName: 'bicep4-rg'
}
scope: subscription('DEV2')
}
"),
("resourceGroup.bicep", @"
param rgName string
param location string = 'westeurope'
targetScope = 'subscription'
resource rg 'Microsoft.Resources/resourceGroups@2020-06-01' = {
name: rgName
location: location
}
"));
result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();
result.Template.Should().HaveValueAtPath("$.resources[?(@.name == 'rg30')].location", "[deployment().location]");
result.Template.Should().HaveValueAtPath("$.resources[?(@.name == 'rg31')].location", "[deployment().location]");
}
[TestMethod]
// https://github.com/azure/bicep/issues/1173
public void Test_Issue1173()
{
var result = CompilationHelper.Compile(
("main.bicep", @"
targetScope = 'subscription'
param azRegion string = 'southcentralus'
param vNetAddressPrefix string = '10.1.0.0/24'
param GatewayAddressPrefix string = '10.1.0.0/27'
param AppAddressPrefix string = '10.1.0.128/26'
var rgName = 'testRG'
var vnetName = 'testvnet'
var appNSGName = 'testvnet-appsn01nsg'
var appRTName = 'testvnet-appsn01routetable'
var subnets = [
{
name: 'GatewaySubnet'
properties: {
addressPrefix: GatewayAddressPrefix
}
}
{
name: 'appsn01'
properties: {
addressPrefix: AppAddressPrefix
networkSecurityGroup: {
id: appNSG.outputs.id
}
routeTable: {
id: appRT.outputs.id
}
}
}
]
resource rgVNet 'Microsoft.Resources/resourceGroups@2020-06-01' = {
name: rgName
location: azRegion
}
module vnet './vnet.bicep' = {
name: vnetName
scope: resourceGroup(rgVNet.name)
params: {
vnetName: vnetName
vNetAddressPrefix: vNetAddressPrefix
subnets: subnets
}
}
module appNSG './nsg.bicep' = {
name: appNSGName
scope: resourceGroup(rgVNet.name)
params: {
nsgName: appNSGName
secRules: [
{
name: 'default-allow-rdp'
properties: {
priority: 1010
access: 'Allow'
direction: 'Inbound'
protocol: 'Tcp'
sourcePortRange: '*'
sourceAddressPrefix: 'VirtualNetwork'
destinationAddressPrefix: '*'
destinationPortRange: '3389'
}
}
]
}
}
module appRT './rt.bicep' = {
name: appRTName
scope: resourceGroup(rgVNet.name)
params: {
rtName: appRTName
}
}
"),
("vnet.bicep", @"
param vnetName string
param vNetAddressPrefix string
param subnets array
resource vnet 'Microsoft.Network/virtualNetworks@2020-06-01' = {
name: vnetName
location: resourceGroup().location
properties: {
addressSpace: {
addressPrefixes: [
vNetAddressPrefix
]
}
enableVmProtection: false
enableDdosProtection: false
subnets: subnets
}
}
output id string = vnet.id
"),
("nsg.bicep", @"
param nsgName string
param secRules array
resource nsg 'Microsoft.Network/networkSecurityGroups@2020-06-01' = {
name: nsgName
location: resourceGroup().location
properties: {
securityRules: secRules
}
}
output id string = nsg.id
"),
("rt.bicep", @"
param rtName string
//param azFwlIp string
resource routetable 'Microsoft.Network/routeTables@2020-06-01' = {
name: rtName
location: resourceGroup().location
properties: {
disableBgpRoutePropagation: false
routes: [
// {
// name: 'DefaultRoute'
// properties: {
// addressPrefix: '0.0.0.0/0'
// nextHopType: 'VirtualAppliance'
// nextHopIpAddress: azFwlIp
// }
// }
]
}
}
output id string = routetable.id
"));
// variable 'subnets' should have been inlined
result.Template.Should().HaveValueAtPath("$.resources[?(@.name == '[variables(\\'vnetName\\')]')].properties.parameters.subnets.value[0].name", "GatewaySubnet");
result.Template.Should().HaveValueAtPath("$.resources[?(@.name == '[variables(\\'vnetName\\')]')].properties.parameters.subnets.value[1].name", "appsn01");
// there should be no definition in the variables list for 'subnets'
result.Template.Should().NotHaveValueAtPath("$.variables.subnets");
}
[TestMethod]
// https://github.com/azure/bicep/issues/1185
public void Test_Issue1185()
{
var result = CompilationHelper.Compile(
("main.bicep", @"
targetScope = 'tenant'
param allUpMgName string
module allup_mg './modules/rblab-allup-mg-policies.bicep' = {
name: 'allupmgdeploy'
scope: managementGroup(allUpMgName)
params: {
mgName: allUpMgName
}
}
"),
("modules/rblab-allup-mg-policies.bicep", @"
targetScope = 'managementGroup'
param mgName string
"));
result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();
// deploying a management group module at tenant scope requires an unqualified resource id
result.Template.Should().HaveValueAtPath("$.resources[?(@.name == 'allupmgdeploy')].scope", "[format('Microsoft.Management/managementGroups/{0}', parameters('allUpMgName'))]");
}
[TestMethod]
// https://github.com/azure/bicep/issues/1332
public void Test_Issue1332()
{
var result = CompilationHelper.Compile(@"
var propname = 'ptest'
var issue = true ? {
prop1: {
'${propname}': {}
}
} : {}
");
result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();
result.Template.Should().HaveValueAtPath("$.variables.issue", "[if(true(), createObject('prop1', createObject(format('{0}', variables('propname')), createObject())), createObject())]");
}
[TestMethod]
// https://github.com/azure/bicep/issues/486
public void Test_Issue486()
{
var result = CompilationHelper.Compile(@"
var myInt = 5
var myBigInt = 2199023255552
var myIntExpression = 5 * 5
var myBigIntExpression = 2199023255552 * 2
var myBigIntExpression2 = 2199023255552 * 2199023255552
");
result.Template.Should().HaveValueAtPath("$.variables.myInt", 5);
result.Template.Should().HaveValueAtPath("$.variables.myBigInt", 2199023255552);
result.Template.Should().HaveValueAtPath("$.variables.myIntExpression", "[mul(5, 5)]");
result.Template.Should().HaveValueAtPath("$.variables.myBigIntExpression2", "[mul(json('2199023255552'), json('2199023255552'))]");
}
[TestMethod]
// https://github.com/azure/bicep/issues/1362
public void Test_Issue1362_1()
{
var result = CompilationHelper.Compile(
("main.bicep", @"
targetScope = 'resourceGroup'
module sub './modules/subscription.bicep' = {
name: 'subDeploy'
scope: subscription()
}"),
("modules/subscription.bicep", @"
targetScope = 'subscription'
"));
result.Template.Should().HaveValueAtPath("$.resources[?(@.name == 'subDeploy')].subscriptionId", "[subscription().subscriptionId]");
result.Template.Should().HaveValueAtPath("$.resources[?(@.name == 'subDeploy')].location", "[resourceGroup().location]");
}
[TestMethod]
// https://github.com/azure/bicep/issues/1362
public void Test_Issue1362_2()
{
var result = CompilationHelper.Compile(
("main.bicep", @"
targetScope = 'resourceGroup'
module sub './modules/subscription.bicep' = {
name: 'subDeploy'
scope: subscription('abcd-efgh')
}"),
("modules/subscription.bicep", @"
targetScope = 'subscription'
"));
result.Template.Should().HaveValueAtPath("$.resources[?(@.name == 'subDeploy')].subscriptionId", "abcd-efgh");
result.Template.Should().HaveValueAtPath("$.resources[?(@.name == 'subDeploy')].location", "[resourceGroup().location]");
}
[TestMethod]
// https://github.com/azure/bicep/issues/1402
public void Test_Issue1402()
{
var result = CompilationHelper.Compile(
("main.bicep", @"
targetScope = 'subscription'
module sub './modules/resourceGroup.bicep' = {
name: 'subDeploy'
scope: resourceGroup('abcd-efgh','bicep-rg')
}"),
("modules/resourceGroup.bicep", @"
targetScope = 'resourceGroup'
"));
result.Template.Should().HaveValueAtPath("$.resources[?(@.name == 'subDeploy')].subscriptionId", "abcd-efgh");
result.Template.Should().HaveValueAtPath("$.resources[?(@.name == 'subDeploy')].resourceGroup", "bicep-rg");
result.Template.Should().NotHaveValueAtPath("$.resources[?(@.name == 'subDeploy')].location");
}
[TestMethod]
// https://github.com/azure/bicep/issues/1391
public void Test_Issue1391()
{
var result = CompilationHelper.Compile(@"
resource dep 'Microsoft.Resources/deployments@2020-06-01' = {
name: 'nestedDeployment'
resourceGroup: 'bicep-rg'
subscriptionId: 'abcd-efgh'
location: 'westus'
properties: {
mode: 'Incremental'
}
}
");
result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();
result.Template.Should().HaveValueAtPath("$.resources[?(@.name == 'nestedDeployment')].subscriptionId", "abcd-efgh");
result.Template.Should().HaveValueAtPath("$.resources[?(@.name == 'nestedDeployment')].resourceGroup", "bicep-rg");
}
[TestMethod]
// https://github.com/azure/bicep/issues/1454
public void Test_Issue1454()
{
var result = CompilationHelper.Compile(
("main.bicep", @"
targetScope = 'subscription'
param name string = 'name'
param location string = 'westus'
param subscriptionId string = newGuid()
module rg './resourcegroup.template.bicep' = {
name: '${uniqueString(deployment().name)}-1'
scope: subscription(subscriptionId)
params: {
name: name
location: location
}
}
var appResGrp = resourceGroup(rg.outputs.resourceGroupName)
module redis './redis.template.bicep' = {
name: '${uniqueString(deployment().name)}-2'
scope: appResGrp
}
"),
("resourcegroup.template.bicep", @"
targetScope = 'subscription'
param name string
param location string
resource rg 'Microsoft.Resources/resourceGroups@2018-05-01' = {
name: name
location: location
tags: {
'owner': 'me'
}
}
output resourceGroupName string = rg.name
"),
("redis.template.bicep", @"
param redis_name string = 'redis'
param redis_location string = 'westus'
resource redis 'Microsoft.Cache/Redis@2019-07-01' = {
name: redis_name
location: redis_location
properties: {
sku: {
name: 'Standard'
family: 'C'
capacity: 2
}
}
}
"));
result.Template.Should().NotHaveValue();
result.ExcludingLinterDiagnostics().Should().HaveDiagnostics(new[]
{
("BCP120", DiagnosticLevel.Error, "This expression is being used in an assignment to the \"scope\" property of the \"module\" type, which requires a value that can be calculated at the start of the deployment. You are referencing a variable which cannot be calculated at the start (\"appResGrp\" -> \"rg\"). Properties of rg which can be calculated at the start include \"name\"."),
});
}
[TestMethod]
// https://github.com/azure/bicep/issues/1465
public void Test_Issue1465()
{
var result = CompilationHelper.Compile(@"
resource foo 'Microsoft.foo/bar@2020-01-01' existing = {
name: 'name'
}
output prop1 string = foo.properties.prop1
");
result.Template.Should().HaveValueAtPath("$.outputs['prop1'].value", "[reference(resourceId('Microsoft.foo/bar', 'name'), '2020-01-01').prop1]");
}
[TestMethod]
// https://github.com/azure/bicep/issues/822
public void Test_Issue822()
{
var result = CompilationHelper.Compile(
("main.bicep", @"
targetScope = 'subscription'
resource myRg 'Microsoft.Resources/resourceGroups@2020-06-01' = {
location: 'eastus'
name: 'rg'
}
module vnetmodule './vnet.bicep' = {
scope: myRg
name: 'vnet'
params: {
location: 'eastus'
name: 'myVnet'
}
}
"),
("vnet.bicep", @"
param location string
param name string
"));
result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();
result.Template.Should().NotHaveValueAtPath("$.resources[?(@.name == 'vnet')].subscriptionId");
result.Template.Should().HaveValueAtPath("$.resources[?(@.name == 'vnet')].resourceGroup", "rg");
result.Template.Should().HaveValueAtPath("$.resources[?(@.name == 'vnet')].dependsOn[0]", "[subscriptionResourceId('Microsoft.Resources/resourceGroups', 'rg')]");
}
[TestMethod]
// https://github.com/azure/bicep/issues/822
public void Test_Issue822_scoped()
{
var result = CompilationHelper.Compile(
("main.bicep", @"
resource myRg 'Microsoft.Resources/resourceGroups@2020-06-01' existing = {
scope: subscription('abcdef')
name: 'rg'
}
module vnetmodule './vnet.bicep' = {
scope: myRg
name: 'vnet'
params: {
location: 'eastus'
name: 'myVnet'
}
}
"),
("vnet.bicep", @"
param location string
param name string
"));
result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();
result.Template.Should().HaveValueAtPath("$.resources[?(@.name == 'vnet')].subscriptionId", "abcdef");
result.Template.Should().HaveValueAtPath("$.resources[?(@.name == 'vnet')].resourceGroup", "rg");
}
[TestMethod]
// https://github.com/azure/bicep/issues/1388
public void Test_Issue1388()
{
var result = CompilationHelper.Compile(@"
targetScope = 'subscription'
param rgName string
param location string = deployment().location
param groupOwnerId string
param groupContributorId string
param groupReaderId string
resource rg 'Microsoft.Resources/resourceGroups@2020-06-01' = {
name: rgName
location: location
}
resource rgOwner 'Microsoft.Authorization/roleAssignments@2020-10-01-preview' = {
name: '${guid(rg.name, 'owner')}'
scope: rg
properties: {
roleDefinitionId: '${subscriptionResourceId('Microsoft.Authorization/roleDefinitions', '8e3af657-a8ff-443c-a75c-2fe8c4bcb635')}'
principalId: groupOwnerId
principalType: 'Group'
}
}
resource rgContributor 'Microsoft.Authorization/roleAssignments@2020-10-01-preview' = {
name: '${guid(rg.name, 'contributor')}'
scope: rg
properties: {
roleDefinitionId: '${subscriptionResourceId('Microsoft.Authorization/roleDefinitions', 'b24988ac-6180-42a0-ab88-20f7382dd24c')}'
principalId: groupContributorId
principalType: 'Group'
}
}
resource rgReader 'Microsoft.Authorization/roleAssignments@2020-10-01-preview' = {
name: '${guid(rg.name, 'reader')}'
scope: rg
properties: {
roleDefinitionId: '${subscriptionResourceId('Microsoft.Authorization/roleDefinitions', 'acdd72a7-3385-48ef-bd42-f606fba81ae7')}'
principalId: groupReaderId
principalType: 'Group'
}
}
");
result.Template.Should().NotHaveValue();
result.ExcludingLinterDiagnostics().Should().HaveDiagnostics(new[]
{
("BCP139", DiagnosticLevel.Error, "A resource's scope must match the scope of the Bicep file for it to be deployable. You must use modules to deploy resources to a different scope."),
("BCP139", DiagnosticLevel.Error, "A resource's scope must match the scope of the Bicep file for it to be deployable. You must use modules to deploy resources to a different scope."),
("BCP139", DiagnosticLevel.Error, "A resource's scope must match the scope of the Bicep file for it to be deployable. You must use modules to deploy resources to a different scope."),
});
}
[TestMethod]
// https://github.com/azure/bicep/issues/1364
public void Test_Issue1364()
{
var result = CompilationHelper.Compile(@"
targetScope = 'blablah'
");
result.Template.Should().NotHaveValue();
result.ExcludingLinterDiagnostics().Should().HaveDiagnostics(new[]
{
("BCP033", DiagnosticLevel.Error, "Expected a value of type \"'managementGroup' | 'resourceGroup' | 'subscription' | 'tenant'\" but the provided value is of type \"'blablah'\"."),
});
}
[TestMethod]
// https://github.com/azure/bicep/issues/569
public void Test_Issue569_success()
{
var result = CompilationHelper.Compile(@"
param myparam string
var myvar = 'hello'
output myparam string = myparam
output myvar string = myvar
");
result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();
result.Template.Should().HaveValueAtPath("$.outputs['myparam'].value", "[parameters('myparam')]");
result.Template.Should().HaveValueAtPath("$.outputs['myvar'].value", "[variables('myvar')]");
}
[TestMethod]
// https://github.com/azure/bicep/issues/569
public void Test_Issue569_duplicates()
{
var result = CompilationHelper.Compile(@"
output duplicate string = 'hello'
output duplicate string = 'hello'
");
result.Template.Should().NotHaveValue();
result.ExcludingLinterDiagnostics().Should().HaveDiagnostics(new[]
{
("BCP145", DiagnosticLevel.Error, "Output \"duplicate\" is declared multiple times. Remove or rename the duplicates."),
("BCP145", DiagnosticLevel.Error, "Output \"duplicate\" is declared multiple times. Remove or rename the duplicates."),
});
}
[TestMethod]
// https://github.com/azure/bicep/issues/569
public void Test_Issue569_outputs_cannot_be_referenced()
{
var result = CompilationHelper.Compile(@"
output output1 string = 'hello'
output output2 string = output1
");
result.Template.Should().NotHaveValue();
result.ExcludingLinterDiagnostics().Should().HaveDiagnostics(new[]
{
("BCP057", DiagnosticLevel.Error, "The name \"output1\" does not exist in the current context."),
});
}
[TestMethod]
// https://github.com/azure/bicep/issues/1599
public void Test_Issue1599()
{
var result = CompilationHelper.Compile(@"
param x string = 't'
output xx = x
");
result.Template.Should().NotHaveValue();
result.ExcludingLinterDiagnostics().Should().HaveDiagnostics(new[]
{
("BCP279", DiagnosticLevel.Error, "Expected a type at this location. Please specify a valid type expression or one of the following types: \"array\", \"bool\", \"int\", \"object\", \"string\"."),
});
}
[TestMethod]
// https://github.com/azure/bicep/issues/1661
public void Test_Issue1661()
{
// Issue 1661 only repros if global-resources.bicep exists and kevault-secrets.bicep does not
var result = CompilationHelper.Compile(("main.bicep", @"
targetScope = 'subscription'
param prefix string
param deploymentId string
param tags object
param stampLocations array = [
'northeurope'
'eastus2'
]
resource rg_stamps 'Microsoft.Resources/resourceGroups@2020-06-01' = [for stamp in stampLocations: {
name: '${prefix}-${stamp}-rg'
location: stamp
tags: tags
}]
//... more modules
module global_resources './global-resources.bicep' = {
name: 'global_resources-${deploymentId}'
scope: rg_global
params: {
location: rg_global.location
prefix: prefix
tags: tags
stamps: [for index in range(0, length(stampLocations)): {
location: stampLocations[index]
aksKubletIdentityPrincipalId: stamps[index].outputs.aksKubletIdentityPrincipalId
aksSubnetId: stamps[index].outputs.aksSubnetId
backend_fqdn: stamps[index].outputs.ingressFqdn
}]
}
}
var secrets = [
{
name: 'CosmosDb-Endpoint'
value: global_resources.outputs.cosmosDbEndpoint
}
{
name: 'CosmosDb-PrimaryKey'
value: global_resources.outputs.cosmosDbKey
}
]
module stamp_0_secrets './kevault-secrets.bicep' = [for secret in secrets: {
name: 'stamp_0_secrets-${deploymentId}'
scope: resourceGroup(rg_stamps[0].name)
dependsOn: [
stamps
]
params: {
keyVaultName: '${prefix}${rg_stamps[0].location}kv'
secretName: secret.name
secretValue: secret.value
}
}]
module stamp_1_secrets './kevault-secrets.bicep' = [for secret in secrets: {
name: 'stamp_1_secrets-${deploymentId}'
scope: resourceGroup(rg_stamps[1].name)
dependsOn: [
stamps
]
params: {
keyVaultName: '${prefix}${rg_stamps[1].location}kv'
secretName: secret.name
secretValue: secret.value
}
}]
"), ("global-resources.bicep", string.Empty));
result.Template.Should().NotHaveValue();
result.Should().ContainDiagnostic("BCP057", DiagnosticLevel.Error, "The name \"rg_global\" does not exist in the current context.");
result.Should().ContainDiagnostic("BCP057", DiagnosticLevel.Error, "The name \"rg_global\" does not exist in the current context.");
result.Should().ContainDiagnostic("BCP057", DiagnosticLevel.Error, "The name \"stamps\" does not exist in the current context.");
result.Should().ContainDiagnostic("BCP057", DiagnosticLevel.Error, "The name \"stamps\" does not exist in the current context.");
result.Should().ContainDiagnostic("BCP057", DiagnosticLevel.Error, "The name \"stamps\" does not exist in the current context.");
result.Should().ContainDiagnostic("BCP052", DiagnosticLevel.Error, "The type \"outputs\" does not contain property \"cosmosDbEndpoint\".");
result.Should().ContainDiagnostic("BCP052", DiagnosticLevel.Error, "The type \"outputs\" does not contain property \"cosmosDbKey\".");
}
[TestMethod]
// https://github.com/azure/bicep/issues/1592
public void Test_Issue1592()
{
var result = CompilationHelper.Compile(
("main.bicep", @"
module foo 'test.bicep' = {
name: 'foo'
}
output fooName string = foo.name
"),
("test.bicep", @""));
result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();
result.Template.Should().HaveValueAtPath("$.outputs['fooName'].value", "foo");
}
[TestMethod]
// https://github.com/azure/bicep/issues/1592
public void Test_Issue1592_special_cases()
{
var result = CompilationHelper.Compile(
("main.bicep", @"
param someParam string
module foo 'test.bicep' = {
name: '${someParam}-test'
}
output fooName string = foo.name
output fooOutput string = foo.outputs.test
"),
("test.bicep", @"
output test string = 'hello'
"));
result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();
result.Template.Should().HaveValueAtPath("$.outputs['fooName'].value", "[format('{0}-test', parameters('someParam'))]");
result.Template.Should().HaveValueAtPath("$.outputs['fooOutput'].value", "[reference(resourceId('Microsoft.Resources/deployments', format('{0}-test', parameters('someParam'))), '2022-09-01').outputs.test.value]");
}
[TestMethod]
// https://github.com/azure/bicep/issues/1432
public void Test_Issue1432()
{
var result = CompilationHelper.Compile(@"
resource foo 'Microsoft.Compute/virtualMachines@2020-06-01' = {
name: 'myVM'
name: 'myVm'
}
");
result.Template.Should().NotHaveValue();
result.ExcludingLinterDiagnostics().Should().HaveDiagnostics(new[]
{
("BCP025", DiagnosticLevel.Error, "The property \"name\" is declared multiple times in this object. Remove or rename the duplicate properties."),
("BCP025", DiagnosticLevel.Error, "The property \"name\" is declared multiple times in this object. Remove or rename the duplicate properties."),
});
}
[TestMethod]
// https://github.com/azure/bicep/issues/1817
public void Test_Issue1817()
{
var result = CompilationHelper.Compile(@"
targetScope = w
var foo = 42
");
result.Template.Should().NotHaveValue();
result.ExcludingLinterDiagnostics().Should().HaveDiagnostics(new[]
{
("BCP032", DiagnosticLevel.Error, "The value must be a compile-time constant."),
("BCP057", DiagnosticLevel.Error, "The name \"w\" does not exist in the current context."),
});
}
[TestMethod]
// https://github.com/azure/bicep/issues/1630
public void Test_Issue1630()
{
var result = CompilationHelper.Compile(@"
#disable-next-line BCP241
var singleResource = providers('Microsoft.Insights', 'components')
#disable-next-line BCP241
var allResources = providers('Microsoft.Insights')
// singleResource is an object!
var firstApiVersion = singleResource.apiVersions[0]
// allResources is an array of objects!
var firstResourceFirstApiVersion = allResources.resourceTypes[0].apiVersions[0]
output singleResource object = singleResource
output allResources array = allResources.resourceTypes
");
result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();
result.Template.Should().HaveValueAtPath("$.variables['singleResource']", "[providers('Microsoft.Insights', 'components')]");
result.Template.Should().HaveValueAtPath("$.variables['firstApiVersion']", "[variables('singleResource').apiVersions[0]]");
result.Template.Should().HaveValueAtPath("$.variables['allResources']", "[providers('Microsoft.Insights')]");
result.Template.Should().HaveValueAtPath("$.variables['firstResourceFirstApiVersion']", "[variables('allResources').resourceTypes[0].apiVersions[0]]");
}
[TestMethod]
// https://github.com/azure/bicep/issues/1627
public void Test_Issue1627()
{
var result = CompilationHelper.Compile(("main.bicep", @"
module modulea 'modulea.bicep' = {
name: 'modulea'
params: {
foo: 'test'