forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDSCv3ProcessorTests.cs
More file actions
321 lines (257 loc) · 12.5 KB
/
DSCv3ProcessorTests.cs
File metadata and controls
321 lines (257 loc) · 12.5 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// -----------------------------------------------------------------------------
// <copyright file="DSCv3ProcessorTests.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
// </copyright>
// -----------------------------------------------------------------------------
namespace Microsoft.Management.Configuration.UnitTests.Tests
{
using System.Collections.Generic;
using System.Linq;
using Microsoft.Management.Configuration.Processor;
using Microsoft.Management.Configuration.Processor.DSCv3.Model;
using Microsoft.Management.Configuration.Processor.Exceptions;
using Microsoft.Management.Configuration.UnitTests.Fixtures;
using Microsoft.Management.Configuration.UnitTests.Helpers;
using Windows.Foundation.Collections;
using Xunit;
using Xunit.Abstractions;
/// <summary>
/// Tests for the DSCv3 processor.
/// </summary>
[Collection("UnitTestCollection")]
[InProc]
public class DSCv3ProcessorTests : ConfigurationProcessorTestBase
{
private readonly UnitTestFixture fixture;
private readonly ITestOutputHelper log;
/// <summary>
/// Initializes a new instance of the <see cref="DSCv3ProcessorTests"/> class.
/// </summary>
/// <param name="fixture">Unit test fixture.</param>
/// <param name="log">Log helper.</param>
public DSCv3ProcessorTests(UnitTestFixture fixture, ITestOutputHelper log)
: base(fixture, log)
{
this.fixture = fixture;
this.log = log;
}
/// <summary>
/// Tests for the unit details caching.
/// </summary>
[Fact]
public void Set_UnitPropertyDetailsCached()
{
var (factory, dsc) = CreateTestFactory();
var set = this.ConfigurationSet();
string type1 = "Type1";
string type2 = "Type2";
var unit1 = this.ConfigurationUnit().Assign(new { Type = type1 });
var unit2 = this.ConfigurationUnit().Assign(new { Type = type2 });
var setProcessor = factory.CreateSetProcessor(set);
// Initially, no details
var details = setProcessor.GetUnitProcessorDetails(unit1, ConfigurationUnitDetailFlags.Local);
Assert.Null(details);
// Null result not cached
dsc.GetResourceByTypeResult = new TestResourceListItem() { Type = type1 };
details = setProcessor.GetUnitProcessorDetails(unit1, ConfigurationUnitDetailFlags.Local);
Assert.NotNull(details);
Assert.Equal(type1, details.UnitType);
// Not-null result cached
dsc.GetResourceByTypeResult = null;
dsc.GetResourceByTypeDelegate = s => throw new System.Exception("Shouldn't be called");
details = setProcessor.GetUnitProcessorDetails(unit1, ConfigurationUnitDetailFlags.Local);
Assert.NotNull(details);
Assert.Equal(type1, details.UnitType);
// Different type, no details
dsc.GetResourceByTypeDelegate = null;
details = setProcessor.GetUnitProcessorDetails(unit2, ConfigurationUnitDetailFlags.Local);
Assert.Null(details);
// Null result not cached
dsc.GetResourceByTypeResult = new TestResourceListItem() { Type = type2 };
details = setProcessor.GetUnitProcessorDetails(unit2, ConfigurationUnitDetailFlags.Local);
Assert.NotNull(details);
Assert.Equal(type2, details.UnitType);
// First type is still first type
dsc.GetResourceByTypeResult = null;
dsc.GetResourceByTypeDelegate = s => throw new System.Exception("Shouldn't be called");
details = setProcessor.GetUnitProcessorDetails(unit1, ConfigurationUnitDetailFlags.Local);
Assert.NotNull(details);
Assert.Equal(type1, details.UnitType);
}
/// <summary>
/// Test for unit processor creation requiring resource to be found.
/// </summary>
[Fact(Skip = "Disable this test while we have the bypass in place")]
public void Set_ResourceNotFoundIsError()
{
var (factory, dsc) = CreateTestFactory();
var set = this.ConfigurationSet();
string type1 = "Type1";
var unit1 = this.ConfigurationUnit().Assign(new { Type = type1 });
var setProcessor = factory.CreateSetProcessor(set);
// Not found is error
Assert.Throws<FindDscResourceNotFoundException>(() => setProcessor.CreateUnitProcessor(unit1));
// Found is not error
dsc.GetResourceByTypeResult = new TestResourceListItem() { Type = type1 };
var unitProcessor = setProcessor.CreateUnitProcessor(unit1);
Assert.NotNull(unitProcessor);
Assert.Equal(type1, unitProcessor.Unit.Type);
}
/// <summary>
/// Test for settings export.
/// </summary>
[Fact]
public void GetAllSettings_Expected()
{
var (factory, dsc) = CreateTestFactory();
var processor = this.CreateConfigurationProcessorWithDiagnostics(factory);
string type1 = "Type1";
var unit1 = this.ConfigurationUnit().Assign(new { Type = type1 });
dsc.GetResourceByTypeDelegate = (type) =>
{
Assert.Equal(type1, type);
return new TestResourceListItem() { Type = type1 };
};
ValueSet set1 = new ValueSet();
set1.Add("key1", "val1");
ValueSet set2 = new ValueSet();
set2.Add("key2", "val2");
dsc.ExportResourceResult = new List<IResourceExportItem>()
{
new TestResourceExportItem() { Type = type1, Name = "1", Settings = set1 },
new TestResourceExportItem() { Type = type1, Name = "2", Settings = set2 },
};
var result = processor.GetAllUnitSettings(unit1);
Assert.NotNull(result);
Assert.NotNull(result.ResultInformation);
Assert.Null(result.ResultInformation.ResultCode);
Assert.Equal(2, result.Settings.Count);
Assert.NotNull(result.Settings.Single(set => set.Contains(set1.First())));
Assert.NotNull(result.Settings.Single(set => set.Contains(set2.First())));
}
/// <summary>
/// Test for settings export with differing types.
/// </summary>
[Fact]
public void GetAllSettings_DifferentType()
{
var (factory, dsc) = CreateTestFactory();
var processor = this.CreateConfigurationProcessorWithDiagnostics(factory);
string type1 = "Type1";
string type2 = "Type2";
var unit1 = this.ConfigurationUnit().Assign(new { Type = type1 });
dsc.GetResourceByTypeDelegate = (type) =>
{
Assert.Equal(type1, type);
return new TestResourceListItem() { Type = type1 };
};
ValueSet set1 = new ValueSet();
set1.Add("key1", "val1");
ValueSet set2 = new ValueSet();
set2.Add("key2", "val2");
dsc.ExportResourceResult = new List<IResourceExportItem>()
{
new TestResourceExportItem() { Type = type1, Name = "1", Settings = set1 },
new TestResourceExportItem() { Type = type2, Name = "2", Settings = set2 },
};
var result = processor.GetAllUnitSettings(unit1);
Assert.NotNull(result);
Assert.NotNull(result.ResultInformation);
Assert.NotNull(result.ResultInformation.ResultCode);
Assert.Equal(ErrorCodes.WinGetConfigUnitUnsupportedType, result.ResultInformation.ResultCode.HResult);
}
/// <summary>
/// Test for unit export.
/// </summary>
[Fact]
public void GetAllUnits_Simple()
{
var (factory, dsc) = CreateTestFactory();
var processor = this.CreateConfigurationProcessorWithDiagnostics(factory);
string type1 = "Type1";
var unit1 = this.ConfigurationUnit().Assign(new { Type = type1 });
dsc.GetResourceByTypeDelegate = (type) =>
{
Assert.Equal(type1, type);
return new TestResourceListItem() { Type = type1 };
};
ValueSet set1 = new ValueSet();
set1.Add("key1", "val1");
ValueSet set2 = new ValueSet();
set2.Add("key2", "val2");
dsc.ExportResourceResult = new List<IResourceExportItem>()
{
new TestResourceExportItem() { Type = type1, Name = "1", Settings = set1 },
new TestResourceExportItem() { Type = type1, Name = "2", Settings = set2 },
};
var result = processor.GetAllUnits(unit1);
Assert.NotNull(result);
Assert.NotNull(result.ResultInformation);
Assert.Null(result.ResultInformation.ResultCode);
Assert.Equal(2, result.Units.Count);
foreach (var unit in result.Units)
{
Assert.Equal(type1, unit.Type);
Assert.NotEmpty(unit.Identifier);
}
Assert.NotEqual(result.Units[0].Identifier, result.Units[1].Identifier);
Assert.NotNull(result.Units.Single(unit => unit.Settings.Contains(set1.First())));
Assert.NotNull(result.Units.Single(unit => unit.Settings.Contains(set2.First())));
}
/// <summary>
/// Test for unit export with complex data.
/// </summary>
[Fact]
public void GetAllUnits_Complex()
{
var (factory, dsc) = CreateTestFactory();
var processor = this.CreateConfigurationProcessorWithDiagnostics(factory);
string type1 = "Type1";
string type2 = "Type2";
string name1 = "1";
string name2 = "2";
var unit1 = this.ConfigurationUnit().Assign(new { Type = type1 });
dsc.GetResourceByTypeDelegate = (type) =>
{
Assert.Equal(type1, type);
return new TestResourceListItem() { Type = type1 };
};
ValueSet set1 = new ValueSet();
set1.Add("key1", "val1");
ValueSet metadata1 = new ValueSet();
metadata1.Add("met1", "val11");
ValueSet set2 = new ValueSet();
set2.Add("key2", "val2");
List<string> dependencies2 = new List<string>();
dependencies2.Add(name1);
dsc.ExportResourceResult = new List<IResourceExportItem>()
{
new TestResourceExportItem() { Type = type1, Name = name1, Settings = set1, Metadata = metadata1 },
new TestResourceExportItem() { Type = type2, Name = name2, Settings = set2, Dependencies = dependencies2 },
};
var result = processor.GetAllUnits(unit1);
Assert.NotNull(result);
Assert.NotNull(result.ResultInformation);
Assert.Null(result.ResultInformation.ResultCode);
Assert.Equal(2, result.Units.Count);
var result1 = result.Units.Single(unit => unit.Identifier == name1);
var result2 = result.Units.Single(unit => unit.Identifier == name2);
Assert.Equal(type1, result1.Type);
Assert.Equal(type2, result2.Type);
Assert.Contains(set1.First(), result1.Settings);
Assert.Contains(set2.First(), result2.Settings);
Assert.Contains(metadata1.First(), result1.Metadata);
Assert.Empty(result2.Metadata);
Assert.Empty(result1.Dependencies);
Assert.Contains(dependencies2.First(), result2.Dependencies);
}
private static (DSCv3ConfigurationSetProcessorFactory, TestDSCv3) CreateTestFactory()
{
DSCv3ConfigurationSetProcessorFactory factory = new DSCv3ConfigurationSetProcessorFactory();
TestDSCv3 dsc = new TestDSCv3();
factory.Settings.DSCv3 = dsc;
factory.Settings.DscExecutablePath = "Test-Path-Not-Used.txt";
return (factory, dsc);
}
}
}