-
Notifications
You must be signed in to change notification settings - Fork 938
Expand file tree
/
Copy pathAzureBicepResourceTests.cs
More file actions
321 lines (255 loc) · 13.3 KB
/
Copy pathAzureBicepResourceTests.cs
File metadata and controls
321 lines (255 loc) · 13.3 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
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#pragma warning disable ASPIREPIPELINES001
#pragma warning disable ASPIREAZURE001
using System.Text.Json.Nodes;
using Aspire.Hosting.ApplicationModel;
using Aspire.Hosting.Lifecycle;
using Aspire.Hosting.Utils;
using Azure.Provisioning;
using Azure.Provisioning.Roles;
using Microsoft.Extensions.DependencyInjection;
namespace Aspire.Hosting.Azure.Tests;
public class AzureBicepResourceTests
{
[Fact]
public void AddBicepResource()
{
using var builder = TestDistributedApplicationBuilder.Create();
var bicepResource = builder.AddBicepTemplateString("mytemplate", "content")
.WithParameter("param1", "value1")
.WithParameter("param2", "value2");
Assert.Equal("content", bicepResource.Resource.TemplateString);
Assert.Equal("value1", bicepResource.Resource.Parameters["param1"]);
Assert.Equal("value2", bicepResource.Resource.Parameters["param2"]);
}
public static TheoryData<Func<IDistributedApplicationBuilder, IResourceBuilder<IResource>>> AzureExtensions =>
CreateAllAzureExtensions("x");
private static TheoryData<Func<IDistributedApplicationBuilder, IResourceBuilder<IResource>>> CreateAllAzureExtensions(string resourceName)
{
static void CreateInfrastructure(AzureResourceInfrastructure infrastructure)
{
var id = new UserAssignedIdentity("id");
infrastructure.Add(id);
infrastructure.Add(new ProvisioningOutput("cid", typeof(string)) { Value = id.ClientId.ToBicepExpression() });
}
return new()
{
{ builder => builder.AddAzureAppConfiguration(resourceName) },
{ builder => builder.AddAzureApplicationInsights(resourceName) },
{ builder => builder.AddBicepTemplate(resourceName, "template.bicep") },
{ builder => builder.AddBicepTemplateString(resourceName, "content") },
{ builder => builder.AddAzureInfrastructure(resourceName, CreateInfrastructure) },
{ builder => builder.AddAzureOpenAI(resourceName) },
{ builder => builder.AddAzureCosmosDB(resourceName) },
{ builder => builder.AddAzureEventHubs(resourceName) },
{ builder => builder.AddAzureKeyVault(resourceName) },
{ builder => builder.AddAzureLogAnalyticsWorkspace(resourceName) },
#pragma warning disable CS0618 // Type or member is obsolete
{ builder => builder.AddPostgres(resourceName).AsAzurePostgresFlexibleServer() },
{ builder => builder.AddRedis(resourceName).AsAzureRedis() },
{ builder => builder.AddSqlServer(resourceName).AsAzureSqlDatabase() },
{ builder => builder.AddAzureRedis(resourceName) },
#pragma warning restore CS0618 // Type or member is obsolete
{ builder => builder.AddAzurePostgresFlexibleServer(resourceName) },
{ builder => builder.AddAzureManagedRedis(resourceName) },
{ builder => builder.AddAzureSearch(resourceName) },
{ builder => builder.AddAzureServiceBus(resourceName) },
{ builder => builder.AddAzureSignalR(resourceName) },
{ builder => builder.AddAzureSqlServer(resourceName) },
{ builder => builder.AddAzureStorage(resourceName) },
{ builder => builder.AddAzureWebPubSub(resourceName) },
};
}
[Theory]
[MemberData(nameof(AzureExtensions))]
public void AzureExtensionsAutomaticallyAddAzureProvisioning(Func<IDistributedApplicationBuilder, IResourceBuilder<IResource>> addAzureResource)
{
using var builder = TestDistributedApplicationBuilder.Create();
addAzureResource(builder);
var app = builder.Build();
var eventingServices = app.Services.GetServices<IDistributedApplicationEventingSubscriber>();
Assert.Single(eventingServices.OfType<AzureProvisioner>());
}
[Theory]
[MemberData(nameof(AzureExtensions))]
public void BicepResourcesAreIdempotent(Func<IDistributedApplicationBuilder, IResourceBuilder<IResource>> addAzureResource)
{
using var builder = TestDistributedApplicationBuilder.Create();
var azureResourceBuilder = addAzureResource(builder);
if (azureResourceBuilder.Resource is not AzureProvisioningResource bicepResource)
{
// Skip
return;
}
// This makes sure that these don't throw
bicepResource.GetBicepTemplateFile();
bicepResource.GetBicepTemplateFile();
}
public static TheoryData<Func<IDistributedApplicationBuilder, IResourceBuilder<IResource>>> AzureExtensionsWithHyphen =>
CreateAllAzureExtensions("x-y");
[Theory]
[MemberData(nameof(AzureExtensionsWithHyphen))]
public void AzureResourcesProduceValidBicep(Func<IDistributedApplicationBuilder, IResourceBuilder<IResource>> addAzureResource)
{
using var builder = TestDistributedApplicationBuilder.Create();
var azureResourceBuilder = addAzureResource(builder);
if (azureResourceBuilder.Resource is not AzureProvisioningResource bicepResource)
{
// Skip
return;
}
var bicep = bicepResource.GetBicepTemplateString();
Assert.DoesNotContain("resource x-y", bicep);
}
[Fact]
public void GetOutputReturnsOutputValue()
{
using var builder = TestDistributedApplicationBuilder.Create();
var bicepResource = builder.AddBicepTemplateString("templ", "content");
bicepResource.Resource.Outputs["resourceEndpoint"] = "https://myendpoint";
Assert.Equal("https://myendpoint", bicepResource.GetOutput("resourceEndpoint").Value);
}
[Fact]
public void GetSecretOutputReturnsSecretOutputValue()
{
using var builder = TestDistributedApplicationBuilder.Create();
var bicepResource = builder.AddBicepTemplateString("templ", "content");
bicepResource.Resource.SecretOutputs["connectionString"] = "https://myendpoint;Key=43";
#pragma warning disable CS0618 // Type or member is obsolete
Assert.Equal("https://myendpoint;Key=43", bicepResource.GetSecretOutput("connectionString").Value);
#pragma warning restore CS0618 // Type or member is obsolete
}
[Fact]
public void GetOutputValueThrowsIfNoOutput()
{
using var builder = TestDistributedApplicationBuilder.Create();
var bicepResource = builder.AddBicepTemplateString("templ", "content");
Assert.Throws<InvalidOperationException>(() => bicepResource.GetOutput("resourceEndpoint").Value);
}
[Fact]
public void GetSecretOutputValueThrowsIfNoOutput()
{
using var builder = TestDistributedApplicationBuilder.Create();
var bicepResource = builder.AddBicepTemplateString("templ", "content");
#pragma warning disable CS0618 // Type or member is obsolete
Assert.Throws<InvalidOperationException>(() => bicepResource.GetSecretOutput("connectionString").Value);
#pragma warning restore CS0618 // Type or member is obsolete
}
[Fact]
public async Task AssertManifestLayout()
{
using var builder = TestDistributedApplicationBuilder.Create();
var param = builder.AddParameter("p1");
var b2 = builder.AddBicepTemplateString("temp2", "content");
var bicepResource = builder.AddBicepTemplateString("templ", "content")
.WithParameter("param1", "value1")
.WithParameter("param2", ["1", "2"])
.WithParameter("param3", new JsonObject() { ["value"] = "nested" })
.WithParameter("param4", param)
.WithParameter("param5", b2.GetOutput("value1"))
.WithParameter("param6", () => b2.GetOutput("value2"));
bicepResource.Resource.TempDirectory = Environment.CurrentDirectory;
var manifest = await ManifestUtils.GetManifest(bicepResource.Resource);
var expectedManifest = """
{
"type": "azure.bicep.v0",
"path": "templ.module.bicep",
"params": {
"param1": "value1",
"param2": [
"1",
"2"
],
"param3": {
"value": "nested"
},
"param4": "{p1.value}",
"param5": "{temp2.outputs.value1}",
"param6": "{temp2.outputs.value2}"
}
}
""";
Assert.Equal(expectedManifest, manifest.ToString());
}
[Fact]
public async Task BicepResourceHasPipelineStepAnnotationWithCorrectConfiguration()
{
// Arrange
using var builder = TestDistributedApplicationBuilder.Create();
var bicepResource = builder.AddBicepTemplateString("myresource", "content");
// Act - Get the annotation
var annotation = bicepResource.Resource.Annotations.OfType<Aspire.Hosting.Pipelines.PipelineStepAnnotation>().FirstOrDefault();
// Assert - Annotation exists
Assert.NotNull(annotation);
// Act - Create the step from the annotation
var factoryContext = new Aspire.Hosting.Pipelines.PipelineStepFactoryContext
{
PipelineContext = null!, // Not needed for this test
Resource = bicepResource.Resource
};
var steps = await annotation.CreateStepsAsync(factoryContext);
var step = steps.First();
// Assert - Step has correct name
Assert.Equal("provision-myresource", step.Name);
// Assert - Step is configured with RequiredBy relationship to ProvisionInfrastructure
// Note: RequiredBy relationships are stored internally and converted to DependsOn during pipeline execution
// This test verifies the step is created correctly; the conversion is tested in pipeline tests
// Assert - Step depends on CreateProvisioningContext
Assert.Contains(AzureEnvironmentResource.CreateProvisioningContextStepName, step.DependsOnSteps);
}
[Fact]
public void GetBicepTemplateFile_WithTemplateFile_ReturnsOriginalPathWhenDirectoryProvided()
{
// This test verifies the fix for https://github.com/dotnet/aspire/issues/13967
// When a templateFile is specified, GetBicepTemplateFile should return the original path
// and not combine it with the directory parameter.
using var tempDir = new TestTempDirectory();
// Create a test bicep file
var bicepFileName = "test-template.bicep";
var bicepFilePath = Path.Combine(tempDir.Path, bicepFileName);
File.WriteAllText(bicepFilePath, "param location string = resourceGroup().location");
// Create the AzureBicepResource with the templateFile
var resource = new AzureBicepResource("test-resource", templateFile: bicepFilePath);
// Create a different directory to pass to GetBicepTemplateFile
var outputDir = Path.Combine(tempDir.Path, "output");
Directory.CreateDirectory(outputDir);
// Get the bicep template file with a directory parameter
using var templateFile = resource.GetBicepTemplateFile(outputDir);
// The path should be the original template file path, not combined with outputDir
Assert.Equal(bicepFilePath, templateFile.Path);
Assert.True(File.Exists(templateFile.Path), $"The template file should exist at {templateFile.Path}");
}
[Fact]
public void GetBicepTemplateFile_WithTemplateFile_ReturnsOriginalPathWithoutDirectory()
{
using var tempDir = new TestTempDirectory();
// Create a test bicep file
var bicepFileName = "test-template.bicep";
var bicepFilePath = Path.Combine(tempDir.Path, bicepFileName);
File.WriteAllText(bicepFilePath, "param location string = resourceGroup().location");
// Create the AzureBicepResource with the templateFile
var resource = new AzureBicepResource("test-resource", templateFile: bicepFilePath);
// Get the bicep template file without a directory parameter
using var templateFile = resource.GetBicepTemplateFile();
// The path should be the original template file path
Assert.Equal(bicepFilePath, templateFile.Path);
}
[Fact]
public void GetBicepTemplateFile_WithTemplateString_WritesToDirectory()
{
using var tempDir = new TestTempDirectory();
var bicepContent = "param location string = resourceGroup().location";
// Create the AzureBicepResource with a template string (not a file)
var resource = new AzureBicepResource("test-resource", templateString: bicepContent);
// Create a directory to pass to GetBicepTemplateFile
var outputDir = Path.Combine(tempDir.Path, "output");
Directory.CreateDirectory(outputDir);
// Get the bicep template file with a directory parameter
using var templateFile = resource.GetBicepTemplateFile(outputDir);
// The path should be in the output directory
Assert.StartsWith(outputDir, templateFile.Path);
Assert.True(File.Exists(templateFile.Path), $"The template file should exist at {templateFile.Path}");
Assert.Equal(bicepContent, File.ReadAllText(templateFile.Path));
}
}