1+ using System ;
2+ using System . Linq ;
3+ using System . Net . Http ;
4+ using Microsoft . VisualStudio . TestTools . UnitTesting ;
5+ using Newtonsoft . Json ;
6+ using TugDSC . Client ;
7+ using TugDSC . Testing . MSTest ;
8+
9+ namespace TugDSC . Client . CLIApp
10+ {
11+ [ TestClass ]
12+ public class ClassicPullServerProtocolCompatibilityReportTests : ProtocolCompatibilityTestsBase
13+ {
14+ [ ClassInitialize ]
15+ public new static void ClassInit ( TestContext ctx )
16+ {
17+ ProtocolCompatibilityTestsBase . ClassInit ( ctx ) ;
18+ }
19+
20+ [ TestMethod ]
21+ public void TestSendReport ( )
22+ {
23+ var config = BuildConfig ( newAgentId : true ) ;
24+ using ( var client = new DscPullClient ( config ) )
25+ {
26+ client . DisableReportAdditionalData = _testConfig . adjust_for_wmf_50 ;
27+ client . RegisterDscAgent ( ) . Wait ( ) ;
28+ client . SendReport ( "SimpleInventoryDefaults" ,
29+ overrides : new Model . SendReportBody
30+ {
31+ NodeName = "MY_NAME" ,
32+ IpAddress = "::1;127.0.01" ,
33+ } ) . Wait ( ) ;
34+ client . SendReport ( "DetailedStatusDefaults" ,
35+ statusData : new [ ] { "STATUS" } ) . Wait ( ) ;
36+ client . SendReport ( "ErrorDefaults" ,
37+ errors : new [ ] { "ERROR" } ) . Wait ( ) ;
38+ }
39+ }
40+
41+ [ TestMethod ]
42+ public void TestSendReport_BadEmptyBody ( )
43+ {
44+ var config = BuildConfig ( ) ;
45+ using ( var client = new DscPullClient ( config ) )
46+ {
47+ client . DisableReportAdditionalData = _testConfig . adjust_for_wmf_50 ;
48+ Assert . That . ThrowsWhen < AggregateException > (
49+ condition : ( ex ) =>
50+ ex . InnerException is HttpRequestException
51+ && ex . InnerException . Message . Contains (
52+ "Response status code does not indicate success: 400 (Bad Request)" ) ,
53+ action : ( ) =>
54+ client . SendReport ( null ) . Wait ( ) ,
55+ message :
56+ "Throws HTTP exception for bad request (400)" ) ;
57+ }
58+ }
59+
60+ [ TestMethod ]
61+ public void TestSendReport_BadMissingJobId ( )
62+ {
63+ var config = BuildConfig ( ) ;
64+ using ( var client = new DscPullClient ( config ) )
65+ {
66+ client . DisableReportAdditionalData = _testConfig . adjust_for_wmf_50 ;
67+ Assert . That . ThrowsWhen < AggregateException > (
68+ condition : ( ex ) =>
69+ ex . InnerException is HttpRequestException
70+ && ex . InnerException . Message . Contains (
71+ "Response status code does not indicate success: 400 (Bad Request)" ) ,
72+ action : ( ) =>
73+ client . SendReport ( new BadSendReportBody ( ) ) . Wait ( ) ,
74+ message :
75+ "Throws HTTP exception for bad request (400)" ) ;
76+ }
77+ }
78+
79+ [ TestMethod ]
80+ public void TestSendReport_BadDateFormat ( )
81+ {
82+ var report = new Model . SendReportBody
83+ {
84+ JobId = Guid . NewGuid ( ) ,
85+ StartTime = "NOW" ,
86+ EndTime = "THEN" ,
87+ OperationType = "FOO" ,
88+ ReportFormatVersion = "BAR" ,
89+ } ;
90+
91+ var config = BuildConfig ( ) ;
92+ using ( var client = new DscPullClient ( config ) )
93+ {
94+ client . DisableReportAdditionalData = _testConfig . adjust_for_wmf_50 ;
95+ Assert . That . ThrowsWhen < AggregateException > (
96+ condition : ( ex ) =>
97+ ex . InnerException is HttpRequestException
98+ && ex . InnerException . Message . Contains (
99+ "Response status code does not indicate success: 500 (Internal Server Error)" ) ,
100+ action : ( ) =>
101+ client . SendReport ( report ) . Wait ( ) ,
102+ message :
103+ "Throws HTTP exception for internal server error (500)" ) ;
104+ }
105+ }
106+
107+ [ TestMethod ]
108+ public void TestGetReports_Single ( )
109+ {
110+ var reportDateFormat = CLASSIC_SERVER_REPORT_DATE_FORMAT ;
111+ if ( _testConfig . adjust_for_wmf_50 )
112+ reportDateFormat = CLASSIC_SERVER_REPORT_DATE_FORMAT_ALT ;
113+
114+ var config = BuildConfig ( newAgentId : true ) ;
115+ var report = new Model . SendReportBody
116+ {
117+ JobId = Guid . NewGuid ( ) ,
118+ OperationType = "FOO" ,
119+ RefreshMode = Model . DscRefreshMode . Pull ,
120+ Status = "BAR" ,
121+ ReportFormatVersion = "Spooky" ,
122+ ConfigurationVersion = "Scary" ,
123+ //StartTime = DateTime.Now.ToString(Model.SendReportBody.REPORT_DATE_FORMAT),
124+ StartTime = DateTime . Now . ToString ( reportDateFormat ) ,
125+ //EndTime = DateTime.Now.ToString(Model.SendReportBody.REPORT_DATE_FORMAT),
126+ EndTime = DateTime . Now . ToString ( reportDateFormat ) ,
127+ RebootRequested = Model . DscTrueFalse . False ,
128+ StatusData = new [ ] { "STATUS-DATA" } ,
129+ Errors = new [ ] { "ERRORS" } ,
130+ AdditionalData = new [ ]
131+ {
132+ new Model . SendReportBody . AdditionalDataItem { Key = "1" , Value = "ONE" , } ,
133+ new Model . SendReportBody . AdditionalDataItem { Key = "2" , Value = "TWO" , } ,
134+ } ,
135+ } ;
136+
137+ using ( var client = new DscPullClient ( config ) )
138+ {
139+ client . DisableReportAdditionalData = _testConfig . adjust_for_wmf_50 ;
140+ client . RegisterDscAgent ( ) . Wait ( ) ;
141+ client . SendReport ( report ) . Wait ( ) ;
142+
143+ var sr = client . GetReports ( ) . Result ;
144+ Assert . IsNotNull ( sr , "Reports not null" ) ;
145+ var srArr = sr . ToArray ( ) ;
146+ Assert . AreEqual ( 1 , srArr . Length , "Reports length is exactly 1" ) ;
147+
148+ // Unfortunate kludge to deal with broken DscService on WMF 5.1
149+ // See https://github.com/PowerShell/PowerShell/issues/2921
150+ if ( _testConfig . adjust_for_wmf_50 )
151+ report . AdditionalData = Model . SendReportBody . AdditionalDataItem . EMPTY_ITEMS ;
152+
153+ var ser1 = JsonConvert . SerializeObject ( report ) ;
154+ var ser2 = JsonConvert . SerializeObject ( srArr [ 0 ] ) ;
155+ Assert . AreEqual ( ser1 , ser2 , "Submitted and retrieved reports are the same" ) ;
156+
157+ sr = client . GetReports ( ) . Result ;
158+ Assert . IsNotNull ( sr , "All reports not null" ) ;
159+ srArr = sr . ToArray ( ) ;
160+ Assert . AreEqual ( 1 , srArr . Length , "All reports length is exactly 1" ) ;
161+ }
162+ }
163+
164+ [ TestMethod ]
165+ public void TestGetReports_Multi ( )
166+ {
167+ var strArr1 = new [ ] { "STATUS-1" } ;
168+ var strArr2 = new [ ] { "STATUS-2" } ;
169+ var strArr3 = new [ ] { "ERROR-1" } ;
170+ var strArr4 = new [ ] { "ERROR-2" } ;
171+
172+ var config = BuildConfig ( newAgentId : true ) ;
173+ using ( var client = new DscPullClient ( config ) )
174+ {
175+ client . DisableReportAdditionalData = _testConfig . adjust_for_wmf_50 ;
176+ client . RegisterDscAgent ( ) . Wait ( ) ;
177+ client . SendReport ( operationType : "1" , statusData : strArr1 ) . Wait ( ) ;
178+ client . SendReport ( operationType : "2" , statusData : strArr2 ) . Wait ( ) ;
179+ client . SendReport ( operationType : "3" , errors : strArr3 ) . Wait ( ) ;
180+ client . SendReport ( operationType : "4" , errors : strArr4 ) . Wait ( ) ;
181+
182+ var sr = client . GetReports ( ) . Result ;
183+ Assert . IsNotNull ( sr , "All reports not null" ) ;
184+ var srArr = sr . ToArray ( ) ;
185+ Assert . AreEqual ( 4 , srArr . Length , "All reports length" ) ;
186+
187+ var srArrOrd = srArr . OrderBy ( x => x . OperationType ) . ToArray ( ) ;
188+ CollectionAssert . AreEqual ( strArr1 , srArrOrd [ 0 ] . StatusData ) ;
189+ CollectionAssert . AreEqual ( strArr2 , srArrOrd [ 1 ] . StatusData ) ;
190+ CollectionAssert . AreEqual ( strArr3 , srArrOrd [ 2 ] . Errors ) ;
191+ CollectionAssert . AreEqual ( strArr4 , srArrOrd [ 3 ] . Errors ) ;
192+ }
193+ }
194+
195+ public class BadSendReportBody : Model . SendReportBody
196+ {
197+ public new string JobId
198+ { get ; set ; }
199+
200+ [ JsonProperty ( NullValueHandling = NullValueHandling . Ignore ) ]
201+ public new string OperationType
202+ { get ; set ; }
203+
204+ [ JsonProperty ( NullValueHandling = NullValueHandling . Ignore ) ]
205+ public new string ReportFormatVersion
206+ { get ; set ; }
207+
208+ [ JsonProperty ( NullValueHandling = NullValueHandling . Ignore ) ]
209+ public new string StartTime
210+ { get ; set ; }
211+
212+ [ JsonProperty ( NullValueHandling = NullValueHandling . Ignore ) ]
213+ public new string Errors
214+ { get ; set ; }
215+
216+ [ JsonProperty ( NullValueHandling = NullValueHandling . Ignore ) ]
217+ public new string StatusData
218+ { get ; set ; }
219+
220+ [ JsonProperty ( NullValueHandling = NullValueHandling . Ignore ) ]
221+ public new string AdditionalData
222+ { get ; set ; }
223+ }
224+ }
225+ }
0 commit comments