DscModuleV2SimpleFileResourceTests.cs (11047B)
1 // ----------------------------------------------------------------------------- 2 // <copyright file="DscModuleV2SimpleFileResourceTests.cs" company="Microsoft Corporation"> 3 // Copyright (c) Microsoft Corporation. Licensed under the MIT License. 4 // </copyright> 5 // ----------------------------------------------------------------------------- 6 7 namespace Microsoft.Management.Configuration.UnitTests.Tests 8 { 9 using System.IO; 10 using System.Management.Automation; 11 using Microsoft.Management.Configuration.Processor.PowerShell.DscModules; 12 using Microsoft.Management.Configuration.Processor.PowerShell.Helpers; 13 using Microsoft.Management.Configuration.UnitTests.Fixtures; 14 using Microsoft.Management.Configuration.UnitTests.Helpers; 15 using Windows.Foundation.Collections; 16 using Xunit; 17 using Xunit.Abstractions; 18 using static Microsoft.Management.Configuration.UnitTests.Helpers.PowerShellTestsConstants; 19 20 /// <summary> 21 /// Class that tests a little not that complex resource. 22 /// </summary> 23 [Collection("UnitTestCollection")] 24 [InProc] 25 public class DscModuleV2SimpleFileResourceTests 26 { 27 private readonly UnitTestFixture fixture; 28 private readonly ITestOutputHelper log; 29 30 /// <summary> 31 /// Initializes a new instance of the <see cref="DscModuleV2SimpleFileResourceTests"/> class. 32 /// </summary> 33 /// <param name="fixture">Fixture.</param> 34 /// <param name="log">log.</param> 35 public DscModuleV2SimpleFileResourceTests(UnitTestFixture fixture, ITestOutputHelper log) 36 { 37 this.fixture = fixture; 38 this.log = log; 39 } 40 41 /// <summary> 42 /// Test SimpleFileResource Ensure Present and Absent when file doesn't exist. 43 /// </summary> 44 /// <param name="ensureValue">Ensure value.</param> 45 /// <param name="expectedResult">Expected result.</param> 46 [Theory] 47 [InlineData("Absent", true)] 48 [InlineData("Present", false)] 49 public void SimpleFileResource_FileAbsent(string ensureValue, bool expectedResult) 50 { 51 var processorEnv = this.fixture.PrepareTestProcessorEnvironment(); 52 53 // Doesn't create a file. 54 using var tmpFile = new TempFile(); 55 56 var settings = new ValueSet 57 { 58 { "Path", tmpFile.FullFileName }, 59 { "Ensure", ensureValue }, 60 }; 61 62 var dscModule = new DscModuleV2(); 63 64 using PowerShell pwsh = PowerShell.Create(processorEnv.Runspace); 65 Assert.Equal( 66 expectedResult, 67 dscModule.InvokeTestResource( 68 pwsh, 69 settings, 70 TestModule.SimpleFileResourceName, 71 PowerShellHelpers.CreateModuleSpecification( 72 TestModule.SimpleTestResourceModuleName))); 73 } 74 75 /// <summary> 76 /// Test SimpleFileResource when the file exists. 77 /// </summary> 78 [Fact] 79 public void SimpleFileResource_FilePresent() 80 { 81 var processorEnv = this.fixture.PrepareTestProcessorEnvironment(); 82 83 using var tmpFile = new TempFile(); 84 tmpFile.CreateFile(); 85 86 var settings = new ValueSet 87 { 88 { "Path", tmpFile.FullFileName }, 89 { "Ensure", "Present" }, 90 }; 91 92 var dscModule = new DscModuleV2(); 93 using PowerShell pwsh = PowerShell.Create(processorEnv.Runspace); 94 Assert.True(dscModule.InvokeTestResource( 95 pwsh, 96 settings, 97 TestModule.SimpleFileResourceName, 98 PowerShellHelpers.CreateModuleSpecification( 99 TestModule.SimpleTestResourceModuleName))); 100 } 101 102 /// <summary> 103 /// Test SimpleFileResource with different content. 104 /// </summary> 105 [Fact] 106 public void SimpleFileResource_DifferentContent() 107 { 108 var processorEnv = this.fixture.PrepareTestProcessorEnvironment(); 109 110 using var tmpFile = new TempFile(content: "All work and no play makes Ruben a dull boy"); 111 112 var settings = new ValueSet 113 { 114 { "Path", tmpFile.FullFileName }, 115 { "Ensure", "Present" }, 116 { "Content", "Is that a from somewhere?" }, 117 }; 118 119 var dscModule = new DscModuleV2(); 120 using PowerShell pwsh = PowerShell.Create(processorEnv.Runspace); 121 Assert.False(dscModule.InvokeTestResource( 122 pwsh, 123 settings, 124 TestModule.SimpleFileResourceName, 125 PowerShellHelpers.CreateModuleSpecification( 126 TestModule.SimpleTestResourceModuleName))); 127 } 128 129 /// <summary> 130 /// Test SimpleFileResource with same context. 131 /// </summary> 132 [Fact] 133 public void SimpleFileResource_SameContent() 134 { 135 var processorEnv = this.fixture.PrepareTestProcessorEnvironment(); 136 137 string content = "This is literally the same, dont fail"; 138 using var tmpFile = new TempFile(content: content); 139 140 var settings = new ValueSet 141 { 142 { "Path", tmpFile.FullFileName }, 143 { "Ensure", "Present" }, 144 { "Content", content }, 145 }; 146 147 var dscModule = new DscModuleV2(); 148 using PowerShell pwsh = PowerShell.Create(processorEnv.Runspace); 149 Assert.True(dscModule.InvokeTestResource( 150 pwsh, 151 settings, 152 TestModule.SimpleFileResourceName, 153 PowerShellHelpers.CreateModuleSpecification( 154 TestModule.SimpleTestResourceModuleName))); 155 } 156 157 /// <summary> 158 /// Test SimpleFileResource Set creates the file. 159 /// </summary> 160 /// <param name="ensureValue">Ensure value.</param> 161 /// <param name="preCondition">If file exists before calling set.</param> 162 /// <param name="postCondition">If file exists after calling set.</param> 163 [Theory] 164 [InlineData("Absent", false, false)] 165 [InlineData("Present", false, true)] 166 [InlineData("Absent", true, false)] 167 [InlineData("Present", true, true)] 168 public void SimpleFileResource_Set_Ensure(string ensureValue, bool preCondition, bool postCondition) 169 { 170 var processorEnv = this.fixture.PrepareTestProcessorEnvironment(); 171 172 // Doesn't create a file. 173 using var tmpFile = new TempFile(); 174 175 var settings = new ValueSet 176 { 177 { "Path", tmpFile.FullFileName }, 178 { "Ensure", ensureValue }, 179 }; 180 181 if (preCondition) 182 { 183 tmpFile.CreateFile(); 184 } 185 186 Assert.Equal( 187 preCondition, 188 File.Exists(tmpFile.FullFileName)); 189 190 var dscModule = new DscModuleV2(); 191 using PowerShell pwsh = PowerShell.Create(processorEnv.Runspace); 192 dscModule.InvokeSetResource( 193 pwsh, 194 settings, 195 TestModule.SimpleFileResourceName, 196 PowerShellHelpers.CreateModuleSpecification( 197 TestModule.SimpleTestResourceModuleName)); 198 199 Assert.Equal( 200 postCondition, 201 File.Exists(tmpFile.FullFileName)); 202 } 203 204 /// <summary> 205 /// Tests SimpleFileResource Set writes to the file. 206 /// </summary> 207 /// <param name="preSetContent">The text in the file before calling Set.</param> 208 /// <param name="postSetContent">The test in the file after calling Set.</param> 209 [Theory] 210 [InlineData(null, "after content")] 211 [InlineData("i am a content", "and im another")] 212 [InlineData("copy paste", "copy paste")] 213 public void SimpleFileResource_Set_Content(string? preSetContent, string postSetContent) 214 { 215 var processorEnv = this.fixture.PrepareTestProcessorEnvironment(); 216 217 // Doesn't create a file. 218 using var tmpFile = new TempFile(); 219 if (preSetContent is not null) 220 { 221 tmpFile.CreateFile(preSetContent); 222 } 223 224 var settings = new ValueSet 225 { 226 { "Path", tmpFile.FullFileName }, 227 { "Ensure", "Present" }, 228 { "Content", postSetContent }, 229 }; 230 231 var dscModule = new DscModuleV2(); 232 using PowerShell pwsh = PowerShell.Create(processorEnv.Runspace); 233 dscModule.InvokeSetResource( 234 pwsh, 235 settings, 236 TestModule.SimpleFileResourceName, 237 PowerShellHelpers.CreateModuleSpecification( 238 TestModule.SimpleTestResourceModuleName)); 239 240 Assert.Equal( 241 postSetContent, 242 File.ReadAllText(tmpFile.FullFileName)); 243 } 244 245 /// <summary> 246 /// Test SimpleFileResource Get. 247 /// </summary> 248 [Fact] 249 public void SimpleFileResource_Get() 250 { 251 var processorEnv = this.fixture.PrepareTestProcessorEnvironment(); 252 253 string content = "I'm out of ideas"; 254 using var tmpFile = new TempFile(content: content); 255 256 var settings = new ValueSet 257 { 258 { "Path", tmpFile.FullFileName }, 259 }; 260 261 var dscModule = new DscModuleV2(); 262 using PowerShell pwsh = PowerShell.Create(processorEnv.Runspace); 263 var properties = dscModule.InvokeGetResource( 264 pwsh, 265 settings, 266 TestModule.SimpleFileResourceName, 267 PowerShellHelpers.CreateModuleSpecification( 268 TestModule.SimpleTestResourceModuleName)); 269 270 Assert.True(properties.ContainsKey("Path")); 271 Assert.True(properties.TryGetValue("Path", out object pathResult)); 272 Assert.Equal(tmpFile.FullFileName, pathResult as string); 273 274 // Present is just the default value. 275 Assert.True(properties.ContainsKey("Ensure")); 276 Assert.True(properties.TryGetValue("Ensure", out object ensureResult)); 277 Assert.Equal("Present", ensureResult as string); 278 279 Assert.True(properties.ContainsKey("Content")); 280 Assert.True(properties.TryGetValue("Content", out object contentResult)); 281 Assert.Equal(content, contentResult); 282 } 283 } 284 }