/
PowershellScriptTest.java
266 lines (249 loc) · 12.4 KB
/
PowershellScriptTest.java
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
/*
* The MIT License
*
* Copyright 2017 Gabriel Loewen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.jenkinsci.plugins.durabletask;
import hudson.EnvVars;
import hudson.FilePath;
import hudson.Launcher;
import hudson.Proc;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.io.IOException;
import hudson.util.StreamTaskListener;
import java.io.ByteArrayOutputStream;
import java.io.File;
import org.apache.commons.io.output.TeeOutputStream;
import static org.hamcrest.Matchers.containsString;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Assume;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import java.util.Properties;
import java.util.*;
import org.apache.commons.io.IOUtils;
import org.junit.AssumptionViolatedException;
public class PowershellScriptTest {
@Rule public JenkinsRule j = new JenkinsRule();
private StreamTaskListener listener;
private FilePath ws;
private Launcher launcher;
private int psVersion;
@Before public void vars() throws IOException, InterruptedException {
listener = StreamTaskListener.fromStdout();
ws = j.jenkins.getRootPath().child("ws");
launcher = j.jenkins.createLauncher(listener);
Properties properties = System.getProperties();
String pathSeparator = properties.getProperty("path.separator");
String[] paths = System.getenv("PATH").split(pathSeparator);
boolean powershellExists = false;
// Note: This prevents this set of tests from running on PowerShell core unless a symlink is created that maps 'powershell' to 'pwsh' on *nix systems
String cmd = "powershell";
for (String p : paths) {
// If running on *nix then the binary does not have an extension. Check for both variants to ensure *nix and windows+cygwin are both supported.
File withoutExtension = new File(p, cmd);
File withExtension = new File(p, cmd + ".exe");
if (withoutExtension.exists() || withExtension.exists()) {
powershellExists = true;
break;
}
}
Assume.assumeTrue("This test should only run if powershell is available", powershellExists == true);
// Assume Powershell major version is at least 3
if (powershellExists == true) {
List<String> args = new ArrayList<>(Collections.singleton(cmd));
if (!launcher.isUnix()) {
args.addAll(Arrays.asList("-ExecutionPolicy", "Bypass"));
}
args.addAll(Arrays.asList("-Command", "& {Write-Output $PSVersionTable.PSVersion.Major}"));
Launcher.ProcStarter ps = launcher.launch().cmds(args).quiet(true);
ps.readStdout();
Proc proc = ps.start();
String psVersionStr = IOUtils.toString(proc.getStdout());
try {
psVersion = Integer.parseInt(psVersionStr.trim());
} catch (NumberFormatException x) {
throw new AssumptionViolatedException(psVersionStr, x);
}
Assume.assumeTrue("This test should only run if the powershell major version is at least 3", psVersion >= 3);
}
}
@Test public void explicitExit() throws Exception {
Controller c = new PowershellScript("Write-Output \"Hello, World!\"; exit 1;").launch(new EnvVars(), ws, launcher, listener);
while (c.exitStatus(ws, launcher, listener) == null) {
Thread.sleep(100);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
c.writeLog(ws, baos);
assertEquals(Integer.valueOf(1), c.exitStatus(ws, launcher));
assertThat(baos.toString(), containsString("Hello, World!"));
c.cleanup(ws);
}
@Test public void implicitExit() throws Exception {
Controller c = new PowershellScript("Write-Output \"Success!\";").launch(new EnvVars(), ws, launcher, listener);
while (c.exitStatus(ws, launcher, listener) == null) {
Thread.sleep(100);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
c.writeLog(ws, baos);
assertEquals(Integer.valueOf(0), c.exitStatus(ws, launcher));
assertThat(baos.toString(), containsString("Success!"));
c.cleanup(ws);
}
@Test public void implicitError() throws Exception {
Controller c = new PowershellScript("$ErrorActionPreference = 'Stop'; Write-Error \"Bogus error\"").launch(new EnvVars(), ws, launcher, listener);
while (c.exitStatus(ws, launcher, listener) == null) {
Thread.sleep(100);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
c.writeLog(ws, baos);
assertTrue(c.exitStatus(ws, launcher).intValue() != 0);
assertThat(baos.toString(), containsString("Bogus error"));
c.cleanup(ws);
}
@Test public void implicitErrorNegativeTest() throws Exception {
Controller c = new PowershellScript("$ErrorActionPreference = 'SilentlyContinue'; Write-Error \"Bogus error\"").launch(new EnvVars(), ws, launcher, listener);
while (c.exitStatus(ws, launcher, listener) == null) {
Thread.sleep(100);
}
assertTrue(c.exitStatus(ws, launcher).intValue() == 0);
c.cleanup(ws);
}
@Test public void explicitThrow() throws Exception {
DurableTask task = new PowershellScript("Write-Output \"Hello, World!\"; throw \"explicit error\";");
task.captureOutput();
Controller c = task.launch(new EnvVars(), ws, launcher, listener);
while (c.exitStatus(ws, launcher, listener) == null) {
Thread.sleep(100);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
c.writeLog(ws, baos);
assertTrue(c.exitStatus(ws, launcher, listener).intValue() != 0);
assertThat(baos.toString(), containsString("explicit error"));
if (launcher.isUnix()) {
assertEquals("Hello, World!\n", new String(c.getOutput(ws, launcher)));
} else {
assertEquals("Hello, World!\r\n", new String(c.getOutput(ws, launcher)));
}
c.cleanup(ws);
}
@Test public void implicitThrow() throws Exception {
DurableTask task = new PowershellScript("$ErrorActionPreference = 'Stop'; My-BogusCmdlet;");
Controller c = task.launch(new EnvVars(), ws, launcher, listener);
while (c.exitStatus(ws, launcher, listener) == null) {
Thread.sleep(100);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
c.writeLog(ws, baos);
assertTrue(c.exitStatus(ws, launcher, listener).intValue() != 0);
assertThat(baos.toString(), containsString("My-BogusCmdlet"));
c.cleanup(ws);
}
@Test public void noStdoutPollution() throws Exception {
DurableTask task = new PowershellScript("$VerbosePreference = \"Continue\"; " +
"$WarningPreference = \"Continue\"; " +
"$DebugPreference = \"Continue\"; " +
"Write-Verbose \"Hello, Verbose!\"; " +
"Write-Warning \"Hello, Warning!\"; " +
"Write-Debug \"Hello, Debug!\"; " +
"Write-Output \"Success\"");
task.captureOutput();
Controller c = task.launch(new EnvVars(), ws, launcher, listener);
while (c.exitStatus(ws, launcher, listener) == null) {
Thread.sleep(100);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
c.writeLog(ws, baos);
assertTrue(c.exitStatus(ws, launcher, listener).intValue() == 0);
assertThat(baos.toString(), containsString("Hello, Verbose!"));
assertThat(baos.toString(), containsString("Hello, Warning!"));
assertThat(baos.toString(), containsString("Hello, Debug!"));
if (launcher.isUnix()) {
assertEquals("Success\n", new String(c.getOutput(ws, launcher)));
} else {
assertEquals("Success\r\n", new String(c.getOutput(ws, launcher)));
}
c.cleanup(ws);
}
@Test public void specialStreams() throws Exception {
DurableTask task = new PowershellScript("$VerbosePreference = \"Continue\"; " +
"$WarningPreference = \"Continue\"; " +
"$DebugPreference = \"Continue\"; " +
"Write-Verbose \"Hello, Verbose!\"; " +
"Write-Warning \"Hello, Warning!\"; " +
"Write-Debug \"Hello, Debug!\";");
Controller c = task.launch(new EnvVars(), ws, launcher, listener);
while (c.exitStatus(ws, launcher, listener) == null) {
Thread.sleep(100);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
c.writeLog(ws, baos);
assertEquals(0, c.exitStatus(ws, launcher).intValue());
if (psVersion >= 5) {
assertThat(baos.toString(), containsString("VERBOSE: Hello, Verbose!"));
assertThat(baos.toString(), containsString("WARNING: Hello, Warning!"));
assertThat(baos.toString(), containsString("DEBUG: Hello, Debug!"));
} else {
assertThat(baos.toString(), containsString("Hello, Verbose!"));
assertThat(baos.toString(), containsString("Hello, Warning!"));
assertThat(baos.toString(), containsString("Hello, Debug!"));
}
c.cleanup(ws);
}
@Test public void spacesInWorkspace() throws Exception {
final FilePath newWs = new FilePath(ws, "subdirectory with spaces");
Controller c = new PowershellScript("Write-Host 'Running in a workspace with spaces in the path'").launch(new EnvVars(), newWs, launcher, listener);
while (c.exitStatus(newWs, launcher, listener) == null) {
Thread.sleep(100);
}
assertEquals(0, c.exitStatus(newWs, launcher).intValue());
c.cleanup(ws);
}
@Test public void echoEnvVar() throws Exception {
Controller c = new PowershellScript("echo envvar=$env:MYVAR").launch(new EnvVars("MYVAR", "power$hell"), ws, launcher, listener);
while (c.exitStatus(ws, launcher, listener) == null) {
Thread.sleep(100);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
c.writeLog(ws,baos);
assertEquals(0, c.exitStatus(ws, launcher, listener).intValue());
assertThat(baos.toString(), containsString("envvar=power$hell"));
c.cleanup(ws);
}
@Test public void unicodeChars() throws Exception {
Controller c = new PowershellScript("Write-Output \"Helló, Wõrld ®\";").launch(new EnvVars(), ws, launcher, listener);
while (c.exitStatus(ws, launcher, listener) == null) {
Thread.sleep(100);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
c.writeLog(ws, baos);
assertEquals(Integer.valueOf(0), c.exitStatus(ws, launcher));
String log = baos.toString("UTF-8");
assertTrue(log, log.contains("Helló, Wõrld ®"));
c.cleanup(ws);
}
}