This repository was archived by the owner on Jul 20, 2024. It is now read-only.
forked from splunk/splunk-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSDKTestCase.java
More file actions
409 lines (356 loc) · 13.2 KB
/
SDKTestCase.java
File metadata and controls
409 lines (356 loc) · 13.2 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
* Copyright 2012 Splunk, Inc.
*
* 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
*
* http://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 com.splunk;
import junit.framework.TestCase;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* Base test case for SDK test suite.
*
* TestCase does the following actions in the various test contexts:
*
* @@Before:
* - Read ~/.splunkrc to get host, port, user, and password to connect with.
* - Create a Service object connected to the Splunk instance.
*
* @@After:
* - Logout the Service object.
*/
public abstract class SDKTestCase {
protected static final boolean WORKAROUND_KNOWN_BUGS = true;
private static final boolean VERBOSE_PORT_SCAN = false;
protected static Service service;
protected List<String> installedApps;
protected Command command;
public static String streamToString(java.io.InputStream is) {
Reader r = null;
try {
r = new InputStreamReader(is, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Your JVM does not support UTF-8!");
}
StringBuffer sb = new StringBuffer();
int ch;
try {
while ((ch = r.read()) != -1) {
sb.append((char)ch);
}
} catch (IOException ioex) {
throw new RuntimeException(ioex.getMessage());
}
return sb.toString();
}
public void connect() {
if (service != null) {
service.login(service.username, service.password);
} else {
service = Service.connect(command.opts);
}
}
@Before
public void setUp() throws Exception {
// If using Charles Proxy for debugging, uncomment these lines.
//System.setProperty("https.proxyHost", "127.0.0.1");
//System.setProperty("https.proxyPort", "8888");
command = Command.splunk();
connect();
if (restartRequired()) {
System.out.println(
"WARNING: Splunk was already in a state requiring a " +
"restart prior to running this test. Trying to recover...");
splunkRestart();
System.out.println("Restart complete.");
}
installedApps = new ArrayList<String>();
}
@After
public void tearDown() throws Exception {
if (restartRequired()) {
Assert.fail("Test left Splunk in a state that required restart.");
}
// Cannot delete applications in Splunk 4.2.
if (service.versionIsAtLeast("4.3")) {
// We delete the apps we installed for capabilities after checking
// for restarts that might be required, since deleting an app
// triggers a restart (but one that we can safely ignore).
for (String applicationName : installedApps) {
try {
service.getApplications().remove(applicationName);
} catch (HttpException e) {
// WORKAROUND (SPL-75224): Under Splunk 6.0 on Windows, deleting apps sometimes fails with
// the message "Operation completed successfully." The app is actually deleted, but it will
// cause tests to fail.
if (service.versionCompare("6.0.0") == 0 && e.getStatus() != 500) {
throw e;
}
}
clearRestartMessage();
}
}
}
// === Temporary Names ===
protected static String createTimestamp() {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd-HH:mm:ss");
return dateFormat.format(new Date());
}
protected static String createTemporaryName() {
return "delete-me-" + UUID.randomUUID().toString();
}
public static InputStream openResource(String path) {
if (path.startsWith("splunk_search:")) {
path = path.substring("splunk_search:".length());
String[] pathComponents = path.split("/");
String searchType = pathComponents[0];
String outputMode = pathComponents[1];
String search = pathComponents[2];
Args resultsArgs = new Args("output_mode", outputMode);
if (searchType.equals("blocking")) {
Job job = service.getJobs().create(
search,
new Args("exec_mode", "blocking"));
return job.getResults(resultsArgs);
}
else if (searchType.equals("oneshot")) {
return service.oneshotSearch(search, resultsArgs);
}
else {
throw new IllegalArgumentException(
"Unrecognized search type: " + searchType);
}
}
InputStream input = ResourceRoot.class.getResourceAsStream(path);
Assert.assertNotNull("Could not open " + path, input);
return input;
}
// === Asserts ===
public static abstract class EventuallyTrueBehavior {
public int tries;
public int pauseTime;
{
tries = 10;
pauseTime = 1000;
}
public String timeoutMessage = "Test timed out before true.";
public abstract boolean predicate();
}
public static boolean assertEventuallyTrue(EventuallyTrueBehavior behavior) {
int remainingTries = behavior.tries;
while (remainingTries > 0) {
boolean succeeded = behavior.predicate();
if (succeeded) {
return true;
} else {
remainingTries -= 1;
try {
Thread.sleep(behavior.pauseTime);
} catch (InterruptedException e) {}
}
}
Assert.fail(behavior.timeoutMessage);
return false;
}
// === Test Data Installation ===
public boolean hasTestData() {
String collectionName = "sdk-app-collection";
return service.getApplications().containsKey("sdk-app-collection");
}
public void installApplicationFromTestData(String applicationName) {
String collectionName = "sdk-app-collection";
if (!service.getApplications().containsKey("sdk-app-collection")) {
throw new TestDataNotInstalledException();
}
String splunkHome = service.getSettings().getSplunkHome();
// This is the filename separator sequence for splunkd, not
// the Splunk SDK. Therefore we can't just use File.separator here.
String separator;
if (splunkHome.contains("/") && splunkHome.contains("\\")) {
// Windows - allows mixed paths
separator = "\\";
} else if (splunkHome.contains("/")) {
// Unix or Mac OS X
separator = "/";
} else if (splunkHome.contains("\\")) {
// Windows
separator = "\\";
} else {
throw new RuntimeException(
"Couldn't determine what the path separator was " +
"for splunkd.");
}
String[] pathComponents = {splunkHome, "etc", "apps",
collectionName, "build", applicationName + ".tar"};
String appPath = Util.join(separator, pathComponents);
Args args = new Args();
args.put("name", appPath);
args.put("update", "1");
service.post("apps/appinstall", args);
installedApps.add(applicationName);
}
// === Restarts ===
public void clearRestartMessage() {
final MessageCollection messages = service.getMessages();
if (messages.containsKey("restart_required")) {
messages.remove("restart_required");
assertEventuallyTrue(new EventuallyTrueBehavior() {
@Override public boolean predicate() {
messages.refresh();
return !messages.containsKey("restart_required");
}
});
}
}
public boolean restartRequired() {
for (Message message : service.getMessages().values()) {
if (message.containsKey("restart_required")) {
return true;
}
}
return false;
}
private void markAsRestartRequired() {
service.getMessages().create(
"restart_required",
"Unit test called markAsRestartRequired().");
}
public void splunkRestart() {
if (!restartRequired()) {
Assert.fail("Asked to restart Splunk when no restart was required.");
}
uncheckedSplunkRestart();
}
public void uncheckedSplunkRestart() {
ResponseMessage response = service.restart();
if (response.getStatus() != 200) {
Assert.fail("Restart command failed: " + response.getContent());
}
// (This status will be cleared when Splunk actually restarts.)
markAsRestartRequired();
// Wait for splunkd to come back up fresh.
// (And update 'service'.)
assertEventuallyTrue(new EventuallyTrueBehavior() {
{
tries = 20;
pauseTime = 1000;
}
@Override
public boolean predicate() {
try {
connect();
return !restartRequired();
} catch (Exception e) {
return false;
}
}
});
}
// === Misc ===
protected static void sleep(int milliseconds) {
try {
Thread.sleep(milliseconds);
}
catch (InterruptedException e) {}
}
protected int findNextUnusedPort(int startingPort) {
int port = startingPort;
while (isPortInUse(port)) {
port++;
}
return port;
}
public boolean isPortInUse(int port) {
try {
Socket pingSocket = new Socket();
// On Windows, the firewall doesn't respond at all if you connect to an unbound port, so we need to
// take lack of a connection as an empty port. Timeout is 1000ms.
try {
pingSocket.connect(new InetSocketAddress(service.getHost(), port), 1000);
} catch (SocketTimeoutException ste) {
return false;
}
pingSocket.close();
if (VERBOSE_PORT_SCAN) {
System.out.println("IN-USE(" + port + ")");
}
return true;
} catch (IOException e) {
if (VERBOSE_PORT_SCAN) {
System.out.println("OPEN(" + port + "): " + e.getMessage());
}
return false;
}
}
protected static boolean contains(String[] array, String value) {
return Arrays.asList(array).contains(value);
}
protected static void assertEquals(String[] a1, String[] a2) {
Assert.assertArrayEquals(a1, a2);
}
protected static String locateSystemLog() {
String filename = null;
String osName = service.getInfo().getOsName();
if (osName.equals("Windows"))
filename = "C:\\Windows\\WindowsUpdate.log";
else if (osName.equals("Linux")) {
filename = "/etc/fstab";
}
else if (osName.equals("Darwin")) {
filename = "/var/log/system.log";
} else {
throw new RuntimeException("OS " + osName + " not recognized");
}
Assert.assertNotNull(filename);
return filename;
}
protected static String joinServerPath(String[] components) {
String separator;
String osName = service.getInfo().getOsName();
if (osName.equals("Windows"))
separator = "\\";
else if (osName.equals("Linux"))
separator = "/";
else if (osName.equals("Darwin")) {
separator = "/";
} else {
throw new RuntimeException("OS " + osName + " not recognized");
}
return Util.join(separator, components);
}
protected boolean firstLineIsXmlDtd(InputStream stream) {
InputStreamReader reader;
try {
reader = new InputStreamReader(stream, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new Error(e);
}
BufferedReader lineReader = new BufferedReader(reader);
try {
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>".equals(
lineReader.readLine()
);
} catch (IOException e) {
Assert.fail(e.toString());
return false;
}
}
}