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 pathIndexTest.java
More file actions
745 lines (647 loc) · 26.9 KB
/
IndexTest.java
File metadata and controls
745 lines (647 loc) · 26.9 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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
/*
* 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.AssertionFailedError;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.io.*;
import java.net.Socket;
public class IndexTest extends SDKTestCase {
private String indexName;
private Index index;
@Before
@Override
public void setUp() throws Exception {
super.setUp();
indexName = createTemporaryName();
index = service.getIndexes().create(indexName);
assertEventuallyTrue(new EventuallyTrueBehavior() {
@Override
public boolean predicate() {
return service.getIndexes().containsKey(indexName);
}
});
}
@After
@Override
public void tearDown() throws Exception {
if (service.versionIsAtLeast("5.0.0")) {
if (service.getIndexes().containsKey(indexName)) {
index.remove();
}
} else {
// Can't delete indexes via the REST API. Just let them build up.
}
super.tearDown();
}
@Test
public void testDeletion() {
if (service.versionIsEarlierThan("5.0.0")) {
// Can't delete indexes via the REST API.
return;
}
Assert.assertTrue(service.getIndexes().containsKey(indexName));
index.remove();
assertEventuallyTrue(new EventuallyTrueBehavior() {
@Override
public boolean predicate() {
return !service.getIndexes().containsKey(indexName);
}
});
}
@Test
public void testDeletionFromCollection() {
if (service.versionIsEarlierThan("5.0.0")) {
// Can't delete indexes via the REST API.
return;
}
Assert.assertTrue(service.getIndexes().containsKey(indexName));
service.getIndexes().remove(indexName);
assertEventuallyTrue(new EventuallyTrueBehavior() {
@Override
public boolean predicate() {
return !service.getIndexes().containsKey(indexName);
}
});
}
@Test
public void testAttachWith() throws IOException {
final int originalEventCount = index.getTotalEventCount();
index.attachWith(new ReceiverBehavior() {
public void run(OutputStream stream) throws IOException {
String s = createTimestamp() + " Boris the mad baboon!\r\n";
stream.write(s.getBytes("UTF-8"));
}
});
assertEventuallyTrue(new EventuallyTrueBehavior() {
{ tries = 60; }
@Override
public boolean predicate() {
index.refresh();
return index.getTotalEventCount() == originalEventCount + 1;
}
});
}
@Test
@SuppressWarnings("deprecation")
public void testIndexGettersThrowNoErrors() {
index.getAssureUTF8();
index.getBlockSignatureDatabase();
index.getBlockSignSize();
index.getBloomfilterTotalSizeKB();
index.getColdPath();
index.getColdPathExpanded();
index.getColdToFrozenDir();
index.getColdToFrozenScript();
index.getCompressRawdata();
index.getCurrentDBSizeMB();
index.getDefaultDatabase();
index.getEnableRealtimeSearch();
index.getFrozenTimePeriodInSecs();
index.getHomePath();
index.getHomePathExpanded();
index.getIndexThreads();
index.getLastInitTime();
index.getMaxBloomBackfillBucketAge();
index.getMaxConcurrentOptimizes();
index.getMaxDataSize();
index.getMaxHotBuckets();
index.getMaxHotIdleSecs();
index.getMaxHotSpanSecs();
index.getMaxMemMB();
index.getMaxMetaEntries();
index.getMaxRunningProcessGroups();
index.getMaxTime();
index.getMaxTotalDataSizeMB();
index.getMaxWarmDBCount();
index.getMemPoolMB();
index.getMinRawFileSyncSecs();
index.getMinTime();
index.getNumBloomfilters();
index.getNumHotBuckets();
index.getNumWarmBuckets();
index.getPartialServiceMetaPeriod();
index.getQuarantineFutureSecs();
index.getQuarantinePastSecs();
index.getRawChunkSizeBytes();
index.getRotatePeriodInSecs();
index.getServiceMetaPeriod();
index.getSuppressBannerList();
index.getSync();
index.getSyncMeta();
index.getThawedPath();
index.getThawedPathExpanded();
index.getThrottleCheckPeriod();
index.getTotalEventCount();
index.isDisabled();
index.isInternal();
// Fields only available from 5.0 on.
if (service.versionIsAtLeast("5.0.0")) {
index.getBucketRebuildMemoryHint();
index.getMaxTimeUnreplicatedNoAcks();
index.getMaxTimeUnreplicatedWithAcks();
}
}
@Test
public void testSetters() {
int newBlockSignSize = index.getBlockSignSize() + 1;
index.setBlockSignSize(newBlockSignSize);
int newFrozenTimePeriodInSecs = index.getFrozenTimePeriodInSecs()+1;
index.setFrozenTimePeriodInSecs(newFrozenTimePeriodInSecs);
int newMaxConcurrentOptimizes = index.getMaxConcurrentOptimizes()+1;
index.setMaxConcurrentOptimizes(newMaxConcurrentOptimizes);
String newMaxDataSize = "auto";
index.setMaxDataSize(newMaxDataSize);
int newMaxHotBuckets = index.getMaxHotBuckets()+1;
index.setMaxHotBuckets(newMaxHotBuckets);
int newMaxHotIdleSecs = index.getMaxHotIdleSecs()+1;
index.setMaxHotIdleSecs(newMaxHotIdleSecs);
int newMaxMemMB = index.getMaxMemMB()+1;
index.setMaxMemMB(newMaxMemMB);
int newMaxMetaEntries = index.getMaxMetaEntries()+1;
index.setMaxMetaEntries(newMaxMetaEntries);
int newMaxTotalDataSizeMB = index.getMaxTotalDataSizeMB()+1;
index.setMaxTotalDataSizeMB(newMaxTotalDataSizeMB);
int newMaxWarmDBCount = index.getMaxWarmDBCount()+1;
index.setMaxWarmDBCount(newMaxWarmDBCount);
String newMinRawFileSyncSecs = "disable";
index.setMinRawFileSyncSecs(newMinRawFileSyncSecs);
int newPartialServiceMetaPeriod = index.getPartialServiceMetaPeriod()+1;
index.setPartialServiceMetaPeriod(newPartialServiceMetaPeriod);
int newQuarantineFutureSecs = index.getQuarantineFutureSecs()+1;
index.setQuarantineFutureSecs(newQuarantineFutureSecs);
int newQuarantinePastSecs = index.getQuarantinePastSecs()+1;
index.setQuarantinePastSecs(newQuarantinePastSecs);
int newRawChunkSizeBytes = index.getRawChunkSizeBytes()+1;
index.setRawChunkSizeBytes(newRawChunkSizeBytes);
int newRotatePeriodInSecs = index.getRotatePeriodInSecs()+1;
index.setRotatePeriodInSecs(newRotatePeriodInSecs);
int newServiceMetaPeriod = index.getServiceMetaPeriod()+1;
index.setServiceMetaPeriod(newServiceMetaPeriod);
boolean newSyncMeta = !index.getSyncMeta();
index.setSyncMeta(newSyncMeta);
int newThrottleCheckPeriod = index.getThrottleCheckPeriod()+1;
index.setThrottleCheckPeriod(newThrottleCheckPeriod);
String coldToFrozenDir = index.getColdToFrozenDir();
if (service.getInfo().getOsName().equals("Windows")) {
index.setColdToFrozenDir("C:\\frozenDir\\" + index.getName());
} else {
index.setColdToFrozenDir("/tmp/foobar" + index.getName());
}
boolean newEnableOnlineBucketRepair = false;
String newMaxBloomBackfillBucketAge = null;
if (service.versionIsAtLeast("4.3")) {
newEnableOnlineBucketRepair = !index.getEnableOnlineBucketRepair();
index.setEnableOnlineBucketRepair(newEnableOnlineBucketRepair);
newMaxBloomBackfillBucketAge = "20d";
index.setMaxBloomBackfillBucketAge(newMaxBloomBackfillBucketAge);
}
String newBucketRebuildMemoryHint = null;
int newMaxTimeUnreplicatedNoAcks = -1;
int newMaxTimeUnreplicatedWithAcks = -1;
if (service.versionIsAtLeast("5.0")) {
newBucketRebuildMemoryHint = "auto";
index.setBucketRebuildMemoryHint(newBucketRebuildMemoryHint);
newMaxTimeUnreplicatedNoAcks = 300;
index.setMaxTimeUnreplicatedNoAcks(newMaxTimeUnreplicatedNoAcks);
newMaxTimeUnreplicatedWithAcks = 60;
index.setMaxTimeUnreplicatedWithAcks(newMaxTimeUnreplicatedWithAcks);
}
index.update();
index.refresh();
Assert.assertEquals(newBlockSignSize, index.getBlockSignSize());
Assert.assertEquals(newFrozenTimePeriodInSecs, index.getFrozenTimePeriodInSecs());
Assert.assertEquals(newMaxConcurrentOptimizes, index.getMaxConcurrentOptimizes());
Assert.assertEquals(newMaxDataSize, index.getMaxDataSize());
Assert.assertEquals(newMaxHotBuckets, index.getMaxHotBuckets());
Assert.assertEquals(newMaxHotIdleSecs, index.getMaxHotIdleSecs());
Assert.assertEquals(newMaxMemMB, index.getMaxMemMB());
Assert.assertEquals(newMaxMetaEntries, index.getMaxMetaEntries());
Assert.assertEquals(newMaxTotalDataSizeMB, index.getMaxTotalDataSizeMB());
Assert.assertEquals(newMaxWarmDBCount, index.getMaxWarmDBCount());
Assert.assertEquals(newMinRawFileSyncSecs, index.getMinRawFileSyncSecs());
Assert.assertEquals(newPartialServiceMetaPeriod, index.getPartialServiceMetaPeriod());
Assert.assertEquals(newQuarantineFutureSecs, index.getQuarantineFutureSecs());
Assert.assertEquals(newQuarantinePastSecs, index.getQuarantinePastSecs());
Assert.assertEquals(newRawChunkSizeBytes, index.getRawChunkSizeBytes());
Assert.assertEquals(newRotatePeriodInSecs, index.getRotatePeriodInSecs());
Assert.assertEquals(newServiceMetaPeriod, index.getServiceMetaPeriod());
Assert.assertEquals(newSyncMeta, index.getSyncMeta());
Assert.assertEquals(newThrottleCheckPeriod, index.getThrottleCheckPeriod());
if (service.getInfo().getOsName().equals("Windows")) {
Assert.assertEquals("C:\\frozenDir\\" + index.getName(), index.getColdToFrozenDir());
} else {
Assert.assertEquals("/tmp/foobar" + index.getName(), index.getColdToFrozenDir());
}
if (service.versionIsAtLeast("4.3")) {
Assert.assertEquals(
newEnableOnlineBucketRepair,
index.getEnableOnlineBucketRepair()
);
Assert.assertEquals(
newMaxBloomBackfillBucketAge,
index.getMaxBloomBackfillBucketAge()
);
}
if (service.versionIsAtLeast("5.0")) {
Assert.assertEquals(
newBucketRebuildMemoryHint,
index.getBucketRebuildMemoryHint()
);
Assert.assertEquals(
newMaxTimeUnreplicatedNoAcks,
index.getMaxTimeUnreplicatedNoAcks()
);
Assert.assertEquals(
newMaxTimeUnreplicatedWithAcks,
index.getMaxTimeUnreplicatedWithAcks()
);
}
index.setColdToFrozenDir(coldToFrozenDir == null ? "" : coldToFrozenDir);
index.update();
String coldToFrozenScript = index.getColdToFrozenScript();
index.setColdToFrozenScript("/bin/sh");
index.update();
Assert.assertEquals("/bin/sh", index.getColdToFrozenScript());
index.setColdToFrozenScript(coldToFrozenScript == null ? "" : coldToFrozenScript);
index.update();
//index.setColdToFrozenScript(coldToFrozenScript);
if (restartRequired()) {
splunkRestart();
}
}
@Test
public void testEnable() {
Assert.assertFalse(index.isDisabled());
// Force the index to be disabled
index.disable();
assertEventuallyTrue(new EventuallyTrueBehavior() {
@Override public boolean predicate() {
index.refresh();
return index.isDisabled();
}
});
// Disabling an index before Splunk 6 puts Splunk into a weird state that actually
// requires a restart to get out of.
if (service.versionIsEarlierThan("6.0.0")) {
splunkRestart();
}
// And then enable it
index.enable();
assertEventuallyTrue(new EventuallyTrueBehavior() {
@Override public boolean predicate() {
index.refresh();
return !index.isDisabled();
}
});
}
@Test
public void testSubmitOne() throws Exception {
try {
tryTestSubmitOne();
} catch (AssertionFailedError e) {
if (e.getMessage().contains("Test timed out before true.") &&
restartRequired()) {
System.out.println(
"WARNING: Splunk indicated restart required while " +
"running a test. Trying to recover...");
splunkRestart();
tryTestSubmitOne();
} else {
throw e;
}
}
}
private void tryTestSubmitOne() {
Assert.assertTrue(getResultCountOfIndex() == 0);
Assert.assertTrue(index.getTotalEventCount() == 0);
index.submit(createTimestamp() + " This is a test of the emergency broadcasting system.");
assertEventuallyTrue(new EventuallyTrueBehavior() {
@Override
public boolean predicate() {
return getResultCountOfIndex() == 1;
}
});
assertEventuallyTrue(new EventuallyTrueBehavior() {
@Override
public boolean predicate() {
index.refresh();
return index.getTotalEventCount() == 1;
}
});
}
@Test
public void testSubmitOneArgs() throws Exception {
try {
tryTestSubmitOneArgs();
} catch (AssertionFailedError e) {
if (e.getMessage().contains("Test timed out before true.") &&
restartRequired()) {
System.out.println(
"WARNING: Splunk indicated restart required while " +
"running a test. Trying to recover...");
splunkRestart();
tryTestSubmitOne();
} else {
throw e;
}
}
}
private void tryTestSubmitOneArgs() {
Assert.assertTrue(getResultCountOfIndex() == 0);
Assert.assertTrue(index.getTotalEventCount() == 0);
Args args = Args.create("sourcetype", "mysourcetype");
index.submit(args, createTimestamp() + " This is a test of the emergency broadcasting system.");
assertEventuallyTrue(new EventuallyTrueBehavior() {
@Override
public boolean predicate() {
return getResultCountOfIndex() == 1;
}
});
assertEventuallyTrue(new EventuallyTrueBehavior() {
@Override
public boolean predicate() {
index.refresh();
return index.getTotalEventCount() == 1;
}
});
}
@Test
public void testSubmitOneInEachCall() {
Assert.assertTrue(getResultCountOfIndex() == 0);
Assert.assertTrue(index.getTotalEventCount() == 0);
index.submit(createTimestamp() + " Hello world!\u0150");
index.submit(createTimestamp() + " Goodbye world!\u0150");
assertEventuallyTrue(new EventuallyTrueBehavior() {
@Override
public boolean predicate() {
return getResultCountOfIndex() == 2;
}
});
assertEventuallyTrue(new EventuallyTrueBehavior() {
@Override
public boolean predicate() {
index.refresh();
// Some versions of Splunk only increase event count by 1.
// Event count should never go up by more than the result count.
int tec = index.getTotalEventCount();
return (1 <= tec) && (tec <= 2);
}
});
}
@Test
public void testSubmitMultipleInOneCall() {
Assert.assertTrue(getResultCountOfIndex() == 0);
Assert.assertTrue(index.getTotalEventCount() == 0);
index.submit(
createTimestamp() + " Hello world!\u0150" + "\r\n" +
createTimestamp() + " Goodbye world!\u0150");
assertEventuallyTrue(new EventuallyTrueBehavior() {
@Override
public boolean predicate() {
return getResultCountOfIndex() == 2;
}
});
assertEventuallyTrue(new EventuallyTrueBehavior() {
@Override
public boolean predicate() {
index.refresh();
// Some versions of Splunk only increase event count by 1.
// Event count should never go up by more than the result count.
int tec = index.getTotalEventCount();
return (1 <= tec) && (tec <= 2);
}
});
}
@Test
public void testAttach() throws IOException {
Assert.assertTrue(getResultCountOfIndex() == 0);
Assert.assertTrue(index.getTotalEventCount() == 0);
Socket socket = index.attach();
OutputStream ostream = socket.getOutputStream();
Writer out = new OutputStreamWriter(ostream, "UTF-8");
out.write(createTimestamp() + " Hello world!\u0150\r\n");
out.write(createTimestamp() + " Goodbye world!\u0150\r\n");
out.flush();
socket.close();
index.refresh();
assertEventuallyTrue(new EventuallyTrueBehavior() {
{ tries = 60; }
@Override
public boolean predicate() {
index.refresh();
return getResultCountOfIndex() == 2;
}
});
assertEventuallyTrue(new EventuallyTrueBehavior() {
@Override
public boolean predicate() {
index.refresh();
// Some versions of Splunk only increase event count by 1.
// Event count should never go up by more than the result count.
int tec = index.getTotalEventCount();
return (1 <= tec) && (tec <= 2);
}
});
}
@Test
public void testAttachArgs() throws IOException {
Assert.assertTrue(getResultCountOfIndex() == 0);
Assert.assertTrue(index.getTotalEventCount() == 0);
Args args = Args.create("sourcetype", "mysourcetype");
Socket socket = index.attach(args);
OutputStream ostream = socket.getOutputStream();
Writer out = new OutputStreamWriter(ostream, "UTF-8");
out.write(createTimestamp() + " Hello world!\u0150\r\n");
out.write(createTimestamp() + " Goodbye world!\u0150\r\n");
out.write(createTimestamp() + " Goodbye world again!\u0150\r\n");
out.flush();
socket.close();
assertEventuallyTrue(new EventuallyTrueBehavior() {
{ tries = 60; }
@Override
public boolean predicate() {
return getResultCountOfIndex() == 3;
}
});
assertEventuallyTrue(new EventuallyTrueBehavior() {
{ tries = 60; }
@Override
public boolean predicate() {
index.refresh();
// Some versions of Splunk only increase event count by 1.
// Event count should never go up by more than the result count.
int tec = index.getTotalEventCount();
return (1 <= tec) && (tec <= 3);
}
});
}
@Test
public void testUploadArgs() throws Exception {
if (!hasTestData()) {
System.out.println("WARNING: sdk-app-collection not installed in Splunk; skipping test.");
return;
}
installApplicationFromTestData("file_to_upload");
Assert.assertTrue(getResultCountOfIndex() == 0);
Assert.assertTrue(index.getTotalEventCount() == 0);
String fileToUpload = joinServerPath(new String[] {
service.getSettings().getSplunkHome(),
"etc", "apps", "file_to_upload", "log.txt"});
Args args = new Args();
args.add("sourcetype", "log");
args.add("host", "IndexTest");
args.add("rename-source", "IndexTestSrc");
index.upload(fileToUpload, args);
assertEventuallyTrue(new EventuallyTrueBehavior() {
@Override
public boolean predicate() {
Service con = index.getService();
Job search = con.search("search index=" + index.getTitle() + " sourcetype=log host=IndexTest source=IndexTestSrc");
return getResultCountOfIndex() == 4 && search.getEventCount() == 4;
}
});
assertEventuallyTrue(new EventuallyTrueBehavior() {
@Override
public boolean predicate() {
index.refresh();
// Some versions of Splunk only increase event count by 1.
// Event count should never go up by more than the result count.
int tec = index.getTotalEventCount();
return (1 <= tec) && (tec <= 4);
}
});
}
@Test
public void testUploadArgsFailure() throws Exception{
if (!hasTestData()) {
System.out.println("WARNING: sdk-app-collection not installed in Splunk; skipping test.");
return;
}
installApplicationFromTestData("file_to_upload");
Assert.assertTrue(getResultCountOfIndex() == 0);
Assert.assertTrue(index.getTotalEventCount() == 0);
String fileToUpload = joinServerPath(new String[] {
service.getSettings().getSplunkHome(),
"etc", "apps", "file_to_upload", "log.txt"});
Args args = new Args();
args.add("sourcetype", "log");
args.add("host", "IndexTest");
args.add("index", index.getTitle());
args.add("rename-source", "IndexTestSrc");
// The index argument cannot be passed into the upload function.
try{
index.upload(fileToUpload, args);
Assert.fail("Uploading to an index with an index argument? No need for redundency!");
}
catch(Exception e){
Assert.assertEquals(e.getMessage(), "The 'index' parameter cannot be passed to an index's oneshot upload.");
}
}
@Test
public void testUpload() throws Exception {
if (!hasTestData()) {
System.out.println("WARNING: sdk-app-collection not installed in Splunk; skipping test.");
return;
}
installApplicationFromTestData("file_to_upload");
Assert.assertTrue(getResultCountOfIndex() == 0);
Assert.assertTrue(index.getTotalEventCount() == 0);
String fileToUpload = joinServerPath(new String[] {
service.getSettings().getSplunkHome(),
"etc", "apps", "file_to_upload", "log.txt"});
index.upload(fileToUpload);
assertEventuallyTrue(new EventuallyTrueBehavior() {
@Override
public boolean predicate() {
return getResultCountOfIndex() == 4;
}
});
assertEventuallyTrue(new EventuallyTrueBehavior() {
@Override
public boolean predicate() {
index.refresh();
// Some versions of Splunk only increase event count by 1.
// Event count should never go up by more than the result count.
int tec = index.getTotalEventCount();
return (1 <= tec) && (tec <= 4);
}
});
}
@Test
public void testSubmitAndClean() throws InterruptedException {
try {
tryTestSubmitAndClean();
} catch (SplunkException e) {
if (e.getCode() == SplunkException.TIMEOUT) {
// Due to flakiness of the underlying implementation,
// this index clean method doesn't always work on a "dirty"
// Splunk instance. Try again on a "clean" instance.
System.out.println(
"WARNING: Index clean timed out. Trying again on a " +
"freshly restarted Splunk instance...");
uncheckedSplunkRestart();
tryTestSubmitAndClean();
} else {
throw e;
}
}
}
private void tryTestSubmitAndClean() throws InterruptedException {
Assert.assertTrue(getResultCountOfIndex() == 0);
// Make sure the index is not empty.
index.submit("Hello world");
assertEventuallyTrue(new EventuallyTrueBehavior() {
{
tries = 50;
}
@Override
public boolean predicate() {
return getResultCountOfIndex() == 1;
}
});
// Clean the index and make sure it's empty.
// NOTE: Average time for this is 65s (!!!). Have seen 110+.
index.clean(150);
Assert.assertTrue(getResultCountOfIndex() == 0);
}
@Test
public void testUpdateNameShouldFail() {
try {
index.update(new Args("name", createTemporaryName()));
Assert.fail("Expected IllegalStateException.");
}
catch (IllegalStateException e) {
// Good
}
}
// === Utility ===
private int getResultCountOfIndex() {
InputStream results = service.oneshotSearch("search index=" + indexName);
try {
ResultsReaderXml resultsReader = new ResultsReaderXml(results);
int numEvents = 0;
while (resultsReader.getNextEvent() != null) {
numEvents++;
}
return numEvents;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}