This file is indexed.

/usr/lib/python2.7/dist-packages/os_faults/tests/unit/ansible/test_executor.py is in python-os-faults 0.1.17-0ubuntu1.1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
# 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.

import ddt
import mock

from os_faults.ansible import executor
from os_faults.api import node_collection
from os_faults.tests.unit import test


class MyCallbackTestCase(test.TestCase):

    def test__store(self,):
        ex = executor.MyCallback(mock.Mock())

        my_host = 'my_host'
        my_task = 'my_task'
        my_result = 'my_result'
        r = mock.Mock()
        r._host.get_name.return_value = my_host
        r._task.get_name.return_value = my_task
        r._result = my_result
        stat = 'OK'

        ex._store(r, stat)
        ex.storage.append.assert_called_once_with(
            executor.AnsibleExecutionRecord(host=my_host, status=stat,
                                            task=my_task, payload=my_result))

    @mock.patch('ansible.plugins.callback.CallbackBase.v2_runner_on_failed')
    @mock.patch('os_faults.ansible.executor.MyCallback._store')
    def test_v2_runner_on_failed_super(self, mock_store, mock_callback):
        ex = executor.MyCallback(mock.Mock())
        result = mock.Mock()
        ex.v2_runner_on_failed(result)
        mock_callback.assert_called_once_with(result)

    @mock.patch('os_faults.ansible.executor.MyCallback._store')
    def test_v2_runner_on_failed(self, mock_store):
        result = mock.Mock()
        ex = executor.MyCallback(mock.Mock())
        ex.v2_runner_on_failed(result)
        mock_store.assert_called_once_with(result, executor.STATUS_FAILED)

    @mock.patch('ansible.plugins.callback.CallbackBase.v2_runner_on_ok')
    @mock.patch('os_faults.ansible.executor.MyCallback._store')
    def test_v2_runner_on_ok_super(self, mock_store, mock_callback):
        ex = executor.MyCallback(mock.Mock())
        result = mock.Mock()
        ex.v2_runner_on_ok(result)
        mock_callback.assert_called_once_with(result)

    @mock.patch('os_faults.ansible.executor.MyCallback._store')
    def test_v2_runner_on_ok(self, mock_store):
        result = mock.Mock()
        ex = executor.MyCallback(mock.Mock())
        ex.v2_runner_on_ok(result)
        mock_store.assert_called_once_with(result, executor.STATUS_OK)

    @mock.patch('ansible.plugins.callback.CallbackBase.v2_runner_on_skipped')
    @mock.patch('os_faults.ansible.executor.MyCallback._store')
    def test_v2_runner_on_skipped_super(self, mock_store, mock_callback):
        ex = executor.MyCallback(mock.Mock())
        result = mock.Mock()
        ex.v2_runner_on_skipped(result)
        mock_callback.assert_called_once_with(result)

    @mock.patch('os_faults.ansible.executor.MyCallback._store')
    def test_v2_runner_on_skipped(self, mock_store):
        result = mock.Mock()
        ex = executor.MyCallback(mock.Mock())
        ex.v2_runner_on_skipped(result)
        mock_store.assert_called_once_with(result, executor.STATUS_SKIPPED)

    @mock.patch(
        'ansible.plugins.callback.CallbackBase.v2_runner_on_unreachable')
    @mock.patch('os_faults.ansible.executor.MyCallback._store')
    def test_v2_runner_on_unreachable_super(self, mock_store, mock_callback):
        ex = executor.MyCallback(mock.Mock())
        result = mock.Mock()
        ex.v2_runner_on_unreachable(result)
        mock_callback.assert_called_once_with(result)

    @mock.patch('os_faults.ansible.executor.MyCallback._store')
    def test_v2_runner_on_unreachable(self, mock_store):
        result = mock.Mock()
        ex = executor.MyCallback(mock.Mock())
        ex.v2_runner_on_unreachable(result)
        mock_store.assert_called_once_with(result, executor.STATUS_UNREACHABLE)


@ddt.ddt
class AnsibleRunnerTestCase(test.TestCase):

    @mock.patch('os_faults.ansible.executor.os.path.exists')
    def test_resolve_relative_path_doesnt_exist(self, mock_exist):
        mock_exist.return_value = False
        r = executor.resolve_relative_path('')
        self.assertIsNone(r)

    @mock.patch('os_faults.ansible.executor.os.path.exists')
    def test_resolve_relative_path_exists(self, mock_exist):
        mock_exist.return_value = True
        r = executor.resolve_relative_path('')
        self.assertIsNotNone(r)

    @mock.patch.object(executor, 'Options')
    @ddt.data((
        {},
        dict(become=None, become_method='sudo', become_user='root',
             check=False, connection='smart', diff=None, forks=100,
             private_key_file=None,
             remote_user='root', scp_extra_args=None, sftp_extra_args=None,
             ssh_common_args=executor.SSH_COMMON_ARGS,
             ssh_extra_args=None, verbosity=100),
        dict(conn_pass=None, become_pass=None),
    ), (
        dict(remote_user='root', password='foobar'),
        dict(become=None, become_method='sudo', become_user='root',
             check=False, connection='smart', diff=None, forks=100,
             private_key_file=None,
             remote_user='root', scp_extra_args=None, sftp_extra_args=None,
             ssh_common_args=executor.SSH_COMMON_ARGS,
             ssh_extra_args=None, verbosity=100),
        dict(conn_pass='foobar', become_pass=None),
    ), (
        dict(remote_user='root', password='foobar', become_password='secret'),
        dict(become=None, become_method='sudo', become_user='root',
             check=False, connection='smart', diff=None, forks=100,
             private_key_file=None,
             remote_user='root', scp_extra_args=None, sftp_extra_args=None,
             ssh_common_args=executor.SSH_COMMON_ARGS,
             ssh_extra_args=None, verbosity=100),
        dict(conn_pass='foobar', become_pass='secret'),
    ), (
        dict(remote_user='root', jump_host='jhost.com',
             private_key_file='/path/my.key'),
        dict(become=None, become_method='sudo', become_user='root',
             check=False, connection='smart', diff=None, forks=100,
             private_key_file='/path/my.key',
             remote_user='root', scp_extra_args=None, sftp_extra_args=None,
             ssh_common_args=('-o UserKnownHostsFile=/dev/null '
                              '-o StrictHostKeyChecking=no '
                              '-o ConnectTimeout=60 '
                              '-o ProxyCommand='
                              '"ssh -i /path/my.key '
                              '-W %h:%p '
                              '-o UserKnownHostsFile=/dev/null '
                              '-o StrictHostKeyChecking=no '
                              '-o ConnectTimeout=60 '
                              'root@jhost.com"'),
             ssh_extra_args=None, verbosity=100),
        dict(conn_pass=None, become_pass=None),
    ), (
        dict(remote_user='root', jump_host='jhost.com', jump_user='juser',
             private_key_file='/path/my.key'),
        dict(become=None, become_method='sudo', become_user='root',
             check=False, connection='smart', diff=None, forks=100,
             private_key_file='/path/my.key',
             remote_user='root', scp_extra_args=None, sftp_extra_args=None,
             ssh_common_args=('-o UserKnownHostsFile=/dev/null '
                              '-o StrictHostKeyChecking=no '
                              '-o ConnectTimeout=60 '
                              '-o ProxyCommand='
                              '"ssh -i /path/my.key '
                              '-W %h:%p '
                              '-o UserKnownHostsFile=/dev/null '
                              '-o StrictHostKeyChecking=no '
                              '-o ConnectTimeout=60 '
                              'juser@jhost.com"'),
             ssh_extra_args=None, verbosity=100),
        dict(conn_pass=None, become_pass=None),
    ))
    @ddt.unpack
    def test___init__options(self, config, options_args, passwords,
                             mock_options):
        runner = executor.AnsibleRunner(**config)
        module_path = executor.make_module_path_option()
        mock_options.assert_called_once_with(module_path=module_path,
                                             **options_args)
        self.assertEqual(passwords, runner.passwords)

    @mock.patch.object(executor.task_queue_manager, 'TaskQueueManager')
    @mock.patch('ansible.playbook.play.Play.load')
    @mock.patch('os_faults.ansible.executor.Inventory')
    @mock.patch('os_faults.ansible.executor.VariableManager')
    @mock.patch('ansible.parsing.dataloader.DataLoader')
    def test__run_play(self, mock_dataloader, mock_vmanager, mock_inventory,
                       mock_play_load, mock_taskqm):
        mock_play_load.return_value = 'my_load'
        variable_manager = mock_vmanager.return_value
        host_inst = mock_inventory.return_value.get_host.return_value
        host_vars = {
            '0.0.0.0': {
                'ansible_user': 'foo',
                'ansible_ssh_pass': 'bar',
                'ansible_become': True,
                'ansible_ssh_private_key_file': None,
                'ansible_ssh_common_args': '-o Option=yes',
            }
        }
        ex = executor.AnsibleRunner()
        ex._run_play({'hosts': ['0.0.0.0']}, host_vars)

        mock_taskqm.assert_called_once()
        self.assertEqual(mock_taskqm.mock_calls[1], mock.call().run('my_load'))
        self.assertEqual(mock_taskqm.mock_calls[2], mock.call().cleanup())

        variable_manager.set_host_variable.assert_has_calls((
            mock.call(host_inst, 'ansible_user', 'foo'),
            mock.call(host_inst, 'ansible_ssh_pass', 'bar'),
            mock.call(host_inst, 'ansible_become', True),
            mock.call(host_inst, 'ansible_ssh_common_args', '-o Option=yes'),
        ), any_order=True)

    @mock.patch.object(executor.task_queue_manager, 'TaskQueueManager')
    @mock.patch('ansible.playbook.play.Play.load')
    @mock.patch('os_faults.ansible.executor.Inventory')
    @mock.patch('os_faults.ansible.executor.VariableManager')
    @mock.patch('ansible.parsing.dataloader.DataLoader')
    def test__run_play_no_host_vars(
            self, mock_dataloader, mock_vmanager, mock_inventory,
            mock_play_load, mock_taskqm):
        mock_play_load.return_value = 'my_load'
        variable_manager = mock_vmanager.return_value
        host_vars = {}
        ex = executor.AnsibleRunner()
        ex._run_play({'hosts': ['0.0.0.0']}, host_vars)

        mock_taskqm.assert_called_once()
        self.assertEqual(mock_taskqm.mock_calls[1], mock.call().run('my_load'))
        self.assertEqual(mock_taskqm.mock_calls[2], mock.call().cleanup())

        self.assertEqual(0, variable_manager.set_host_variable.call_count)

    @mock.patch('os_faults.ansible.executor.AnsibleRunner._run_play')
    def test_run_playbook(self, mock_run_play):
        ex = executor.AnsibleRunner()
        my_playbook = [{'gather_facts': 'yes'}, {'gather_facts': 'no'}]
        ex.run_playbook(my_playbook, {})

        self.assertEqual(my_playbook, [{'gather_facts': 'no'},
                                       {'gather_facts': 'no'}])
        self.assertEqual(mock_run_play.call_count, 2)

    @mock.patch('os_faults.ansible.executor.AnsibleRunner.run_playbook')
    def test_execute(self, mock_run_playbook):
        my_hosts = [node_collection.Host('0.0.0.0'),
                    node_collection.Host('255.255.255.255')]
        my_tasks = 'my_task'
        ex = executor.AnsibleRunner()
        ex.execute(my_hosts, my_tasks)
        mock_run_playbook.assert_called_once_with(
            [{'tasks': ['my_task'],
              'hosts': ['0.0.0.0', '255.255.255.255'],
              'serial': 10}], {'0.0.0.0': {}, '255.255.255.255': {}})

    @mock.patch('os_faults.ansible.executor.AnsibleRunner.run_playbook')
    def test_execute_with_host_vars(self, mock_run_playbook):
        my_hosts = [
            node_collection.Host('0.0.0.0', auth={'username': 'foo',
                                                  'password': 'bar',
                                                  'sudo': True}),
            node_collection.Host('255.255.255.255',
                                 auth={'jump': {'host': '192.168.1.100',
                                                'username': 'foo'}})]
        my_tasks = 'my_task'
        ex = executor.AnsibleRunner()
        ex.execute(my_hosts, my_tasks)
        mock_run_playbook.assert_called_once_with(
            [{'tasks': ['my_task'],
              'hosts': ['0.0.0.0', '255.255.255.255'],
              'serial': 10}],
            {
                '0.0.0.0': {
                    'ansible_user': 'foo',
                    'ansible_ssh_pass': 'bar',
                    'ansible_become': True,
                    'ansible_become_password': None,
                    'ansible_ssh_private_key_file': None,
                    'ansible_ssh_common_args': None,
                },
                '255.255.255.255': {
                    'ansible_user': None,
                    'ansible_ssh_pass': None,
                    'ansible_become': None,
                    'ansible_become_password': None,
                    'ansible_ssh_private_key_file': None,
                    'ansible_ssh_common_args':
                        '-o UserKnownHostsFile=/dev/null '
                        '-o StrictHostKeyChecking=no '
                        '-o ConnectTimeout=60 '
                        '-o ProxyCommand="'
                        'ssh  -W %h:%p '
                        '-o UserKnownHostsFile=/dev/null '
                        '-o StrictHostKeyChecking=no '
                        '-o ConnectTimeout=60 '
                        'foo@192.168.1.100"'}})

    @mock.patch('os_faults.ansible.executor.AnsibleRunner.run_playbook')
    def test_execute_with_serial(self, mock_run_playbook):
        my_hosts = [node_collection.Host('0.0.0.0'),
                    node_collection.Host('255.255.255.255')]
        my_tasks = 'my_task'
        ex = executor.AnsibleRunner(serial=50)
        ex.execute(my_hosts, my_tasks)
        mock_run_playbook.assert_called_once_with(
            [{'tasks': ['my_task'],
              'hosts': ['0.0.0.0', '255.255.255.255'],
              'serial': 50}], {'0.0.0.0': {}, '255.255.255.255': {}})

    @mock.patch('os_faults.ansible.executor.AnsibleRunner.run_playbook')
    def test_execute_status_unreachable(self, mock_run_playbook):
        my_hosts = [node_collection.Host('0.0.0.0'),
                    node_collection.Host('255.255.255.255')]
        my_tasks = 'my_task'
        my_statuses = {executor.STATUS_FAILED,
                       executor.STATUS_SKIPPED, executor.STATUS_UNREACHABLE}
        r0 = executor.AnsibleExecutionRecord(
            host='0.0.0.0', status=executor.STATUS_OK, task={}, payload={})
        r1 = executor.AnsibleExecutionRecord(
            host='255.255.255.255', status=executor.STATUS_UNREACHABLE,
            task={}, payload={})

        mock_run_playbook.return_value = [r0, r1]
        ex = executor.AnsibleRunner()

        err = self.assertRaises(executor.AnsibleExecutionException,
                                ex.execute, my_hosts, my_tasks, my_statuses)
        self.assertEqual(type(err), executor.AnsibleExecutionUnreachable)

    @mock.patch('os_faults.ansible.executor.AnsibleRunner.run_playbook')
    def test_execute_status_failed(self, mock_run_playbook):
        my_hosts = [node_collection.Host('0.0.0.0'),
                    node_collection.Host('255.255.255.255')]
        my_tasks = 'my_task'
        my_statuses = {executor.STATUS_OK, executor.STATUS_FAILED,
                       executor.STATUS_SKIPPED, executor.STATUS_UNREACHABLE}
        r0 = executor.AnsibleExecutionRecord(
            host='0.0.0.0', status=executor.STATUS_OK, task={}, payload={})
        r1 = executor.AnsibleExecutionRecord(
            host='255.255.255.255', status=executor.STATUS_UNREACHABLE,
            task={}, payload={})

        mock_run_playbook.return_value = [r0, r1]
        ex = executor.AnsibleRunner()

        err = self.assertRaises(executor.AnsibleExecutionException,
                                ex.execute, my_hosts, my_tasks, my_statuses)
        self.assertEqual(type(err), executor.AnsibleExecutionException)

    @mock.patch('copy.deepcopy')
    @mock.patch('os_faults.ansible.executor.AnsibleRunner.run_playbook')
    def test_execute_stdout_is_more_than_stdout_limit(
            self, mock_run_playbook, mock_deepcopy):
        result = mock.Mock()
        result.payload = {'stdout': 'a' * (executor.STDOUT_LIMIT + 1),
                          'stdout_lines': 'a' * (executor.STDOUT_LIMIT + 1)}
        mock_run_playbook.return_value = [result]

        mock_deepcopy.return_value = [result]
        log_result = mock_deepcopy.return_value[0]

        my_hosts = [node_collection.Host('0.0.0.0'),
                    node_collection.Host('255.255.255.255')]
        my_tasks = 'my_task'
        ex = executor.AnsibleRunner()
        ex.execute(my_hosts, my_tasks)

        self.assertEqual('a' * executor.STDOUT_LIMIT + '... <cut>',
                         log_result.payload['stdout'])

    @mock.patch('os_faults.ansible.executor.LOG.debug')
    @mock.patch('os_faults.ansible.executor.AnsibleRunner.run_playbook')
    def test_execute_payload_without_stdout(self, mock_run_playbook,
                                            mock_debug):
        task = {'task': 'foo'}
        host = '0.0.0.0'
        result = executor.AnsibleExecutionRecord(
            host=host, status=executor.STATUS_OK,
            task=task, payload={'foo': 'bar'})
        mock_run_playbook.return_value = [result]

        hosts = [node_collection.Host('0.0.0.0')]
        ex = executor.AnsibleRunner()
        ex.execute(hosts, task)

        mock_debug.assert_has_calls((
            mock.call('Executing task: %s on hosts: %s with serial: %s',
                      task, hosts, 10),
            mock.call('Execution completed with 1 result(s):'),
            mock.call(result),
        ))

    @mock.patch('os_faults.executor.get_module_paths')
    @mock.patch('os_faults.executor.PRE_24_ANSIBLE', False)
    def test_make_module_path_option_ansible_24(self, mock_mp):
        mock_mp.return_value = ['/path/one', 'path/two']
        self.assertEqual(['/path/one', 'path/two'],
                         executor.make_module_path_option())

    @mock.patch('os_faults.executor.get_module_paths')
    @mock.patch('os_faults.executor.PRE_24_ANSIBLE', False)
    def test_make_module_path_option_ansible_24_one_item(self, mock_mp):
        mock_mp.return_value = ['/path/one']
        self.assertEqual(['/path/one', '/path/one'],
                         executor.make_module_path_option())

    @mock.patch('os_faults.executor.get_module_paths')
    @mock.patch('os_faults.executor.PRE_24_ANSIBLE', True)
    def test_make_module_path_option_ansible_pre24(self, mock_mp):
        mock_mp.return_value = ['/path/one', 'path/two']
        self.assertEqual('/path/one:path/two',
                         executor.make_module_path_option())