|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Text; |
| 5 | +using System.Runtime.CompilerServices; |
| 6 | +using GitHub.Runner.Common.Util; |
| 7 | +using GitHub.Runner.Sdk; |
| 8 | +using GitHub.Runner.Worker; |
| 9 | +using Moq; |
| 10 | +using Xunit; |
| 11 | +using DTWebApi = GitHub.DistributedTask.WebApi; |
| 12 | +using GitHub.DistributedTask.WebApi; |
| 13 | + |
| 14 | +namespace GitHub.Runner.Common.Tests.Worker |
| 15 | +{ |
| 16 | + public sealed class CreateStepSummaryCommandL0 |
| 17 | + { |
| 18 | + private Mock<IExecutionContext> _executionContext; |
| 19 | + private List<Tuple<DTWebApi.Issue, string>> _issues; |
| 20 | + private Variables _variables; |
| 21 | + private string _rootDirectory; |
| 22 | + private CreateStepSummaryCommand _createStepCommand; |
| 23 | + private ITraceWriter _trace; |
| 24 | + |
| 25 | + [Fact] |
| 26 | + [Trait("Level", "L0")] |
| 27 | + [Trait("Category", "Worker")] |
| 28 | + public void CreateStepSummaryCommand_FeatureDisabled() |
| 29 | + { |
| 30 | + using (var hostContext = Setup(featureFlagState: "false")) |
| 31 | + { |
| 32 | + var stepSummaryFile = Path.Combine(_rootDirectory, "feature-off"); |
| 33 | + |
| 34 | + _executionContext.Verify(e => e.QueueAttachFile(ChecksAttachmentType.StepSummary, It.IsAny<string>(), It.IsAny<string>()), Times.Never()); |
| 35 | + |
| 36 | + Assert.Equal(0, _issues.Count); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + [Fact] |
| 41 | + [Trait("Level", "L0")] |
| 42 | + [Trait("Category", "Worker")] |
| 43 | + public void CreateStepSummaryCommand_FileNull() |
| 44 | + { |
| 45 | + using (var hostContext = Setup()) |
| 46 | + { |
| 47 | + _createStepCommand.ProcessCommand(_executionContext.Object, null, null); |
| 48 | + |
| 49 | + _executionContext.Verify(e => e.QueueAttachFile(ChecksAttachmentType.StepSummary, It.IsAny<string>(), It.IsAny<string>()), Times.Never()); |
| 50 | + Assert.Equal(0, _issues.Count); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + [Fact] |
| 55 | + [Trait("Level", "L0")] |
| 56 | + [Trait("Category", "Worker")] |
| 57 | + public void CreateStepSummaryCommand_DirectoryNotFound() |
| 58 | + { |
| 59 | + using (var hostContext = Setup()) |
| 60 | + { |
| 61 | + var stepSummaryFile = Path.Combine(_rootDirectory, "directory-not-found", "env"); |
| 62 | + |
| 63 | + _createStepCommand.ProcessCommand(_executionContext.Object, stepSummaryFile, null); |
| 64 | + |
| 65 | + _executionContext.Verify(e => e.QueueAttachFile(ChecksAttachmentType.StepSummary, It.IsAny<string>(), It.IsAny<string>()), Times.Never()); |
| 66 | + Assert.Equal(0, _issues.Count); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + [Fact] |
| 71 | + [Trait("Level", "L0")] |
| 72 | + [Trait("Category", "Worker")] |
| 73 | + public void CreateStepSummaryCommand_FileNotFound() |
| 74 | + { |
| 75 | + using (var hostContext = Setup()) |
| 76 | + { |
| 77 | + var stepSummaryFile = Path.Combine(_rootDirectory, "file-not-found"); |
| 78 | + |
| 79 | + _createStepCommand.ProcessCommand(_executionContext.Object, stepSummaryFile, null); |
| 80 | + |
| 81 | + _executionContext.Verify(e => e.QueueAttachFile(ChecksAttachmentType.StepSummary, It.IsAny<string>(), It.IsAny<string>()), Times.Never()); |
| 82 | + Assert.Equal(0, _issues.Count); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + [Fact] |
| 87 | + [Trait("Level", "L0")] |
| 88 | + [Trait("Category", "Worker")] |
| 89 | + public void CreateStepSummaryCommand_EmptyFile() |
| 90 | + { |
| 91 | + using (var hostContext = Setup()) |
| 92 | + { |
| 93 | + var stepSummaryFile = Path.Combine(_rootDirectory, "empty-file"); |
| 94 | + File.Create(stepSummaryFile).Dispose(); |
| 95 | + |
| 96 | + _createStepCommand.ProcessCommand(_executionContext.Object, stepSummaryFile, null); |
| 97 | + |
| 98 | + _executionContext.Verify(e => e.QueueAttachFile(ChecksAttachmentType.StepSummary, It.IsAny<string>(), It.IsAny<string>()), Times.Never()); |
| 99 | + Assert.Equal(0, _issues.Count); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + [Fact] |
| 104 | + [Trait("Level", "L0")] |
| 105 | + [Trait("Category", "Worker")] |
| 106 | + public void CreateStepSummaryCommand_LargeFile() |
| 107 | + { |
| 108 | + using (var hostContext = Setup()) |
| 109 | + { |
| 110 | + var stepSummaryFile = Path.Combine(_rootDirectory, "empty-file"); |
| 111 | + File.WriteAllBytes(stepSummaryFile, new byte[128 * 1024 + 1]); |
| 112 | + |
| 113 | + _createStepCommand.ProcessCommand(_executionContext.Object, stepSummaryFile, null); |
| 114 | + |
| 115 | + _executionContext.Verify(e => e.QueueAttachFile(ChecksAttachmentType.StepSummary, It.IsAny<string>(), It.IsAny<string>()), Times.Never()); |
| 116 | + Assert.Equal(1, _issues.Count); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + [Fact] |
| 121 | + [Trait("Level", "L0")] |
| 122 | + [Trait("Category", "Worker")] |
| 123 | + public void CreateStepSummaryCommand_Simple() |
| 124 | + { |
| 125 | + using (var hostContext = Setup()) |
| 126 | + { |
| 127 | + var stepSummaryFile = Path.Combine(_rootDirectory, "simple"); |
| 128 | + var content = new List<string> |
| 129 | + { |
| 130 | + "# This is some markdown content", |
| 131 | + "", |
| 132 | + "## This is more markdown content", |
| 133 | + }; |
| 134 | + WriteContent(stepSummaryFile, content); |
| 135 | + |
| 136 | + _createStepCommand.ProcessCommand(_executionContext.Object, stepSummaryFile, null); |
| 137 | + |
| 138 | + _executionContext.Verify(e => e.QueueAttachFile(ChecksAttachmentType.StepSummary, _executionContext.Object.Id.ToString(), stepSummaryFile + "-scrubbed"), Times.Once()); |
| 139 | + Assert.Equal(0, _issues.Count); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + [Fact] |
| 144 | + [Trait("Level", "L0")] |
| 145 | + [Trait("Category", "Worker")] |
| 146 | + public void CreateStepSummaryCommand_ScrubSecrets() |
| 147 | + { |
| 148 | + using (var hostContext = Setup()) |
| 149 | + { |
| 150 | + // configure secretmasker to actually mask secrets |
| 151 | + hostContext.SecretMasker.AddRegex("Password=.*"); |
| 152 | + hostContext.SecretMasker.AddRegex("ghs_.*"); |
| 153 | + |
| 154 | + var stepSummaryFile = Path.Combine(_rootDirectory, "simple"); |
| 155 | + var scrubbedFile = stepSummaryFile + "-scrubbed"; |
| 156 | + var content = new List<string> |
| 157 | + { |
| 158 | + "# Password=ThisIsMySecretPassword!", |
| 159 | + "", |
| 160 | + "# GITHUB_TOKEN ghs_verysecuretoken", |
| 161 | + }; |
| 162 | + WriteContent(stepSummaryFile, content); |
| 163 | + |
| 164 | + _createStepCommand.ProcessCommand(_executionContext.Object, stepSummaryFile, null); |
| 165 | + |
| 166 | + var scrubbedFileContents = File.ReadAllText(scrubbedFile); |
| 167 | + Assert.DoesNotContain("ThisIsMySecretPassword!", scrubbedFileContents); |
| 168 | + Assert.DoesNotContain("ghs_verysecuretoken", scrubbedFileContents); |
| 169 | + |
| 170 | + _executionContext.Verify(e => e.QueueAttachFile(ChecksAttachmentType.StepSummary, _executionContext.Object.Id.ToString(), scrubbedFile), Times.Once()); |
| 171 | + Assert.Equal(0, _issues.Count); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + private void WriteContent( |
| 176 | + string path, |
| 177 | + List<string> content, |
| 178 | + string newline = null) |
| 179 | + { |
| 180 | + if (string.IsNullOrEmpty(newline)) |
| 181 | + { |
| 182 | + newline = Environment.NewLine; |
| 183 | + } |
| 184 | + |
| 185 | + var encoding = new UTF8Encoding(true); // Emit BOM |
| 186 | + var contentStr = string.Join(newline, content); |
| 187 | + File.WriteAllText(path, contentStr, encoding); |
| 188 | + } |
| 189 | + |
| 190 | + private TestHostContext Setup([CallerMemberName] string name = "", string featureFlagState = "true") |
| 191 | + { |
| 192 | + _issues = new List<Tuple<DTWebApi.Issue, string>>(); |
| 193 | + |
| 194 | + var hostContext = new TestHostContext(this, name); |
| 195 | + |
| 196 | + // Trace |
| 197 | + _trace = hostContext.GetTrace(); |
| 198 | + |
| 199 | + _variables = new Variables(hostContext, new Dictionary<string, VariableValue> |
| 200 | + { |
| 201 | + { "MySecretName", new VariableValue("My secret value", true) }, |
| 202 | + { "DistributedTask.UploadStepSummary", featureFlagState }, |
| 203 | + }); |
| 204 | + |
| 205 | + // Directory for test data |
| 206 | + var workDirectory = hostContext.GetDirectory(WellKnownDirectory.Work); |
| 207 | + ArgUtil.NotNullOrEmpty(workDirectory, nameof(workDirectory)); |
| 208 | + Directory.CreateDirectory(workDirectory); |
| 209 | + _rootDirectory = Path.Combine(workDirectory, nameof(CreateStepSummaryCommandL0)); |
| 210 | + Directory.CreateDirectory(_rootDirectory); |
| 211 | + |
| 212 | + // Execution context |
| 213 | + _executionContext = new Mock<IExecutionContext>(); |
| 214 | + _executionContext.Setup(x => x.Global) |
| 215 | + .Returns(new GlobalContext |
| 216 | + { |
| 217 | + EnvironmentVariables = new Dictionary<string, string>(VarUtil.EnvironmentVariableKeyComparer), |
| 218 | + WriteDebug = true, |
| 219 | + Variables = _variables, |
| 220 | + }); |
| 221 | + _executionContext.Setup(x => x.AddIssue(It.IsAny<DTWebApi.Issue>(), It.IsAny<string>())) |
| 222 | + .Callback((DTWebApi.Issue issue, string logMessage) => |
| 223 | + { |
| 224 | + _issues.Add(new Tuple<DTWebApi.Issue, string>(issue, logMessage)); |
| 225 | + var message = !string.IsNullOrEmpty(logMessage) ? logMessage : issue.Message; |
| 226 | + _trace.Info($"Issue '{issue.Type}': {message}"); |
| 227 | + }); |
| 228 | + _executionContext.Setup(x => x.Write(It.IsAny<string>(), It.IsAny<string>())) |
| 229 | + .Callback((string tag, string message) => |
| 230 | + { |
| 231 | + _trace.Info($"{tag}{message}"); |
| 232 | + }); |
| 233 | + |
| 234 | + //CreateStepSummaryCommand |
| 235 | + _createStepCommand = new CreateStepSummaryCommand(); |
| 236 | + _createStepCommand.Initialize(hostContext); |
| 237 | + |
| 238 | + return hostContext; |
| 239 | + } |
| 240 | + } |
| 241 | +} |
0 commit comments