Skip to content

Commit 9d37bfa

Browse files
sunnynagavosmartysanthoshSebastienDegodez
authored
[fix] Enable easy DEBUG log level for Microcks containers (#196)
* Enable easy DEBUG log level for Microcks containers Add WithDebugLogLevel() methods to MicrocksBuilder, MicrocksAsyncMinionBuilder, and MicrocksContainerEnsemble to set appropriate environment variables for DEBUG logging. Update README with usage examples. This simplifies troubleshooting by allowing users to enable detailed logs in both main and async minion containers, following Microcks documentation. Fixes #191 Signed-off-by: Santhosh Reddy Vootukuri (SUNNY) <nagavo@microsoft.com> * Centralize logging env vars in ConfigurationConstants Move all logging-related environment variable constants to a new ConfigurationConstants class for better maintainability. Update all usages in builder and ensemble classes to reference the centralized constants. Ensure builder methods are chainable and update builder state as expected. Add copyright and license header to the new file. sign off by: nagavo Signed-off-by: Santhosh Reddy Vootukuri (SUNNY) <nagavo@microsoft.com> * Rename DebugLogLevel to DebugLogLevelEnvVar constant Clarifies that the "DEBUG" value is for environment variables by renaming DebugLogLevel to DebugLogLevelEnvVar in ConfigurationConstants. Updates all usages in MicrocksAsyncMinionBuilder and MicrocksBuilder for improved code clarity. Signed-off-by: Santhosh Reddy Vootukuri<vns_smart5@hotmail.com> Signed-off-by: Santhosh Reddy Vootukuri (SUNNY) <nagavo@microsoft.com> * Update README: streamline debug logging instructions Revised the README to remove the old, detailed section on enabling DEBUG logs in Microcks containers. Added a new, concise "Troubleshooting" section with updated code examples for enabling debug logs on both single containers and ensembles using .WithDebugLogLevel(). This improves clarity and makes troubleshooting steps easier to follow. Signed-off-by: Santhosh Reddy Vootukuri (SUNNY) <nagavo@microsoft.com> * Refactor ConfigurationConstants class structure and cleanup Flatten ConfigurationConstants by removing inner class structure and unnecessary using directives. Preserve and reformat XML documentation comments for clarity. Signed-off-by: Santhosh Reddy Vootukuri (SUNNY) <nagavo@microsoft.com> * test(DebugLogLevel): add tests for WithDebugLogLevel feature Signed-off-by: SebastienDegodez <sebastien.degodez@gmail.com> --------- Signed-off-by: Santhosh Reddy Vootukuri (SUNNY) <nagavo@microsoft.com> Signed-off-by: Santhosh Reddy Vootukuri<vns_smart5@hotmail.com> Signed-off-by: SebastienDegodez <sebastien.degodez@gmail.com> Co-authored-by: Santhosh Reddy Vootukuri (SUNNY) <nagavo@microsoft.com> Co-authored-by: SebastienDegodez <sebastien.degodez@gmail.com>
1 parent cce95c0 commit 9d37bfa

File tree

7 files changed

+243
-1
lines changed

7 files changed

+243
-1
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ await TestcontainersSettings.ExposeHostPortsAsync(ports)
221221
.ConfigureAwait(false);
222222
```
223223

224+
224225
### Advanced features with MicrocksContainersEnsemble
225226

226227
The `MicrocksContainer` referenced above supports essential features of Microcks provided by the main Microcks container.
@@ -353,3 +354,18 @@ testResult.IsSuccess.Should().BeTrue();
353354
```
354355

355356
In addition, you can use the `GetEventMessagesForTestCaseAsync()` method to retrieve the events received during the test.
357+
358+
#### Troubleshooting
359+
360+
You can enable debug logs on the Microcks container by setting the debug log level and then retrieving the logs:
361+
362+
```csharp
363+
MicrocksContainer container = new MicrocksBuilder()
364+
.WithImage("quay.io/microcks/microcks-uber:1.13.0")
365+
.WithDebugLogLevel()
366+
.Build();
367+
await container.StartAsync();
368+
```
369+
370+
The same .withDebugLogLevel() method is available on also MicrocksContainersEnsemble for enabling debug logs on all contained Microcks containers.
371+
```
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//
2+
// Copyright The Microcks Authors.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License")
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
//
17+
18+
namespace Microcks.Testcontainers.Helpers;
19+
20+
/// <summary>
21+
/// Configuration constants for Microcks Testcontainers.
22+
/// </summary>
23+
public class ConfigurationConstants
24+
{
25+
/// <summary>
26+
/// Environment variable name used to configure Microcks logging level for the Microcks package.
27+
/// </summary>
28+
public const string MicrocksLoggingLevelEnvVar = "LOGGING_LEVEL_IO_GITHUB_MICROCKS";
29+
30+
/// <summary>
31+
/// Environment variable value for enabling DEBUG logging.
32+
/// </summary>
33+
public const string DebugLogLevelEnvVar = "DEBUG";
34+
35+
/// <summary>
36+
/// Environment variable name used by Quarkus to configure console log level.
37+
/// </summary>
38+
public const string QuarkusConsoleLogLevelEnvVar = "QUARKUS_LOG_CONSOLE_LEVEL";
39+
40+
/// <summary>
41+
/// Environment variable name used by Quarkus to configure the Microcks category log level.
42+
/// </summary>
43+
public const string QuarkusMicrocksCategoryLogLevelEnvVar = "QUARKUS_LOG_CATEGORY__IO_GITHUB_MICROCKS__LEVEL";
44+
}

src/Microcks.Testcontainers/MicrocksAsyncMinionBuilder.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
using DotNet.Testcontainers.Networks;
2020
using Microcks.Testcontainers.Connection;
21+
using Microcks.Testcontainers.Helpers;
2122

2223
namespace Microcks.Testcontainers;
2324

@@ -133,4 +134,19 @@ public MicrocksAsyncMinionBuilder WithAmqpConnection(GenericConnection amqpConne
133134

134135
return Merge(DockerResourceConfiguration, new MicrocksAsyncMinionConfiguration(new ContainerConfiguration(environments: environments)));
135136
}
137+
138+
139+
/// <summary>
140+
/// Enables DEBUG log level for Microcks async minion components inside the container.
141+
/// </summary>
142+
/// <remarks>
143+
/// This follows Microcks documentation for the Quarkus-based async-minion image.
144+
/// It must be called before <see cref="Build"/> / container start.
145+
/// </remarks>
146+
public MicrocksAsyncMinionBuilder WithDebugLogLevel()
147+
{
148+
return this
149+
.WithEnvironment(ConfigurationConstants.QuarkusConsoleLogLevelEnvVar, ConfigurationConstants.DebugLogLevelEnvVar)
150+
.WithEnvironment(ConfigurationConstants.QuarkusMicrocksCategoryLogLevelEnvVar, ConfigurationConstants.DebugLogLevelEnvVar);
151+
}
136152
}

src/Microcks.Testcontainers/MicrocksBuilder.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,4 +268,12 @@ public MicrocksBuilder WithSecrets(params Model.Secret[] secrets)
268268

269269
return this;
270270
}
271+
272+
/// <summary>
273+
/// Enables DEBUG log level for Microcks components inside the container.
274+
/// </summary>
275+
public MicrocksBuilder WithDebugLogLevel()
276+
{
277+
return this.WithEnvironment(ConfigurationConstants.MicrocksLoggingLevelEnvVar, ConfigurationConstants.DebugLogLevelEnvVar);
278+
}
271279
}

src/Microcks.Testcontainers/MicrocksContainerEnsemble.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ namespace Microcks.Testcontainers;
2929
/// </summary>
3030
public class MicrocksContainerEnsemble : IAsyncDisposable, IArtifactAndSnapshotManager<MicrocksContainerEnsemble>
3131
{
32-
private readonly MicrocksBuilder _microcksBuilder;
32+
private MicrocksBuilder _microcksBuilder;
3333

3434
private ContainerBuilder _postmanBuilder;
3535
private MicrocksAsyncMinionBuilder _asyncMinionBuilder;
3636

37+
private bool _debugLogLevelEnabled;
38+
3739
/// <summary>
3840
/// Gets the Postman runtime container.
3941
/// </summary>
@@ -186,6 +188,11 @@ public MicrocksContainerEnsemble WithAsyncFeature()
186188
.WithEnvironment(MacOSHelper.GetJavaOptions())
187189
.WithImage(image);
188190

191+
if (this._debugLogLevelEnabled)
192+
{
193+
this._asyncMinionBuilder = this._asyncMinionBuilder.WithDebugLogLevel();
194+
}
195+
189196
return this;
190197
}
191198

@@ -221,6 +228,24 @@ public MicrocksContainerEnsemble WithAmqpConnection(GenericConnection amqpConnec
221228
return this;
222229
}
223230

231+
/// <summary>
232+
/// Enables DEBUG log level for the containers of this ensemble.
233+
/// </summary>
234+
/// <remarks>
235+
/// Applies to the main Microcks container (Spring) and, when the async feature is enabled,
236+
/// to the async-minion container (Quarkus).
237+
/// </remarks>
238+
public MicrocksContainerEnsemble WithDebugLogLevel()
239+
{
240+
this._debugLogLevelEnabled = true;
241+
this._microcksBuilder = this._microcksBuilder.WithDebugLogLevel();
242+
243+
// If async feature has already been enabled, apply immediately.
244+
this._asyncMinionBuilder = this._asyncMinionBuilder?.WithDebugLogLevel();
245+
246+
return this;
247+
}
248+
224249
/// <summary>
225250
/// Starts the Microcks container ensemble asynchronously.
226251
/// </summary>
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
//
2+
// Copyright The Microcks Authors.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License")
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
//
17+
18+
using Microcks.Testcontainers.Helpers;
19+
20+
namespace Microcks.Testcontainers.Tests.DebugLogLevel;
21+
22+
/// <summary>
23+
/// Tests for the WithDebugLogLevel feature on MicrocksContainerEnsemble.
24+
/// Verifies that debug logging environment variables are correctly set or not set
25+
/// on both Microcks (Spring) and Async Minion (Quarkus) containers.
26+
/// </summary>
27+
[Collection("DisableParallelization")]
28+
public sealed class MicrocksDebugLogLevelTests
29+
{
30+
/// <summary>
31+
/// Verifies that when WithDebugLogLevel() is called, the appropriate debug
32+
/// environment variables are set on both Microcks and Async Minion containers.
33+
/// - Microcks (Spring): LOGGING_LEVEL_IO_GITHUB_MICROCKS=DEBUG
34+
/// - Async Minion (Quarkus): QUARKUS_LOG_CONSOLE_LEVEL=DEBUG and
35+
/// QUARKUS_LOG_CATEGORY__IO_GITHUB_MICROCKS__LEVEL=DEBUG
36+
/// </summary>
37+
[Fact]
38+
public async Task WithDebugLogLevel_ShouldSetEnvironmentVariables()
39+
{
40+
// Arrange - Create ensemble WITH WithDebugLogLevel()
41+
await using var ensemble = new MicrocksContainerEnsemble(MicrocksBuilder.MicrocksImage)
42+
.WithDebugLogLevel()
43+
.WithAsyncFeature();
44+
45+
await ensemble.StartAsync(TestContext.Current.CancellationToken);
46+
47+
// Assert - Use Docker Inspect API to verify environment variables
48+
using var dockerClient = new Docker.DotNet.DockerClientConfiguration().CreateClient();
49+
50+
// Verify Microcks container has the debug environment variable
51+
var microcksInspect = await dockerClient.Containers.InspectContainerAsync(
52+
ensemble.MicrocksContainer.Id,
53+
TestContext.Current.CancellationToken);
54+
55+
var expectedMicrocksEnvVar = $"{ConfigurationConstants.MicrocksLoggingLevelEnvVar}={ConfigurationConstants.DebugLogLevelEnvVar}";
56+
Assert.Contains(expectedMicrocksEnvVar, microcksInspect.Config.Env);
57+
58+
// Verify Async Minion container has the Quarkus debug environment variables
59+
var minionInspect = await dockerClient.Containers.InspectContainerAsync(
60+
ensemble.AsyncMinionContainer.Id,
61+
TestContext.Current.CancellationToken);
62+
63+
var expectedConsoleLogLevel = $"{ConfigurationConstants.QuarkusConsoleLogLevelEnvVar}={ConfigurationConstants.DebugLogLevelEnvVar}";
64+
var expectedMicrocksLogLevel = $"{ConfigurationConstants.QuarkusMicrocksCategoryLogLevelEnvVar}={ConfigurationConstants.DebugLogLevelEnvVar}";
65+
66+
Assert.Contains(expectedConsoleLogLevel, minionInspect.Config.Env);
67+
Assert.Contains(expectedMicrocksLogLevel, minionInspect.Config.Env);
68+
}
69+
70+
/// <summary>
71+
/// Verifies that when WithDebugLogLevel() is NOT called, the debug environment
72+
/// variables are not present on the containers. This ensures the default behavior
73+
/// does not enable debug logging.
74+
/// </summary>
75+
[Fact]
76+
public async Task WithoutDebugLogLevel_ShouldNotSetEnvironmentVariables()
77+
{
78+
// Arrange - Create ensemble WITHOUT WithDebugLogLevel()
79+
await using var ensemble = new MicrocksContainerEnsemble(MicrocksBuilder.MicrocksImage)
80+
.WithAsyncFeature();
81+
82+
await ensemble.StartAsync(TestContext.Current.CancellationToken);
83+
84+
// Assert - Use Docker Inspect API to verify environment variables are NOT set
85+
using var dockerClient = new Docker.DotNet.DockerClientConfiguration().CreateClient();
86+
87+
// Verify Microcks container does NOT have the debug environment variable
88+
var microcksInspect = await dockerClient.Containers.InspectContainerAsync(
89+
ensemble.MicrocksContainer.Id,
90+
TestContext.Current.CancellationToken);
91+
92+
Assert.DoesNotContain(microcksInspect.Config.Env,
93+
env => env.StartsWith($"{ConfigurationConstants.MicrocksLoggingLevelEnvVar}="));
94+
95+
// Verify Async Minion container does NOT have the Quarkus debug environment variables
96+
var minionInspect = await dockerClient.Containers.InspectContainerAsync(
97+
ensemble.AsyncMinionContainer.Id,
98+
TestContext.Current.CancellationToken);
99+
100+
Assert.DoesNotContain(minionInspect.Config.Env,
101+
env => env.StartsWith($"{ConfigurationConstants.QuarkusConsoleLogLevelEnvVar}="));
102+
Assert.DoesNotContain(minionInspect.Config.Env,
103+
env => env.StartsWith($"{ConfigurationConstants.QuarkusMicrocksCategoryLogLevelEnvVar}="));
104+
}
105+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// Copyright The Microcks Authors.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License")
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
//
17+
18+
namespace Microcks.Testcontainers.Tests;
19+
20+
/// <summary>
21+
/// Collection definition that disables parallel execution for tests.
22+
/// This prevents issues on GitHub Actions agents with limited resources.
23+
/// Use [Collection("DisableParallelization")] on test classes that should not run in parallel.
24+
/// </summary>
25+
[CollectionDefinition("DisableParallelization", DisableParallelization = true)]
26+
public class DisableParallelizationCollection
27+
{
28+
}

0 commit comments

Comments
 (0)