1+ // Copyright (c) Microsoft Corporation.
2+ // Licensed under the MIT License.
3+
4+ namespace VirtualClient . Logging
5+ {
6+ using System ;
7+ using System . Collections . Generic ;
8+ using System . IO ;
9+ using System . Runtime . InteropServices ;
10+ using Microsoft . Extensions . DependencyInjection ;
11+ using Moq ;
12+ using NUnit . Framework ;
13+ using VirtualClient . Common . Extensions ;
14+ using VirtualClient . Contracts ;
15+
16+ [ TestFixture ]
17+ [ Category ( "Unit" ) ]
18+ public class MetadataFileLoggerTests
19+ {
20+ private MockFixture mockFixture ;
21+
22+ public void SetupTest ( PlatformID platform )
23+ {
24+ this . mockFixture = new MockFixture ( ) ;
25+ this . mockFixture . Setup ( platform , useUnixStylePathsOnly : true ) ;
26+ }
27+
28+ [ Test ]
29+ [ TestCase ( PlatformID . Unix ) ]
30+ [ TestCase ( PlatformID . Win32NT ) ]
31+ public void MetadataFileLoggerIdentifiesTheExpectedMetadata ( PlatformID platform )
32+ {
33+ this . SetupTest ( platform ) ;
34+
35+ var logger = new TestMetadataFileLogger ( this . mockFixture . Dependencies , null ) ;
36+ IDictionary < string , IConvertible > metadata = logger . GetMetadata ( ) ;
37+
38+ Assert . IsTrue ( metadata . ContainsKey ( "clientId" ) ) ;
39+ Assert . IsTrue ( metadata . ContainsKey ( "experimentId" ) ) ;
40+ Assert . IsTrue ( metadata . ContainsKey ( "machineName" ) ) ;
41+ Assert . IsTrue ( metadata . ContainsKey ( "platformArchitecture" ) ) ;
42+ Assert . IsTrue ( metadata . ContainsKey ( "operatingSystemVersion" ) ) ;
43+ Assert . IsTrue ( metadata . ContainsKey ( "operatingSystemDescription" ) ) ;
44+ Assert . IsTrue ( metadata . ContainsKey ( "timestamp" ) ) ;
45+ Assert . IsTrue ( metadata . ContainsKey ( "timezone" ) ) ;
46+
47+ ISystemInfo systemInfo = this . mockFixture . Dependencies . GetService < ISystemInfo > ( ) ;
48+ PlatformSpecifics platformSpecifics = this . mockFixture . PlatformSpecifics ;
49+
50+ Assert . AreEqual ( systemInfo . AgentId , metadata [ "clientId" ] ) ;
51+ Assert . AreEqual ( systemInfo . ExperimentId , metadata [ "experimentId" ] ) ;
52+ Assert . AreEqual ( Environment . MachineName , metadata [ "machineName" ] ) ;
53+ Assert . AreEqual ( platformSpecifics . PlatformArchitectureName , metadata [ "platformArchitecture" ] ) ;
54+ Assert . AreEqual ( Environment . OSVersion . ToString ( ) , metadata [ "operatingSystemVersion" ] ) ;
55+ Assert . AreEqual ( RuntimeInformation . OSDescription , metadata [ "operatingSystemDescription" ] ) ;
56+ Assert . AreEqual ( TimeZoneInfo . Local . StandardName , metadata [ "timezone" ] ) ;
57+ }
58+
59+ [ Test ]
60+ [ TestCase ( PlatformID . Unix ) ]
61+ [ TestCase ( PlatformID . Win32NT ) ]
62+ public void MetadataFileLoggerWritesTheExpectedMetadataToFile ( PlatformID platform )
63+ {
64+ this . SetupTest ( platform ) ;
65+
66+ var logger = new TestMetadataFileLogger ( this . mockFixture . Dependencies , null ) ;
67+ IDictionary < string , IConvertible > expectedMetadata = logger . GetMetadata ( ) ;
68+
69+ this . mockFixture . FileSystem
70+ . Setup ( fs => fs . Path . GetFullPath ( It . IsAny < string > ( ) ) )
71+ . Returns < string > ( path => Path . GetFullPath ( path ) ) ;
72+
73+ this . mockFixture . FileSystem
74+ . Setup ( fs => fs . File . WriteAllText ( It . IsAny < string > ( ) , It . IsAny < string > ( ) ) )
75+ . Callback < string , string > ( ( filePath , content ) =>
76+ {
77+ Assert . IsTrue ( content . Contains ( "clientId" ) ) ;
78+ Assert . IsTrue ( content . Contains ( "experimentId" ) ) ;
79+ Assert . IsTrue ( content . Contains ( "machineName" ) ) ;
80+ Assert . IsTrue ( content . Contains ( "platformArchitecture" ) ) ;
81+ Assert . IsTrue ( content . Contains ( "operatingSystemVersion" ) ) ;
82+ Assert . IsTrue ( content . Contains ( "operatingSystemDescription" ) ) ;
83+ Assert . IsTrue ( content . Contains ( "timestamp" ) ) ;
84+ Assert . IsTrue ( content . Contains ( "timezone" ) ) ;
85+ } ) ;
86+
87+ logger . WriteMetadataFile ( expectedMetadata ) ;
88+ }
89+
90+ [ Test ]
91+ [ Platform ( "Win" ) ]
92+ [ TestCase ( PlatformID . Unix ) ]
93+ [ TestCase ( PlatformID . Win32NT ) ]
94+ public void MetadataFileLoggerWritesTheMetadataToTheExpectedFilePathLocation ( PlatformID platform )
95+ {
96+ this . SetupTest ( platform ) ;
97+
98+ string expectedFilePath = this . mockFixture . PlatformSpecifics . GetLogsPath ( "metadata.log" ) ;
99+ bool confirmed = false ;
100+
101+ var logger = new TestMetadataFileLogger ( this . mockFixture . Dependencies , null ) ;
102+ var metadata = new Dictionary < string , IConvertible >
103+ {
104+ { "any" , "metadata" }
105+ } ;
106+
107+ this . mockFixture . FileSystem
108+ . Setup ( fs => fs . Path . GetFullPath ( It . IsAny < string > ( ) ) )
109+ . Returns < string > ( path => Path . GetFullPath ( path ) ) ;
110+
111+ this . mockFixture . FileSystem
112+ . Setup ( fs => fs . File . WriteAllText ( It . IsAny < string > ( ) , It . IsAny < string > ( ) ) )
113+ . Callback < string , string > ( ( actualFilePath , content ) =>
114+ {
115+ Assert . AreEqual ( expectedFilePath , actualFilePath ) ;
116+ confirmed = true ;
117+ } ) ;
118+
119+ logger . WriteMetadataFile ( metadata ) ;
120+ Assert . IsTrue ( confirmed ) ;
121+ }
122+
123+ [ Test ]
124+ [ Platform ( "Win" ) ]
125+ [ TestCase ( PlatformID . Unix ) ]
126+ [ TestCase ( PlatformID . Win32NT ) ]
127+ public void MetadataFileLoggerWritesTheMetadataToTheExpectedFilePathLocationWhenAFileNameIsDefined ( PlatformID platform )
128+ {
129+ this . SetupTest ( platform ) ;
130+
131+ string expectedFileName = "marker.log" ;
132+ string expectedFilePath = this . mockFixture . PlatformSpecifics . GetLogsPath ( expectedFileName ) ;
133+ bool confirmed = false ;
134+
135+ var logger = new TestMetadataFileLogger ( this . mockFixture . Dependencies , expectedFileName ) ;
136+ var metadata = new Dictionary < string , IConvertible >
137+ {
138+ { "any" , "metadata" }
139+ } ;
140+
141+ this . mockFixture . FileSystem . Setup ( fs => fs . Path . GetDirectoryName ( It . IsAny < string > ( ) ) )
142+ . Returns ( null as string ) ;
143+
144+ this . mockFixture . FileSystem
145+ . Setup ( fs => fs . File . WriteAllText ( It . IsAny < string > ( ) , It . IsAny < string > ( ) ) )
146+ . Callback < string , string > ( ( actualFilePath , content ) =>
147+ {
148+ Assert . AreEqual ( expectedFilePath , actualFilePath ) ;
149+ confirmed = true ;
150+ } ) ;
151+
152+ logger . WriteMetadataFile ( metadata ) ;
153+ Assert . IsTrue ( confirmed ) ;
154+ }
155+
156+ [ Test ]
157+ [ Platform ( "Win" ) ]
158+ [ TestCase ( ".\\ logs\\ marker.log" ) ]
159+ [ TestCase ( "..\\ ..\\ logs\\ marker.log" ) ]
160+ [ TestCase ( "..\\ ..\\ test\\ marker.log" ) ]
161+ public void MetadataFileLoggerWritesTheMetadataToTheExpectedFilePathLocationWhenARelativeFilePathIsDefined ( string relativeFilePath )
162+ {
163+ this . SetupTest ( PlatformID . Win32NT ) ;
164+
165+ string expectedFilePath = Path . GetFullPath ( relativeFilePath ) ;
166+ bool confirmed = false ;
167+
168+ var logger = new TestMetadataFileLogger ( this . mockFixture . Dependencies , relativeFilePath ) ;
169+ var metadata = new Dictionary < string , IConvertible >
170+ {
171+ { "any" , "metadata" }
172+ } ;
173+
174+ this . mockFixture . FileSystem . Setup ( fs => fs . Path . GetDirectoryName ( It . IsAny < string > ( ) ) )
175+ . Returns < string > ( path => Path . GetDirectoryName ( path ) ) ;
176+
177+ this . mockFixture . FileSystem
178+ . Setup ( fs => fs . Path . GetFullPath ( It . IsAny < string > ( ) ) )
179+ . Returns < string > ( path => Path . GetFullPath ( path ) ) ;
180+
181+ this . mockFixture . FileSystem
182+ . Setup ( fs => fs . File . WriteAllText ( It . IsAny < string > ( ) , It . IsAny < string > ( ) ) )
183+ . Callback < string , string > ( ( actualFilePath , content ) =>
184+ {
185+ Assert . AreEqual ( expectedFilePath , actualFilePath ) ;
186+ confirmed = true ;
187+ } ) ;
188+
189+ logger . WriteMetadataFile ( metadata ) ;
190+ Assert . IsTrue ( confirmed ) ;
191+ }
192+
193+ [ Test ]
194+ [ Platform ( "Win" ) ]
195+ [ TestCase ( "C:\\ Users\\ User\\ logs\\ marker.log" ) ]
196+ [ TestCase ( "S:\\ Users\\ User\\ test\\ marker.log" ) ]
197+ public void MetadataFileLoggerWritesTheMetadataToTheExpectedFilePathLocationWhenAFullFilePathIsDefined ( string relativeFilePath )
198+ {
199+ this . SetupTest ( PlatformID . Win32NT ) ;
200+
201+ string expectedFilePath = Path . GetFullPath ( relativeFilePath ) ;
202+ bool confirmed = false ;
203+
204+ var logger = new TestMetadataFileLogger ( this . mockFixture . Dependencies , relativeFilePath ) ;
205+ var metadata = new Dictionary < string , IConvertible >
206+ {
207+ { "any" , "metadata" }
208+ } ;
209+
210+ this . mockFixture . FileSystem . Setup ( fs => fs . Path . GetDirectoryName ( It . IsAny < string > ( ) ) )
211+ . Returns < string > ( path => Path . GetDirectoryName ( path ) ) ;
212+
213+ this . mockFixture . FileSystem
214+ . Setup ( fs => fs . Path . GetFullPath ( It . IsAny < string > ( ) ) )
215+ . Returns < string > ( path => Path . GetFullPath ( path ) ) ;
216+
217+ this . mockFixture . FileSystem
218+ . Setup ( fs => fs . File . WriteAllText ( It . IsAny < string > ( ) , It . IsAny < string > ( ) ) )
219+ . Callback < string , string > ( ( actualFilePath , content ) =>
220+ {
221+ Assert . AreEqual ( expectedFilePath , actualFilePath ) ;
222+ confirmed = true ;
223+ } ) ;
224+
225+ logger . WriteMetadataFile ( metadata ) ;
226+ Assert . IsTrue ( confirmed ) ;
227+ }
228+
229+ private class TestMetadataFileLogger : MetadataFileLogger
230+ {
231+ public TestMetadataFileLogger ( IServiceCollection dependencies , string filePath )
232+ : base ( dependencies , filePath )
233+ {
234+ }
235+
236+ public new IDictionary < string , IConvertible > GetMetadata ( )
237+ {
238+ return base . GetMetadata ( ) ;
239+ }
240+
241+ public new void WriteMetadataFile ( IDictionary < string , IConvertible > metadata )
242+ {
243+ base . WriteMetadataFile ( metadata ) ;
244+ }
245+ }
246+ }
247+ }
0 commit comments