@@ -5,13 +5,16 @@ namespace VirtualClient.Actions
5
5
{
6
6
using System ;
7
7
using System . Collections . Generic ;
8
+ using System . Linq ;
8
9
using System . Net ;
10
+ using System . Text ;
9
11
using System . Threading ;
10
12
using System . Threading . Tasks ;
11
13
using Microsoft . Extensions . DependencyInjection ;
12
14
using Moq ;
13
15
using NUnit . Framework ;
14
16
using VirtualClient . Actions . Memtier ;
17
+ using VirtualClient . Common ;
15
18
using VirtualClient . Common . Telemetry ;
16
19
using VirtualClient . Contracts ;
17
20
@@ -21,13 +24,19 @@ public class RedisServerExecutorTests
21
24
{
22
25
private MockFixture fixture ;
23
26
private DependencyPath mockRedisPackage ;
27
+ private InMemoryProcess memoryProcess ;
24
28
25
29
[ SetUp ]
26
30
public void SetupTests ( )
27
31
{
28
32
this . fixture = new MockFixture ( ) ;
29
33
this . fixture . Setup ( PlatformID . Unix ) ;
30
-
34
+ this . memoryProcess = new InMemoryProcess
35
+ {
36
+ ExitCode = 0 ,
37
+ OnStart = ( ) => true ,
38
+ OnHasExited = ( ) => true
39
+ } ;
31
40
this . mockRedisPackage = new DependencyPath ( "redis" , this . fixture . GetPackagePath ( "redis" ) ) ;
32
41
33
42
this . fixture . Parameters = new Dictionary < string , IConvertible > ( )
@@ -62,6 +71,17 @@ public async Task RedisServerExecutorConfirmsTheExpectedPackagesOnInitialization
62
71
{
63
72
using ( var component = new TestRedisServerExecutor ( this . fixture . Dependencies , this . fixture . Parameters ) )
64
73
{
74
+ this . fixture . ProcessManager . OnCreateProcess = ( command , arguments , workingDirectory ) =>
75
+ {
76
+ if ( arguments ? . Contains ( "redis-server" ) == true && arguments ? . Contains ( "--version" ) == true )
77
+ {
78
+ this . memoryProcess . StandardOutput = new ConcurrentBuffer (
79
+ new StringBuilder ( "Redis server v=7.0.15 sha=00000000 malloc=jemalloc-5.1.0 bits=64 build=abc123" )
80
+ ) ;
81
+ return this . memoryProcess ;
82
+ }
83
+ return this . memoryProcess ;
84
+ } ;
65
85
await component . InitializeAsync ( EventContext . None , CancellationToken . None ) ;
66
86
this . fixture . PackageManager . Verify ( mgr => mgr . GetPackageAsync ( this . mockRedisPackage . Name , It . IsAny < CancellationToken > ( ) ) ) ;
67
87
}
@@ -83,6 +103,13 @@ public async Task RedisMemtierServerExecutorExecutesExpectedProcessWhenBindingTo
83
103
84
104
this . fixture . ProcessManager . OnCreateProcess = ( exe , arguments , workingDirectory ) =>
85
105
{
106
+ if ( arguments ? . Contains ( "redis-server" ) == true && arguments ? . Contains ( "--version" ) == true )
107
+ {
108
+ this . memoryProcess . StandardOutput = new ConcurrentBuffer (
109
+ new StringBuilder ( "Redis server v=7.0.15 sha=00000000 malloc=jemalloc-5.1.0 bits=64 build=abc123" )
110
+ ) ;
111
+ return this . memoryProcess ;
112
+ }
86
113
expectedCommands . Remove ( $ "{ exe } { arguments } ") ;
87
114
return this . fixture . Process ;
88
115
} ;
@@ -113,6 +140,13 @@ public async Task RedisMemtierServerExecutorExecutesExpectedProcessWhenBindingTo
113
140
114
141
this . fixture . ProcessManager . OnCreateProcess = ( exe , arguments , workingDirectory ) =>
115
142
{
143
+ if ( arguments ? . Contains ( "redis-server" ) == true && arguments ? . Contains ( "--version" ) == true )
144
+ {
145
+ this . memoryProcess . StandardOutput = new ConcurrentBuffer (
146
+ new StringBuilder ( "Redis server v=7.0.15 sha=00000000 malloc=jemalloc-5.1.0 bits=64 build=abc123" )
147
+ ) ;
148
+ return this . memoryProcess ;
149
+ }
116
150
expectedCommands . Remove ( $ "{ exe } { arguments } ") ;
117
151
return this . fixture . Process ;
118
152
} ;
@@ -140,6 +174,13 @@ public async Task RedisMemtierServerExecutorExecutesExpectedProcessWhenNotBindin
140
174
141
175
this . fixture . ProcessManager . OnCreateProcess = ( exe , arguments , workingDirectory ) =>
142
176
{
177
+ if ( arguments ? . Contains ( "redis-server" ) == true && arguments ? . Contains ( "--version" ) == true )
178
+ {
179
+ this . memoryProcess . StandardOutput = new ConcurrentBuffer (
180
+ new StringBuilder ( "Redis server v=7.0.15 sha=00000000 malloc=jemalloc-5.1.0 bits=64 build=abc123" )
181
+ ) ;
182
+ return this . memoryProcess ;
183
+ }
143
184
expectedCommands . Remove ( $ "{ exe } { arguments } ") ;
144
185
return this . fixture . Process ;
145
186
} ;
@@ -149,6 +190,38 @@ public async Task RedisMemtierServerExecutorExecutesExpectedProcessWhenNotBindin
149
190
}
150
191
}
151
192
193
+ [ Test ]
194
+ public async Task RedisServerExecutorCapturesRedisVersionSuccessfully ( )
195
+ {
196
+ using ( var executor = new TestRedisServerExecutor ( this . fixture . Dependencies , this . fixture . Parameters ) )
197
+ {
198
+ this . fixture . ProcessManager . OnCreateProcess = ( command , arguments , workingDirectory ) =>
199
+ {
200
+ if ( arguments ? . Contains ( "redis-server" ) == true && arguments ? . Contains ( "--version" ) == true )
201
+ {
202
+ this . memoryProcess . StandardOutput = new ConcurrentBuffer (
203
+ new StringBuilder ( "Redis server v=7.0.15 sha=00000000 malloc=jemalloc-5.1.0 bits=64 build=abc123" )
204
+ ) ;
205
+ return this . memoryProcess ;
206
+ }
207
+ return this . memoryProcess ;
208
+ } ;
209
+ // Act
210
+ await executor . InitializeAsync ( EventContext . None , CancellationToken . None ) ;
211
+ // Assert
212
+ var messages = this . fixture . Logger . MessagesLogged ( $ "{ nameof ( TestRedisServerExecutor ) } .RedisVersionCaptured") ;
213
+ Assert . IsNotEmpty ( messages , "Expected at least one log message indicating the Redis version was captured." ) ;
214
+ bool versionCapturedCorrectly = messages . Any ( msg =>
215
+ {
216
+ var eventContext = msg . Item3 as EventContext ;
217
+ return eventContext != null &&
218
+ eventContext . Properties . ContainsKey ( "redisVersion" ) &&
219
+ eventContext . Properties [ "redisVersion" ] . ToString ( ) == "7.0.15" ;
220
+ } ) ;
221
+ Assert . IsTrue ( versionCapturedCorrectly , "The Redis version '7.0.15' was not captured correctly in the logs." ) ;
222
+ }
223
+ }
224
+
152
225
private class TestRedisServerExecutor : RedisServerExecutor
153
226
{
154
227
public TestRedisServerExecutor ( IServiceCollection services , IDictionary < string , IConvertible > parameters = null )
0 commit comments