Skip to content

Commit

Permalink
fix most ansible-test issues, suppress the rest
Browse files Browse the repository at this point in the history
Automation Hub, and possibly Galaxy in the future, require the
collection to be screened with `ansible-test sanity` among other
checks.  The role had a number of issues:
* Use `AssertionError` instead of `assert`
* Use of `logging` module not in accordance with standards, but these
  are ok and the errors were suppressed
* Several import errors which are ok because they are checked
  elsewhere
* Many of the module files use `#!` shebang - not sure why, but
  the usage is allowed
* __init__.py in the module_utils directories must be empty, so a
  new file myerror.py was added to move the code from __init__.py
* The documentation block in the module was not properly constructed
  or formatted.
* shellcheck issues, including removing unused files
* use `dummy` instead of `_` (underscore) for variables that are
  unused

add WARNING to module docs - collection users should not use directly

Signed-off-by: Rich Megginson <[email protected]>
  • Loading branch information
richm committed Apr 7, 2021
1 parent 167edab commit 6f92848
Show file tree
Hide file tree
Showing 23 changed files with 202 additions and 128 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
toxenvs="py${toxpyver}"
case "$toxpyver" in
27) toxenvs="${toxenvs},coveralls,flake8,pylint" ;;
36) toxenvs="${toxenvs},coveralls,black,yamllint,ansible-lint,collection" ;;
36) toxenvs="${toxenvs},coveralls,black,yamllint,ansible-lint,collection,ansible-test" ;;
37) toxenvs="${toxenvs},coveralls" ;;
38) toxenvs="${toxenvs},coveralls" ;;
esac
Expand Down
47 changes: 47 additions & 0 deletions .sanity-ansible-ignore-2.9.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
tests/network/ensure_provider_tests.py compile-2.7!skip
tests/network/ensure_provider_tests.py compile-3.5!skip
plugins/module_utils/network_lsr/nm/__init__.py empty-init!skip
plugins/module_utils/network_lsr/nm/active_connection.py import-2.7!skip
plugins/module_utils/network_lsr/nm/client.py import-2.7!skip
plugins/module_utils/network_lsr/nm/connection.py import-2.7!skip
plugins/module_utils/network_lsr/nm/provider.py import-2.7!skip
plugins/module_utils/network_lsr/nm/active_connection.py import-3.5!skip
plugins/module_utils/network_lsr/nm/client.py import-3.5!skip
plugins/module_utils/network_lsr/nm/connection.py import-3.5!skip
plugins/module_utils/network_lsr/nm/provider.py import-3.5!skip
plugins/module_utils/network_lsr/nm/active_connection.py import-3.6!skip
plugins/module_utils/network_lsr/nm/client.py import-3.6!skip
plugins/module_utils/network_lsr/nm/connection.py import-3.6!skip
plugins/module_utils/network_lsr/nm/provider.py import-3.6!skip
plugins/module_utils/network_lsr/nm/active_connection.py import-3.7!skip
plugins/module_utils/network_lsr/nm/client.py import-3.7!skip
plugins/module_utils/network_lsr/nm/connection.py import-3.7!skip
plugins/module_utils/network_lsr/nm/provider.py import-3.7!skip
plugins/module_utils/network_lsr/nm/active_connection.py import-3.8!skip
plugins/module_utils/network_lsr/nm/client.py import-3.8!skip
plugins/module_utils/network_lsr/nm/connection.py import-3.8!skip
plugins/module_utils/network_lsr/nm/provider.py import-3.8!skip
plugins/module_utils/network_lsr/__init__.py shebang!skip
plugins/module_utils/network_lsr/argument_validator.py shebang!skip
plugins/module_utils/network_lsr/utils.py shebang!skip
plugins/module_utils/network_lsr/myerror.py shebang!skip
tests/network/covstats shebang!skip
tests/network/ensure_provider_tests.py shebang!skip
tests/network/get_coverage.sh shebang!skip
tests/network/get_total_coverage.sh shebang!skip
tests/network/merge_coverage.sh shebang!skip
tests/network/ensure_provider_tests.py future-import-boilerplate!skip
tests/network/integration/conftest.py future-import-boilerplate!skip
tests/network/integration/test_ethernet.py future-import-boilerplate!skip
tests/network/unit/test_network_connections.py future-import-boilerplate!skip
tests/network/unit/test_nm_provider.py future-import-boilerplate!skip
tests/network/ensure_provider_tests.py metaclass-boilerplate!skip
tests/network/integration/conftest.py metaclass-boilerplate!skip
tests/network/integration/test_ethernet.py metaclass-boilerplate!skip
tests/network/unit/test_network_connections.py metaclass-boilerplate!skip
tests/network/unit/test_nm_provider.py metaclass-boilerplate!skip
plugins/modules/network_connections.py validate-modules:missing-examples
plugins/modules/network_connections.py validate-modules:missing-gplv3-license
plugins/modules/network_connections.py validate-modules:no-default-for-required-parameter
plugins/modules/network_connections.py validate-modules:parameter-type-not-in-doc
plugins/modules/network_connections.py validate-modules:undocumented-parameter
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ a consequence, `state: up` always changes the system.
You can deactivate a connection profile, even if is currently not active. As a
consequence, `state: down` always changes the system.

Note that if the `state` option is unset, the connection profiles runtime state will
Note that if the `state` option is unset, the connection profile's runtime state will
not be changed.

### `persistent_state`
Expand Down
92 changes: 60 additions & 32 deletions library/network_connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,30 @@
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: BSD-3-Clause

from __future__ import absolute_import, division, print_function

__metaclass__ = type

DOCUMENTATION = """
---
module: network_connections
author: Thomas Haller (@thom311)
short_description: module for network role to manage connection profiles
requirements: [pygobject, dbus, NetworkManager]
version_added: "2.0"
description:
- "WARNING: Do not use this module directly! It is only for role internal use."
- |
Manage networking profiles (connections) for NetworkManager and
initscripts networking providers. Documentation needs to be written. Note
that the network_connections module tightly integrates with the network
role and currently it is not expected to use this module outside the role.
Thus, consult README.md for examples for the role. The requirements are
only for the NetworkManager (nm) provider.
options: {}
"""


import errno
import functools
import os
Expand All @@ -16,7 +40,7 @@
# pylint: disable=import-error, no-name-in-module
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.network_lsr import ethtool # noqa:E501
from ansible.module_utils.network_lsr import MyError # noqa:E501
from ansible.module_utils.network_lsr.myerror import MyError # noqa:E501

from ansible.module_utils.network_lsr.argument_validator import ( # noqa:E501
ArgUtil,
Expand All @@ -30,22 +54,6 @@
# pylint: enable=import-error, no-name-in-module


DOCUMENTATION = """
---
module: network_connections
author: "Thomas Haller ([email protected])"
short_description: module for network role to manage connection profiles
requirements: for 'nm' provider requires pygobject, dbus and NetworkManager.
version_added: "2.0"
description: Manage networking profiles (connections) for NetworkManager and
initscripts networking providers.
options: Documentation needs to be written. Note that the network_connections
module tightly integrates with the network role and currently it is not
expected to use this module outside the role. Thus, consult README.md for
examples for the role.
"""


###############################################################################
PERSISTENT_STATE = "persistent_state"
ABSENT_STATE = "absent"
Expand Down Expand Up @@ -772,7 +780,7 @@ def connection_compare(
if compare_flags is None:
compare_flags = NM.SettingCompareFlags.IGNORE_TIMESTAMP

return not (not (con_a.compare(con_b, compare_flags)))
return con_a.compare(con_b, compare_flags)

def connection_is_active(self, con):
NM = Util.NM()
Expand Down Expand Up @@ -1399,7 +1407,7 @@ def _check_mode_changed(self, old_check_mode, new_check_mode, connections):
def check_mode_set(self, check_mode, connections=None):
c = self._check_mode
self._check_mode = check_mode
assert (
if not (
(c is None and check_mode in [CheckMode.PREPARE])
or (
c == CheckMode.PREPARE
Expand All @@ -1408,7 +1416,12 @@ def check_mode_set(self, check_mode, connections=None):
or (c == CheckMode.PRE_RUN and check_mode in [CheckMode.REAL_RUN])
or (c == CheckMode.REAL_RUN and check_mode in [CheckMode.DONE])
or (c == CheckMode.DRY_RUN and check_mode in [CheckMode.DONE])
)
):
raise AssertionError(
"updating check_mode value from {0} into {1} is incorrect".format(
c, check_mode
)
)
self._check_mode_changed(c, check_mode, connections)


Expand Down Expand Up @@ -1470,7 +1483,8 @@ def log(
warn_traceback=False,
force_fail=False,
):
assert idx >= -1
if not idx >= -1:
raise AssertionError("idx {0} is less than -1".format(idx))
self._log_idx += 1
self.run_results[idx]["log"].append((severity, msg, self._log_idx))
if severity == LogLevel.ERROR:
Expand Down Expand Up @@ -1607,14 +1621,15 @@ def connections(self):
def connections_data(self):
c = self._connections_data
if c is None:
assert self.check_mode in [
if self.check_mode not in [
CheckMode.DRY_RUN,
CheckMode.PRE_RUN,
CheckMode.REAL_RUN,
]
c = []
for _ in range(0, len(self.connections)):
c.append({"changed": False})
]:
raise AssertionError(
"invalid value {0} for self.check_mode".format(self.check_mode)
)
c = [{"changed": False}] * len(self.connections)
self._connections_data = c
return c

Expand All @@ -1623,11 +1638,14 @@ def connections_data_reset(self):
c["changed"] = False

def connections_data_set_changed(self, idx, changed=True):
assert self._check_mode in [
if self._check_mode not in [
CheckMode.PRE_RUN,
CheckMode.DRY_RUN,
CheckMode.REAL_RUN,
]
]:
raise AssertionError(
"invalid value {0} for self._check_mode".format(self._check_mode)
)
if not changed:
return
self.connections_data[idx]["changed"] = changed
Expand Down Expand Up @@ -1697,7 +1715,10 @@ def connection_modified_earlier(self, idx):
# modify the connection.

con = self.connections[idx]
assert con["state"] in ["up", "down"]
if con["state"] not in ["up", "down"]:
raise AssertionError(
"connection state {0} not 'up' or 'down'".format(con["state"])
)

# also check, if the current profile is 'up' with a 'type' (which
# possibly modifies the connection as well)
Expand Down Expand Up @@ -1745,7 +1766,9 @@ def check_mode_next(self):
elif self._check_mode != CheckMode.DONE:
c = CheckMode.DONE
else:
assert False
raise AssertionError(
"invalid value {0} for self._check_mode".format(self._check_mode)
)
self._check_mode = c
self.run_env.check_mode_set(c)
return c
Expand Down Expand Up @@ -1911,7 +1934,12 @@ def run_prepare(self):

name = connection["name"]
if not name:
assert connection["persistent_state"] == "absent"
if not connection["persistent_state"] == "absent":
raise AssertionError(
"persistent_state must be 'absent' not {0} when there is no connection 'name'".format(
connection["persistent_state"]
)
)
continue
if name in names:
exists = names[name]["nm.exists"]
Expand Down Expand Up @@ -1988,7 +2016,7 @@ def _check_ethtool_setting_support(self, idx, connection):
idx, "ethtool.%s specified but not supported by NM", specified
)

for option, _ in specified.items():
for option in specified.keys():
nm_name = nm_get_name_fcnt(option)
if not nm_name:
self.log_fatal(
Expand Down
7 changes: 0 additions & 7 deletions module_utils/network_lsr/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +0,0 @@
#!/usr/bin/python3 -tt
# vim: fileencoding=utf8
# SPDX-License-Identifier: BSD-3-Clause


class MyError(Exception):
pass
9 changes: 7 additions & 2 deletions module_utils/network_lsr/argument_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
# vim: fileencoding=utf8
# SPDX-License-Identifier: BSD-3-Clause

from __future__ import absolute_import, division, print_function

__metaclass__ = type

import posixpath
import socket
import re

# pylint: disable=import-error, no-name-in-module
from ansible.module_utils.network_lsr import MyError # noqa:E501
from ansible.module_utils.network_lsr.myerror import MyError # noqa:E501
from ansible.module_utils.network_lsr.utils import Util # noqa:E501

UINT32_MAX = 0xFFFFFFFF
Expand Down Expand Up @@ -72,7 +76,8 @@ def connection_get_non_absent_names(connections):

class ValidationError(MyError):
def __init__(self, name, message):
Exception.__init__(self, name + ": " + message)
# pylint: disable=non-parent-init-called
super(ValidationError, self).__init__(name + ": " + message)
self.error_message = message
self.name = name

Expand Down
6 changes: 5 additions & 1 deletion module_utils/network_lsr/ethtool.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# SPDX-License-Identifier: BSD-3-Clause

from __future__ import absolute_import, division, print_function

__metaclass__ = type

import array
import struct
import fcntl
Expand Down Expand Up @@ -46,7 +50,7 @@ def get_perm_addr(ifname):
res = ecmd.tobytes()
except AttributeError: # tobytes() is not available in python2
res = ecmd.tostring()
_, size, perm_addr = struct.unpack("II%is" % MAX_ADDR_LEN, res)
unused, size, perm_addr = struct.unpack("II%is" % MAX_ADDR_LEN, res)
perm_addr = Util.mac_ntoa(perm_addr[:size])
except IOError:
perm_addr = None
Expand Down
11 changes: 11 additions & 0 deletions module_utils/network_lsr/myerror.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/python3 -tt
# vim: fileencoding=utf8
# SPDX-License-Identifier: BSD-3-Clause

from __future__ import absolute_import, division, print_function

__metaclass__ = type


class MyError(Exception):
pass
4 changes: 4 additions & 0 deletions module_utils/network_lsr/nm/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Relative import is not support by ansible 2.8 yet
# pylint: disable=import-error, no-name-in-module
from __future__ import absolute_import, division, print_function

__metaclass__ = type

from ansible.module_utils.network_lsr.nm import provider # noqa:E501

# pylint: enable=import-error, no-name-in-module
Expand Down
Loading

0 comments on commit 6f92848

Please sign in to comment.