forked from icio/mrjob
/
test_option_store.py
343 lines (280 loc) · 11 KB
/
test_option_store.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
# Copyright 2012 Yelp
#
# 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.
"""Test OptionStore's functionality"""
from __future__ import with_statement
import os
from tempfile import mkdtemp
from shutil import rmtree
from StringIO import StringIO
try:
import unittest2 as unittest
unittest # quiet "redefinition of unused ..." warning from pyflakes
except ImportError:
import unittest
try:
import boto
import boto.emr
import boto.emr.connection
import boto.exception
boto # quiet "redefinition of unused ..." warning from pyflakes
except ImportError:
boto = None
from mrjob.conf import dump_mrjob_conf
from mrjob.runner import RunnerOptionStore
from mrjob.util import log_to_stream
from tests.quiet import logger_disabled
from tests.quiet import no_handlers_for_logger
from tests.sandbox import EmptyMrjobConfTestCase
class TempdirTestCase(unittest.TestCase):
"""Patch mrjob.conf, create a temp directory, and save the environment for
each test
"""
def setUp(self):
super(TempdirTestCase, self).setUp()
# tmp dir
self.tmp_dir = mkdtemp()
self.addCleanup(rmtree, self.tmp_dir)
# environment
self._old_environ = os.environ.copy()
def tearDown(self):
os.environ.clear()
os.environ.update(self._old_environ)
def makedirs(self, path):
abs_path = os.path.join(self.tmp_dir, path)
if not os.path.isdir(abs_path):
os.makedirs(abs_path)
return abs_path
def makefile(self, path, contents):
self.makedirs(os.path.split(path)[0])
abs_path = os.path.join(self.tmp_dir, path)
with open(abs_path, 'w') as f:
f.write(contents)
return abs_path
def abs_paths(self, *paths):
return [os.path.join(self.tmp_dir, path) for path in paths]
class RunnerOptionStoreTestCase(EmptyMrjobConfTestCase):
def _assert_interp(self, val, **kwargs):
opts = RunnerOptionStore('inline', kwargs, [])
self.assertEqual(opts['interpreter'], val)
def test_interpreter_fallback(self):
self._assert_interp(['python'])
def test_interpreter_fallback_2(self):
self._assert_interp(['python', '-v'], python_bin=['python', '-v'])
def test_interpreter(self):
self._assert_interp(['ruby'], interpreter=['ruby'])
class ConfigFilesTestCase(TempdirTestCase):
def save_conf(self, name, conf):
conf_path = os.path.join(self.tmp_dir, name)
with open(conf_path, 'w') as f:
dump_mrjob_conf(conf, f)
return conf_path
def opts_for_conf(self, name, conf):
conf_path = self.save_conf(name, conf)
return RunnerOptionStore('inline', {}, [conf_path])
class MultipleConfigFilesValuesTestCase(ConfigFilesTestCase):
BASIC_CONF = {
'runners': {
'inline': {
'base_tmp_dir': '/tmp',
'cmdenv': {
'A_PATH': 'A',
'SOMETHING': 'X',
},
'hadoop_extra_args': [
'thing1',
],
'hadoop_streaming_jar': 'monkey.jar',
'jobconf': {
'lorax_speaks_for': 'trees',
},
'python_bin': 'py3k',
'setup_scripts': ['/myscript.py'],
}
}
}
def larger_conf(self):
return {
'include': os.path.join(self.tmp_dir, 'mrjob.conf'),
'runners': {
'inline': {
'base_tmp_dir': '/var/tmp',
'bootstrap_mrjob': False,
'cmdenv': {
'A_PATH': 'B',
'SOMETHING': 'Y',
'SOMETHING_ELSE': 'Z',
},
'hadoop_extra_args': [
'thing2',
],
'hadoop_streaming_jar': 'banana.jar',
'jobconf': {
'lorax_speaks_for': 'mazda',
'dr_seuss_is': 'awesome',
},
'python_bin': 'py4k',
'setup_scripts': ['/yourscript.py'],
}
}
}
def setUp(self):
super(MultipleConfigFilesValuesTestCase, self).setUp()
self.opts_1 = self.opts_for_conf('mrjob.conf',
self.BASIC_CONF)
self.opts_2 = self.opts_for_conf('mrjob.larger.conf',
self.larger_conf())
def test_combine_cmds(self):
self.assertEqual(self.opts_1['python_bin'], ['py3k'])
self.assertEqual(self.opts_2['python_bin'], ['py4k'])
def test_combine_dicts(self):
self.assertEqual(self.opts_1['jobconf'], {
'lorax_speaks_for': 'trees',
})
self.assertEqual(self.opts_2['jobconf'], {
'lorax_speaks_for': 'mazda',
'dr_seuss_is': 'awesome',
})
def test_combine_envs(self):
self.assertEqual(self.opts_1['cmdenv'], {
'A_PATH': 'A',
'SOMETHING': 'X',
})
self.assertEqual(self.opts_2['cmdenv'], {
'A_PATH': 'B:A',
'SOMETHING': 'Y',
'SOMETHING_ELSE': 'Z',
})
def test_combine_lists(self):
self.assertEqual(self.opts_1['hadoop_extra_args'], ['thing1'])
self.assertEqual(self.opts_2['hadoop_extra_args'],
['thing1', 'thing2'])
def test_combine_paths(self):
self.assertEqual(self.opts_1['base_tmp_dir'], '/tmp')
self.assertEqual(self.opts_2['base_tmp_dir'], '/var/tmp')
def test_combine_path_lists(self):
self.assertEqual(self.opts_1['setup_scripts'], ['/myscript.py'])
self.assertEqual(self.opts_2['setup_scripts'],
['/myscript.py', '/yourscript.py'])
def test_combine_values(self):
self.assertEqual(self.opts_1['hadoop_streaming_jar'], 'monkey.jar')
self.assertEqual(self.opts_2['hadoop_streaming_jar'], 'banana.jar')
class MultipleConfigFilesMachineryTestCase(ConfigFilesTestCase):
def test_recurse(self):
path = os.path.join(self.tmp_dir, 'LOL.conf')
recurse_conf = dict(include=path)
with open(path, 'w') as f:
dump_mrjob_conf(recurse_conf, f)
stderr = StringIO()
with no_handlers_for_logger():
log_to_stream('mrjob.conf', stderr)
RunnerOptionStore('inline', {}, [path])
self.assertIn('%s tries to recursively include %s!' % (path, path),
stderr.getvalue())
def test_empty_runner_error(self):
conf = dict(runner=dict(local=dict(base_tmp_dir='/tmp')))
path = self.save_conf('basic', conf)
stderr = StringIO()
with no_handlers_for_logger():
log_to_stream('mrjob.runner', stderr)
RunnerOptionStore('inline', {}, [path])
self.assertEqual(
"No configs specified for inline runner\n",
stderr.getvalue())
def test_conf_contain_only_include_file(self):
"""If a config file only include other configuration files
no warnings are thrown as long as the included files are
not empty.
"""
# dummy configuration for include file 1
conf = {
'runners': {
'inline': {
'base_tmp_dir': "include_file1_base_tmp_dir"
}
}
}
include_file_1 = self.save_conf('include_file_1', conf)
# dummy configuration for include file 2
conf = {
'runners': {
'inline': {
'base_tmp_dir': "include_file2_base_tmp_dir"
}
}
}
include_file_2 = self.save_conf('include_file_2', conf)
# test configuration
conf = {
'include': [include_file_1, include_file_2]
}
path = self.save_conf('twoincludefiles', conf)
stderr = StringIO()
with no_handlers_for_logger():
log_to_stream('mrjob.runner', stderr)
RunnerOptionStore('inline', {}, [path])
self.assertEqual(
"",
stderr.getvalue())
class MultipleMultipleConfigFilesTestCase(ConfigFilesTestCase):
BASE_CONFIG_LEFT = {
'runners': {
'inline': {
'jobconf': dict(from_left=1, from_both=1),
'label': 'i_dont_like_to_be_labelled',
}
}
}
BASE_CONFIG_RIGHT = {
'runners': {
'inline': {
'jobconf': dict(from_right=2, from_both=2),
'owner': 'ownership_is_against_my_principles'
}
}
}
def test_mrjob_has_multiple_inheritance_next_lets_add_generics(self):
path_left = self.save_conf('left.conf', self.BASE_CONFIG_LEFT)
path_right = self.save_conf('right.conf', self.BASE_CONFIG_RIGHT)
opts_both = self.opts_for_conf('both.conf',
dict(include=[path_left, path_right]))
self.assertEqual(opts_both['jobconf'],
dict(from_left=1, from_both=2, from_right=2))
self.assertEqual(opts_both['label'],
'i_dont_like_to_be_labelled')
self.assertEqual(opts_both['owner'],
'ownership_is_against_my_principles')
def test_multiple_configs_via_runner_args(self):
path_left = self.save_conf('left.conf', self.BASE_CONFIG_LEFT)
path_right = self.save_conf('right.conf', self.BASE_CONFIG_RIGHT)
opts = RunnerOptionStore('inline', {}, [path_left, path_right])
self.assertEqual(opts['jobconf'],
dict(from_left=1, from_both=2, from_right=2))
class TestExtraKwargs(ConfigFilesTestCase):
CONFIG = {'runners': {'inline': {
'qux': 'quux',
'setup_cmds': ['echo foo']}}}
def setUp(self):
super(TestExtraKwargs, self).setUp()
self.path = self.save_conf('config', self.CONFIG)
def test_extra_kwargs_in_mrjob_conf_okay(self):
with logger_disabled('mrjob.runner'):
opts = RunnerOptionStore('inline', {}, [self.path])
self.assertEqual(opts['setup_cmds'], ['echo foo'])
self.assertNotIn('qux', opts)
def test_extra_kwargs_passed_in_directly_okay(self):
with logger_disabled('mrjob.runner'):
opts = RunnerOptionStore(
'inline', {'base_tmp_dir': '/var/tmp', 'foo': 'bar'}, [])
self.assertEqual(opts['base_tmp_dir'], '/var/tmp')
self.assertNotIn('bar', opts)