forked from mongodb/mongo-csharp-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandEventHelperTests.cs
More file actions
152 lines (135 loc) · 7.26 KB
/
CommandEventHelperTests.cs
File metadata and controls
152 lines (135 loc) · 7.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System.Diagnostics;
using FluentAssertions;
using Microsoft.Extensions.Logging;
using MongoDB.Bson;
using MongoDB.Bson.TestHelpers;
using MongoDB.Driver.Core.Configuration;
using MongoDB.TestHelpers.XunitExtensions;
using MongoDB.Driver.Core.Events;
using MongoDB.Driver.Core.Logging;
using MongoDB.Driver.Core.Misc;
using Moq;
using Xunit;
namespace MongoDB.Driver.Core.Connections
{
public class CommandEventHelperTests
{
[Theory]
[InlineData("{ xyz : 1 }", false)]
[InlineData("{ aUTHENTICATE: 1 }", true)]
[InlineData("{ sASLSTART : 1 }", true)]
[InlineData("{ sASLCONTINUE : 1 }", true)]
[InlineData("{ gETNONCE : 1 }", true)]
[InlineData("{ cREATEUSER : 1 }", true)]
[InlineData("{ uPDATEUSER : 1, }", true)]
[InlineData("{ cOPYDBSASLSTART : 1 }", true)]
[InlineData("{ cOPYDB : 1 }", true)]
[InlineData("{ " + OppressiveLanguageConstants.LegacyHelloCommandName + " : 1 }", false)]
[InlineData("{ " + OppressiveLanguageConstants.LegacyHelloCommandName + " : 1, sPECULATIVEAUTHENTICATE : null }", true)]
[InlineData("{ hello : 1, helloOk : true }", false)]
[InlineData("{ hello : 1, helloOk : true, sPECULATIVEAUTHENTICATE : null }", true)]
public void ShouldRedactCommand_should_return_expected_result(string commandJson, bool expectedResult)
{
var command = BsonDocument.Parse(commandJson);
var result = CommandEventHelperReflector.ShouldRedactCommand(command);
result.Should().Be(expectedResult);
}
[Theory]
[ParameterAttributeData]
public void ShouldTrackState_should_be_correct(
[Values(false, true)] bool logCommands,
[Values(false, true)] bool captureCommandSucceeded,
[Values(false, true)] bool captureCommandFailed)
{
var mockLogger = new Mock<ILogger<LogCategories.Command>>();
mockLogger.Setup(m => m.IsEnabled(LogLevel.Debug)).Returns(logCommands);
var eventCapturer = new EventCapturer();
eventCapturer.Capture<SdamInformationEvent>();
if (captureCommandSucceeded)
{
eventCapturer.Capture<CommandSucceededEvent>(_ => true);
}
if (captureCommandFailed)
{
eventCapturer.Capture<CommandFailedEvent>(_ => true);
}
var eventLogger = new EventLogger<LogCategories.Command>(eventCapturer, mockLogger.Object);
var tracingOptions = new TracingOptions { Disabled = true };
var commandHelper = new CommandEventHelper(eventLogger, tracingOptions);
// No ActivityListener, so tracing doesn't contribute to _eventsNeedState
commandHelper._eventsNeedState().Should().Be(logCommands || captureCommandSucceeded || captureCommandFailed);
}
[Theory]
[ParameterAttributeData]
public void Callbacks_turn_on_when_listener_is_added_even_if_no_events(
[Values(false, true)] bool logCommands,
[Values(false, true)] bool captureCommandSucceeded,
[Values(false, true)] bool captureCommandFailed,
[Values(false, true)] bool traceCommands)
{
ActivityListener listener = null;
try
{
var mockLogger = new Mock<ILogger<LogCategories.Command>>();
mockLogger.Setup(m => m.IsEnabled(LogLevel.Debug)).Returns(logCommands);
var eventCapturer = new EventCapturer();
eventCapturer.Capture<SdamInformationEvent>();
if (captureCommandSucceeded)
{
eventCapturer.Capture<CommandSucceededEvent>(_ => true);
}
if (captureCommandFailed)
{
eventCapturer.Capture<CommandFailedEvent>(_ => true);
}
var eventLogger = new EventLogger<LogCategories.Command>(eventCapturer, mockLogger.Object);
var tracingOptions = traceCommands ? new TracingOptions() : new TracingOptions { Disabled = true };
var commandHelper = new CommandEventHelper(eventLogger, tracingOptions);
// When there are no listeners, these only return true if logging is enabled or an event is registered,
// regardless of whether tracing is enabled.
commandHelper.ShouldCallBeforeSending.Should().Be(captureCommandSucceeded || captureCommandFailed || logCommands);
commandHelper.ShouldCallAfterSending.Should().Be(captureCommandSucceeded || captureCommandFailed || logCommands);
commandHelper.ShouldCallErrorSending.Should().Be(captureCommandSucceeded || captureCommandFailed || logCommands);
commandHelper.ShouldCallAfterReceiving.Should().Be(captureCommandSucceeded || captureCommandFailed || logCommands);
commandHelper.ShouldCallErrorReceiving.Should().Be(captureCommandSucceeded || captureCommandFailed || logCommands);
listener = new ActivityListener
{
ShouldListenTo = source => source.Name == "MongoDB.Driver",
Sample = (ref ActivityCreationOptions<ActivityContext> _) => ActivitySamplingResult.AllData
};
ActivitySource.AddActivityListener(listener);
// With listeners registered, these always return true unless everything is disabled.
commandHelper.ShouldCallBeforeSending.Should().Be(captureCommandSucceeded || captureCommandFailed || logCommands || traceCommands);
commandHelper.ShouldCallAfterSending.Should().Be(captureCommandSucceeded || captureCommandFailed || logCommands || traceCommands);
commandHelper.ShouldCallErrorSending.Should().Be(captureCommandSucceeded || captureCommandFailed || logCommands || traceCommands);
commandHelper.ShouldCallAfterReceiving.Should().Be(captureCommandSucceeded || captureCommandFailed || logCommands || traceCommands);
commandHelper.ShouldCallErrorReceiving.Should().Be(captureCommandSucceeded || captureCommandFailed || logCommands || traceCommands);
}
finally
{
listener?.Dispose();
}
}
}
internal static class CommandEventHelperReflector
{
public static bool _eventsNeedState(this CommandEventHelper commandEventHelper) =>
(bool)Reflector.GetFieldValue(commandEventHelper, nameof(_eventsNeedState));
public static bool ShouldRedactCommand(BsonDocument command) =>
(bool)Reflector.InvokeStatic(typeof(CommandEventHelper), nameof(ShouldRedactCommand), command);
}
}