Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bicep deploy - fix content modified error #6325

Merged
merged 4 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 38 additions & 12 deletions src/Bicep.LangServer.UnitTests/Deploy/DeploymentHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,19 @@ public async Task CreateDeployment_WithInvalidScope_ReturnsDeploymentFailedMessa
deploymentCollectionProvider
.Setup(m => m.GetDeploymentCollection(It.IsAny<ArmClient>(), It.IsAny<ResourceIdentifier>(), scope))
.Throws(new Exception(string.Format(LangServerResources.UnsupportedTargetScopeMessage, scope)));
var documentPath = "some_path";

var result = await DeploymentHelper.CreateDeployment(
deploymentCollectionProvider.Object,
armClient,
documentPath,
string.Empty,
string.Empty,
"/subscriptions/07268dd7-4c50-434b-b1ff-67b8164edb41",
scope,
string.Empty);

var expectedDeploymentOutputMessage = string.Format(LangServerResources.DeploymentFailedWithExceptionMessage,
var expectedDeploymentOutputMessage = string.Format(LangServerResources.DeploymentFailedWithExceptionMessage, documentPath,
string.Format(LangServerResources.UnsupportedTargetScopeMessage, scope));

result.Should().Be(expectedDeploymentOutputMessage);
Expand All @@ -63,16 +65,19 @@ public async Task CreateDeployment_WithInvalidScope_ReturnsDeploymentFailedMessa
public async Task CreateDeployment_WithSubscriptionScopeAndInvalidLocation_ReturnsDeploymentFailedMessage(string location)
{
var armClient = CreateMockArmClient();
var documentPath = "some_path";

var result = await DeploymentHelper.CreateDeployment(
CreateDeploymentCollectionProvider(),
armClient,
documentPath,
string.Empty,
string.Empty,
"/subscriptions/07268dd7-4c50-434b-b1ff-67b8164edb41",
LanguageConstants.TargetScopeTypeSubscription,
location);

result.Should().Be(LangServerResources.MissingLocationDeploymentFailedMessage);
result.Should().Be(string.Format(LangServerResources.MissingLocationDeploymentFailedMessage, documentPath));
}

[DataRow(null)]
Expand All @@ -82,16 +87,19 @@ public async Task CreateDeployment_WithSubscriptionScopeAndInvalidLocation_Retur
public async Task CreateDeployment_WithManagementGroupScopeAndInvalidLocation_ReturnsDeploymentFailedMessage(string location)
{
var armClient = CreateMockArmClient();
var documentPath = "some_path";

var result = await DeploymentHelper.CreateDeployment(
CreateDeploymentCollectionProvider(),
armClient,
documentPath,
string.Empty,
string.Empty,
"/subscriptions/07268dd7-4c50-434b-b1ff-67b8164edb41",
LanguageConstants.TargetScopeTypeManagementGroup,
location);

result.Should().Be(LangServerResources.MissingLocationDeploymentFailedMessage);
result.Should().Be(string.Format(LangServerResources.MissingLocationDeploymentFailedMessage, documentPath));
}

[TestMethod]
Expand All @@ -102,17 +110,19 @@ public async Task CreateDeployment_WithTenantScope_ReturnsDeploymentNotSupported
deploymentCollectionProvider
.Setup(m => m.GetDeploymentCollection(It.IsAny<ArmClient>(), It.IsAny<ResourceIdentifier>(), LanguageConstants.TargetScopeTypeTenant))
.Throws(new Exception(string.Format(LangServerResources.UnsupportedTargetScopeMessage, LanguageConstants.TargetScopeTypeTenant)));
var documentPath = "some_path";

var result = await DeploymentHelper.CreateDeployment(
deploymentCollectionProvider.Object,
armClient,
documentPath,
string.Empty,
string.Empty,
"/subscriptions/07268dd7-4c50-434b-b1ff-67b8164edb41",
LanguageConstants.TargetScopeTypeTenant,
string.Empty);

var expectedDeploymentOutputMessage = string.Format(LangServerResources.DeploymentFailedWithExceptionMessage,
var expectedDeploymentOutputMessage = string.Format(LangServerResources.DeploymentFailedWithExceptionMessage, documentPath,
string.Format(LangServerResources.UnsupportedTargetScopeMessage, LanguageConstants.TargetScopeTypeTenant));

result.Should().Be(expectedDeploymentOutputMessage);
Expand Down Expand Up @@ -141,17 +151,19 @@ public async Task CreateDeployment_WithValidScopeAndInput_ReturnsDeploymentSucce
deploymentCollectionProvider
.Setup(m => m.GetDeploymentCollection(It.IsAny<ArmClient>(), It.IsAny<ResourceIdentifier>(), scope))
.Returns(deploymentCollection);
var documentPath = "some_path";

var result = await DeploymentHelper.CreateDeployment(
deploymentCollectionProvider.Object,
CreateMockArmClient(),
documentPath,
template,
string.Empty,
"/subscriptions/07268dd7-4c50-434b-b1ff-67b8164edb41/resourceGroups/bhavyatest",
scope,
location);

result.Should().Be(LangServerResources.DeploymentSucceededMessage);
result.Should().Be(string.Format(LangServerResources.DeploymentSucceededMessage, documentPath));
}

[TestMethod]
Expand All @@ -174,17 +186,19 @@ public async Task CreateDeployment_WithInvalidValidParameterFilePath_ReturnsDepl
deploymentCollectionProvider
.Setup(m => m.GetDeploymentCollection(It.IsAny<ArmClient>(), It.IsAny<ResourceIdentifier>(), LanguageConstants.TargetScopeTypeSubscription))
.Returns(deploymentCollection);
var documentPath = "some_path";

var result = await DeploymentHelper.CreateDeployment(
deploymentCollectionProvider.Object,
CreateMockArmClient(),
documentPath,
template,
@"c:\parameter.json",
"/subscriptions/07268dd7-4c50-434b-b1ff-67b8164edb41/resourceGroups/bhavyatest",
LanguageConstants.TargetScopeTypeSubscription,
"eastus");

result.Should().Contain(string.Format(LangServerResources.InvalidParameterFileDeploymentFailedMessage, @"Could not find file"));
result.Should().Contain(string.Format(LangServerResources.InvalidParameterFileDeploymentFailedMessage, documentPath, @"Could not find file"));
}

[TestMethod]
Expand All @@ -208,17 +222,19 @@ public async Task CreateDeployment_WithInvalidValidParameterFileContents_Returns
.Setup(m => m.GetDeploymentCollection(It.IsAny<ArmClient>(), It.IsAny<ResourceIdentifier>(), LanguageConstants.TargetScopeTypeSubscription))
.Returns(deploymentCollection);
string parametersFilePath = FileHelper.SaveResultFile(TestContext, "parameters.json", "invalid_parameters_file");
var documentPath = "some_path";

var result = await DeploymentHelper.CreateDeployment(
deploymentCollectionProvider.Object,
CreateMockArmClient(),
documentPath,
template,
parametersFilePath,
"/subscriptions/07268dd7-4c50-434b-b1ff-67b8164edb41/resourceGroups/bhavyatest",
LanguageConstants.TargetScopeTypeSubscription,
"eastus");

result.Should().Be(string.Format(LangServerResources.InvalidParameterFileDeploymentFailedMessage, @"'i' is an invalid start of a value. LineNumber: 0 | BytePositionInLine: 0."));
result.Should().Be(string.Format(LangServerResources.InvalidParameterFileDeploymentFailedMessage, documentPath, @"'i' is an invalid start of a value. LineNumber: 0 | BytePositionInLine: 0."));
}

[TestMethod]
Expand All @@ -240,17 +256,19 @@ public async Task CreateDeployment_WithNoDeploymentCollection_ReturnsDeploymentF
deploymentCollectionProvider
.Setup(m => m.GetDeploymentCollection(It.IsAny<ArmClient>(), It.IsAny<ResourceIdentifier>(), LanguageConstants.TargetScopeTypeResourceGroup))
.Returns<DeploymentCollection>(null);
var documentPath = "some_path";

var result = await DeploymentHelper.CreateDeployment(
deploymentCollectionProvider.Object,
CreateMockArmClient(),
documentPath,
template,
string.Empty,
"/subscriptions/07268dd7-4c50-434b-b1ff-67b8164edb41/resourceGroups/bhavyatest",
LanguageConstants.TargetScopeTypeResourceGroup,
"");

result.Should().Be(LangServerResources.DeploymentFailedMessage);
result.Should().Be(string.Format(LangServerResources.DeploymentFailedMessage, documentPath));
}

[TestMethod]
Expand All @@ -273,17 +291,19 @@ public async Task CreateDeployment_WithExceptionWhileFetchingDeploymentCollectio
deploymentCollectionProvider
.Setup(m => m.GetDeploymentCollection(It.IsAny<ArmClient>(), It.IsAny<ResourceIdentifier>(), LanguageConstants.TargetScopeTypeResourceGroup))
.Throws(new Exception(errorMessage));
var documentPath = "some_path";

var result = await DeploymentHelper.CreateDeployment(
deploymentCollectionProvider.Object,
CreateMockArmClient(),
documentPath,
template,
string.Empty,
"/subscriptions/07268dd7-4c50-434b-b1ff-67b8164edb41/resourceGroups/bhavyatest",
LanguageConstants.TargetScopeTypeResourceGroup,
"");

result.Should().Be(string.Format(LangServerResources.DeploymentFailedWithExceptionMessage, errorMessage));
result.Should().Be(string.Format(LangServerResources.DeploymentFailedWithExceptionMessage, documentPath, errorMessage));
}

[TestMethod]
Expand Down Expand Up @@ -315,17 +335,19 @@ public async Task CreateDeployment_WhenDeploymentCreateOrUpdateOperationHasNoVal
deploymentCollectionProvider
.Setup(m => m.GetDeploymentCollection(It.IsAny<ArmClient>(), It.IsAny<ResourceIdentifier>(), LanguageConstants.TargetScopeTypeResourceGroup))
.Returns(deploymentCollection.Object);
var documentPath = "some_path";

var result = await DeploymentHelper.CreateDeployment(
deploymentCollectionProvider.Object,
CreateMockArmClient(),
documentPath,
template,
string.Empty,
"/subscriptions/07268dd7-4c50-434b-b1ff-67b8164edb41/resourceGroups/bhavyatest",
LanguageConstants.TargetScopeTypeResourceGroup,
"");

result.Should().Be(LangServerResources.DeploymentFailedMessage);
result.Should().Be(string.Format(LangServerResources.DeploymentFailedMessage, documentPath));
}

[TestMethod]
Expand Down Expand Up @@ -364,17 +386,19 @@ public async Task CreateDeployment_WithStatusMessageOtherThan200Or201_ReturnsDep
deploymentCollectionProvider
.Setup(m => m.GetDeploymentCollection(It.IsAny<ArmClient>(), It.IsAny<ResourceIdentifier>(), LanguageConstants.TargetScopeTypeResourceGroup))
.Returns(deploymentCollection.Object);
var documentPath = "some_path";

var result = await DeploymentHelper.CreateDeployment(
deploymentCollectionProvider.Object,
CreateMockArmClient(),
documentPath,
template,
string.Empty,
"/subscriptions/07268dd7-4c50-434b-b1ff-67b8164edb41/resourceGroups/bhavyatest",
LanguageConstants.TargetScopeTypeResourceGroup,
"");

result.Should().Be(string.Format(LangServerResources.DeploymentFailedWithExceptionMessage, responseMessage));
result.Should().Be(string.Format(LangServerResources.DeploymentFailedWithExceptionMessage, documentPath, responseMessage));
}

[TestMethod]
Expand Down Expand Up @@ -405,17 +429,19 @@ public async Task CreateDeployment_WithExceptionWhileCreatingDeployment_ReturnsD
deploymentCollectionProvider
.Setup(m => m.GetDeploymentCollection(It.IsAny<ArmClient>(), It.IsAny<ResourceIdentifier>(), LanguageConstants.TargetScopeTypeResourceGroup))
.Returns(deploymentCollection.Object);
var documentPath = "some_path";

var result = await DeploymentHelper.CreateDeployment(
deploymentCollectionProvider.Object,
CreateMockArmClient(),
documentPath,
template,
string.Empty,
"/subscriptions/07268dd7-4c50-434b-b1ff-67b8164edb41/resourceGroups/bhavyatest",
LanguageConstants.TargetScopeTypeResourceGroup,
"");

result.Should().Be(string.Format(LangServerResources.DeploymentFailedWithExceptionMessage, errorMessage));
result.Should().Be(string.Format(LangServerResources.DeploymentFailedWithExceptionMessage, documentPath, errorMessage));
}

private static ArmClient CreateMockArmClient()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
using Bicep.Core.TypeSystem;
using Bicep.Core.UnitTests;
using Bicep.Core.UnitTests.Assertions;
using Bicep.Core.UnitTests.Mock;
using Bicep.Core.UnitTests.Utils;
using Bicep.LanguageServer;
using Bicep.LanguageServer.Handlers;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OmniSharp.Extensions.JsonRpc;
using OmniSharp.Extensions.LanguageServer.Protocol;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
using IOFileSystem = System.IO.Abstractions.FileSystem;
Expand All @@ -33,6 +35,7 @@ public class BicepDeploymentScopeRequestHandlerTests
private static readonly FileResolver FileResolver = new();
private static readonly IConfigurationManager configurationManager = new ConfigurationManager(new IOFileSystem());
private readonly ModuleDispatcher ModuleDispatcher = new ModuleDispatcher(BicepTestConstants.RegistryProvider);
private readonly ISerializer Serializer = StrictMock.Of<ISerializer>().Object;

[TestMethod]
public async Task Handle_WithInvalidInputFile_ReturnsBicepDeploymentScopeResponseWithErrorMessage()
Expand All @@ -52,7 +55,8 @@ public async Task Handle_WithInvalidInputFile_ReturnsBicepDeploymentScopeRespons
BicepTestConstants.Features,
FileResolver,
ModuleDispatcher,
BicepTestConstants.NamespaceProvider);
BicepTestConstants.NamespaceProvider,
Serializer);

var textDocumentIdentifier = new TextDocumentIdentifier(documentUri);
BicepDeploymentScopeParams bicepDeploymentScopeParams = new BicepDeploymentScopeParams(textDocumentIdentifier);
Expand Down Expand Up @@ -84,7 +88,8 @@ public async Task Handle_WithInvalidConfigurationFile_ReturnsBicepDeploymentScop
BicepTestConstants.Features,
FileResolver,
ModuleDispatcher,
BicepTestConstants.NamespaceProvider);
BicepTestConstants.NamespaceProvider,
Serializer);

var textDocumentIdentifier = new TextDocumentIdentifier(documentUri);
BicepDeploymentScopeParams bicepDeploymentScopeParams = new BicepDeploymentScopeParams(textDocumentIdentifier);
Expand Down Expand Up @@ -114,7 +119,8 @@ public async Task Handle_WithValidInputFile_ReturnsBicepDeploymentScopeResponse(
BicepTestConstants.Features,
FileResolver,
ModuleDispatcher,
BicepTestConstants.NamespaceProvider);
BicepTestConstants.NamespaceProvider,
Serializer);

var textDocumentIdentifier = new TextDocumentIdentifier(documentUri);
BicepDeploymentScopeParams bicepDeploymentScopeParams = new BicepDeploymentScopeParams(textDocumentIdentifier);
Expand Down Expand Up @@ -168,7 +174,8 @@ public async Task Handle_WithValidInputFile_VerifyDeploymentScope(string scope,
BicepTestConstants.Features,
FileResolver,
ModuleDispatcher,
BicepTestConstants.NamespaceProvider);
BicepTestConstants.NamespaceProvider,
Serializer);

var textDocumentIdentifier = new TextDocumentIdentifier(documentUri);
BicepDeploymentScopeParams bicepDeploymentScopeParams = new BicepDeploymentScopeParams(textDocumentIdentifier);
Expand Down
Loading