Skip to content

Commit 1e2c222

Browse files
update provisioning test cases (#53355)
Co-authored-by: Copilot <[email protected]>
1 parent 2576e89 commit 1e2c222

File tree

4 files changed

+117
-69
lines changed

4 files changed

+117
-69
lines changed

sdk/provisioning/Azure.Provisioning/tests/BicepValues/BicepValueTests.cs

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Linq;
6+
using System.Threading.Tasks;
67
using Azure.Provisioning.Resources;
78
using NUnit.Framework;
89

@@ -14,71 +15,68 @@ public class BicepValueTests
1415
public void ValidateLiteralBicepValue()
1516
{
1617
// string value
17-
AssertExpression("'test'", new BicepValue<string>("test"));
18+
TestHelpers.AssertExpression("'test'", new BicepValue<string>("test"));
1819

1920
// int value
20-
AssertExpression("42", new BicepValue<int>(42));
21-
AssertExpression("-42", new BicepValue<int>(-42));
21+
TestHelpers.AssertExpression("42", new BicepValue<int>(42));
22+
TestHelpers.AssertExpression("-42", new BicepValue<int>(-42));
2223

2324
// long value
24-
AssertExpression("42", new BicepValue<long>(42L));
25-
AssertExpression("2147483647", new BicepValue<long>(2147483647L));
26-
AssertExpression("json('2147483648')", new BicepValue<long>(2147483648));
27-
AssertExpression("-2147483648", new BicepValue<long>(-2147483648L));
28-
AssertExpression("json('-2147483649')", new BicepValue<long>(-2147483649));
29-
AssertExpression("json('9223372036854775807')", new BicepValue<long>(9223372036854775807));
25+
TestHelpers.AssertExpression("42", new BicepValue<long>(42L));
26+
TestHelpers.AssertExpression("2147483647", new BicepValue<long>(2147483647L));
27+
TestHelpers.AssertExpression("json('2147483648')", new BicepValue<long>(2147483648));
28+
TestHelpers.AssertExpression("-2147483648", new BicepValue<long>(-2147483648L));
29+
TestHelpers.AssertExpression("json('-2147483649')", new BicepValue<long>(-2147483649));
30+
TestHelpers.AssertExpression("json('9223372036854775807')", new BicepValue<long>(9223372036854775807));
3031

3132
// bool value
32-
AssertExpression("true", new BicepValue<bool>(true));
33-
AssertExpression("false", new BicepValue<bool>(false));
33+
TestHelpers.AssertExpression("true", new BicepValue<bool>(true));
34+
TestHelpers.AssertExpression("false", new BicepValue<bool>(false));
3435

3536
// double value
36-
AssertExpression("json('3.14')", new BicepValue<double>(3.14));
37-
AssertExpression("json('-3.14')", new BicepValue<double>(-3.14));
37+
TestHelpers.AssertExpression("json('3.14')", new BicepValue<double>(3.14));
38+
TestHelpers.AssertExpression("json('-3.14')", new BicepValue<double>(-3.14));
3839
// double value with whole numbers
39-
AssertExpression("314", new BicepValue<double>(314d));
40-
AssertExpression("2147483647", new BicepValue<double>(2147483647d));
41-
AssertExpression("json('2147483648')", new BicepValue<double>(2147483648d));
42-
AssertExpression("-2147483647", new BicepValue<double>(-2147483647d));
43-
AssertExpression("-2147483648", new BicepValue<double>(-2147483648d));
44-
AssertExpression("json('-2147483649')", new BicepValue<double>(-2147483649d));
45-
46-
static void AssertExpression(string expected, BicepValue bicepValue)
47-
{
48-
Assert.AreEqual(expected, bicepValue.ToString());
49-
}
40+
TestHelpers.AssertExpression("314", new BicepValue<double>(314d));
41+
TestHelpers.AssertExpression("2147483647", new BicepValue<double>(2147483647d));
42+
TestHelpers.AssertExpression("json('2147483648')", new BicepValue<double>(2147483648d));
43+
TestHelpers.AssertExpression("-2147483647", new BicepValue<double>(-2147483647d));
44+
TestHelpers.AssertExpression("-2147483648", new BicepValue<double>(-2147483648d));
45+
TestHelpers.AssertExpression("json('-2147483649')", new BicepValue<double>(-2147483649d));
5046
}
5147

5248
[Test]
53-
public void ValidateTimeSpanPropertyWithFormat()
49+
public async Task ValidateTimeSpanPropertyWithFormat()
5450
{
55-
var infra = new Infrastructure();
56-
var powershell = new AzurePowerShellScript("script", "2023-08-01")
57-
{
58-
RetentionInterval = new TimeSpan(11, 22, 33),
59-
AzPowerShellVersion = "10.0",
60-
ScriptContent = "echo 'Hello, world!'",
61-
};
62-
63-
infra.Add(powershell);
64-
var plan = infra.Build();
65-
66-
var result = plan.Compile();
67-
Assert.AreEqual(
68-
"""
69-
@description('The location for the resource(s) to be deployed.')
70-
param location string = resourceGroup().location
71-
72-
resource script 'Microsoft.Resources/deploymentScripts@2023-08-01' = {
73-
name: take('script${uniqueString(resourceGroup().id)}', 24)
74-
location: location
75-
kind: 'AzurePowerShell'
76-
properties: {
77-
azPowerShellVersion: '10.0'
78-
retentionInterval: 'PT11H22M33S'
79-
scriptContent: 'echo \'Hello, world!\''
80-
}
81-
}
82-
""".NormalizeLineEndings(), result.Values.First().NormalizeLineEndings());
51+
await using Trycep test = new();
52+
test.Define(
53+
ctx =>
54+
{
55+
var infra = new Infrastructure();
56+
var powershell = new AzurePowerShellScript("script", "2023-08-01")
57+
{
58+
RetentionInterval = new TimeSpan(11, 22, 33),
59+
AzPowerShellVersion = "10.0",
60+
ScriptContent = "echo 'Hello, world!'",
61+
};
62+
infra.Add(powershell);
63+
return infra;
64+
})
65+
.Compare(
66+
"""
67+
@description('The location for the resource(s) to be deployed.')
68+
param location string = resourceGroup().location
69+
70+
resource script 'Microsoft.Resources/deploymentScripts@2023-08-01' = {
71+
name: take('script${uniqueString(resourceGroup().id)}', 24)
72+
location: location
73+
kind: 'AzurePowerShell'
74+
properties: {
75+
azPowerShellVersion: '10.0'
76+
retentionInterval: 'PT11H22M33S'
77+
scriptContent: 'echo \'Hello, world!\''
78+
}
79+
}
80+
""");
8381
}
8482
}

sdk/provisioning/Azure.Provisioning/tests/Primitives/ProvisionableResourceTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,44 @@ public async Task ValidateNormalProperties()
123123
""");
124124
}
125125

126+
[Test]
127+
public async Task ValidateListProperties_Unset()
128+
{
129+
await using var test = new Trycep();
130+
131+
test.Define(
132+
ctx =>
133+
{
134+
Infrastructure infra = new();
135+
136+
// Create a test resource with three basic property types: string, location, and enum
137+
var storageAccount = new StorageAccount("storageAccount")
138+
{
139+
// Test basic string property
140+
Name = "test-storage",
141+
142+
// Test location property
143+
Location = AzureLocation.WestUS2,
144+
145+
// Test enum property
146+
StorageTier = StorageTier.Standard
147+
};
148+
149+
infra.Add(storageAccount);
150+
return infra;
151+
})
152+
.Compare(
153+
"""
154+
resource storageAccount 'Test.Provider/storageAccounts@2024-01-01' = {
155+
name: 'test-storage'
156+
location: 'westus2'
157+
properties: {
158+
tier: 'Standard'
159+
}
160+
}
161+
""");
162+
}
163+
126164
[Test]
127165
public async Task ValidateResourceReference()
128166
{

sdk/provisioning/Azure.Provisioning/tests/StringHelpers.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System.Text.RegularExpressions;
5+
using Azure.Provisioning.Expressions;
6+
using NUnit.Framework;
7+
8+
namespace Azure.Provisioning.Tests
9+
{
10+
internal static class TestHelpers
11+
{
12+
public static string NormalizeLineEndings(this string input)
13+
{
14+
// Normalize line endings to LF
15+
return Regex.Replace(input, @"\r\n?", "\n");
16+
}
17+
18+
public static void AssertExpression(string expected, IBicepValue bicepValue)
19+
{
20+
Assert.AreEqual(expected.NormalizeLineEndings(), bicepValue.ToString()?.NormalizeLineEndings());
21+
}
22+
23+
public static void AssertExpression(string expected, BicepExpression expression)
24+
{
25+
Assert.AreEqual(expected.NormalizeLineEndings(), expression.ToString().NormalizeLineEndings());
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)