def test_AnsibleRunner_constructor():
    setattr(__builtin__, "NS", maps.NamedDict())
    NS.publisher_id = 1
    NS["config"] = maps.NamedDict()
    NS.config["data"] = maps.NamedDict(logging_socket_path="test/path")
    NS.node_context = maps.NamedDict()
    NS.node_context.node_id = 1
    with patch.object(os.path, 'isfile', return_value=False):
        with pytest.raises(AnsibleModuleNotFound):
            AnsibleRunner("Test_module")
    with patch.object(os.path, 'isfile', return_value=True):
        with pytest.raises(ValueError):
            AnsibleRunner("Test_module")
        ansible_obj = AnsibleRunner("Test_module", ansible="test_ansible")
        assert "_ansible_selinux_special_fs" in \
               ansible_obj.argument_dict.keys()
 def test_successful_ansible_runner(self, monkeypatch):
     runner = AnsibleRunner(
         "core/commands/command.py",
         key1="value1",
         key2="value2",
     )
     assert runner.module_path == modules.__path__[0] + "/" + \
         "core/commands/command.py"
     assert runner.argument_dict == {"key1": "value1", "key2": "value2"}
    def test_module_executable_generation_failed(self, monkeypatch):
        def mockreturn(path):
            return True

        monkeypatch.setattr(os.path, 'isfile', mockreturn)

        runner = AnsibleRunner("/tmp/testansiblemodulefile",
                               key1="value1",
                               key2="value2")
        pytest.raises(AnsibleExecutableGenerationFailed, runner.run)
    def test_module_run(self, monkeypatch):
        monkeypatch.setattr(ansible_module_runner, 'MODULE_EXECUTION_PATH',
                            "/tmp/.tendrl_runner")

        def mock_modify_module(modname, modpath, argument, task_vars={}):
            return ("echo \'{\"key\":\"test message\"}\'", "new",
                    "#! /usr/bin/sh")

        monkeypatch.setattr(module_common, 'modify_module', mock_modify_module)

        def mock_isfile(path):
            return True

        monkeypatch.setattr(os.path, 'isfile', mock_isfile)

        runner = AnsibleRunner("/tmp/testansiblemodulefile",
                               key1="value1",
                               key2="value2")

        out, err = runner.run()
        assert out == {"key": "test message"}
        assert err == ""
    def test_module_run(self, monkeypatch):
        def mock_modify_module(modname, modpath, argument, task_vars={}):
            return ("echo \'{\"key\":\"test message\"}\'",
                    "new", "#! /usr/bin/sh")

        monkeypatch.setattr(module_common,
                            'modify_module', mock_modify_module)

        def mock_isfile(path):
            return True

        monkeypatch.setattr(os.path, 'isfile', mock_isfile)

        runner = AnsibleRunner(
            "/tmp/testansiblemodulefile",
            "/tmp/",
            key1="value1",
            key2="value2"
        )

        out, err = runner.run()
        assert out == {"key": "test message"}
        assert err == ""
def test_run():
    setattr(__builtin__, "NS", maps.NamedDict())
    NS.publisher_id = 1
    NS["config"] = maps.NamedDict()
    NS.config["data"] = maps.NamedDict(logging_socket_path="test/path")
    NS.node_context = maps.NamedDict()
    NS.node_context.node_id = 1
    with patch.object(os.path, 'isfile', return_value=True) as mock_isfile:
        ansible_obj = AnsibleRunner("path\\to\\test\\module",
                                    ansible="test_ansible")
        with pytest.raises(AnsibleExecutableGenerationFailed):
            ansible_obj.run()
        with patch.object(module_common,
                          'modify_module') as mock_modify_module:
            mock_modify_module.return_value = "Module_data", "module_style", "shebang"
            ansible_obj.run()
        with patch.object(module_common,
                          'modify_module') as mock_modify_module:
            with patch.object(os, "system", system) as mock_system:
                mock_modify_module.return_value = "Module_data", "module_style", "shebang"
                ansible_obj.run()
def test_run():
    setattr(__builtin__, "NS", maps.NamedDict())
    NS.publisher_id = 1
    NS["config"] = maps.NamedDict()
    NS.config["data"] = maps.NamedDict(logging_socket_path="test/path")
    NS.node_context = maps.NamedDict()
    NS.node_context.node_id = 1
    with patch.object(os.path, 'isfile', return_value=True):
        ansible_obj = AnsibleRunner("path\\to\\test\\module",
                                    ansible="test_ansible")
        with pytest.raises(AnsibleExecutableGenerationFailed):
            ansible_obj.run()
        with patch.object(module_common, 'modify_module') as \
                mock_modify_module:
            mock_modify_module.return_value = "Module_data", "module_style",\
                                              "shebang"
            ansible_obj.run()
        with patch.object(module_common, 'modify_module') as \
                mock_modify_module:
            with patch.object(os, "system", system):
                mock_modify_module.return_value = "Module_data",\
                                                  "module_style", "shebang"
                ansible_obj.run()