Skip to content

Commit

Permalink
rewriting various imports and calls for new paths in extension
Browse files Browse the repository at this point in the history
  • Loading branch information
garethgreenaway committed Aug 2, 2023
1 parent 2bcd4a1 commit d17d0a0
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 66 deletions.
54 changes: 33 additions & 21 deletions tests/pytests/unit/modules/test_apache.py
@@ -1,14 +1,14 @@
"""
:codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
"""

import urllib.error
from unittest.mock import MagicMock
from unittest.mock import mock_open
from unittest.mock import patch

import pytest

import salt.modules.apache as apache
import saltext.saltext_apache.modules.apache as apache
from salt.utils.odict import OrderedDict
from tests.support.mock import MagicMock, mock_open, patch


@pytest.fixture
Expand All @@ -23,7 +23,9 @@ def test_version():
"""
Test if return server version (``apachectl -v``)
"""
with patch("salt.modules.apache._detect_os", MagicMock(return_value="apachectl")):
with patch(
"saltext.saltext_apache.modules.apache._detect_os", MagicMock(return_value="apachectl")
):
mock = MagicMock(return_value="Server version: Apache/2.4.7")
with patch.dict(apache.__salt__, {"cmd.run": mock}):
assert apache.version() == "Apache/2.4.7"
Expand All @@ -36,7 +38,9 @@ def test_fullversion():
"""
Test if return server version (``apachectl -V``)
"""
with patch("salt.modules.apache._detect_os", MagicMock(return_value="apachectl")):
with patch(
"saltext.saltext_apache.modules.apache._detect_os", MagicMock(return_value="apachectl")
):
mock = MagicMock(return_value="Server version: Apache/2.4.7")
with patch.dict(apache.__salt__, {"cmd.run": mock}):
assert apache.fullversion() == {
Expand All @@ -52,7 +56,9 @@ def test_modules():
"""
Test if return list of static and shared modules
"""
with patch("salt.modules.apache._detect_os", MagicMock(return_value="apachectl")):
with patch(
"saltext.saltext_apache.modules.apache._detect_os", MagicMock(return_value="apachectl")
):
mock = MagicMock(
return_value=(
"unixd_module (static)\n "
Expand All @@ -73,7 +79,9 @@ def test_servermods():
"""
Test if return list of modules compiled into the server
"""
with patch("salt.modules.apache._detect_os", MagicMock(return_value="apachectl")):
with patch(
"saltext.saltext_apache.modules.apache._detect_os", MagicMock(return_value="apachectl")
):
mock = MagicMock(return_value="core.c\nmod_so.c")
with patch.dict(apache.__salt__, {"cmd.run": mock}):
assert apache.servermods() == ["core.c", "mod_so.c"]
Expand All @@ -86,7 +94,9 @@ def test_directives():
"""
Test if return list of directives
"""
with patch("salt.modules.apache._detect_os", MagicMock(return_value="apachectl")):
with patch(
"saltext.saltext_apache.modules.apache._detect_os", MagicMock(return_value="apachectl")
):
mock = MagicMock(return_value="Salt")
with patch.dict(apache.__salt__, {"cmd.run": mock}):
assert apache.directives() == {"Salt": ""}
Expand All @@ -99,10 +109,12 @@ def test_vhosts():
"""
Test if it shows the virtualhost settings
"""
with patch("salt.modules.apache._detect_os", MagicMock(return_value="apachectl")):
with patch(
"saltext.saltext_apache.modules.apache._detect_os", MagicMock(return_value="apachectl")
):
mock = MagicMock(return_value="")
with patch.dict(apache.__salt__, {"cmd.run": mock}):
assert apache.vhosts() == {}
assert not apache.vhosts()


# 'signal' function tests: 2
Expand All @@ -112,7 +124,9 @@ def test_signal():
"""
Test if return no signal for httpd
"""
with patch("salt.modules.apache._detect_os", MagicMock(return_value="apachectl")):
with patch(
"saltext.saltext_apache.modules.apache._detect_os", MagicMock(return_value="apachectl")
):
mock = MagicMock(return_value="")
with patch.dict(apache.__salt__, {"cmd.run": mock}):
assert apache.signal(None) is None
Expand All @@ -122,21 +136,19 @@ def test_signal_args():
"""
Test if return httpd signal to start, restart, or stop.
"""
with patch("salt.modules.apache._detect_os", MagicMock(return_value="apachectl")):
with patch(
"saltext.saltext_apache.modules.apache._detect_os", MagicMock(return_value="apachectl")
):
ret = 'Command: "apachectl -k start" completed successfully!'
mock = MagicMock(return_value={"retcode": 1, "stderr": "", "stdout": ""})
with patch.dict(apache.__salt__, {"cmd.run_all": mock}):
assert apache.signal("start") == ret

mock = MagicMock(
return_value={"retcode": 1, "stderr": "Syntax OK", "stdout": ""}
)
mock = MagicMock(return_value={"retcode": 1, "stderr": "Syntax OK", "stdout": ""})
with patch.dict(apache.__salt__, {"cmd.run_all": mock}):
assert apache.signal("start") == "Syntax OK"

mock = MagicMock(
return_value={"retcode": 0, "stderr": "Syntax OK", "stdout": ""}
)
mock = MagicMock(return_value={"retcode": 0, "stderr": "Syntax OK", "stdout": ""})
with patch.dict(apache.__salt__, {"cmd.run_all": mock}):
assert apache.signal("start") == "Syntax OK"

Expand Down Expand Up @@ -176,7 +188,7 @@ def test_server_status():
"""
Test if return get information from the Apache server-status
"""
with patch("salt.modules.apache.server_status", MagicMock(return_value={})):
with patch("saltext.saltext_apache.modules.apache.server_status", MagicMock(return_value={})):
mock = MagicMock(return_value="")
with patch.dict(apache.__salt__, {"config.get": mock}):
assert apache.server_status() == {}
Expand All @@ -201,7 +213,7 @@ def test_config():
Test if it create VirtualHost configuration files
"""
with patch(
"salt.modules.apache._parse_config", MagicMock(return_value="Listen 22")
"saltext.saltext_apache.modules.apache._parse_config", MagicMock(return_value="Listen 22")
):
with patch("salt.utils.files.fopen", mock_open()):
assert apache.config("/ports.conf", [{"Listen": "22"}]) == "Listen 22"
Expand Down
5 changes: 2 additions & 3 deletions tests/pytests/unit/modules/test_deb_apache.py
@@ -1,12 +1,11 @@
"""
:codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
"""

from unittest.mock import MagicMock
from unittest.mock import patch

import pytest

import salt.modules.deb_apache as deb_apache
from tests.support.mock import MagicMock, patch


@pytest.fixture
Expand Down
11 changes: 5 additions & 6 deletions tests/pytests/unit/states/apache/test_apache.py
@@ -1,12 +1,13 @@
"""
:codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
"""
from unittest.mock import MagicMock
from unittest.mock import mock_open
from unittest.mock import patch

import pytest

import salt.states.apache as apache
import salt.utils.files
from tests.support.mock import MagicMock, mock_open, patch
import saltext.saltext_apache.states.apache as apache


@pytest.fixture
Expand Down Expand Up @@ -48,8 +49,6 @@ def test_configfile():
with patch.object(salt.utils.files, "fopen", mock_open(read_data=config)):
mock_config = MagicMock(return_value=new_config)
with patch.dict(apache.__salt__, {"apache.config": mock_config}):
ret.update(
{"comment": "Successfully created configuration.", "result": True}
)
ret.update({"comment": "Successfully created configuration.", "result": True})
with patch.dict(apache.__opts__, {"test": False}):
assert apache.configfile(name, config) == ret
23 changes: 11 additions & 12 deletions tests/pytests/unit/states/apache/test_conf.py
@@ -1,7 +1,8 @@
import pytest
from unittest.mock import MagicMock
from unittest.mock import patch

import salt.states.apache_conf as apache_conf
from tests.support.mock import MagicMock, patch
import pytest
import saltext.saltext_apache.states.apache_conf as apache_conf


@pytest.fixture
Expand All @@ -23,18 +24,16 @@ def test_enabled():
apache_conf.__salt__,
{"apache.check_conf_enabled": mock, "apache.a2enconf": mock_str},
):
comt = "{} already enabled.".format(name)
comt = f"{name} already enabled."
ret.update({"comment": comt})
assert apache_conf.enabled(name) == ret

comt = "Apache conf {} is set to be enabled.".format(name)
ret.update(
{"comment": comt, "result": None, "changes": {"new": name, "old": None}}
)
comt = f"Apache conf {name} is set to be enabled."
ret.update({"comment": comt, "result": None, "changes": {"new": name, "old": None}})
with patch.dict(apache_conf.__opts__, {"test": True}):
assert apache_conf.enabled(name) == ret

comt = "Failed to enable {} Apache conf".format(name)
comt = f"Failed to enable {name} Apache conf"
ret.update({"comment": comt, "result": False, "changes": {}})
with patch.dict(apache_conf.__opts__, {"test": False}):
assert apache_conf.enabled(name) == ret
Expand All @@ -54,16 +53,16 @@ def test_disabled():
apache_conf.__salt__,
{"apache.check_conf_enabled": mock, "apache.a2disconf": mock_str},
):
comt = "Apache conf {} is set to be disabled.".format(name)
comt = f"Apache conf {name} is set to be disabled."
ret.update({"comment": comt, "changes": {"new": None, "old": name}})
with patch.dict(apache_conf.__opts__, {"test": True}):
assert apache_conf.disabled(name) == ret

comt = "Failed to disable {} Apache conf".format(name)
comt = f"Failed to disable {name} Apache conf"
ret.update({"comment": comt, "result": False, "changes": {}})
with patch.dict(apache_conf.__opts__, {"test": False}):
assert apache_conf.disabled(name) == ret

comt = "{} already disabled.".format(name)
comt = f"{name} already disabled."
ret.update({"comment": comt, "result": True})
assert apache_conf.disabled(name) == ret
22 changes: 10 additions & 12 deletions tests/pytests/unit/states/apache/test_module.py
@@ -1,11 +1,11 @@
"""
:codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
"""
from unittest.mock import MagicMock
from unittest.mock import patch

import pytest

import salt.states.apache_module as apache_module
from tests.support.mock import MagicMock, patch
import saltext.saltext_apache.states.apache_module as apache_module


@pytest.fixture
Expand All @@ -27,18 +27,16 @@ def test_enabled():
apache_module.__salt__,
{"apache.check_mod_enabled": mock, "apache.a2enmod": mock_str},
):
comt = "{} already enabled.".format(name)
comt = f"{name} already enabled."
ret.update({"comment": comt})
assert apache_module.enabled(name) == ret

comt = "Apache module {} is set to be enabled.".format(name)
ret.update(
{"comment": comt, "result": None, "changes": {"new": "cgi", "old": None}}
)
comt = f"Apache module {name} is set to be enabled."
ret.update({"comment": comt, "result": None, "changes": {"new": "cgi", "old": None}})
with patch.dict(apache_module.__opts__, {"test": True}):
assert apache_module.enabled(name) == ret

comt = "Failed to enable {} Apache module".format(name)
comt = f"Failed to enable {name} Apache module"
ret.update({"comment": comt, "result": False, "changes": {}})
with patch.dict(apache_module.__opts__, {"test": False}):
assert apache_module.enabled(name) == ret
Expand All @@ -58,16 +56,16 @@ def test_disabled():
apache_module.__salt__,
{"apache.check_mod_enabled": mock, "apache.a2dismod": mock_str},
):
comt = "Apache module {} is set to be disabled.".format(name)
comt = f"Apache module {name} is set to be disabled."
ret.update({"comment": comt, "changes": {"new": None, "old": "cgi"}})
with patch.dict(apache_module.__opts__, {"test": True}):
assert apache_module.disabled(name) == ret

comt = "Failed to disable {} Apache module".format(name)
comt = f"Failed to disable {name} Apache module"
ret.update({"comment": comt, "result": False, "changes": {}})
with patch.dict(apache_module.__opts__, {"test": False}):
assert apache_module.disabled(name) == ret

comt = "{} already disabled.".format(name)
comt = f"{name} already disabled."
ret.update({"comment": comt, "result": True})
assert apache_module.disabled(name) == ret
23 changes: 11 additions & 12 deletions tests/pytests/unit/states/apache/test_site.py
@@ -1,7 +1,8 @@
import pytest
from unittest.mock import MagicMock
from unittest.mock import patch

import salt.states.apache_site as apache_site
from tests.support.mock import MagicMock, patch
import pytest
import saltext.saltext_apache.states.apache_site as apache_site


@pytest.fixture
Expand All @@ -23,18 +24,16 @@ def test_enabled():
apache_site.__salt__,
{"apache.check_site_enabled": mock, "apache.a2ensite": mock_str},
):
comt = "{} already enabled.".format(name)
comt = f"{name} already enabled."
ret.update({"comment": comt})
assert apache_site.enabled(name) == ret

comt = "Apache site {} is set to be enabled.".format(name)
ret.update(
{"comment": comt, "result": None, "changes": {"new": name, "old": None}}
)
comt = f"Apache site {name} is set to be enabled."
ret.update({"comment": comt, "result": None, "changes": {"new": name, "old": None}})
with patch.dict(apache_site.__opts__, {"test": True}):
assert apache_site.enabled(name) == ret

comt = "Failed to enable {} Apache site".format(name)
comt = f"Failed to enable {name} Apache site"
ret.update({"comment": comt, "result": False, "changes": {}})
with patch.dict(apache_site.__opts__, {"test": False}):
assert apache_site.enabled(name) == ret
Expand All @@ -54,16 +53,16 @@ def test_disabled():
apache_site.__salt__,
{"apache.check_site_enabled": mock, "apache.a2dissite": mock_str},
):
comt = "Apache site {} is set to be disabled.".format(name)
comt = f"Apache site {name} is set to be disabled."
ret.update({"comment": comt, "changes": {"new": None, "old": name}})
with patch.dict(apache_site.__opts__, {"test": True}):
assert apache_site.disabled(name) == ret

comt = "Failed to disable {} Apache site".format(name)
comt = f"Failed to disable {name} Apache site"
ret.update({"comment": comt, "result": False, "changes": {}})
with patch.dict(apache_site.__opts__, {"test": False}):
assert apache_site.disabled(name) == ret

comt = "{} already disabled.".format(name)
comt = f"{name} already disabled."
ret.update({"comment": comt, "result": True})
assert apache_site.disabled(name) == ret

0 comments on commit d17d0a0

Please sign in to comment.