Example #1
0
    def test_get_error_lines_from_file(self):
        m = mock_open()
        m.return_value.readlines.return_value = ['this is line 1\n']

        with patch('{0}.open'.format(BUILTINS), m):
            # this line will be found in the file
            self.obj.ansible_pos = ('foo.yml', 1, 1)
            e = AnsibleError(self.message, self.obj)
            self.assertEqual(e.message, (
                "This is the error message\n\nThe error appears to be in 'foo.yml': line 1, column 1, but may\nbe elsewhere in the file depending on "
                "the exact syntax problem.\n\nThe offending line appears to be:\n\n\nthis is line 1\n^ here\n"
            ))

            # this line will not be found, as it is out of the index range
            self.obj.ansible_pos = ('foo.yml', 2, 1)
            e = AnsibleError(self.message, self.obj)
            self.assertEqual(e.message, (
                "This is the error message\n\nThe error appears to be in 'foo.yml': line 2, column 1, but may\nbe elsewhere in the file depending on "
                "the exact syntax problem.\n\n(specified line no longer in file, maybe it changed?)"
            ))

        m = mock_open()
        m.return_value.readlines.return_value = [
            'this line has unicode \xf0\x9f\x98\xa8 in it!\n'
        ]

        with patch('{0}.open'.format(BUILTINS), m):
            # this line will be found in the file
            self.obj.ansible_pos = ('foo.yml', 1, 1)
            e = AnsibleError(self.unicode_message, self.obj)
            self.assertEqual(e.message, (
                "This is an error with \xf0\x9f\x98\xa8 in it\n\nThe error appears to be in 'foo.yml': line 1, column 1, but may\nbe elsewhere in the "
                "file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\nthis line has unicode \xf0\x9f\x98\xa8 in it!\n^ "
                "here\n"))
Example #2
0
    def test_module_utils_basic_ansible_module_is_special_selinux_path(self):
        from ansible.module_utils import basic

        args = json.dumps(
            dict(
                ANSIBLE_MODULE_ARGS={
                    '_ansible_selinux_special_fs': "nfs,nfsd,foos",
                    '_ansible_remote_tmp': "/tmp",
                    '_ansible_keep_remote_files': False
                }))

        with swap_stdin_and_argv(stdin_data=args):
            basic._ANSIBLE_ARGS = None
            am = basic.AnsibleModule(argument_spec=dict(), )

            def _mock_find_mount_point(path):
                if path.startswith('/some/path'):
                    return '/some/path'
                elif path.startswith('/weird/random/fstype'):
                    return '/weird/random/fstype'
                return '/'

            am.find_mount_point = MagicMock(side_effect=_mock_find_mount_point)
            am.selinux_context = MagicMock(
                return_value=['foo_u', 'foo_r', 'foo_t', 's0'])

            m = mock_open()
            m.side_effect = OSError

            with patch.object(builtins, 'open', m, create=True):
                self.assertEqual(
                    am.is_special_selinux_path(
                        '/some/path/that/should/be/nfs'), (False, None))

            mount_data = [
                '/dev/disk1 / ext4 rw,seclabel,relatime,data=ordered 0 0\n',
                '10.1.1.1:/path/to/nfs /some/path nfs ro 0 0\n',
                'whatever /weird/random/fstype foos rw 0 0\n',
            ]

            # mock_open has a broken readlines() implementation apparently...
            # this should work by default but doesn't, so we fix it
            m = mock_open(read_data=''.join(mount_data))
            m.return_value.readlines.return_value = mount_data

            with patch.object(builtins, 'open', m, create=True):
                self.assertEqual(
                    am.is_special_selinux_path('/some/random/path'),
                    (False, None))
                self.assertEqual(
                    am.is_special_selinux_path(
                        '/some/path/that/should/be/nfs'),
                    (True, ['foo_u', 'foo_r', 'foo_t', 's0']))
                self.assertEqual(
                    am.is_special_selinux_path('/weird/random/fstype/path'),
                    (True, ['foo_u', 'foo_r', 'foo_t', 's0']))
Example #3
0
    def test_is_firmware_bundled_pass(self):
        """Determine whether firmware file is bundled."""
        self._set_args({"firmware": "test.dlp", "nvsram": "test.dlp"})
        with patch(builtin_path,
                   mock_open(read_data=b"firmwarexxxxxxxx")) as mock_file:
            firmware = NetAppESeriesFirmware()
            self.assertEqual(firmware.is_firmware_bundled(), False)

        self._set_args({"firmware": "test.dlp", "nvsram": "test.dlp"})
        with patch(builtin_path,
                   mock_open(read_data=self.BUNDLE_HEADER[:16])) as mock_file:
            firmware = NetAppESeriesFirmware()
            self.assertEqual(firmware.is_firmware_bundled(), True)
Example #4
0
 def test_firmware_update_success_case03(self, redfish_default_args,
                                         redfish_firmware_connection_mock,
                                         redfish_response_mock, mocker):
     mocker.patch(
         "ansible.modules.remote_management.dellemc.redfish_firmware._get_update_service_target",
         return_value=('2134', 'nhttp://dell.com', 'multipart/form-data'))
     mocker.patch(
         "ansible.modules.remote_management.dellemc.redfish_firmware._encode_form_data",
         return_value=({
             "file": (3, "nhttp://dell.com", "multipart/form-data")
         }, "multipart/form-data"))
     redfish_default_args.update({
         "image_uri": "nhttp://home/firmware_repo/component.exe",
         "transfer_protocol": "HTTP"
     })
     f_module = self.get_module_mock(params=redfish_default_args)
     redfish_response_mock.status_code = 201
     redfish_response_mock.success = True
     redfish_response_mock.json_data = {
         "image_uri": "nhttp://home/firmware_repo/component.exe",
         "transfer_protocol": "HTTP"
     }
     if sys.version_info.major == 3:
         builtin_module_name = 'builtins'
     else:
         builtin_module_name = '__builtin__'
     with patch("{0}.open".format(builtin_module_name),
                mock_open(read_data="data")) as mock_file:
         result = self.module.firmware_update(
             redfish_firmware_connection_mock, f_module)
     assert result == redfish_response_mock
Example #5
0
    def test_content_written(self):

        with patch.object(builtins, 'open', mock_open()) as m:
            password._write_password_file(b'/this/is/a/test/caf\xc3\xa9', u'Testing Café')

            m.assert_called_once_with(b'/this/is/a/test/caf\xc3\xa9', 'wb')
            m().write.assert_called_once_with(u'Testing Café\n'.encode('utf-8'))
Example #6
0
    def test_password_already_created_encrypt(self, mock_get_paths, mock_write_file):
        mock_get_paths.return_value = ['/path/one', '/path/two', '/path/three']
        password.os.path.exists = lambda x: x == to_bytes('/path/to/somewhere')

        with patch.object(builtins, 'open', mock_open(read_data=b'hunter42 salt=87654321\n')) as m:
            results = self.password_lookup.run([u'/path/to/somewhere chars=anything encrypt=pbkdf2_sha256'], None)
        for result in results:
            self.assertEqual(result, u'$pbkdf2-sha256$20000$ODc2NTQzMjE$Uikde0cv0BKaRaAXMrUQB.zvG4GmnjClwjghwIRf2gU')
Example #7
0
    def test_password_already_created_no_encrypt(self, mock_get_paths, mock_write_file):
        mock_get_paths.return_value = ['/path/one', '/path/two', '/path/three']
        password.os.path.exists = lambda x: x == to_bytes('/path/to/somewhere')

        with patch.object(builtins, 'open', mock_open(read_data=b'hunter42 salt=87654321\n')) as m:
            results = self.password_lookup.run([u'/path/to/somewhere chars=anything'], None)

        for result in results:
            self.assertEqual(result, u'hunter42')
Example #8
0
def mock_open_with_iter(*args, **kwargs):
    mock = mock_open(*args, **kwargs)

    if six.PY2:
        mock.return_value.__iter__ = lambda x: iter(x.readline, "")
    else:
        mock.return_value.__iter__ = lambda x: x
        mock.return_value.__next__ = lambda x: iter(x.readline, "")
    return mock
Example #9
0
    def test_upload_file_raises_exception_when_invalid_response(self):
        self.connection_mock.send.return_value = self._connection_response('invalidJsonResponse')

        open_mock = mock_open()
        with patch('%s.open' % BUILTINS_NAME, open_mock):
            with self.assertRaises(ConnectionError) as res:
                self.ftd_plugin.upload_file('/tmp/test.txt', '/files')

        assert 'Invalid JSON response' in str(res.exception)
Example #10
0
 def test_is_firmware_bundles_fail(self):
     """Verify non-firmware fails."""
     self._set_args({"firmware": "test.dlp", "nvsram": "test.dlp"})
     with patch(builtin_path,
                mock_open(read_data=b"xxxxxxxxxxxxxxxx")) as mock_file:
         firmware = NetAppESeriesFirmware()
         with self.assertRaisesRegexp(AnsibleFailJson,
                                      "Firmware file is invalid."):
             firmware.is_firmware_bundled()
Example #11
0
    def test_download_file(self):
        self.connection_mock.send.return_value = self._connection_response('File content')

        open_mock = mock_open()
        with patch('%s.open' % BUILTINS_NAME, open_mock):
            self.ftd_plugin.download_file('/files/1', '/tmp/test.txt')

        open_mock.assert_called_once_with('/tmp/test.txt', 'wb')
        open_mock().write.assert_called_once_with(b'File content')
def test_systemid_with_requirements(capfd, mocker, patch_rhn):
    """Check 'msg' and 'changed' results"""

    mocker.patch.object(rhn_register.Rhn, 'enable')
    mock_isfile = mocker.patch('os.path.isfile', return_value=True)
    mocker.patch('ansible.modules.packaging.os.rhn_register.open',
                 mock_open(read_data=SYSTEMID),
                 create=True)
    rhn = rhn_register.Rhn()
    assert '123456789' == to_native(rhn.systemid)
Example #13
0
 def test_lock_been_held(self, mock_sleep):
     # pretend the lock file is here
     password.os.path.exists = lambda x: True
     try:
         with patch.object(builtins, 'open', mock_open(read_data=b'hunter42 salt=87654321\n')) as m:
             # should timeout here
             results = self.password_lookup.run([u'/path/to/somewhere chars=anything'], None)
             self.fail("Lookup didn't timeout when lock already been held")
     except AnsibleError:
         pass
Example #14
0
    def test_lock_not_been_held(self):
        # pretend now there is password file but no lock
        password.os.path.exists = lambda x: x == to_bytes('/path/to/somewhere')
        try:
            with patch.object(builtins, 'open', mock_open(read_data=b'hunter42 salt=87654321\n')) as m:
                # should not timeout here
                results = self.password_lookup.run([u'/path/to/somewhere chars=anything'], None)
        except AnsibleError:
            self.fail('Lookup timeouts when lock is free')

        for result in results:
            self.assertEqual(result, u'hunter42')
Example #15
0
    def test_download_file_should_extract_filename_from_headers(self):
        filename = 'test_file.txt'
        response = mock.Mock()
        response.info.return_value = {'Content-Disposition': 'attachment; filename="%s"' % filename}
        dummy, response_data = self._connection_response('File content')
        self.connection_mock.send.return_value = response, response_data

        open_mock = mock_open()
        with patch('%s.open' % BUILTINS_NAME, open_mock):
            self.ftd_plugin.download_file('/files/1', '/tmp/')

        open_mock.assert_called_once_with('/tmp/%s' % filename, 'wb')
        open_mock().write.assert_called_once_with(b'File content')
Example #16
0
 def test_validate_credentials_file(self):
     # TODO(supertom): Only dealing with p12 here, check the other states
     # of this function
     module = FakeModule()
     with mock.patch("ansible.module_utils.gcp.open",
                     mock.mock_open(read_data='foobar'), create=True):
         # pem condition, warning is suppressed with the return_value
         credentials_file = '/foopath/pem.pem'
         with self.assertRaises(ValueError):
             _validate_credentials_file(module,
                                        credentials_file=credentials_file,
                                        require_valid_json=False,
                                        check_libcloud=False)
Example #17
0
    def test_upload_file(self):
        self.connection_mock.send.return_value = self._connection_response({'id': '123'})

        open_mock = mock_open()
        with patch('%s.open' % BUILTINS_NAME, open_mock):
            resp = self.ftd_plugin.upload_file('/tmp/test.txt', '/files')

        assert {'id': '123'} == resp
        exp_headers = dict(BASE_HEADERS)
        exp_headers['Content-Length'] = len('--Encoded data--')
        exp_headers['Content-Type'] = 'multipart/form-data'
        self.connection_mock.send.assert_called_once_with('/files', data='--Encoded data--',
                                                          headers=exp_headers, method=HTTPMethod.POST)
        open_mock.assert_called_once_with('/tmp/test.txt', 'rb')
Example #18
0
    def test_get_error_lines_error_in_last_line(self):
        m = mock_open()
        m.return_value.readlines.return_value = [
            'this is line 1\n', 'this is line 2\n', 'this is line 3\n'
        ]

        with patch('{0}.open'.format(BUILTINS), m):
            # If the error occurs in the last line of the file, use the correct index to get the line
            # and avoid the IndexError
            self.obj.ansible_pos = ('foo.yml', 4, 1)
            e = AnsibleError(self.message, self.obj)
            self.assertEqual(e.message, (
                "This is the error message\n\nThe error appears to be in 'foo.yml': line 4, column 1, but may\nbe elsewhere in the file depending on "
                "the exact syntax problem.\n\nThe offending line appears to be:\n\nthis is line 2\nthis is line 3\n^ here\n"
            ))
Example #19
0
    def test_get_error_lines_error_empty_lines_around_error(self):
        """Test that trailing whitespace after the error is removed"""
        m = mock_open()
        m.return_value.readlines.return_value = [
            'this is line 1\n', 'this is line 2\n', 'this is line 3\n', '  \n',
            '   \n', ' '
        ]

        with patch('{0}.open'.format(BUILTINS), m):
            self.obj.ansible_pos = ('foo.yml', 5, 1)
            e = AnsibleError(self.message, self.obj)
            self.assertEqual(e.message, (
                "This is the error message\n\nThe error appears to be in 'foo.yml': line 5, column 1, but may\nbe elsewhere in the file depending on "
                "the exact syntax problem.\n\nThe offending line appears to be:\n\nthis is line 2\nthis is line 3\n^ here\n"
            ))
def test_systemid_requirements_missing(capfd, mocker, patch_rhn,
                                       import_libxml):
    """Check that missing dependencies are detected"""

    mocker.patch('os.path.isfile', return_value=True)
    mocker.patch('ansible.modules.packaging.os.rhn_register.open',
                 mock_open(read_data=SYSTEMID),
                 create=True)

    with pytest.raises(SystemExit):
        rhn_register.main()

    out, err = capfd.readouterr()
    results = json.loads(out)
    assert results['failed']
    assert 'Missing arguments' in results['msg']
Example #21
0
    def test_parse_from_vault_1_1_file(self):
        vaulted_data = """$ANSIBLE_VAULT;1.1;AES256
33343734386261666161626433386662623039356366656637303939306563376130623138626165
6436333766346533353463636566313332623130383662340a393835656134633665333861393331
37666233346464636263636530626332623035633135363732623332313534306438393366323966
3135306561356164310a343937653834643433343734653137383339323330626437313562306630
3035
"""
        if PY3:
            builtins_name = 'builtins'
        else:
            builtins_name = '__builtin__'

        with patch(builtins_name + '.open',
                   mock_open(read_data=vaulted_data.encode('utf-8'))):
            output = self._loader.load_from_file('dummy_vault.txt')
            self.assertEqual(output, dict(foo='bar'))
Example #22
0
def test_verify_file_hash_matching_hash(manifest_info):

    data = to_bytes(json.dumps(manifest_info))
    digest = sha256(data).hexdigest()

    namespace = manifest_info['collection_info']['namespace']
    name = manifest_info['collection_info']['name']
    version = manifest_info['collection_info']['version']
    server = 'http://galaxy.ansible.com'

    error_queue = []

    with patch.object(builtins, 'open', mock_open(read_data=data)) as m:
        with patch.object(collection.os.path, 'isfile', MagicMock(return_value=True)) as mock_isfile:
            collection._verify_file_hash(b'path/', 'file', digest, error_queue)

            assert mock_isfile.called_once

    assert error_queue == []
Example #23
0
    def test_create_gtm_irule_src(self, *args):
        set_module_args(
            dict(name='foo',
                 module='gtm',
                 src='{0}/create_ltm_irule.tcl'.format(fixture_path),
                 partition='Common',
                 server='localhost',
                 password='******',
                 user='******'))

        module = AnsibleModule(
            argument_spec=self.spec.argument_spec,
            supports_check_mode=self.spec.supports_check_mode,
            mutually_exclusive=self.spec.mutually_exclusive,
        )

        if PY3:
            builtins_name = 'builtins'
        else:
            builtins_name = '__builtin__'

        with patch(builtins_name + '.open',
                   mock_open(read_data='this is my content'),
                   create=True):
            # Override methods in the specific type of manager
            tm = GtmManager(module=module, params=module.params)
            tm.exists = Mock(side_effect=[False, True])
            tm.create_on_device = Mock(return_value=True)

            # Override methods to force specific logic in the module to happen
            mm = ModuleManager(module=module)
            mm.get_manager = Mock(return_value=tm)

            results = mm.exec_module()

        assert results['changed'] is True
        assert results['content'] == 'this is my content'
        assert results['module'] == 'gtm'
        assert results['src'] == '{0}/create_ltm_irule.tcl'.format(
            fixture_path)
        assert len(results.keys()) == 4
Example #24
0
    def test_encrypt_wrapped_crypt_algo(self, mock_write_file):

        password.os.path.exists = self.password_lookup._loader.path_exists
        with patch.object(
                builtins, 'open',
                mock_open(read_data=self.password_lookup._loader.
                          _get_file_contents('/path/to/somewhere')[0])) as m:
            results = self.password_lookup.run(
                [u'/path/to/somewhere encrypt=ldap_sha256_crypt'], None)

            wrapper = getattr(passlib.hash, 'ldap_sha256_crypt')

            self.assertEqual(len(results), 1)
            result = results[0]
            self.assertIsInstance(result, text_type)

            expected_password_length = 76
            self.assertEqual(len(result), expected_password_length)

            # result should have 5 parts split by '$'
            str_parts = result.split('$')
            self.assertEqual(len(str_parts), 5)

            # verify the string and passlib agree on the number of rounds
            self.assertEqual(str_parts[2],
                             "rounds=%s" % wrapper.default_rounds)

            # verify it used the right algo type
            self.assertEqual(str_parts[0], '{CRYPT}')

            # verify it used the right algo type
            self.assertTrue(
                wrapper.verify(
                    self.password_lookup._loader._get_file_contents(
                        '/path/to/somewhere')[0], result))

            # verify a password with a non default rounds value
            # generated with: echo test | mkpasswd -s --rounds 660000 -m sha-256 --salt testansiblepass.
            hashpw = '{CRYPT}$5$rounds=660000$testansiblepass.$KlRSdA3iFXoPI.dEwh7AixiXW3EtCkLrlQvlYA2sluD'
            self.assertTrue(wrapper.verify('test', hashpw))
Example #25
0
    def test_stategy_get_never_writes_in_check_mode(self, isfile):
        isfile.return_value = True

        set_module_args({'name': 'fooname', '_ansible_check_mode': True})
        subclasses = get_all_subclasses(hostname.BaseStrategy)
        module = MagicMock()
        for cls in subclasses:
            instance = cls(module)

            instance.module.run_command = MagicMock()
            instance.module.run_command.return_value = (0, '', '')

            m = mock_open()
            builtins = 'builtins'
            if PY2:
                builtins = '__builtin__'
            with patch('%s.open' % builtins, m):
                instance.get_permanent_hostname()
                instance.get_current_hostname()
                self.assertFalse(m.return_value.write.called,
                                 msg='%s called write, should not have' %
                                 str(cls))
def manifest(manifest_info):
    b_data = to_bytes(json.dumps(manifest_info))

    with patch.object(builtins, 'open', mock_open(read_data=b_data)) as m:
        with open('MANIFEST.json', mode='rb') as fake_file:
            yield fake_file, sha256(b_data).hexdigest()
Example #27
0
    def test_action_base__configure_module(self):
        fake_loader = DictDataLoader({})

        # create our fake task
        mock_task = MagicMock()
        mock_task.action = "copy"
        mock_task.async_val = 0
        mock_task.delegate_to = None

        # create a mock connection, so we don't actually try and connect to things
        mock_connection = MagicMock()

        # create a mock shared loader object
        def mock_find_plugin(name, options, collection_list=None):
            if name == 'badmodule':
                return None
            elif '.ps1' in options:
                return '/fake/path/to/%s.ps1' % name
            else:
                return '/fake/path/to/%s' % name

        mock_module_loader = MagicMock()
        mock_module_loader.find_plugin.side_effect = mock_find_plugin
        mock_shared_obj_loader = MagicMock()
        mock_shared_obj_loader.module_loader = mock_module_loader

        # we're using a real play context here
        play_context = PlayContext()

        # our test class
        action_base = DerivedActionBase(
            task=mock_task,
            connection=mock_connection,
            play_context=play_context,
            loader=fake_loader,
            templar=Templar(loader=fake_loader),
            shared_loader_obj=mock_shared_obj_loader,
        )

        # test python module formatting
        with patch.object(
                builtins, 'open',
                mock_open(read_data=to_bytes(python_module_replacers.strip(),
                                             encoding='utf-8'))):
            with patch.object(os, 'rename'):
                mock_task.args = dict(a=1, foo='fö〩')
                mock_connection.module_implementation_preferences = ('', )
                (style, shebang, data, path) = action_base._configure_module(
                    mock_task.action,
                    mock_task.args,
                    task_vars=dict(
                        ansible_python_interpreter='/usr/bin/python'))
                self.assertEqual(style, "new")
                self.assertEqual(shebang, u"#!/usr/bin/python")

                # test module not found
                self.assertRaises(AnsibleError, action_base._configure_module,
                                  'badmodule', mock_task.args)

        # test powershell module formatting
        with patch.object(
                builtins, 'open',
                mock_open(read_data=to_bytes(
                    powershell_module_replacers.strip(), encoding='utf-8'))):
            mock_task.action = 'win_copy'
            mock_task.args = dict(b=2)
            mock_connection.module_implementation_preferences = ('.ps1', )
            (style, shebang, data,
             path) = action_base._configure_module('stat', mock_task.args)
            self.assertEqual(style, "new")
            self.assertEqual(shebang, u'#!powershell')

            # test module not found
            self.assertRaises(AnsibleError, action_base._configure_module,
                              'badmodule', mock_task.args)
Example #28
0
 def test_with_password_file(self):
     password.os.path.exists = lambda x: True
     with patch.object(builtins, 'open',
                       mock_open(read_data=b'Testing\n')) as m:
         self.assertEqual(password._read_password_file(b'/etc/motd'),
                          u'Testing')