/
test_local.py
780 lines (611 loc) · 27.1 KB
/
test_local.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
# Copyright 2009-2012 Yelp and Contributors
# Copyright 2013 David Marin and Lyft
#
# 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.
"""Tests for LocalMRJobRunner"""
import gzip
import os
import shutil
import signal
import stat
import sys
import tempfile
from io import BytesIO
import mrjob
from mrjob.local import LocalMRJobRunner
from mrjob.util import bash_wrap
from mrjob.util import cmd_line
from mrjob.util import read_file
from tests.mr_cmd_job import CmdJob
from tests.mr_counting_job import MRCountingJob
from tests.mr_exit_42_job import MRExit42Job
from tests.mr_filter_job import FilterJob
from tests.mr_job_where_are_you import MRJobWhereAreYou
from tests.mr_two_step_job import MRTwoStepJob
from tests.mr_verbose_job import MRVerboseJob
from tests.mr_word_count import MRWordCount
from tests.py2 import TestCase
from tests.py2 import call
from tests.py2 import patch
from tests.py2 import skipIf
from tests.quiet import logger_disabled
from tests.quiet import no_handlers_for_logger
from tests.sandbox import EmptyMrjobConfTestCase
from tests.sandbox import SandboxedTestCase
from tests.sandbox import mrjob_conf_patcher
from tests.test_inline import InlineMRJobRunnerFSTestCase
from tests.test_inline import InlineMRJobRunnerJobConfTestCase
from tests.test_inline import InlineMRJobRunnerNoMapperTestCase
class LocalMRJobRunnerEndToEndTestCase(SandboxedTestCase):
def test_end_to_end(self):
# read from STDIN, a regular file, and a .gz
stdin = BytesIO(b'foo\nbar\n')
input_path = os.path.join(self.tmp_dir, 'input')
with open(input_path, 'w') as input_file:
input_file.write('bar\nqux\n')
input_gz_path = os.path.join(self.tmp_dir, 'input.gz')
input_gz_glob = os.path.join(self.tmp_dir, '*.gz')
input_gz = gzip.GzipFile(input_gz_path, 'wb')
input_gz.write(b'foo\n')
input_gz.close()
mr_job = MRTwoStepJob(['-r', 'local', '-', input_path, input_gz_glob])
mr_job.sandbox(stdin=stdin)
local_tmp_dir = None
results = []
with mr_job.make_runner() as runner:
assert isinstance(runner, LocalMRJobRunner)
runner.run()
for line in runner.stream_output():
key, value = mr_job.parse_output_line(line)
results.append((key, value))
local_tmp_dir = runner._get_local_tmp_dir()
assert os.path.exists(local_tmp_dir)
self.assertEqual(runner.counters()[0]['count']['combiners'], 8)
# make sure cleanup happens
assert not os.path.exists(local_tmp_dir)
self.assertEqual(sorted(results),
[(1, 'qux'), (2, 'bar'), (2, 'foo'), (5, None)])
def test_end_to_end_multiple_tasks(self):
# read from STDIN, a regular file, and a .gz
stdin = BytesIO(b'foo\nbar\n')
input_path = os.path.join(self.tmp_dir, 'input')
with open(input_path, 'wb') as input_file:
input_file.write(b'bar\nqux\n')
input_gz_path = os.path.join(self.tmp_dir, 'input.gz')
input_gz = gzip.GzipFile(input_gz_path, 'wb')
input_gz.write(b'foo\n')
input_gz.close()
mr_job = MRTwoStepJob(['-r', 'local',
'--jobconf=mapred.map.tasks=2',
'--jobconf=mapred.reduce.tasks=2',
'-', input_path, input_gz_path])
mr_job.sandbox(stdin=stdin)
local_tmp_dir = None
results = []
with mr_job.make_runner() as runner:
assert isinstance(runner, LocalMRJobRunner)
runner.run()
for line in runner.stream_output():
key, value = mr_job.parse_output_line(line)
results.append((key, value))
local_tmp_dir = runner._get_local_tmp_dir()
assert os.path.exists(local_tmp_dir)
# make sure cleanup happens
assert not os.path.exists(local_tmp_dir)
self.assertEqual(sorted(results),
[(1, 'qux'), (2, 'bar'), (2, 'foo'), (5, None)])
def test_get_file_splits_test(self):
# set up input paths
input_path = os.path.join(self.tmp_dir, 'input')
with open(input_path, 'w') as input_file:
input_file.write('bar\nqux\nfoo\nbar\nqux\nfoo\n')
input_path2 = os.path.join(self.tmp_dir, 'input2')
with open(input_path2, 'wb') as input_file:
input_file.write(b'foo\nbar\nbar\n')
runner = LocalMRJobRunner(conf_paths=[])
# split into 3 files
file_splits = runner._get_file_splits([input_path, input_path2], 3)
# make sure we get 3 files
self.assertEqual(len(file_splits), 3)
# make sure all the data is preserved
content = []
for file_name in file_splits:
with open(file_name, 'rb') as f:
content.extend(f.readlines())
self.assertEqual(sorted(content),
[b'bar\n', b'bar\n', b'bar\n', b'bar\n', b'foo\n',
b'foo\n', b'foo\n', b'qux\n', b'qux\n'])
def test_get_file_splits_sorted_test(self):
# set up input paths
input_path = os.path.join(self.tmp_dir, 'input')
with open(input_path, 'wb') as input_file:
input_file.write(
b'1\tbar\n1\tbar\n1\tbar\n2\tfoo\n2\tfoo\n2\tfoo\n3\tqux\n'
b'3\tqux\n3\tqux\n')
runner = LocalMRJobRunner(conf_paths=[])
file_splits = runner._get_file_splits([input_path], 3,
keep_sorted=True)
# make sure we get 3 files
self.assertEqual(len(file_splits), 3)
# make sure all the data is preserved in sorted order
content = []
for file_name in sorted(file_splits.keys()):
with open(file_name, 'rb') as f:
content.extend(f.readlines())
self.assertEqual(content,
[b'1\tbar\n', b'1\tbar\n', b'1\tbar\n',
b'2\tfoo\n', b'2\tfoo\n', b'2\tfoo\n',
b'3\tqux\n', b'3\tqux\n', b'3\tqux\n'])
def gz_test(self, dir_path_name):
contents_gz = [b'bar\n', b'qux\n', b'foo\n', b'bar\n',
b'qux\n', b'foo\n']
contents_normal = [b'foo\n', b'bar\n', b'bar\n']
all_contents_sorted = sorted(contents_gz + contents_normal)
input_gz_path = os.path.join(dir_path_name, 'input.gz')
input_gz = gzip.GzipFile(input_gz_path, 'wb')
input_gz.write(b''.join(contents_gz))
input_gz.close()
input_path2 = os.path.join(dir_path_name, 'input2')
with open(input_path2, 'wb') as input_file:
input_file.write(b''.join(contents_normal))
runner = LocalMRJobRunner(conf_paths=[])
# split into 3 files
file_splits = runner._get_file_splits([input_gz_path, input_path2], 3)
# Make sure that input.gz occurs in a single split that starts at
# its beginning and ends at its end
for split_info in file_splits.values():
if split_info['orig_name'] == input_gz_path:
self.assertEqual(split_info['start'], 0)
self.assertEqual(split_info['length'],
os.stat(input_gz_path)[stat.ST_SIZE])
# make sure we get 3 files
self.assertEqual(len(file_splits), 3)
# make sure all the data is preserved
content = []
for file_name in file_splits:
lines = list(read_file(file_name))
# make sure the input_gz split got its entire contents
if file_name == input_gz_path:
self.assertEqual(lines, contents_gz)
content.extend(lines)
self.assertEqual(sorted(content),
all_contents_sorted)
def test_dont_split_gz(self):
self.gz_test(self.tmp_dir)
def test_relative_gz_path(self):
current_directory = os.getcwd()
def change_back_directory():
os.chdir(current_directory)
self.addCleanup(change_back_directory)
os.chdir(self.tmp_dir)
self.gz_test('')
def test_multi_step_counters(self):
stdin = BytesIO(b'foo\nbar\n')
mr_job = MRCountingJob(['-r', 'local', '-'])
mr_job.sandbox(stdin=stdin)
with mr_job.make_runner() as runner:
runner.run()
self.assertEqual(runner.counters(),
[{'group': {'counter_name': 2}},
{'group': {'counter_name': 2}},
{'group': {'counter_name': 2}}])
def test_gz_split_regression(self):
gz_path_1 = os.path.join(self.tmp_dir, '1.gz')
gz_path_2 = os.path.join(self.tmp_dir, '2.gz')
path_3 = os.path.join(self.tmp_dir, '3')
input_gz_1 = gzip.GzipFile(gz_path_1, 'wb')
input_gz_1.write(b'x\n')
input_gz_1.close()
input_gz_2 = gzip.GzipFile(gz_path_2, 'wb')
input_gz_2.write(b'y\n')
input_gz_2.close()
with open(path_3, 'wb') as f:
f.write(b'z')
mr_job = MRCountingJob(['--no-conf', '-r', 'local', gz_path_1,
gz_path_2, path_3])
with mr_job.make_runner() as r:
splits = r._get_file_splits([gz_path_1, gz_path_2, path_3], 1)
self.assertEqual(
len(set(s['task_num'] for s in splits.values())), 3)
class LocalMRJobRunnerNoSymlinksTestCase(LocalMRJobRunnerEndToEndTestCase):
"""Test systems without os.symlink (e.g. Windows). See Issue #46"""
def setUp(self):
super(LocalMRJobRunnerNoSymlinksTestCase, self).setUp()
self.remove_os_symlink()
def tearDown(self):
self.restore_os_symlink()
super(LocalMRJobRunnerNoSymlinksTestCase, self).tearDown()
def remove_os_symlink(self):
if hasattr(os, 'symlink'):
self._real_os_symlink = os.symlink
del os.symlink # sorry, were you using that? :)
def restore_os_symlink(self):
if hasattr(self, '_real_os_symlink'):
os.symlink = self._real_os_symlink
class TimeoutException(Exception):
pass
class LargeAmountsOfStderrTestCase(TestCase):
def setUp(self):
self.set_alarm()
def tearDown(self):
self.restore_old_alarm_handler()
def set_alarm(self):
# if the test fails, it'll stall forever, so set an alarm
def alarm_handler(*args, **kwargs):
raise TimeoutException('Stalled on large amounts of stderr;'
' probably pipe buffer is full.')
self._old_alarm_handler = signal.signal(signal.SIGALRM, alarm_handler)
signal.alarm(30)
def restore_old_alarm_handler(self):
signal.alarm(0)
signal.signal(signal.SIGALRM, self._old_alarm_handler)
def test_large_amounts_of_stderr(self):
mr_job = MRVerboseJob(['--no-conf', '-r', 'local', '-v'])
mr_job.sandbox()
try:
with no_handlers_for_logger():
mr_job.run_job()
except TimeoutException:
raise
except SystemExit:
# we expect the job to throw a StepFailedException,
# which causes run_job to call sys.exit()
# look for expected output from MRVerboseJob
stderr = mr_job.stderr.getvalue()
self.assertIn(
b"Counters: 1\n\tFoo\n\t\tBar=10000", stderr)
self.assertIn(b'Status: 0\n', stderr)
self.assertIn(b'Status: 99\n', stderr)
self.assertNotIn(b'Status: 100\n', stderr)
self.assertIn(b'STDERR: Qux\n', stderr)
# exception should appear in exception message
self.assertIn(b'BOOM', stderr)
else:
raise AssertionError()
class ExitWithoutExceptionTestCase(TestCase):
def test_exit_42_job(self):
mr_job = MRExit42Job(['--no-conf', '--runner=local'])
mr_job.sandbox()
self.assertRaises(SystemExit, mr_job.run_job)
self.assertIn(b'returned non-zero exit status 42',
mr_job.stderr.getvalue())
class PythonBinTestCase(EmptyMrjobConfTestCase):
def test_echo_as_python_bin(self):
# "echo" is a pretty poor substitute for Python, but it
# should be available on most systems
mr_job = MRTwoStepJob(
['--python-bin', 'echo', '--steps-python-bin', sys.executable,
'--no-conf', '-r', 'local'])
mr_job.sandbox()
with mr_job.make_runner() as runner:
assert isinstance(runner, LocalMRJobRunner)
runner.run()
output = b''.join(runner.stream_output())
# the output should basically be the command we used to
# run the last step, which in this case is a mapper
self.assertIn(b'mr_two_step_job.py', output)
self.assertIn(b'--step-num=1', output)
self.assertIn(b'--mapper', output)
@skipIf(hasattr(sys, 'pypy_version_info'),
"-v option doesn't work with pypy")
def test_python_dash_v_as_python_bin(self):
python_cmd = cmd_line([sys.executable or 'python', '-v'])
mr_job = MRTwoStepJob(['--python-bin', python_cmd, '--no-conf',
'-r', 'local', '-v'])
mr_job.sandbox(stdin=[b'bar\n'])
with no_handlers_for_logger():
mr_job.run_job()
# expect debugging messages in stderr.
stderr = mr_job.stderr.getvalue()
# stderr is huge, so don't use assertIn()
self.assertTrue(b'import mrjob' in stderr or # Python 2
b"import 'mrjob'" in stderr) # Python 3
self.assertTrue(b'#' in stderr)
# should still get expected results
self.assertEqual(sorted(mr_job.stdout.getvalue().splitlines()),
sorted([b'1\tnull', b'1\t"bar"']))
class StepsPythonBinTestCase(TestCase):
def test_echo_as_steps_python_bin(self):
mr_job = MRTwoStepJob(
['--steps', '--steps-python-bin', 'echo', '--no-conf',
'-r', 'local'])
mr_job.sandbox()
with mr_job.make_runner() as runner:
assert isinstance(runner, LocalMRJobRunner)
# MRTwoStepJob populates _steps in the runner, so un-populate
# it here so that the runner actually tries to get the steps
# via subprocess
runner._steps = None
self.assertRaises(ValueError, runner._get_steps)
def test_echo_as_steps_interpreter(self):
import logging
logging.basicConfig()
mr_job = MRTwoStepJob(
['--steps', '--steps-interpreter', 'echo', '--no-conf', '-r',
'local'])
mr_job.sandbox()
with mr_job.make_runner() as runner:
assert isinstance(runner, LocalMRJobRunner)
# MRTwoStepJob populates _steps in the runner, so un-populate
# it here so that the runner actually tries to get the steps
# via subprocess
runner._steps = None
self.assertRaises(ValueError, runner._get_steps)
class LocalBootstrapMrjobTestCase(TestCase):
def setUp(self):
self.make_tmp_dir()
def tearDown(self):
self.rm_tmp_dir()
def make_tmp_dir(self):
self.tmp_dir = tempfile.mkdtemp()
def rm_tmp_dir(self):
shutil.rmtree(self.tmp_dir)
def test_loading_bootstrapped_mrjob_library(self):
# track the dir we're loading mrjob from rather than the full path
# to deal with edge cases where we load from the .py file,
# and the script loads from the .pyc compiled from that .py file.
our_mrjob_dir = os.path.dirname(os.path.realpath(mrjob.__file__))
with mrjob_conf_patcher():
mr_job = MRJobWhereAreYou(['-r', 'local', '--bootstrap-mrjob'])
mr_job.sandbox()
with mr_job.make_runner() as runner:
# sanity check
self.assertEqual(runner._bootstrap_mrjob(), True)
local_tmp_dir = os.path.realpath(runner._get_local_tmp_dir())
runner.run()
output = list(runner.stream_output())
self.assertEqual(len(output), 1)
# script should load mrjob from its working dir
_, script_mrjob_dir = mr_job.parse_output_line(output[0])
self.assertNotEqual(our_mrjob_dir, script_mrjob_dir)
self.assertTrue(script_mrjob_dir.startswith(local_tmp_dir))
def test_can_turn_off_bootstrap_mrjob(self):
with mrjob_conf_patcher(
{'runners': {'local': {'bootstrap_mrjob': False}}}):
mr_job = MRJobWhereAreYou(['-r', 'local'])
mr_job.sandbox()
with mr_job.make_runner() as runner:
# sanity check
self.assertEqual(runner.get_opts()['bootstrap_mrjob'], False)
local_tmp_dir = os.path.realpath(runner._get_local_tmp_dir())
try:
with no_handlers_for_logger():
runner.run()
except Exception as e:
# if mrjob is not installed, script won't be able to run
self.assertIn('ImportError', str(e))
return
output = list(runner.stream_output())
self.assertEqual(len(output), 1)
# script should not load mrjob from local_tmp_dir
_, script_mrjob_dir = mr_job.parse_output_line(output[0])
self.assertFalse(script_mrjob_dir.startswith(local_tmp_dir))
class LocalMRJobRunnerJobConfTestCase(InlineMRJobRunnerJobConfTestCase):
RUNNER = 'local'
class LocalMRJobRunnerNoMapperTestCase(InlineMRJobRunnerNoMapperTestCase):
RUNNER = 'local'
class LocalMRJobRunnerFSTestCase(InlineMRJobRunnerFSTestCase):
RUNNER_CLASS = LocalMRJobRunner
class CompatTestCase(EmptyMrjobConfTestCase):
def test_environment_variables_version_agnostic(self):
job = MRWordCount(['-r', 'local'])
with job.make_runner() as runner:
simulated_jobconf = runner._simulate_jobconf_for_step(
0, 'mapper', 0, '/tmp/foo')
self.assertIn(
'mapred.cache.localArchives', simulated_jobconf)
self.assertIn(
'mapreduce.job.cache.local.archives', simulated_jobconf)
def test_environment_variables_hadoop_1(self):
job = MRWordCount(['-r', 'local', '--hadoop-version', '1.2.1'])
with job.make_runner() as runner:
simulated_jobconf = runner._simulate_jobconf_for_step(
0, 'mapper', 0, '/tmp/foo')
self.assertIn(
'mapred.cache.localArchives', simulated_jobconf)
self.assertNotIn(
'mapreduce.job.cache.local.archives', simulated_jobconf)
def test_environment_variables_hadoop_2(self):
job = MRWordCount(['-r', 'local', '--hadoop-version', '2.7.2'])
with job.make_runner() as runner:
simulated_jobconf = runner._simulate_jobconf_for_step(
0, 'mapper', 0, '/tmp/foo')
self.assertIn(
'mapreduce.job.cache.local.archives', simulated_jobconf)
self.assertNotIn(
'mapred.cache.localArchives', simulated_jobconf)
class CommandSubstepTestCase(SandboxedTestCase):
def test_cat_mapper(self):
data = b'x\ny\nz\n'
job = CmdJob(['--mapper-cmd=cat', '--runner=local'])
job.sandbox(stdin=BytesIO(data))
with job.make_runner() as r:
self.assertEqual(
r._get_steps(),
[{
'type': 'streaming',
'mapper': {
'type': 'command',
'command': 'cat'}}])
r.run()
lines = [line.strip() for line in list(r.stream_output())]
self.assertEqual(sorted(lines), sorted(data.split()))
def test_uniq_combiner(self):
data = b'x\nx\nx\nx\nx\nx\n'
job = CmdJob(['--combiner-cmd=uniq', '--runner=local'])
job.sandbox(stdin=BytesIO(data))
with job.make_runner() as r:
self.assertEqual(
r._get_steps(),
[{
'type': 'streaming',
'mapper': {
'type': 'script',
},
'combiner': {
'type': 'command',
'command': 'uniq'}}])
r.run()
# there are 2 map tasks, each of which has 1 combiner, and all rows
# are the same, so we should end up with just 2 values
self.assertEqual(b''.join(r.stream_output()), b'x\nx\n')
def test_cat_reducer(self):
data = b'x\ny\nz\n'
job = CmdJob(['--reducer-cmd', 'cat -e', '--runner=local'])
job.sandbox(stdin=BytesIO(data))
with job.make_runner() as r:
self.assertEqual(
r._get_steps(),
[{
'type': 'streaming',
'mapper': {
'type': 'script',
},
'reducer': {
'type': 'command',
'command': 'cat -e'}}])
r.run()
lines = list(r.stream_output())
self.assertEqual(sorted(lines), [b'x$\n', b'y$\n', b'z$\n'])
def test_multiple(self):
data = b'x\nx\nx\nx\nx\nx\n'
mapper_cmd = 'cat -e'
reducer_cmd = bash_wrap('wc -l | tr -Cd "[:digit:]"')
job = CmdJob([
'--runner', 'local',
'--mapper-cmd', mapper_cmd,
'--combiner-cmd', 'uniq',
'--reducer-cmd', reducer_cmd])
job.sandbox(stdin=BytesIO(data))
with job.make_runner() as r:
self.assertEqual(
r._get_steps(),
[{
'type': 'streaming',
'mapper': {'type': 'command', 'command': mapper_cmd},
'combiner': {'type': 'command', 'command': 'uniq'},
'reducer': {'type': 'command', 'command': reducer_cmd},
}])
r.run()
self.assertEqual(list(r.stream_output()), [b'2'])
def test_multiple_2(self):
data = b'x\ny\nz\n'
job = CmdJob(['--mapper-cmd=cat', '--reducer-cmd-2', 'wc -l',
'--runner=local', '--no-conf'])
job.sandbox(stdin=BytesIO(data))
with job.make_runner() as r:
r.run()
self.assertEqual(sum(int(l) for l in r.stream_output()), 3)
class FilterTestCase(SandboxedTestCase):
def test_mapper_pre_filter(self):
data = b'x\ny\nz\n'
job = FilterJob(['--mapper-filter', 'cat -e', '--runner=local'])
job.sandbox(stdin=BytesIO(data))
with job.make_runner() as r:
self.assertEqual(
r._get_steps(),
[{
'type': 'streaming',
'mapper': {
'type': 'script',
'pre_filter': 'cat -e'}}])
r.run()
lines = [line.strip() for line in list(r.stream_output())]
self.assertEqual(sorted(lines), [b'x$', b'y$', b'z$'])
def test_combiner_pre_filter(self):
data = b'x\ny\nz\n'
job = FilterJob(['--combiner-filter', 'cat -e', '--runner=local'])
job.sandbox(stdin=BytesIO(data))
with job.make_runner() as r:
self.assertEqual(
r._get_steps(),
[{
'type': 'streaming',
'mapper': {
'type': 'script',
},
'combiner': {
'type': 'script',
'pre_filter': 'cat -e',
}}])
r.run()
lines = [line.strip() for line in list(r.stream_output())]
self.assertEqual(sorted(lines), [b'x$', b'y$', b'z$'])
def test_reducer_pre_filter(self):
data = b'x\ny\nz\n'
job = FilterJob(['--reducer-filter', 'cat -e', '--runner=local'])
job.sandbox(stdin=BytesIO(data))
with job.make_runner() as r:
self.assertEqual(
r._get_steps(),
[{
'type': 'streaming',
'mapper': {
'type': 'script',
},
'reducer': {
'type': 'script',
'pre_filter': 'cat -e'}}])
r.run()
lines = [line.strip() for line in list(r.stream_output())]
self.assertEqual(sorted(lines), [b'x$', b'y$', b'z$'])
def test_pre_filter_failure(self):
# regression test for #1524
data = b'x\ny\nz\n'
# grep will return exit code 1 because there are no matches
job = FilterJob(['--mapper-filter', 'grep w', '--runner=local'])
job.sandbox(stdin=BytesIO(data))
with job.make_runner() as r:
self.assertEqual(
r._get_steps(),
[{
'type': 'streaming',
'mapper': {
'type': 'script',
'pre_filter': 'grep w'}}])
r.run()
lines = [line.strip() for line in list(r.stream_output())]
self.assertEqual(sorted(lines), [])
def test_pre_filter_on_compressed_data(self):
# regression test for #1061
input_gz_path = self.makefile('data.gz')
input_gz = gzip.GzipFile(input_gz_path, 'wb')
input_gz.write(b'x\ny\nz\n')
input_gz.close()
job = FilterJob([
'--mapper-filter', 'cat -e', '--runner=local', input_gz_path])
with job.make_runner() as r:
self.assertEqual(
r._get_steps(),
[{
'type': 'streaming',
'mapper': {
'type': 'script',
'pre_filter': 'cat -e'}}])
r.run()
lines = [line.strip() for line in list(r.stream_output())]
self.assertEqual(sorted(lines), [b'x$', b'y$', b'z$'])
class SetupLineEncodingTestCase(TestCase):
def test_setup_wrapper_script_uses_local_line_endings(self):
job = MRTwoStepJob(['-r', 'local', '--setup', 'true'])
job.sandbox(stdin=BytesIO())
# tests #1071. Unfortunately, we mostly run these tests on machines
# that use unix line endings anyway. So monitor open() instead
with patch(
'mrjob.runner.open', create=True, side_effect=open) as m_open:
with logger_disabled('mrjob.local'):
with job.make_runner() as runner:
runner.run()
self.assertIn(
call(runner._setup_wrapper_script_path, 'w'),
m_open.mock_calls)