-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
SystemCommandTaskletIntegrationTests.java
345 lines (290 loc) · 11.3 KB
/
SystemCommandTaskletIntegrationTests.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
* Copyright 2008-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.core.step.tasklet;
import java.io.File;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobInterruptedException;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.scope.context.StepContext;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.util.Assert;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Tests for {@link SystemCommandTasklet}.
*/
@ExtendWith(MockitoExtension.class)
class SystemCommandTaskletIntegrationTests {
private static final Log log = LogFactory.getLog(SystemCommandTaskletIntegrationTests.class);
private SystemCommandTasklet tasklet;
private final StepExecution stepExecution = new StepExecution("systemCommandStep",
new JobExecution(new JobInstance(1L, "systemCommandJob"), 1L, new JobParameters()));
@Mock
private JobExplorer jobExplorer;
@BeforeEach
void setUp() throws Exception {
initializeTasklet();
tasklet.afterPropertiesSet();
tasklet.beforeStep(stepExecution);
}
private void initializeTasklet() {
tasklet = new SystemCommandTasklet();
tasklet.setEnvironmentParams(null); // inherit from parent process
tasklet.setWorkingDirectory(null); // inherit from parent process
tasklet.setSystemProcessExitCodeMapper(new SimpleSystemProcessExitCodeMapper());
tasklet.setTimeout(5000); // long enough timeout
tasklet.setTerminationCheckInterval(500);
tasklet.setCommand("invalid command, change value for successful execution");
tasklet.setInterruptOnCancel(true);
tasklet.setTaskExecutor(new SimpleAsyncTaskExecutor());
}
/*
* Regular usage scenario - successful execution of system command.
*/
@Test
void testExecute() throws Exception {
String command = getJavaCommand() + " --version";
tasklet.setCommand(command);
tasklet.afterPropertiesSet();
log.info("Executing command: " + command);
RepeatStatus exitStatus = tasklet.execute(stepExecution.createStepContribution(), null);
assertEquals(RepeatStatus.FINISHED, exitStatus);
}
/*
* Failed execution scenario - error exit code returned by system command.
*/
@Test
void testExecuteFailure() throws Exception {
String command = getJavaCommand() + " org.springframework.batch.sample.tasklet.UnknownClass";
tasklet.setCommand(command);
tasklet.setTimeout(200L);
tasklet.afterPropertiesSet();
log.info("Executing command: " + command);
try {
StepContribution contribution = stepExecution.createStepContribution();
RepeatStatus exitStatus = tasklet.execute(contribution, null);
assertEquals(RepeatStatus.FINISHED, exitStatus);
assertEquals(ExitStatus.FAILED, contribution.getExitStatus());
}
catch (RuntimeException e) {
// on some platforms the system call does not return
assertEquals("Execution of system command did not finish within the timeout", e.getMessage());
}
}
/*
* The attempt to execute the system command results in exception
*/
@Test
void testExecuteException() throws Exception {
String command = "non-sense-that-should-cause-exception-when-attempted-to-execute";
tasklet.setCommand(command);
tasklet.afterPropertiesSet();
assertThrows(java.util.concurrent.ExecutionException.class, () -> tasklet.execute(null, null));
}
/*
* Failed execution scenario - execution time exceeds timeout.
*/
@Test
void testExecuteTimeout() throws Exception {
String command = isRunningOnWindows() ? "ping 127.0.0.1" : "sleep 3";
tasklet.setCommand(command);
tasklet.setTimeout(10);
tasklet.afterPropertiesSet();
log.info("Executing command: " + command);
Exception exception = assertThrows(SystemCommandException.class, () -> tasklet.execute(null, null));
assertTrue(exception.getMessage().contains("did not finish within the timeout"));
}
/*
* Job interrupted scenario.
*/
@Test
void testInterruption() throws Exception {
String command = isRunningOnWindows() ? "ping 127.0.0.1" : "sleep 5";
tasklet.setCommand(command);
tasklet.setTerminationCheckInterval(10);
tasklet.afterPropertiesSet();
stepExecution.setTerminateOnly();
Exception exception = assertThrows(JobInterruptedException.class, () -> tasklet.execute(null, null));
String message = exception.getMessage();
System.out.println(message);
assertTrue(message.contains("Job interrupted while executing system command"));
assertTrue(message.contains(command));
}
/*
* Command Runner is required to be set.
*/
@Test
public void testCommandRunnerNotSet() throws Exception {
SystemCommandTasklet.presetCommandRunner(null);
try {
tasklet.afterPropertiesSet();
fail();
}
catch (IllegalArgumentException e) {
// expected
} finally {
SystemCommandTasklet.presetCommandRunner(new JvmCommandRunner());
}
tasklet.setCommandRunner(null);
try {
tasklet.afterPropertiesSet();
fail();
}
catch (IllegalArgumentException e) {
// expected
} finally {
SystemCommandTasklet.presetCommandRunner(new JvmCommandRunner());
}
}
/*
* Command property value is required to be set.
*/
@Test
void testCommandNotSet() {
tasklet.setCommand(null);
assertThrows(IllegalArgumentException.class, tasklet::afterPropertiesSet);
tasklet.setCommand("");
assertThrows(IllegalArgumentException.class, tasklet::afterPropertiesSet);
}
/*
* Timeout must be set to non-zero value.
*/
@Test
void testTimeoutNotSet() {
tasklet.setCommand("not-empty placeholder");
tasklet.setTimeout(0);
assertThrows(IllegalArgumentException.class, tasklet::afterPropertiesSet);
}
/*
* Working directory property must point to an existing location and it must be a
* directory
*/
@Test
void testWorkingDirectory() throws Exception {
File notExistingFile = new File("not-existing-path");
Assert.state(!notExistingFile.exists(), "not-existing-path does actually exist");
assertThrows(IllegalArgumentException.class,
() -> tasklet.setWorkingDirectory(notExistingFile.getCanonicalPath()));
File notDirectory = File.createTempFile(this.getClass().getName(), null);
Assert.state(notDirectory.exists(), "The file does not exist");
Assert.state(!notDirectory.isDirectory(), "The file is actually a directory");
assertThrows(IllegalArgumentException.class,
() -> tasklet.setWorkingDirectory(notDirectory.getCanonicalPath()));
File directory = notDirectory.getParentFile();
Assert.state(directory.exists(), "The directory does not exist");
Assert.state(directory.isDirectory(), "The directory is not a directory");
// no error expected now
tasklet.setWorkingDirectory(directory.getCanonicalPath());
}
/*
* test stopping a tasklet
*/
@Test
void testStopped() throws Exception {
initializeTasklet();
tasklet.setJobExplorer(jobExplorer);
tasklet.afterPropertiesSet();
tasklet.beforeStep(stepExecution);
JobExecution stoppedJobExecution = new JobExecution(stepExecution.getJobExecution());
stoppedJobExecution.setStatus(BatchStatus.STOPPING);
when(jobExplorer.getJobExecution(1L)).thenReturn(stepExecution.getJobExecution(),
stepExecution.getJobExecution(), stoppedJobExecution);
String command = isRunningOnWindows() ? "ping 127.0.0.1 -n 5" : "sleep 15";
tasklet.setCommand(command);
tasklet.setTerminationCheckInterval(10);
tasklet.afterPropertiesSet();
StepContribution contribution = stepExecution.createStepContribution();
StepContext stepContext = new StepContext(stepExecution);
ChunkContext chunkContext = new ChunkContext(stepContext);
tasklet.execute(contribution, chunkContext);
assertEquals(ExitStatus.STOPPED.getExitCode(), contribution.getExitStatus().getExitCode());
}
private String getJavaCommand() {
String javaHome = System.getProperty("java.home");
String fileSeparator = System.getProperty("file.separator");
StringBuilder command = new StringBuilder();
command.append(javaHome);
command.append(fileSeparator);
command.append("bin");
command.append(fileSeparator);
command.append("java");
if (isRunningOnWindows()) {
command.append(".exe");
}
return command.toString();
}
private boolean isRunningOnWindows() {
return System.getProperty("os.name").toLowerCase().contains("win");
}
@Test
public void testExecuteWithSuccessfulCommandRunnerMockExecution() throws Exception {
try {
StepContribution stepContribution = stepExecution.createStepContribution();
CommandRunner commandRunner = mock(CommandRunner.class);
Process process = mock(Process.class);
String command = "invalid command";
when(commandRunner.exec(eq(command), any(), any())).thenReturn(process);
when(process.waitFor()).thenReturn(0);
SystemCommandTasklet.presetCommandRunner(commandRunner);
tasklet.setCommand(command);
tasklet.afterPropertiesSet();
RepeatStatus exitStatus = tasklet.execute(stepContribution, null);
assertEquals(RepeatStatus.FINISHED, exitStatus);
assertEquals(ExitStatus.COMPLETED, stepContribution.getExitStatus());
} finally {
SystemCommandTasklet.presetCommandRunner(new JvmCommandRunner());
}
}
@Test
public void testExecuteWithFailedCommandRunnerMockExecution() throws Exception {
try {
StepContribution stepContribution = stepExecution.createStepContribution();
CommandRunner commandRunner = mock(CommandRunner.class);
Process process = mock(Process.class);
String command = "invalid command";
when(commandRunner.exec(eq(command), any(), any())).thenReturn(process);
when(process.waitFor()).thenReturn(1);
SystemCommandTasklet.presetCommandRunner(commandRunner);
tasklet.setCommand(command);
tasklet.afterPropertiesSet();
RepeatStatus exitStatus = tasklet.execute(stepContribution, null);
assertEquals(RepeatStatus.FINISHED, exitStatus);
assertEquals(ExitStatus.FAILED, stepContribution.getExitStatus());
} finally {
SystemCommandTasklet.presetCommandRunner(new JvmCommandRunner());
}
}
}