Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions plugins/module_utils/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,17 @@
from __future__ import annotations

import datetime as _datetime
import sys


_USE_TIMEZONE = sys.version_info >= (3, 6)


def ensure_timezone_info(value):
if not _USE_TIMEZONE or value.tzinfo is not None:
if value.tzinfo is not None:
return value
return value.astimezone(_datetime.timezone.utc)


def fromtimestamp(value):
if _USE_TIMEZONE:
return _datetime.fromtimestamp(value, tz=_datetime.timezone.utc)
return _datetime.utcfromtimestamp(value)
return _datetime.fromtimestamp(value, tz=_datetime.timezone.utc)


def now():
if _USE_TIMEZONE:
return _datetime.datetime.now(tz=_datetime.timezone.utc)
return _datetime.datetime.utcnow()
return _datetime.datetime.now(tz=_datetime.timezone.utc)
9 changes: 1 addition & 8 deletions plugins/modules/jenkins_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@
type: bool
"""

import sys
import traceback
from xml.etree import ElementTree as et

Expand All @@ -160,9 +159,6 @@
import jenkins


IS_PYTHON_2 = sys.version_info[0] <= 2


class JenkinsNode:
def __init__(self, module: AnsibleModule) -> None:
self.module = module
Expand Down Expand Up @@ -246,10 +242,7 @@ def configure_node(self, present):
configured = True

if configured:
if IS_PYTHON_2:
data = et.tostring(root)
else:
data = et.tostring(root, encoding="unicode")
data = et.tostring(root, encoding="unicode")

self.instance.reconfig_node(self.name, data)

Expand Down
20 changes: 7 additions & 13 deletions tests/integration/targets/launchd/files/ansible_test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,12 @@
from __future__ import annotations

import sys
import http.server
import socketserver


if __name__ == '__main__':
if sys.version_info[0] >= 3:
import http.server
import socketserver
PORT = int(sys.argv[1])
Handler = http.server.SimpleHTTPRequestHandler
httpd = socketserver.TCPServer(("", PORT), Handler)
httpd.serve_forever()
else:
import mimetypes
mimetypes.init()
mimetypes.add_type('application/json', '.json')
import SimpleHTTPServer
SimpleHTTPServer.test()
PORT = int(sys.argv[1])
Handler = http.server.SimpleHTTPRequestHandler
httpd = socketserver.TCPServer(("", PORT), Handler)
httpd.serve_forever()
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,15 @@
from __future__ import annotations

import sys
from xmlrpc.client import ServerProxy
from urllib.parse import quote


proc = sys.argv[1]
value = sys.argv[2]
username = sys.argv[3]
password = sys.argv[4]

if sys.version_info[0] == 2:
from xmlrpclib import ServerProxy
from urllib import quote
else:
from xmlrpc.client import ServerProxy
from urllib.parse import quote

if username:
url = 'http://%s:%[email protected]:9001/RPC2' % (quote(username, safe=''), quote(password, safe=''))
else:
Expand Down
5 changes: 0 additions & 5 deletions tests/unit/plugins/callback/test_elastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from __future__ import annotations

import sys
import unittest
from collections import OrderedDict
from unittest.mock import patch, MagicMock, Mock
Expand All @@ -13,14 +12,10 @@
from ansible.executor.task_result import TaskResult
from ansible_collections.community.general.plugins.callback.elastic import ElasticSource, TaskData

ELASTIC_MINIMUM_PYTHON_VERSION = (3, 6)


class TestOpentelemetry(unittest.TestCase):
@patch("ansible_collections.community.general.plugins.callback.elastic.socket")
def setUp(self, mock_socket):
if sys.version_info < ELASTIC_MINIMUM_PYTHON_VERSION:
self.skipTest(f"Python {'.'.join(map(str, ELASTIC_MINIMUM_PYTHON_VERSION))}+ is needed for Elastic")
mock_socket.gethostname.return_value = "my-host"
mock_socket.gethostbyname.return_value = "1.2.3.4"
self.elastic = ElasticSource(display=None)
Expand Down
9 changes: 0 additions & 9 deletions tests/unit/plugins/callback/test_opentelemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from __future__ import annotations

import sys
import unittest
from collections import OrderedDict
from unittest.mock import patch, MagicMock, Mock
Expand All @@ -13,18 +12,10 @@
from ansible.executor.task_result import TaskResult
from ansible_collections.community.general.plugins.callback.opentelemetry import OpenTelemetrySource, TaskData

OPENTELEMETRY_MINIMUM_PYTHON_VERSION = (3, 7)


class TestOpentelemetry(unittest.TestCase):
@patch("ansible_collections.community.general.plugins.callback.opentelemetry.socket")
def setUp(self, mock_socket):
# TODO: this python version validation won't be needed as long as the _time_ns call is mocked.
if sys.version_info < OPENTELEMETRY_MINIMUM_PYTHON_VERSION:
self.skipTest(
f"Python {'.'.join(map(str, OPENTELEMETRY_MINIMUM_PYTHON_VERSION))}+ is needed for OpenTelemetry"
)

mock_socket.gethostname.return_value = "my-host"
mock_socket.gethostbyname.return_value = "1.2.3.4"
self.opentelemetry = OpenTelemetrySource(display=None)
Expand Down
20 changes: 0 additions & 20 deletions tests/unit/plugins/modules/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from __future__ import annotations

import sys
import unittest

from httmock import response # noqa
Expand All @@ -27,22 +26,11 @@ def exit_json(self, **args):

class GitlabModuleTestCase(unittest.TestCase):
def setUp(self):
unitest_python_version_check_requirement(self)

self.mock_module = FakeAnsibleModule()

self.gitlab_instance = gitlab.Gitlab("http://localhost", private_token="private_token", api_version=4)


# Python 2.7+ is needed for python-gitlab
GITLAB_MINIMUM_PYTHON_VERSION = (2, 7)


# Verify if the current Python version is higher than GITLAB_MINIMUM_PYTHON_VERSION
def python_version_match_requirement():
return sys.version_info >= GITLAB_MINIMUM_PYTHON_VERSION


def python_gitlab_module_version():
return gitlab.__version__

Expand All @@ -51,14 +39,6 @@ def python_gitlab_version_match_requirement():
return "2.3.0"


# Skip unittest test case if python version don't match requirement
def unitest_python_version_check_requirement(unittest_testcase):
if not python_version_match_requirement():
unittest_testcase.skipTest(
f"Python {'.'.join(map(str, GITLAB_MINIMUM_PYTHON_VERSION))}+ is needed for python-gitlab"
)


"""
USER API
"""
Expand Down
4 changes: 0 additions & 4 deletions tests/unit/plugins/modules/test_dnsimple.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@
)
from unittest.mock import patch
import pytest
import sys

dnsimple = pytest.importorskip("dnsimple")
mandatory_py_version = pytest.mark.skipif(
sys.version_info < (3, 6), reason="The dnsimple dependency requires python3.6 or higher"
)

from dnsimple import DNSimpleException

Expand Down
4 changes: 1 addition & 3 deletions tests/unit/plugins/modules/test_gitlab_deploy_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@ def _dummy(x):
try:
from .gitlab import (
GitlabModuleTestCase,
python_version_match_requirement,
resp_get_project,
resp_find_project_deploy_key,
resp_create_project_deploy_key,
resp_delete_project_deploy_key,
)

# GitLab module requirements
if python_version_match_requirement():
from gitlab.v4.objects import ProjectKey
from gitlab.v4.objects import ProjectKey
except ImportError:
pytestmark.append(pytest.mark.skip("Could not load gitlab module required for testing"))
# Need to set these to something so that we don't fail when parsing
Expand Down
4 changes: 1 addition & 3 deletions tests/unit/plugins/modules/test_gitlab_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def _dummy(x):
try:
from .gitlab import (
GitlabModuleTestCase,
python_version_match_requirement,
resp_get_group,
resp_get_missing_group,
resp_create_group,
Expand All @@ -29,8 +28,7 @@ def _dummy(x):
)

# GitLab module requirements
if python_version_match_requirement():
from gitlab.v4.objects import Group
from gitlab.v4.objects import Group
except ImportError:
pytestmark.append(pytest.mark.skip("Could not load gitlab module required for testing"))
# Need to set these to something so that we don't fail when parsing
Expand Down
4 changes: 1 addition & 3 deletions tests/unit/plugins/modules/test_gitlab_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@ def _dummy(x):
try:
from .gitlab import (
GitlabModuleTestCase,
python_version_match_requirement,
resp_get_project,
resp_find_project_hook,
resp_create_project_hook,
resp_delete_project_hook,
)

# GitLab module requirements
if python_version_match_requirement():
from gitlab.v4.objects import ProjectHook
from gitlab.v4.objects import ProjectHook
except ImportError:
pytestmark.append(pytest.mark.skip("Could not load gitlab module required for testing"))
# Need to set these to something so that we don't fail when parsing
Expand Down
4 changes: 1 addition & 3 deletions tests/unit/plugins/modules/test_gitlab_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def _dummy(x):
try:
from .gitlab import (
GitlabModuleTestCase,
python_version_match_requirement,
resp_get_group,
resp_get_project_by_name,
resp_create_project,
Expand All @@ -29,8 +28,7 @@ def _dummy(x):
)

# GitLab module requirements
if python_version_match_requirement():
from gitlab.v4.objects import Project
from gitlab.v4.objects import Project
except ImportError:
pytestmark.append(pytest.mark.skip("Could not load gitlab module required for testing"))
# Need to set these to something so that we don't fail when parsing
Expand Down
7 changes: 3 additions & 4 deletions tests/unit/plugins/modules/test_gitlab_protected_branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def _dummy(x):
try:
from .gitlab import (
GitlabModuleTestCase,
python_version_match_requirement,
python_gitlab_module_version,
python_gitlab_version_match_requirement,
resp_get_protected_branch,
Expand All @@ -32,12 +31,12 @@ def _dummy(x):
)

# GitLab module requirements
if python_version_match_requirement():
from gitlab.v4.objects import Project # noqa: F401, pylint: disable=unused-import
from gitlab.v4.objects import Project # noqa: F401, pylint: disable=unused-import

gitlab_req_version = python_gitlab_version_match_requirement()
gitlab_module_version = python_gitlab_module_version()
if LooseVersion(gitlab_module_version) < LooseVersion(gitlab_req_version):
pytestmark.append(pytest.mark.skip("Could not load gitlab module required for testing (Wrong version)"))
pytestmark.append(pytest.mark.skip("Could not load gitlab module required for testing (Wrong version)"))
except ImportError:
pytestmark.append(pytest.mark.skip("Could not load gitlab module required for testing"))

Expand Down
4 changes: 1 addition & 3 deletions tests/unit/plugins/modules/test_gitlab_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def _dummy(x):
from .gitlab import (
FakeAnsibleModule,
GitlabModuleTestCase,
python_version_match_requirement,
resp_find_runners_all,
resp_find_runners_list,
resp_find_project_runners,
Expand All @@ -35,8 +34,7 @@ def _dummy(x):
)

# GitLab module requirements
if python_version_match_requirement():
from gitlab.v4.objects import Runner
from gitlab.v4.objects import Runner
except ImportError:
pytestmark.append(pytest.mark.skip("Could not load gitlab module required for testing"))
# Need to set these to something so that we don't fail when parsing
Expand Down
4 changes: 1 addition & 3 deletions tests/unit/plugins/modules/test_gitlab_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def _dummy(x):
try:
from .gitlab import (
GitlabModuleTestCase,
python_version_match_requirement,
resp_find_user,
resp_get_user,
resp_get_user_keys,
Expand All @@ -34,8 +33,7 @@ def _dummy(x):
)

# GitLab module requirements
if python_version_match_requirement():
from gitlab.v4.objects import User
from gitlab.v4.objects import User
except ImportError:
pytestmark.append(pytest.mark.skip("Could not load gitlab module required for testing"))
# Need to set these to something so that we don't fail when parsing
Expand Down
6 changes: 0 additions & 6 deletions tests/unit/plugins/modules/test_modprobe.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from __future__ import annotations

import sys
from unittest.mock import patch, Mock, mock_open
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
ModuleTestCase,
Expand Down Expand Up @@ -152,9 +151,6 @@ def test_unload_module_failure(self):

class TestModuleIsLoadedPersistently(ModuleTestCase):
def setUp(self):
if sys.version_info[0] == 3 and sys.version_info[1] < 7:
self.skipTest("open_mock doesn't support readline in earlier python versions")

super().setUp()

self.mock_get_bin_path = patch("ansible.module_utils.basic.AnsibleModule.get_bin_path")
Expand Down Expand Up @@ -216,8 +212,6 @@ def test_module_is_not_loaded_no_files(self):

class TestPermanentParams(ModuleTestCase):
def setUp(self):
if sys.version_info[0] == 3 and sys.version_info[1] < 7:
self.skipTest("open_mock doesn't support readline in earlier python versions")
super().setUp()

self.mock_get_bin_path = patch("ansible.module_utils.basic.AnsibleModule.get_bin_path")
Expand Down
Loading