-
Notifications
You must be signed in to change notification settings - Fork 259
Adding 'add-telemetry' Options to CLI for Azure Log Analytics #2772
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
Closed
Changes from all commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
ec54f52
Initial plan for issue
Copilot 3e5a6ae
Complete: Add Azure Log Analytics properties support to DAB
Copilot 38c272c
Fix Unit Tests
RubenCerna2079 967431d
Fix Unit Tests
RubenCerna2079 e063ab3
Changes to properties and tests
RubenCerna2079 982a37a
Change tests
RubenCerna2079 b3fb72e
Fix Unit Tests
RubenCerna2079 b446e75
Update AzureLogAnalyticsOptions to follow established pattern with Us…
Copilot bf47476
Add JsonConverterFactory for AzureLogAnalyticsOptions to use UserProv…
Copilot 31a5d1d
Fix unit tests
RubenCerna2079 9f8d9b4
Changes based on comments
RubenCerna2079 dabfaed
Changes based on comments
RubenCerna2079 9fd5d75
Changes based on comments
RubenCerna2079 adf616a
Remove unecessary tests
RubenCerna2079 a2842d9
Fix Unit Tests
RubenCerna2079 eee31c2
Changes based on comments
RubenCerna2079 43cf250
Fix launch settings file
RubenCerna2079 5d3f0a9
Fix errors
RubenCerna2079 34560a3
Fix test failure
RubenCerna2079 f4dea9e
Changes based on comments
RubenCerna2079 2c4dacc
Fix Unit Tests
RubenCerna2079 1283d20
Initial plan for issue
Copilot d2baa81
Changes based on comments
RubenCerna2079 b7a2d85
Add CLI
RubenCerna2079 7faa652
Add CLI
RubenCerna2079 6ee43c3
Finish CLI functionality
RubenCerna2079 a9cad34
Add log error to CLI
RubenCerna2079 c3153c4
Test Refactoring
RubenCerna2079 b277ec3
Test Refactoring 2
RubenCerna2079 a93d302
Validation Changes
RubenCerna2079 a7147ba
Fix tests
RubenCerna2079 e4f506a
Fix tests
RubenCerna2079 792ab77
Fix changes
RubenCerna2079 a61be00
Add tests
RubenCerna2079 7ee41ed
Fix Unit Tests
RubenCerna2079 e24dd76
Add File with Tests
RubenCerna2079 aa349c8
Changes based on comments
RubenCerna2079 25a0deb
Fix launch settings
RubenCerna2079 b50261f
Comments based on changes
RubenCerna2079 c650698
Merge branch 'main' into dev/rubencerna/add-cli-log-analytics
RubenCerna2079 5c1d0dd
Merge branch 'main' into dev/rubencerna/add-cli-log-analytics
RubenCerna2079 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
namespace Cli.Tests | ||
{ | ||
/// <summary> | ||
/// Tests for verifying the functionality of adding AzureLogAnalytics to the config file. | ||
/// </summary> | ||
[TestClass] | ||
public class AddAzureLogAnalyticsTests | ||
{ | ||
public static string RUNTIME_SECTION_WITH_AZURE_LOG_ANALYTICS_SECTION = GenerateRuntimeSection(TELEMETRY_SECTION_WITH_AZURE_LOG_ANALYTICS); | ||
public static string RUNTIME_SECTION_WITH_EMPTY_TELEMETRY_SECTION = GenerateRuntimeSection(EMPTY_TELEMETRY_SECTION); | ||
public static string RUNTIME_SECTION_WITH_EMPTY_AUTH_SECTION = GenerateRuntimeSection(EMPTY_AUTH_TELEMETRY_SECTION); | ||
[TestInitialize] | ||
public void TestInitialize() | ||
{ | ||
ILoggerFactory loggerFactory = TestLoggerSupport.ProvisionLoggerFactory(); | ||
|
||
ConfigGenerator.SetLoggerForCliConfigGenerator(loggerFactory.CreateLogger<ConfigGenerator>()); | ||
Utils.SetCliUtilsLogger(loggerFactory.CreateLogger<Utils>()); | ||
} | ||
|
||
/// <summary> | ||
/// Testing to check AzureLogAnalytics options are correctly added to the config. | ||
/// Verifying scenarios such as enabling/disabling AzureLogAnalytics and providing a valid/empty endpoint. | ||
/// </summary> | ||
[DataTestMethod] | ||
[DataRow(CliBool.True, "", "", "", false, DisplayName = "Fail to add AzureLogAnalytics with empty auth properties.")] | ||
[DataRow(CliBool.True, "workspaceId", "dcrImmutableId", "dceEndpoint", true, DisplayName = "Successfully adds AzureLogAnalytics with valid endpoint")] | ||
[DataRow(CliBool.False, "workspaceId", "dcrImmutableId", "dceEndpoint", true, DisplayName = "Successfully adds AzureLogAnalytics but disabled")] | ||
public void TestAddAzureLogAnalytics(CliBool isTelemetryEnabled, string workspaceId, string dcrImmutableId, string dceEndpoint, bool expectSuccess) | ||
{ | ||
MockFileSystem fileSystem = FileSystemUtils.ProvisionMockFileSystem(); | ||
string configPath = "test-azureloganalytics-config.json"; | ||
fileSystem.AddFile(configPath, new MockFileData(INITIAL_CONFIG)); | ||
|
||
// Initial State | ||
Assert.IsTrue(fileSystem.FileExists(configPath)); | ||
Assert.IsTrue(RuntimeConfigLoader.TryParseConfig(fileSystem.File.ReadAllText(configPath), out RuntimeConfig? config)); | ||
Assert.IsNotNull(config); | ||
Assert.IsNotNull(config.Runtime); | ||
Assert.IsNull(config.Runtime.Telemetry); | ||
|
||
// Add AzureLogAnalytics | ||
bool isSuccess = ConfigGenerator.TryAddTelemetry( | ||
new AddTelemetryOptions( | ||
azureLogAnalyticsEnabled: isTelemetryEnabled, | ||
azureLogAnalyticsWorkspaceId: workspaceId, | ||
azureLogAnalyticsDcrImmutableId: dcrImmutableId, | ||
azureLogAnalyticsDceEndpoint: dceEndpoint, | ||
config: configPath), | ||
new FileSystemRuntimeConfigLoader(fileSystem), | ||
fileSystem); | ||
|
||
// Assert after adding AzureLogAnalytics | ||
Assert.AreEqual(expectSuccess, isSuccess); | ||
if (expectSuccess) | ||
{ | ||
Assert.IsTrue(fileSystem.FileExists(configPath)); | ||
Assert.IsTrue(RuntimeConfigLoader.TryParseConfig(fileSystem.File.ReadAllText(configPath), out config)); | ||
Assert.IsNotNull(config); | ||
Assert.IsNotNull(config.Runtime); | ||
Assert.IsNotNull(config.Runtime.Telemetry); | ||
TelemetryOptions telemetryOptions = config.Runtime.Telemetry; | ||
Assert.IsNotNull(telemetryOptions.AzureLogAnalytics); | ||
Assert.AreEqual(isTelemetryEnabled is CliBool.True ? true : false, telemetryOptions.AzureLogAnalytics.Enabled); | ||
Assert.IsNotNull(telemetryOptions.AzureLogAnalytics.Auth); | ||
Assert.AreEqual(workspaceId, telemetryOptions.AzureLogAnalytics.Auth.WorkspaceId); | ||
Assert.AreEqual(dcrImmutableId, telemetryOptions.AzureLogAnalytics.Auth.DcrImmutableId); | ||
Assert.AreEqual(dceEndpoint, telemetryOptions.AzureLogAnalytics.Auth.DceEndpoint); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Test to verify when Telemetry section is present in the config | ||
/// It should add AzureLogAnalytics if telemetry section is empty | ||
/// or overwrite the existing AzureLogAnalytics with the given AzureLogAnalytics options. | ||
/// </summary> | ||
[DataTestMethod] | ||
[DataRow(true, DisplayName = "Add AzureLogAnalytics when telemetry section is empty.")] | ||
[DataRow(false, DisplayName = "Overwrite AzureLogAnalytics when telemetry section already exists.")] | ||
public void TestAddAzureLogAnalyticsWhenTelemetryAlreadyExists(bool isEmptyTelemetry) | ||
{ | ||
MockFileSystem fileSystem = FileSystemUtils.ProvisionMockFileSystem(); | ||
string configPath = "test-azureloganalytics-config.json"; | ||
string runtimeSection = isEmptyTelemetry ? RUNTIME_SECTION_WITH_EMPTY_TELEMETRY_SECTION : RUNTIME_SECTION_WITH_AZURE_LOG_ANALYTICS_SECTION; | ||
string configData = $"{{{SAMPLE_SCHEMA_DATA_SOURCE},{runtimeSection}}}"; | ||
fileSystem.AddFile(configPath, new MockFileData(configData)); | ||
|
||
// Initial State | ||
Assert.IsTrue(fileSystem.FileExists(configPath)); | ||
Assert.IsTrue(RuntimeConfigLoader.TryParseConfig(fileSystem.File.ReadAllText(configPath), out RuntimeConfig? config)); | ||
Assert.IsNotNull(config); | ||
Assert.IsNotNull(config.Runtime); | ||
Assert.IsNotNull(config.Runtime.Telemetry); | ||
|
||
if (isEmptyTelemetry) | ||
{ | ||
Assert.IsNull(config.Runtime.Telemetry.AzureLogAnalytics); | ||
} | ||
else | ||
{ | ||
Assert.IsNotNull(config.Runtime.Telemetry.AzureLogAnalytics); | ||
Assert.AreEqual(true, config.Runtime.Telemetry.AzureLogAnalytics.Enabled); | ||
Assert.IsNotNull(config.Runtime.Telemetry.AzureLogAnalytics.Auth); | ||
Assert.AreEqual("workspaceId", config.Runtime.Telemetry.AzureLogAnalytics.Auth.WorkspaceId); | ||
Assert.AreEqual("dcrImmutableId", config.Runtime.Telemetry.AzureLogAnalytics.Auth.DcrImmutableId); | ||
Assert.AreEqual("dceEndpoint", config.Runtime.Telemetry.AzureLogAnalytics.Auth.DceEndpoint); | ||
} | ||
|
||
// Add AzureLogAnalytics | ||
bool isSuccess = ConfigGenerator.TryAddTelemetry( | ||
new AddTelemetryOptions( | ||
azureLogAnalyticsEnabled: CliBool.False, | ||
azureLogAnalyticsWorkspaceId: "newWorkspaceId", | ||
azureLogAnalyticsDcrImmutableId: "newDcrImmutableId", | ||
azureLogAnalyticsDceEndpoint: "newDceEndpoint", | ||
config: configPath), | ||
new FileSystemRuntimeConfigLoader(fileSystem), | ||
fileSystem); | ||
|
||
// Assert after adding AzureLogAnalytics | ||
Assert.IsTrue(isSuccess); | ||
Assert.IsTrue(fileSystem.FileExists(configPath)); | ||
Assert.IsTrue(RuntimeConfigLoader.TryParseConfig(fileSystem.File.ReadAllText(configPath), out config)); | ||
Assert.IsNotNull(config); | ||
Assert.IsNotNull(config.Runtime); | ||
Assert.IsNotNull(config.Runtime.Telemetry); | ||
Assert.IsNotNull(config.Runtime.Telemetry.AzureLogAnalytics); | ||
Assert.IsFalse(config.Runtime.Telemetry.AzureLogAnalytics.Enabled); | ||
Assert.IsNotNull(config.Runtime.Telemetry.AzureLogAnalytics.Auth); | ||
Assert.AreEqual("newWorkspaceId", config.Runtime.Telemetry.AzureLogAnalytics.Auth.WorkspaceId); | ||
Assert.AreEqual("newDcrImmutableId", config.Runtime.Telemetry.AzureLogAnalytics.Auth.DcrImmutableId); | ||
Assert.AreEqual("newDceEndpoint", config.Runtime.Telemetry.AzureLogAnalytics.Auth.DceEndpoint); | ||
} | ||
|
||
/// <summary> | ||
/// Generates a JSON string representing a runtime section of the config, with a customizable telemetry section. | ||
/// </summary> | ||
private static string GenerateRuntimeSection(string telemetrySection) | ||
{ | ||
return $@" | ||
""runtime"": {{ | ||
""rest"": {{ | ||
""path"": ""/api"", | ||
""enabled"": false | ||
}}, | ||
""graphql"": {{ | ||
""path"": ""/graphql"", | ||
""enabled"": false, | ||
""allow-introspection"": true | ||
}}, | ||
""host"": {{ | ||
""mode"": ""development"", | ||
""cors"": {{ | ||
""origins"": [], | ||
""allow-credentials"": false | ||
}}, | ||
""authentication"": {{ | ||
""provider"": ""StaticWebApps"" | ||
}} | ||
}}, | ||
{telemetrySection} | ||
}}, | ||
""entities"": {{}}"; | ||
} | ||
|
||
/// <summary> | ||
/// Represents a JSON string for the telemetry section of the config, with Azure Log Analytics enabled and specified auth properties. | ||
/// </summary> | ||
private const string TELEMETRY_SECTION_WITH_AZURE_LOG_ANALYTICS = @" | ||
""telemetry"": { | ||
""azure-log-analytics"": { | ||
""enabled"": true, | ||
""auth"": { | ||
""workspace-id"": ""workspaceId"", | ||
""dcr-immutable-id"": ""dcrImmutableId"", | ||
""dce-endpoint"": ""dceEndpoint"" | ||
} | ||
} | ||
}"; | ||
|
||
/// <summary> | ||
/// Represents a JSON string for the empty telemetry section of the config. | ||
/// </summary> | ||
private const string EMPTY_TELEMETRY_SECTION = @" | ||
""telemetry"": {}"; | ||
|
||
/// <summary> | ||
/// Represents a JSON string for the telemetry section of the config, with Azure Log Analytics enabled and an empty auth section. | ||
/// </summary> | ||
private const string EMPTY_AUTH_TELEMETRY_SECTION = @" | ||
""telemetry"": { | ||
""azure-log-analytics"": { | ||
""enabled"": true, | ||
""auth"": {} | ||
} | ||
}"; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.