-
Notifications
You must be signed in to change notification settings - Fork 80
/
runner_test.py
5515 lines (4830 loc) · 197 KB
/
runner_test.py
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
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# SPDX-License-Identifier: Apache-2.0
#
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.
# Modifications Copyright OpenSearch Contributors. See
# GitHub history for details.
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you 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.
import asyncio
import io
import json
import random
import unittest.mock as mock
from unittest import TestCase
import opensearchpy
import pytest
from osbenchmark import client, exceptions
from osbenchmark.worker_coordinator import runner
from tests import run_async, as_future
class BaseUnitTestContextManagerRunner:
async def __aenter__(self):
self.fp = io.StringIO("many\nlines\nin\na\nfile")
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
self.fp.close()
return False
class RegisterRunnerTests(TestCase):
def tearDown(self):
runner.remove_runner("unit_test")
@run_async
async def test_runner_function_should_be_wrapped(self):
async def runner_function(*args):
return args
runner.register_runner(operation_type="unit_test", runner=runner_function, async_runner=True)
returned_runner = runner.runner_for("unit_test")
self.assertIsInstance(returned_runner, runner.NoCompletion)
self.assertEqual("user-defined runner for [runner_function]", repr(returned_runner))
self.assertEqual(("default_client", "param"),
await returned_runner({"default": "default_client", "other": "other_client"}, "param"))
@run_async
async def test_single_cluster_runner_class_with_context_manager_should_be_wrapped_with_context_manager_enabled(self):
class UnitTestSingleClusterContextManagerRunner(BaseUnitTestContextManagerRunner):
async def __call__(self, *args):
return args
def __str__(self):
return "UnitTestSingleClusterContextManagerRunner"
test_runner = UnitTestSingleClusterContextManagerRunner()
runner.register_runner(operation_type="unit_test", runner=test_runner, async_runner=True)
returned_runner = runner.runner_for("unit_test")
self.assertIsInstance(returned_runner, runner.NoCompletion)
self.assertEqual("user-defined context-manager enabled runner for [UnitTestSingleClusterContextManagerRunner]",
repr(returned_runner))
# test that context_manager functionality gets preserved after wrapping
async with returned_runner:
self.assertEqual(("default_client", "param"),
await returned_runner({"default": "default_client", "other": "other_client"}, "param"))
# check that the context manager interface of our inner runner has been respected.
self.assertTrue(test_runner.fp.closed)
@run_async
async def test_multi_cluster_runner_class_with_context_manager_should_be_wrapped_with_context_manager_enabled(self):
class UnitTestMultiClusterContextManagerRunner(BaseUnitTestContextManagerRunner):
multi_cluster = True
async def __call__(self, *args):
return args
def __str__(self):
return "UnitTestMultiClusterContextManagerRunner"
test_runner = UnitTestMultiClusterContextManagerRunner()
runner.register_runner(operation_type="unit_test", runner=test_runner, async_runner=True)
returned_runner = runner.runner_for("unit_test")
self.assertIsInstance(returned_runner, runner.NoCompletion)
self.assertEqual("user-defined context-manager enabled runner for [UnitTestMultiClusterContextManagerRunner]",
repr(returned_runner))
# test that context_manager functionality gets preserved after wrapping
all_clients = {"default": "default_client", "other": "other_client"}
async with returned_runner:
self.assertEqual((all_clients, "param1", "param2"), await returned_runner(all_clients, "param1", "param2"))
# check that the context manager interface of our inner runner has been respected.
self.assertTrue(test_runner.fp.closed)
@run_async
async def test_single_cluster_runner_class_should_be_wrapped(self):
class UnitTestSingleClusterRunner:
async def __call__(self, *args):
return args
def __str__(self):
return "UnitTestSingleClusterRunner"
test_runner = UnitTestSingleClusterRunner()
runner.register_runner(operation_type="unit_test", runner=test_runner, async_runner=True)
returned_runner = runner.runner_for("unit_test")
self.assertIsInstance(returned_runner, runner.NoCompletion)
self.assertEqual("user-defined runner for [UnitTestSingleClusterRunner]", repr(returned_runner))
self.assertEqual(("default_client", "param"),
await returned_runner({"default": "default_client", "other": "other_client"}, "param"))
@run_async
async def test_multi_cluster_runner_class_should_be_wrapped(self):
class UnitTestMultiClusterRunner:
multi_cluster = True
async def __call__(self, *args):
return args
def __str__(self):
return "UnitTestMultiClusterRunner"
test_runner = UnitTestMultiClusterRunner()
runner.register_runner(operation_type="unit_test", runner=test_runner, async_runner=True)
returned_runner = runner.runner_for("unit_test")
self.assertIsInstance(returned_runner, runner.NoCompletion)
self.assertEqual("user-defined runner for [UnitTestMultiClusterRunner]", repr(returned_runner))
all_clients = {"default": "default_client", "other": "other_client"}
self.assertEqual((all_clients, "some_param"), await returned_runner(all_clients, "some_param"))
class AssertingRunnerTests(TestCase):
def setUp(self):
runner.enable_assertions(True)
def tearDown(self):
runner.enable_assertions(False)
@run_async
async def test_asserts_equal_succeeds(self):
opensearch = None
response = {
"hits": {
"hits": {
"value": 5,
"relation": "eq"
}
}
}
delegate = mock.MagicMock()
delegate.return_value = as_future(response)
r = runner.AssertingRunner(delegate)
async with r:
final_response = await r(opensearch, {
"name": "test-task",
"assertions": [
{
"property": "hits.hits.value",
"condition": "==",
"value": 5
},
{
"property": "hits.hits.relation",
"condition": "==",
"value": "eq"
}
]
})
self.assertEqual(response, final_response)
@run_async
async def test_asserts_equal_fails(self):
opensearch = None
response = {
"hits": {
"hits": {
"value": 10000,
"relation": "gte"
}
}
}
delegate = mock.MagicMock()
delegate.return_value = as_future(response)
r = runner.AssertingRunner(delegate)
with self.assertRaisesRegex(exceptions.BenchmarkTaskAssertionError,
r"Expected \[hits.hits.relation\] in \[test-task\] to be == \[eq\] but was \[gte\]."):
async with r:
await r(opensearch, {
"name": "test-task",
"assertions": [
{
"property": "hits.hits.value",
"condition": "==",
"value": 10000
},
{
"property": "hits.hits.relation",
"condition": "==",
"value": "eq"
}
]
})
@run_async
async def test_skips_asserts_for_non_dicts(self):
opensearch = None
response = (1, "ops")
delegate = mock.MagicMock()
delegate.return_value = as_future(response)
r = runner.AssertingRunner(delegate)
async with r:
final_response = await r(opensearch, {
"name": "test-task",
"assertions": [
{
"property": "hits.hits.value",
"condition": "==",
"value": 5
}
]
})
# still passes response as is
self.assertEqual(response, final_response)
def test_predicates(self):
r = runner.AssertingRunner(delegate=None)
self.assertEqual(5, len(r.predicates))
predicate_success = {
# predicate: (expected, actual)
">": (5, 10),
">=": (5, 5),
"<": (5, 4),
"<=": (5, 5),
"==": (5, 5),
}
for predicate, vals in predicate_success.items():
expected, actual = vals
self.assertTrue(r.predicates[predicate](expected, actual),
f"Expected [{expected} {predicate} {actual}] to succeed.")
predicate_fail = {
# predicate: (expected, actual)
">": (5, 5),
">=": (5, 4),
"<": (5, 5),
"<=": (5, 6),
"==": (5, 6),
}
for predicate, vals in predicate_fail.items():
expected, actual = vals
self.assertFalse(r.predicates[predicate](expected, actual),
f"Expected [{expected} {predicate} {actual}] to fail.")
class SelectiveJsonParserTests(TestCase):
def doc_as_text(self, doc):
return io.StringIO(json.dumps(doc))
def test_parse_all_expected(self):
doc = self.doc_as_text({
"title": "Hello",
"meta": {
"length": 100,
"date": {
"year": 2000
}
}
})
parsed = runner.parse(doc, [
# simple property
"title",
# a nested property
"meta.date.year",
# ignores unknown properties
"meta.date.month"
])
self.assertEqual("Hello", parsed.get("title"))
self.assertEqual(2000, parsed.get("meta.date.year"))
self.assertNotIn("meta.date.month", parsed)
def test_list_length(self):
doc = self.doc_as_text({
"title": "Hello",
"meta": {
"length": 100,
"date": {
"year": 2000
}
},
"authors": ["George", "Harry"],
"readers": [
{
"name": "Tom",
"age": 14
},
{
"name": "Bob",
"age": 17
},
{
"name": "Alice",
"age": 22
}
],
"supporters": []
})
parsed = runner.parse(doc, [
# simple property
"title",
# a nested property
"meta.date.year",
# ignores unknown properties
"meta.date.month"
], ["authors", "readers", "supporters"])
self.assertEqual("Hello", parsed.get("title"))
self.assertEqual(2000, parsed.get("meta.date.year"))
self.assertNotIn("meta.date.month", parsed)
# lists
self.assertFalse(parsed.get("authors"))
self.assertFalse(parsed.get("readers"))
self.assertTrue(parsed.get("supporters"))
class BulkIndexRunnerTests(TestCase):
@mock.patch("opensearchpy.OpenSearch")
@run_async
async def test_bulk_index_missing_params(self, opensearch):
bulk_response = {
"errors": False,
"took": 8
}
opensearch.bulk.return_value = as_future(io.StringIO(json.dumps(bulk_response)))
bulk = runner.BulkIndex()
bulk_params = {
"body": "action_meta_data\n" +
"index_line\n" +
"action_meta_data\n" +
"index_line\n" +
"action_meta_data\n" +
"index_line\n"
}
with self.assertRaises(exceptions.DataError) as ctx:
await bulk(opensearch, bulk_params)
self.assertEqual(
"Parameter source for operation 'bulk-index' did not provide the mandatory parameter 'action-metadata-present'. "
"Add it to your parameter source and try again.", ctx.exception.args[0])
@mock.patch("opensearchpy.OpenSearch")
@run_async
async def test_bulk_index_success_with_metadata(self, opensearch):
bulk_response = {
"errors": False,
"took": 8
}
opensearch.bulk.return_value = as_future(io.StringIO(json.dumps(bulk_response)))
bulk = runner.BulkIndex()
bulk_params = {
"body": "action_meta_data\n" +
"index_line\n" +
"action_meta_data\n" +
"index_line\n" +
"action_meta_data\n" +
"index_line\n",
"action-metadata-present": True,
"bulk-size": 3,
"unit": "docs"
}
result = await bulk(opensearch, bulk_params)
self.assertEqual(8, result["took"])
self.assertIsNone(result["index"])
self.assertEqual(3, result["weight"])
self.assertEqual("docs", result["unit"])
self.assertEqual(True, result["success"])
self.assertEqual(0, result["error-count"])
self.assertFalse("error-type" in result)
opensearch.bulk.assert_called_with(body=bulk_params["body"], params={})
@mock.patch("opensearchpy.OpenSearch")
@run_async
async def test_simple_bulk_with_timeout_and_headers(self, opensearch):
bulk_response = {
"errors": False,
"took": 8
}
opensearch.bulk.return_value = as_future(io.StringIO(json.dumps(bulk_response)))
bulk = runner.BulkIndex()
bulk_params = {
"body": "index_line\n" +
"index_line\n" +
"index_line\n",
"action-metadata-present": False,
"type": "_doc",
"index": "test1",
"request-timeout": 3.0,
"headers": { "x-test-id": "1234"},
"opaque-id": "DESIRED-OPAQUE-ID",
"bulk-size": 3,
"unit": "docs"
}
result = await bulk(opensearch, bulk_params)
self.assertEqual(8, result["took"])
self.assertEqual(3, result["weight"])
self.assertEqual("docs", result["unit"])
self.assertEqual(True, result["success"])
self.assertEqual(0, result["error-count"])
self.assertFalse("error-type" in result)
opensearch.bulk.assert_called_with(doc_type="_doc",
params={},
body="index_line\nindex_line\nindex_line\n",
headers={"x-test-id": "1234"},
index="test1",
opaque_id="DESIRED-OPAQUE-ID",
request_timeout=3.0)
@mock.patch("opensearchpy.OpenSearch")
@run_async
async def test_bulk_index_success_without_metadata_with_doc_type(self, opensearch):
bulk_response = {
"errors": False,
"took": 8
}
opensearch.bulk.return_value = as_future(io.StringIO(json.dumps(bulk_response)))
bulk = runner.BulkIndex()
bulk_params = {
"body": "index_line\n" +
"index_line\n" +
"index_line\n",
"action-metadata-present": False,
"bulk-size": 3,
"unit": "docs",
"index": "test-index",
"type": "_doc"
}
result = await bulk(opensearch, bulk_params)
self.assertEqual(8, result["took"])
self.assertEqual("test-index", result["index"])
self.assertEqual(3, result["weight"])
self.assertEqual("docs", result["unit"])
self.assertEqual(True, result["success"])
self.assertEqual(0, result["error-count"])
self.assertFalse("error-type" in result)
opensearch.bulk.assert_called_with(body=bulk_params["body"], index="test-index", doc_type="_doc", params={})
@mock.patch("opensearchpy.OpenSearch")
@run_async
async def test_bulk_index_success_without_metadata_and_without_doc_type(self, opensearch):
bulk_response = {
"errors": False,
"took": 8
}
opensearch.bulk.return_value = as_future(io.StringIO(json.dumps(bulk_response)))
bulk = runner.BulkIndex()
bulk_params = {
"body": "index_line\n" +
"index_line\n" +
"index_line\n",
"action-metadata-present": False,
"bulk-size": 3,
"unit": "docs",
"index": "test-index"
}
result = await bulk(opensearch, bulk_params)
self.assertEqual(8, result["took"])
self.assertEqual("test-index", result["index"])
self.assertEqual(3, result["weight"])
self.assertEqual("docs", result["unit"])
self.assertEqual(True, result["success"])
self.assertEqual(0, result["error-count"])
self.assertFalse("error-type" in result)
opensearch.bulk.assert_called_with(body=bulk_params["body"], index="test-index", doc_type=None, params={})
@mock.patch("opensearchpy.OpenSearch")
@run_async
async def test_bulk_index_error(self, opensearch):
bulk_response = {
"took": 5,
"errors": True,
"items": [
{
"index": {
"status": 201,
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
},
{
"index": {
"status": 500,
"_shards": {
"total": 2,
"successful": 0,
"failed": 2
}
}
},
{
"index": {
"status": 404,
"_shards": {
"total": 2,
"successful": 0,
"failed": 2
}
}
},
]
}
opensearch.bulk.return_value = as_future(io.StringIO(json.dumps(bulk_response)))
bulk = runner.BulkIndex()
bulk_params = {
"body": "action_meta_data\n" +
"index_line\n" +
"action_meta_data\n" +
"index_line\n" +
"action_meta_data\n" +
"index_line\n",
"action-metadata-present": True,
"bulk-size": 3,
"unit": "docs",
"index": "test"
}
result = await bulk(opensearch, bulk_params)
self.assertEqual("test", result["index"])
self.assertEqual(5, result["took"])
self.assertEqual(3, result["weight"])
self.assertEqual("docs", result["unit"])
self.assertEqual(False, result["success"])
self.assertEqual(2, result["error-count"])
self.assertEqual("bulk", result["error-type"])
opensearch.bulk.assert_called_with(body=bulk_params["body"], params={})
@mock.patch("opensearchpy.OpenSearch")
@run_async
async def test_bulk_index_error_no_shards(self, opensearch):
bulk_response = {
"took": 20,
"errors": True,
"items": [
{
"create": {
"_index": "test",
"_type": "doc",
"_id": "1",
"status": 429,
"error": "EsRejectedExecutionException[rejected execution (queue capacity 50) on org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$PrimaryPhase$1@1]" # pylint: disable=line-too-long
}
},
{
"create": {
"_index": "test",
"_type": "doc",
"_id": "2",
"status": 429,
"error": "EsRejectedExecutionException[rejected execution (queue capacity 50) on org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$PrimaryPhase$1@2]" # pylint: disable=line-too-long
}
},
{
"create": {
"_index": "test",
"_type": "doc",
"_id": "3",
"status": 429,
"error": "EsRejectedExecutionException[rejected execution (queue capacity 50) on org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$PrimaryPhase$1@3]" # pylint: disable=line-too-long
}
}
]
}
opensearch.bulk.return_value = as_future(io.StringIO(json.dumps(bulk_response)))
bulk = runner.BulkIndex()
bulk_params = {
"body": "action_meta_data\n" +
"index_line\n" +
"action_meta_data\n" +
"index_line\n" +
"action_meta_data\n" +
"index_line\n",
"action-metadata-present": True,
"detailed-results": False,
"bulk-size": 3,
"unit": "docs",
"index": "test"
}
result = await bulk(opensearch, bulk_params)
self.assertEqual("test", result["index"])
self.assertEqual(20, result["took"])
self.assertEqual(3, result["weight"])
self.assertEqual("docs", result["unit"])
self.assertEqual(False, result["success"])
self.assertEqual(3, result["error-count"])
self.assertEqual("bulk", result["error-type"])
opensearch.bulk.assert_called_with(body=bulk_params["body"], params={})
@mock.patch("opensearchpy.OpenSearch")
@run_async
async def test_mixed_bulk_with_simple_stats(self, opensearch):
bulk_response = {
"took": 30,
"ingest_took": 20,
"errors": True,
"items": [
{
"index": {
"_index": "test",
"_type": "type1",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": True,
"status": 201,
"_seq_no": 0
}
},
{
"update": {
"_index": "test",
"_type": "type1",
"_id": "2",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"status": 200,
"_seq_no": 1
}
},
{
"index": {
"_index": "test",
"_type": "type1",
"_id": "3",
"_version": 1,
"result": "noop",
"_shards": {
"total": 2,
"successful": 0,
"failed": 2
},
"created": False,
"status": 500,
"_seq_no": -2
}
},
{
"update": {
"_index": "test",
"_type": "type1",
"_id": "6",
"_version": 2,
"result": "noop",
"_shards": {
"total": 2,
"successful": 0,
"failed": 2
},
"status": 404,
"_seq_no": 5
}
}
]
}
opensearch.bulk.return_value = as_future(io.StringIO(json.dumps(bulk_response)))
bulk = runner.BulkIndex()
bulk_params = {
"body": "action_meta_data\n" +
"index_line\n" +
"action_meta_data\n" +
"update_line\n" +
"action_meta_data\n" +
"index_line\n" +
"action_meta_data\n" +
"update_line\n",
"action-metadata-present": True,
"detailed-results": False,
"bulk-size": 4,
"unit": "docs",
"index": "test"
}
result = await bulk(opensearch, bulk_params)
self.assertEqual("test", result["index"])
self.assertEqual(30, result["took"])
self.assertNotIn("ingest_took", result, "ingest_took is not extracted with simple stats")
self.assertEqual(4, result["weight"])
self.assertEqual("docs", result["unit"])
self.assertEqual(False, result["success"])
self.assertEqual(2, result["error-count"])
self.assertEqual("bulk", result["error-type"])
opensearch.bulk.assert_called_with(body=bulk_params["body"], params={})
@mock.patch("opensearchpy.OpenSearch")
@run_async
async def test_mixed_bulk_with_detailed_stats_body_as_string(self, opensearch):
opensearch.bulk.return_value = as_future({
"took": 30,
"ingest_took": 20,
"errors": True,
"items": [
{
"index": {
"_index": "test",
"_type": "type1",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": True,
"status": 201,
"_seq_no": 0
}
},
{
"update": {
"_index": "test",
"_type": "type1",
"_id": "2",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"status": 200,
"_seq_no": 1
}
},
{
"index": {
"_index": "test",
"_type": "type1",
"_id": "3",
"_version": 1,
"result": "noop",
"_shards": {
"total": 2,
"successful": 0,
"failed": 2
},
"created": False,
"status": 500,
"_seq_no": -2
}
},
{
"index": {
"_index": "test",
"_type": "type1",
"_id": "4",
"_version": 1,
"result": "noop",
"_shards": {
"total": 2,
"successful": 1,
"failed": 1
},
"created": False,
"status": 500,
"_seq_no": -2
}
},
{
"index": {
"_index": "test",
"_type": "type1",
"_id": "5",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": True,
"status": 201,
"_seq_no": 4
}
},
{
"update": {
"_index": "test",
"_type": "type1",
"_id": "6",
"_version": 2,
"result": "noop",
"_shards": {
"total": 2,
"successful": 0,
"failed": 2
},
"status": 404,
"_seq_no": 5
}
}
]
})
bulk = runner.BulkIndex()
bulk_params = {
"body": '{ "index" : { "_index" : "test", "_type" : "type1" } }\n' +
'{"location" : [-0.1485188, 51.5250666]}\n' +
'{ "update" : { "_index" : "test", "_type" : "type1", "_id: "2" } }\n' +
'{"location" : [-0.1479949, 51.5252071]}\n' +
'{ "index" : { "_index" : "test", "_type" : "type1" } }\n' +
'{"location" : [-0.1458559, 51.5289059]}\n' +
'{ "index" : { "_index" : "test", "_type" : "type1" } }\n' +
'{"location" : [-0.1498551, 51.5282564]}\n' +
'{ "index" : { "_index" : "test", "_type" : "type1" } }\n' +
'{"location" : [-0.1487043, 51.5254843]}\n' +
'{ "update" : { "_index" : "test", "_type" : "type1", "_id: "3" } }\n' +
'{"location" : [-0.1533367, 51.5261779]}\n',
"action-metadata-present": True,
"bulk-size": 6,
"unit": "docs",
"detailed-results": True,
"index": "test"
}
result = await bulk(opensearch, bulk_params)
self.assertEqual("test", result["index"])
self.assertEqual(30, result["took"])
self.assertEqual(20, result["ingest_took"])
self.assertEqual(6, result["weight"])
self.assertEqual("docs", result["unit"])
self.assertEqual(False, result["success"])
self.assertEqual(3, result["error-count"])
self.assertEqual("bulk", result["error-type"])
self.assertEqual(
{
"index": {
"item-count": 4,
"created": 2,
"noop": 2
},
"update": {
"item-count": 2,
"updated": 1,
"noop": 1
}
}, result["ops"])
self.assertEqual(
[
{
"item-count": 3,
"shards": {
"total": 2,
"successful": 1,
"failed": 0
}
},
{
"item-count": 2,
"shards": {
"total": 2,
"successful": 0,
"failed": 2
}
},
{
"item-count": 1,
"shards": {
"total": 2,
"successful": 1,
"failed": 1
}
}
], result["shards_histogram"])
self.assertEqual(582, result["bulk-request-size-bytes"])
self.assertEqual(234, result["total-document-size-bytes"])
opensearch.bulk.assert_called_with(body=bulk_params["body"], params={})
opensearch.bulk.return_value.result().pop("ingest_took")
result = await bulk(opensearch, bulk_params)
self.assertNotIn("ingest_took", result)
@mock.patch("opensearchpy.OpenSearch")
@run_async
async def test_simple_bulk_with_detailed_stats_body_as_list(self, opensearch):
opensearch.bulk.return_value = as_future({
"took": 30,
"ingest_took": 20,
"errors": False,
"items": [
{
"index": {
"_index": "test",
"_type": "type1",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": True,
"status": 201,
"_seq_no": 0
}
}
]
})
bulk = runner.BulkIndex()
bulk_params = {
"body": '{ "index" : { "_index" : "test", "_type" : "type1" } }\n' +
'{"location" : [-0.1485188, 51.5250666]}\n',
"action-metadata-present": True,
"bulk-size": 1,
"unit": "docs",
"detailed-results": True,
"index": "test"
}
result = await bulk(opensearch, bulk_params)
self.assertEqual("test", result["index"])
self.assertEqual(30, result["took"])
self.assertEqual(20, result["ingest_took"])
self.assertEqual(1, result["weight"])