-
Notifications
You must be signed in to change notification settings - Fork 490
Update default log level #1970
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
Update default log level #1970
Changes from 5 commits
a9dfe28
02073a3
da44635
870cc4f
0231ef1
81bc0f6
48f6591
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"Projects": [ | ||
{ | ||
"Name": "Amazon.Lambda.TestTool", | ||
"Type": "Patch", | ||
"ChangelogMessages": [ | ||
"Update default log level" | ||
] | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using System.Reflection; | ||
|
||
namespace Amazon.Lambda.TestTool.Configuration; | ||
|
||
/// <summary> | ||
/// Handles the configuration setup for the Lambda Test Tool by loading settings from JSON files. | ||
/// </summary> | ||
/// <remarks> | ||
/// This class uses dependency injection to receive an Assembly instance, allowing for better testability | ||
/// and flexibility in determining the configuration file locations. | ||
/// </remarks> | ||
public class ConfigurationSetup(Assembly assembly) | ||
{ | ||
/// <summary> | ||
/// Retrieves the application configuration by loading settings from JSON configuration files. | ||
/// </summary> | ||
/// <returns>An IConfiguration instance containing the application settings.</returns> | ||
/// <exception cref="InvalidOperationException">Thrown when unable to determine the assembly location.</exception> | ||
/// <remarks> | ||
/// The method performs the following steps: | ||
/// 1. Locates the directory containing the assembly | ||
/// 2. Loads the base configuration from appsettings.json | ||
/// 3. Loads environment-specific configuration from appsettings.{environment}.json if available | ||
/// </remarks> | ||
public IConfiguration GetConfiguration() | ||
{ | ||
// Get the directory where the assembly is located | ||
var assemblyLocation = assembly.Location; | ||
var packageDirectory = Path.GetDirectoryName(assemblyLocation) | ||
?? throw new InvalidOperationException("Unable to determine assembly location"); | ||
|
||
// Construct path to configuration file | ||
var appsettingsPath = Path.Combine(packageDirectory, "appsettings.json"); | ||
if (!File.Exists(appsettingsPath)) | ||
{ | ||
Console.WriteLine($"Warning: appsettings.json not found at {appsettingsPath}"); | ||
} | ||
|
||
// Determine the current environment | ||
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"; | ||
|
||
// Build and return the configuration | ||
var builder = new ConfigurationBuilder() | ||
.SetBasePath(packageDirectory) | ||
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) | ||
.AddJsonFile($"appsettings.{environment}.json", optional: true, reloadOnChange: true); | ||
|
||
return builder.Build(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using System.Reflection; | ||
using Amazon.Lambda.TestTool.Commands.Settings; | ||
using Amazon.Lambda.TestTool.Components; | ||
using Amazon.Lambda.TestTool.Configuration; | ||
using Amazon.Lambda.TestTool.Services; | ||
using Amazon.Lambda.TestTool.Services.IO; | ||
using Microsoft.Extensions.FileProviders; | ||
|
@@ -37,6 +39,17 @@ public static TestToolProcess Startup(RunCommandSettings settings, CancellationT | |
{ | ||
var builder = WebApplication.CreateBuilder(); | ||
|
||
builder.Services.AddSingleton(typeof(Assembly), typeof(ConfigurationSetup).Assembly); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Since this is a copy of the same code in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i made a |
||
builder.Services.AddSingleton<ConfigurationSetup>(); | ||
|
||
var configSetup = builder.Services.BuildServiceProvider().GetRequiredService<ConfigurationSetup>(); | ||
var configuration = configSetup.GetConfiguration(); | ||
builder.Configuration.AddConfiguration(configuration); | ||
|
||
builder.Logging.ClearProviders(); | ||
builder.Logging.AddConfiguration(configuration.GetSection("Logging")); | ||
builder.Logging.AddConsole(); | ||
|
||
builder.Services.AddSingleton<IRuntimeApiDataStoreManager, RuntimeApiDataStoreManager>(); | ||
builder.Services.AddSingleton<IThemeService, ThemeService>(); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using System.Reflection; | ||
using Amazon.Lambda.TestTool.Configuration; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace Amazon.Lambda.TestTool.UnitTests.Configuration; | ||
|
||
public class ConfigurationSetupTests | ||
{ | ||
[Fact] | ||
public void GetConfiguration_WithValidAssemblyLocation_ReturnsConfiguration() | ||
{ | ||
// Arrange | ||
var mockAssembly = new Mock<Assembly>(); | ||
var testDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); | ||
Directory.CreateDirectory(testDirectory); | ||
|
||
try | ||
{ | ||
// Create test appsettings.json | ||
File.WriteAllText( | ||
Path.Combine(testDirectory, "appsettings.json"), | ||
@"{""TestSetting"": ""TestValue""}" | ||
); | ||
|
||
mockAssembly | ||
.Setup(x => x.Location) | ||
.Returns(Path.Combine(testDirectory, "dummy.dll")); | ||
|
||
var configSetup = new ConfigurationSetup(mockAssembly.Object); | ||
|
||
// Act | ||
var configuration = configSetup.GetConfiguration(); | ||
|
||
// Assert | ||
Assert.NotNull(configuration); | ||
Assert.Equal("TestValue", configuration["TestSetting"]); | ||
} | ||
finally | ||
{ | ||
// Cleanup | ||
Directory.Delete(testDirectory, true); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void GetConfiguration_WithEnvironmentSpecificConfig_LoadsBothConfigs() | ||
{ | ||
// Arrange | ||
var mockAssembly = new Mock<Assembly>(); | ||
var testDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); | ||
Directory.CreateDirectory(testDirectory); | ||
|
||
try | ||
{ | ||
// Create test appsettings.json | ||
File.WriteAllText( | ||
Path.Combine(testDirectory, "appsettings.json"), | ||
@"{""TestSetting"": ""BaseValue"", ""CommonSetting"": ""BaseValue""}" | ||
); | ||
|
||
// Create test appsettings.Development.json | ||
File.WriteAllText( | ||
Path.Combine(testDirectory, "appsettings.Development.json"), | ||
@"{""TestSetting"": ""DevValue""}" | ||
); | ||
|
||
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development"); | ||
|
||
mockAssembly | ||
.Setup(x => x.Location) | ||
.Returns(Path.Combine(testDirectory, "dummy.dll")); | ||
|
||
var configSetup = new ConfigurationSetup(mockAssembly.Object); | ||
|
||
// Act | ||
var configuration = configSetup.GetConfiguration(); | ||
|
||
// Assert | ||
Assert.NotNull(configuration); | ||
Assert.Equal("DevValue", configuration["TestSetting"]); // Overridden value | ||
Assert.Equal("BaseValue", configuration["CommonSetting"]); // Base value | ||
} | ||
finally | ||
{ | ||
// Cleanup | ||
Directory.Delete(testDirectory, true); | ||
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", null); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void GetConfiguration_WithInvalidAssemblyLocation_ThrowsInvalidOperationException() | ||
{ | ||
// Arrange | ||
var mockAssembly = new Mock<Assembly>(); | ||
mockAssembly | ||
.Setup(x => x.Location) | ||
.Returns(string.Empty); | ||
|
||
var configSetup = new ConfigurationSetup(mockAssembly.Object); | ||
|
||
// Act & Assert | ||
var exception = Assert.Throws<InvalidOperationException>( | ||
() => configSetup.GetConfiguration() | ||
); | ||
Assert.Equal("Unable to determine assembly location", exception.Message); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: could you be more descriptive here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated