33
44namespace VirtualClient . Actions
55{
6+ using Microsoft . Extensions . DependencyInjection ;
7+ using Moq ;
8+ using NUnit . Framework ;
69 using System ;
710 using System . Collections . Generic ;
11+ using System . IO ;
12+ using System . Linq ;
813 using System . Net ;
14+ using System . Text ;
15+ using System . Text . RegularExpressions ;
916 using System . Threading ;
1017 using System . Threading . Tasks ;
11- using Microsoft . Extensions . DependencyInjection ;
12- using Moq ;
13- using NUnit . Framework ;
1418 using VirtualClient . Actions . Memtier ;
19+ using VirtualClient . Common ;
1520 using VirtualClient . Common . Telemetry ;
1621 using VirtualClient . Contracts ;
1722
@@ -21,13 +26,19 @@ public class RedisServerExecutorTests
2126 {
2227 private MockFixture fixture ;
2328 private DependencyPath mockRedisPackage ;
29+ private InMemoryProcess memoryProcess ;
2430
2531 [ SetUp ]
2632 public void SetupTests ( )
2733 {
2834 this . fixture = new MockFixture ( ) ;
2935 this . fixture . Setup ( PlatformID . Unix ) ;
30-
36+ this . memoryProcess = new InMemoryProcess
37+ {
38+ ExitCode = 0 ,
39+ OnStart = ( ) => true ,
40+ OnHasExited = ( ) => true
41+ } ;
3142 this . mockRedisPackage = new DependencyPath ( "redis" , this . fixture . GetPackagePath ( "redis" ) ) ;
3243
3344 this . fixture . Parameters = new Dictionary < string , IConvertible > ( )
@@ -83,6 +94,11 @@ public async Task RedisMemtierServerExecutorExecutesExpectedProcessWhenBindingTo
8394
8495 this . fixture . ProcessManager . OnCreateProcess = ( exe , arguments , workingDirectory ) =>
8596 {
97+ if ( arguments . Contains ( "redis-cli INFO SERVER" ) )
98+ {
99+ this . memoryProcess . StandardOutput = new ConcurrentBuffer ( new StringBuilder ( "redis_version:7.0.15" ) ) ;
100+ return this . memoryProcess ;
101+ }
86102 expectedCommands . Remove ( $ "{ exe } { arguments } ") ;
87103 return this . fixture . Process ;
88104 } ;
@@ -113,6 +129,11 @@ public async Task RedisMemtierServerExecutorExecutesExpectedProcessWhenBindingTo
113129
114130 this . fixture . ProcessManager . OnCreateProcess = ( exe , arguments , workingDirectory ) =>
115131 {
132+ if ( arguments . Contains ( "redis-cli INFO SERVER" ) )
133+ {
134+ this . memoryProcess . StandardOutput = new ConcurrentBuffer ( new StringBuilder ( "redis_version:7.0.15" ) ) ;
135+ return this . memoryProcess ;
136+ }
116137 expectedCommands . Remove ( $ "{ exe } { arguments } ") ;
117138 return this . fixture . Process ;
118139 } ;
@@ -140,6 +161,11 @@ public async Task RedisMemtierServerExecutorExecutesExpectedProcessWhenNotBindin
140161
141162 this . fixture . ProcessManager . OnCreateProcess = ( exe , arguments , workingDirectory ) =>
142163 {
164+ if ( arguments . Contains ( "redis-cli INFO SERVER" ) )
165+ {
166+ this . memoryProcess . StandardOutput = new ConcurrentBuffer ( new StringBuilder ( "redis_version:7.0.15" ) ) ;
167+ return this . memoryProcess ;
168+ }
143169 expectedCommands . Remove ( $ "{ exe } { arguments } ") ;
144170 return this . fixture . Process ;
145171 } ;
@@ -149,6 +175,37 @@ public async Task RedisMemtierServerExecutorExecutesExpectedProcessWhenNotBindin
149175 }
150176 }
151177
178+ [ Test ]
179+ public async Task RedisServerExecutorCapturesRedisVersionSuccessfully ( )
180+ {
181+ using ( var executor = new TestRedisServerExecutor ( this . fixture . Dependencies , this . fixture . Parameters ) )
182+ {
183+ this . fixture . ProcessManager . OnCreateProcess = ( command , arguments , workingDirectory ) =>
184+ {
185+ if ( arguments . Contains ( "redis-cli INFO SERVER" ) )
186+ {
187+ this . memoryProcess . StandardOutput = new ConcurrentBuffer ( new StringBuilder ( "redis_version:7.0.15" ) ) ;
188+ }
189+ return this . memoryProcess ;
190+ } ;
191+
192+ // Act
193+ await executor . ExecuteAsync ( CancellationToken . None ) ;
194+
195+ // Assert
196+ var messages = this . fixture . Logger . MessagesLogged ( $ "{ nameof ( TestRedisServerExecutor ) } .RedisVersionCaptured") ;
197+ Assert . IsNotEmpty ( messages , "Expected at least one log message indicating the Redis version was captured2." ) ;
198+ bool versionCapturedCorrectly = messages . Any ( msg =>
199+ {
200+ var eventContext = msg . Item3 as EventContext ;
201+ return eventContext != null &&
202+ eventContext . Properties . ContainsKey ( "redisVersion" ) &&
203+ eventContext . Properties [ "redisVersion" ] . ToString ( ) == "7.0.15" ;
204+ } ) ;
205+ Assert . IsTrue ( versionCapturedCorrectly , "The Redis version '7.0.15' was not captured correctly in the logs." ) ;
206+ }
207+ }
208+
152209 private class TestRedisServerExecutor : RedisServerExecutor
153210 {
154211 public TestRedisServerExecutor ( IServiceCollection services , IDictionary < string , IConvertible > parameters = null )
0 commit comments