-
Notifications
You must be signed in to change notification settings - Fork 53
added tests for helpers and init common lib #265
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5b982f3
added tests for helpers and init common lib
emmanuelrobles 80da004
added a comment
emmanuelrobles 6facb5c
coderabbit suggestion
emmanuelrobles 74064cf
removed comment
emmanuelrobles 06ae9d8
made some test the were using cache run in sequence so they dont step…
emmanuelrobles 0609651
made some test the were using cache run in sequence so they dont step…
emmanuelrobles ca20c6c
cleanup the UT a bit
emmanuelrobles 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
9 changes: 9 additions & 0 deletions
9
test/unit/CollectionDefinitions/CacheTestsCollectionDefinition.cs
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,9 @@ | ||
| using Xunit; | ||
|
|
||
| namespace CommonLibTest.CollectionDefinitions; | ||
|
|
||
| /// <summary> | ||
| /// Test that use cache cannot run in parallel, they can have flaky behavior | ||
| /// </summary> | ||
| [CollectionDefinition(nameof(CacheTestCollectionDefinition), DisableParallelization = true)] | ||
| public class CacheTestCollectionDefinition{} |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| using System; | ||
| using System.Reflection; | ||
| using CommonLibTest.CollectionDefinitions; | ||
| using Microsoft.Extensions.Logging; | ||
| using Moq; | ||
| using SharpHoundCommonLib; | ||
| using Xunit; | ||
|
|
||
| namespace CommonLibTest; | ||
|
|
||
| [Collection(nameof(CacheTestCollectionDefinition))] | ||
| public class CommonLibTests | ||
| { | ||
|
|
||
| public CommonLibTests() | ||
| { | ||
| ResetCommonLibState(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void InitializeCommonLib_FirstCallWithoutCache_CreatesAndSetsCacheInstance() | ||
| { | ||
| // Arrange & Act | ||
| CommonLib.InitializeCommonLib(); | ||
|
|
||
| // Assert | ||
| var cache = Cache.GetCacheInstance(); | ||
| Assert.NotNull(cache); | ||
| Assert.NotNull(cache.IdToTypeCache); | ||
| Assert.NotNull(cache.ValueToIdCache); | ||
| Assert.NotNull(cache.GlobalCatalogCache); | ||
| Assert.NotNull(cache.MachineSidCache); | ||
| Assert.NotNull(cache.SIDToDomainCache); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void InitializeCommonLib_UsesProvidedInstance() | ||
| { | ||
| // Arrange | ||
| var provided = Cache.CreateNewCache(); | ||
|
|
||
| // Act | ||
| CommonLib.InitializeCommonLib(cache: provided); | ||
|
|
||
| // Assert | ||
| Assert.Same(provided, Cache.GetCacheInstance()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void InitializeCommonLib_2Calls_LogsWarningAndDoesNotReplaceCache() | ||
| { | ||
| // Arrange | ||
| var cache1 = Cache.CreateNewCache(); | ||
| CommonLib.InitializeCommonLib(cache: cache1); | ||
|
|
||
| var cache2 = Cache.CreateNewCache(); | ||
| var logger = new Mock<ILogger>(); | ||
|
|
||
| // Act | ||
| CommonLib.InitializeCommonLib(logger.Object, cache2); | ||
|
|
||
| // Assert | ||
| Assert.Same(cache1, Cache.GetCacheInstance()); // cache1 should be then one used since lib was already initialized | ||
emmanuelrobles marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| logger.Verify(x => x.Log( | ||
| LogLevel.Warning, | ||
| It.IsAny<EventId>(), | ||
| It.Is<It.IsAnyType>((v, _) => | ||
| v.ToString() != null && | ||
| v.ToString().Contains("already initialized", StringComparison.InvariantCultureIgnoreCase)), | ||
| It.IsAny<Exception>(), | ||
| It.IsAny<Func<It.IsAnyType, Exception, string>>()), | ||
| Times.Once()); | ||
| } | ||
|
|
||
| private static void ResetCommonLibState() | ||
| { | ||
| // Reset CommonLib._initialized (private static) | ||
| var commonLibType = typeof(CommonLib); | ||
| var initializedField = commonLibType.GetField("_initialized", | ||
| BindingFlags.Static | BindingFlags.NonPublic); | ||
emmanuelrobles marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (initializedField == null) | ||
| throw new InvalidOperationException("CommonLib _initialized field not found"); | ||
|
|
||
| initializedField.SetValue(null, false); | ||
|
|
||
| // Reset cache singleton so tests don't leak state into each other | ||
| Cache.SetCacheInstance(null); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.