Skip to content

[Insights] Using SDK 20.1-preview with some fixes. Fixing reported issues with the Diagnostic Settings cmdlets #6998

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

Closed
wants to merge 3 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,16 @@ public void GetActionGroupCommandParametersProcessing()
// Error
cmdlet.ResourceGroupName = " ";
cmdlet.Name = Utilities.Name;
Assert.Throws<PSArgumentException>(() => cmdlet.ExecuteCmdlet());
try
{
cmdlet.ExecuteCmdlet();
Assert.True(false, "Did not throw exception");
}
catch (PSInvalidOperationException ex)
{
string message = ex.ToString();
Assert.True(message.Contains("PSArgumentException"), "Wrong exception thrown");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Management.Monitor" Version="0.19.1-preview" />
<PackageReference Include="Microsoft.Azure.Management.Monitor" Version="0.20.1-preview" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Azure.Management.Monitor">
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Monitor.0.19.1-preview\lib\net452\Microsoft.Azure.Management.Monitor.dll</HintPath>
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Monitor.0.20.1-preview\lib\net452\Microsoft.Azure.Management.Monitor.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ public class GetDiagnosticSettingCommandTests
private readonly Mock<IDiagnosticSettingsOperations> insightsDiagnosticsOperationsMock;
private Mock<ICommandRuntime> commandRuntimeMock;
private Microsoft.Rest.Azure.AzureOperationResponse<DiagnosticSettingsResource> response;
private Microsoft.Rest.Azure.AzureOperationResponse<DiagnosticSettingsResourceCollection> multipleResponse;
private const string resourceId = "/subscriptions/123/resourcegroups/rg/providers/rp/resource/myresource";
private string calledResourceId;
private string diagnosticSettingName;
private bool singleResult;

public GetDiagnosticSettingCommandTests(Xunit.Abstractions.ITestOutputHelper output)
{
Expand All @@ -49,15 +51,15 @@ public GetDiagnosticSettingCommandTests(Xunit.Abstractions.ITestOutputHelper out
MonitorManagementClient = insightsManagementClientMock.Object
};

response = new Microsoft.Rest.Azure.AzureOperationResponse<DiagnosticSettingsResource>()
singleResult = true;

DiagnosticSettingsResource resource = new DiagnosticSettingsResource
{
Body = new DiagnosticSettingsResource
{
EventHubName = "",
EventHubAuthorizationRuleId = "",
StorageAccountId = "/subscriptions/123/resourcegroups/rg/providers/microsoft.storage/accounts/myaccount",
WorkspaceId = "",
Logs = new List<LogSettings>
EventHubName = "",
EventHubAuthorizationRuleId = "",
StorageAccountId = "/subscriptions/123/resourcegroups/rg/providers/microsoft.storage/accounts/myaccount",
WorkspaceId = "",
Logs = new List<LogSettings>
{
new LogSettings
{
Expand All @@ -80,7 +82,7 @@ public GetDiagnosticSettingCommandTests(Xunit.Abstractions.ITestOutputHelper out
Enabled = false
}
},
Metrics = new List<MetricSettings>
Metrics = new List<MetricSettings>
{
new MetricSettings
{
Expand All @@ -104,7 +106,11 @@ public GetDiagnosticSettingCommandTests(Xunit.Abstractions.ITestOutputHelper out
TimeGrain = TimeSpan.FromHours(1)
}
}
}
};

response = new Microsoft.Rest.Azure.AzureOperationResponse<DiagnosticSettingsResource>()
{
Body = resource
};

insightsDiagnosticsOperationsMock.Setup(f => f.GetWithHttpMessagesAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Dictionary<string, List<string>>>(), It.IsAny<CancellationToken>()))
Expand All @@ -113,6 +119,22 @@ public GetDiagnosticSettingCommandTests(Xunit.Abstractions.ITestOutputHelper out
{
this.calledResourceId = resourceId;
this.diagnosticSettingName = name;
this.singleResult = true;
});

multipleResponse = new Microsoft.Rest.Azure.AzureOperationResponse<DiagnosticSettingsResourceCollection>()
{
Body = new DiagnosticSettingsResourceCollection(
value: new List<DiagnosticSettingsResource>() { resource })
};

insightsDiagnosticsOperationsMock.Setup(f => f.ListWithHttpMessagesAsync(It.IsAny<string>(), It.IsAny<Dictionary<string, List<string>>>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(multipleResponse))
.Callback((string resourceId, Dictionary<string, List<string>> headers, CancellationToken cancellationToken) =>
{
this.calledResourceId = resourceId;
this.diagnosticSettingName = "service";
this.singleResult = false;
});

insightsManagementClientMock.SetupGet(f => f.DiagnosticSettings).Returns(this.insightsDiagnosticsOperationsMock.Object);
Expand All @@ -127,6 +149,15 @@ public void GetDiagnosticSettingCommandParametersProcessing()

Assert.Equal(resourceId, this.calledResourceId);
Assert.Equal("service", this.diagnosticSettingName);
Assert.False(singleResult, "Single result is not false");

cmdlet.ResourceId = resourceId;
cmdlet.Name = "service";
cmdlet.ExecuteCmdlet();

Assert.Equal(resourceId, this.calledResourceId);
Assert.Equal("service", this.diagnosticSettingName);
Assert.True(singleResult, "Single result is not true");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class RemoveDiagnosticSettingCommandTests
private Mock<ICommandRuntime> commandRuntimeMock;
private const string ResourceId = "/subscriptions/123/resourcegroups/rg/providers/rp/resource/myresource";
private Microsoft.Rest.Azure.AzureOperationResponse<DiagnosticSettingsResource> response;
private Microsoft.Rest.Azure.AzureOperationResponse<DiagnosticSettingsResourceCollection> multipleResponse;
DiagnosticSettingsResource calledSettings = null;

private string resourceIdIn;
Expand All @@ -51,20 +52,14 @@ public RemoveDiagnosticSettingCommandTests(Xunit.Abstractions.ITestOutputHelper
MonitorManagementClient = insightsManagementClientMock.Object
};

response = new Microsoft.Rest.Azure.AzureOperationResponse<DiagnosticSettingsResource>()
var resource = new DiagnosticSettingsResource(name: "service")
{
RequestId = Guid.NewGuid().ToString(),
Response = new System.Net.Http.HttpResponseMessage
{
StatusCode = System.Net.HttpStatusCode.OK
},
Body = new DiagnosticSettingsResource
{
EventHubName = "",
EventHubAuthorizationRuleId = "",
StorageAccountId = "/subscriptions/123/resourcegroups/rg/providers/microsoft.storage/accounts/myaccount",
WorkspaceId = "",
Logs = new List<LogSettings>

EventHubName = "",
EventHubAuthorizationRuleId = "",
StorageAccountId = "/subscriptions/123/resourcegroups/rg/providers/microsoft.storage/accounts/myaccount",
WorkspaceId = "",
Logs = new List<LogSettings>
{
new LogSettings
{
Expand All @@ -87,7 +82,7 @@ public RemoveDiagnosticSettingCommandTests(Xunit.Abstractions.ITestOutputHelper
Enabled = false
}
},
Metrics = new List<MetricSettings>
Metrics = new List<MetricSettings>
{
new MetricSettings
{
Expand All @@ -111,7 +106,16 @@ public RemoveDiagnosticSettingCommandTests(Xunit.Abstractions.ITestOutputHelper
TimeGrain = TimeSpan.FromHours(1)
}
}
}
};

response = new Microsoft.Rest.Azure.AzureOperationResponse<DiagnosticSettingsResource>()
{
RequestId = Guid.NewGuid().ToString(),
Response = new System.Net.Http.HttpResponseMessage
{
StatusCode = System.Net.HttpStatusCode.OK
},
Body = resource
};

insightsDiagnosticsOperationsMock.Setup(f => f.DeleteWithHttpMessagesAsync(
Expand Down Expand Up @@ -165,6 +169,20 @@ public RemoveDiagnosticSettingCommandTests(Xunit.Abstractions.ITestOutputHelper
this.settingName = name;
});

multipleResponse = new Microsoft.Rest.Azure.AzureOperationResponse<DiagnosticSettingsResourceCollection>()
{
Body = new DiagnosticSettingsResourceCollection(
value: new List<DiagnosticSettingsResource>() { resource })
};

insightsDiagnosticsOperationsMock.Setup(f => f.ListWithHttpMessagesAsync(It.IsAny<string>(), It.IsAny<Dictionary<string, List<string>>>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(multipleResponse))
.Callback((string resourceId, Dictionary<string, List<string>> headers, CancellationToken cancellationToken) =>
{
resourceIdIn = resourceId;
this.settingName = "service";
});

insightsManagementClientMock.SetupGet(f => f.DiagnosticSettings).Returns(this.insightsDiagnosticsOperationsMock.Object);

cmdlet.ResourceId = ResourceId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public class SetDiagnosticSettingCommandTests
DiagnosticSettingsResource ExistingSetting;
DiagnosticSettingsResource calledSettings = null;

private Microsoft.Rest.Azure.AzureOperationResponse<DiagnosticSettingsResourceCollection> multipleResponse;
private bool singleResult;

public SetDiagnosticSettingCommandTests(Xunit.Abstractions.ITestOutputHelper output)
{
ServiceManagemenet.Common.Models.XunitTracingInterceptor.AddToContext(new ServiceManagemenet.Common.Models.XunitTracingInterceptor(output));
Expand All @@ -54,7 +57,7 @@ public SetDiagnosticSettingCommandTests(Xunit.Abstractions.ITestOutputHelper out
MonitorManagementClient = insightsManagementClientMock.Object
};

this.ExistingSetting = GetDefaultSetting();
this.ExistingSetting = GetDefaultSetting(name: "service");

insightsDiagnosticsOperationsMock.Setup(f => f.GetWithHttpMessagesAsync(
It.IsAny<string>(),
Expand All @@ -81,14 +84,31 @@ public SetDiagnosticSettingCommandTests(Xunit.Abstractions.ITestOutputHelper out
Logs = x.Logs,
Metrics = x.Metrics,
StorageAccountId = x.StorageAccountId,
WorkspaceId = x.WorkspaceId
WorkspaceId = x.WorkspaceId,
ServiceBusRuleId = x.ServiceBusRuleId
};
return Task.FromResult(new AzureOperationResponse<DiagnosticSettingsResource>
{
Body = x
});
});

multipleResponse = new Microsoft.Rest.Azure.AzureOperationResponse<DiagnosticSettingsResourceCollection>()
{
Body = new DiagnosticSettingsResourceCollection(
value: new List<DiagnosticSettingsResource>() { this.ExistingSetting })
};

insightsDiagnosticsOperationsMock.Setup(f => f.ListWithHttpMessagesAsync(It.IsAny<string>(), It.IsAny<Dictionary<string, List<string>>>(), It.IsAny<CancellationToken>()))
.Returns((string rsId, Dictionary<string, List<string>> head, CancellationToken token) =>
{
// this.calledResourceId = resourceId;
// this.diagnosticSettingName = "service";
this.calledSettings = multipleResponse.Body.Value[0];
this.singleResult = false;
return Task.FromResult(multipleResponse);
});

insightsManagementClientMock.SetupGet(f => f.DiagnosticSettings).Returns(this.insightsDiagnosticsOperationsMock.Object);

cmdlet.ResourceId = resourceId;
Expand Down Expand Up @@ -140,7 +160,7 @@ public void SetServiceBus()
cmdlet.ExecuteCmdlet();

DiagnosticSettingsResource expectedSettings = GetDefaultSetting();
expectedSettings.EventHubName = newServiceBusId;
expectedSettings.ServiceBusRuleId = newServiceBusId;

VerifyCalledOnce();
VerifySettings(expectedSettings, this.calledSettings);
Expand All @@ -150,6 +170,8 @@ public void SetServiceBus()
cmdlet.EventHubName = newServiceBusId;
cmdlet.MyInvocation.BoundParameters[SetAzureRmDiagnosticSettingCommand.EventHubNameParamName] = newServiceBusId;
cmdlet.ExecuteCmdlet();
expectedSettings.EventHubName = newServiceBusId;
expectedSettings.ServiceBusRuleId = null;

VerifySettings(expectedSettings, this.calledSettings);
}
Expand All @@ -174,7 +196,7 @@ public void SetWorkspace()
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void SetSomeCategories()
{
cmdlet.Categories = new List<string> { "TestCategory1"};
cmdlet.Categories = new List<string> { "TestCategory1" };
cmdlet.Enabled = false;
cmdlet.MyInvocation.BoundParameters[SetAzureRmDiagnosticSettingCommand.EnabledParamName] = false;
cmdlet.ExecuteCmdlet();
Expand All @@ -189,7 +211,17 @@ public void SetSomeCategories()
cmdlet.Categories = new List<string> { "TestCategory3" };
cmdlet.Enabled = false;
cmdlet.MyInvocation.BoundParameters[SetAzureRmDiagnosticSettingCommand.EnabledParamName] = false;
Assert.Throws<ArgumentException>(() => cmdlet.ExecuteCmdlet());

try
{
cmdlet.ExecuteCmdlet();
Assert.True(false, "Did not throw exception");
}
catch (PSInvalidOperationException ex)
{
string message = ex.ToString();
Assert.True(message.Contains("ArgumentException"), "Not the right type of exception");
}

// Testing the new metric categories must be known before the cmdlet can add them
expectedSettings.Metrics[0].Enabled = false;
Expand All @@ -205,7 +237,16 @@ public void SetSomeCategories()
cmdlet.MetricCategory = new List<string> { "MetricCat3" };
cmdlet.Enabled = false;
cmdlet.MyInvocation.BoundParameters[SetAzureRmDiagnosticSettingCommand.EnabledParamName] = false;
Assert.Throws<ArgumentException>(() => cmdlet.ExecuteCmdlet());
try
{
cmdlet.ExecuteCmdlet();
Assert.True(false, "Did not throw exception #2");
}
catch (PSInvalidOperationException ex)
{
string message = ex.ToString();
Assert.True(message.Contains("ArgumentException"), "Not the right type of exception #2");
}
}

[Fact]
Expand Down Expand Up @@ -299,7 +340,7 @@ private void VerifySettings(
}
else
{
Assert.Equal(expectedSettings.Logs.Count, actualSettings.Logs.Count);
Assert.True(expectedSettings.Logs.Count == actualSettings.Logs.Count, string.Format("Expected: {0}, Actual: {1}, no the same number of Log settings", expectedSettings.Logs.Count, actualSettings.Logs.Count));
for (int i = 0; i < expectedSettings.Logs.Count; i++)
{
var expected = expectedSettings.Logs[i];
Expand All @@ -316,7 +357,7 @@ private void VerifySettings(
}
else
{
Assert.Equal(expectedSettings.Metrics.Count, actualSettings.Metrics.Count);
Assert.True(expectedSettings.Metrics.Count == actualSettings.Metrics.Count, string.Format("Expected: {0}, Actual: {1}, no the same number of Metric settings", expectedSettings.Metrics.Count, actualSettings.Metrics.Count));
for (int i = 0; i < expectedSettings.Metrics.Count; i++)
{
var expected = expectedSettings.Metrics[i];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public ActionGroupsTests(Xunit.Abstractions.ITestOutputHelper output)
XunitTracingInterceptor.AddToContext(_logger);
}

[Fact]
[Fact(Skip = "Needs to be recorded again")]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestAddGetListSetRemoveActionGroup()
{
Expand Down
Loading