diff --git a/changelogs/fragments/427-login-timeout.yaml b/changelogs/fragments/427-login-timeout.yaml new file mode 100644 index 000000000..3a8fddbe5 --- /dev/null +++ b/changelogs/fragments/427-login-timeout.yaml @@ -0,0 +1,2 @@ +major_changes: + - sonic_login_timeout - Add 'sonic_login_timeout' module to Dell enterprise SONiC collection (https://github.com/ansible-collections/dellemc.enterprise_sonic/pull/427). diff --git a/plugins/module_utils/network/sonic/argspec/facts/facts.py b/plugins/module_utils/network/sonic/argspec/facts/facts.py index 21c32be3f..83c48e13e 100644 --- a/plugins/module_utils/network/sonic/argspec/facts/facts.py +++ b/plugins/module_utils/network/sonic/argspec/facts/facts.py @@ -80,7 +80,8 @@ def __init__(self, **kwargs): 'login_lockout', 'poe', 'mgmt_servers', - 'ospf_area' + 'ospf_area', + 'login_timeout' ] argument_spec = { diff --git a/plugins/module_utils/network/sonic/argspec/login_timeout/__init__.py b/plugins/module_utils/network/sonic/argspec/login_timeout/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/plugins/module_utils/network/sonic/argspec/login_timeout/login_timeout.py b/plugins/module_utils/network/sonic/argspec/login_timeout/login_timeout.py new file mode 100644 index 000000000..c198fabb4 --- /dev/null +++ b/plugins/module_utils/network/sonic/argspec/login_timeout/login_timeout.py @@ -0,0 +1,53 @@ +# +# -*- coding: utf-8 -*- +# Copyright 2023 Dell Inc. or its subsidiaries. All Rights Reserved +# GNU General Public License v3.0+ +# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +############################################# +# WARNING # +############################################# +# +# This file is auto generated by the resource +# module builder playbook. +# +# Do not edit this file manually. +# +# Changes to this file will be over written +# by the resource module builder. +# +# Changes should be made in the model used to +# generate this file or in the resource module +# builder template. +# +############################################# + +""" +The arg spec for the sonic_login_timeout module +""" + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + + +class Login_timeoutArgs(object): # pylint: disable=R0903 + """The arg spec for the sonic_login_timeout module + """ + + def __init__(self, **kwargs): + pass + + argument_spec = { + 'config': { + 'options': { + 'exec_timeout': { + 'type': 'int'} + }, + 'type': 'dict' + }, + 'state': { + 'choices': ['merged', 'deleted', 'overridden', 'replaced'], + 'default': 'merged', + 'type': 'str' + } + } # pylint: disable=C0301 diff --git a/plugins/module_utils/network/sonic/config/login_timeout/__init__.py b/plugins/module_utils/network/sonic/config/login_timeout/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/plugins/module_utils/network/sonic/config/login_timeout/login_timeout.py b/plugins/module_utils/network/sonic/config/login_timeout/login_timeout.py new file mode 100644 index 000000000..d4c99489e --- /dev/null +++ b/plugins/module_utils/network/sonic/config/login_timeout/login_timeout.py @@ -0,0 +1,244 @@ +# +# -*- coding: utf-8 -*- +# Copyright 2023 Dell Inc. or its subsidiaries. All Rights Reserved +# GNU General Public License v3.0+ +# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) +""" +The sonic_login_timeout class +It is in this file where the current configuration (as dict) +is compared to the provided configuration (as dict) and the command set +necessary to bring the current configuration to it's desired end-state is +created +""" +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.cfg.base import ( + ConfigBase, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + to_list, +) +from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.facts.facts import Facts +from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.utils.utils import ( + get_diff, + update_states +) +from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.sonic import ( + to_request, + edit_config +) +from ansible.module_utils.connection import ConnectionError + + +PATCH = 'patch' +DELETE = 'delete' + + +class Login_timeout(ConfigBase): + """ + The sonic_login_timeout class + """ + + gather_subset = [ + '!all', + '!min', + ] + + gather_network_resources = [ + 'login_timeout', + ] + + login_timeout_path = 'data/openconfig-system:system/openconfig-system-ext:login/session/config' + login_timeout_config_path = { + 'exec_timeout': login_timeout_path + '/exec-timeout', + } + default_config_dict = {"exec_timeout": 600} + + def __init__(self, module): + super(Login_timeout, self).__init__(module) + + def get_login_timeout_facts(self): + """ Get the 'facts' (the current configuration) + + :rtype: A dictionary + :returns: The current configuration as a dictionary + """ + facts, _warnings = Facts(self._module).get_facts(self.gather_subset, self.gather_network_resources) + login_timeout_facts = facts['ansible_network_resources'].get('login_timeout') + if not login_timeout_facts: + return [] + return login_timeout_facts + + def execute_module(self): + """ Execute the module + + :rtype: A dictionary + :returns: The result from module execution + """ + result = {'changed': False} + warnings = list() + commands = list() + + fp = open("/tmp/timeout_cfg", "a") + fp.write("Entered execute module\r\n") + fp.close() + existing_login_timeout_facts = self.get_login_timeout_facts() + commands, requests = self.set_config(existing_login_timeout_facts) + if commands: + if not self._module.check_mode: + try: + edit_config(self._module, to_request(self._module, requests)) + except ConnectionError as exc: + self._module.fail_json(msg=str(exc), code=exc.code) + result['changed'] = True + result['commands'] = commands + + changed_login_timeout_facts = self.get_login_timeout_facts() + + result['before'] = existing_login_timeout_facts + + if result['changed']: + result['after'] = changed_login_timeout_facts + result['warnings'] = warnings + + return result + + def set_config(self, existing_login_timeout_facts): + """ Collect the configuration from the args passed to the module, + collect the current configuration (as a dict from facts) + + :rtype: A list + :returns: the commands necessary to migrate the current configuration + to the desired configuration + """ + want = self._module.params['config'] + have = existing_login_timeout_facts + resp = self.set_state(want, have) + return to_list(resp) + + def set_state(self, want, have): + """ Select the appropriate function based on the state provided + + :param want: the desired configuration as a dictionary + :param have: the current configuration as a dictionary + :rtype: A list + :returns: the commands necessary to migrate the current configuration + to the desired configuration + """ + state = self._module.params['state'] + diff = get_diff(want, have) + if state == 'deleted': + commands, requests = self._state_deleted(want, have, diff) + elif state == 'merged': + commands, requests = self._state_merged(diff) + elif state == 'replaced': + commands, requests = self._state_replaced_overridden(want, have, diff) + elif state == 'overridden': + commands, requests = self._state_replaced_overridden(want, have, diff) + return commands, requests + + def _state_replaced_overridden(self, want, have, diff): + """ The command generator when state is replaced or overridden + :rtype: A list + :returns: the commands necessary to migrate the current configuration + to the desired configuration + """ + commands = [] + requests = [] + delete = {} + state = self._module.params['state'] + + for key in have: + if key in want: + if want[key] == have[key]: + continue + if key not in diff and have[key] != self.default_config_dict[key]: + delete[key] = have[key] + if delete: + commands = update_states(delete, 'deleted') + requests.extend(self.get_delete_specific_login_timeout_param_requests(delete)) + if diff: + commands.extend(update_states(diff, state)) + requests.extend(self.get_modify_specific_login_timeout_param_requests(diff)) + + return commands, requests + + def _state_merged(self, diff): + """ The command generator when state is merged + :rtype: A list + :returns: the commands necessary to merge the provided into + the current configuration + """ + commands = [] + requests = [] + + if diff: + requests = self.get_modify_specific_login_timeout_param_requests(diff) + if len(requests) > 0: + commands = update_states(diff, 'merged') + + return commands, requests + + def _state_deleted(self, want, have, diff): + """ The command generator when state is deleted + :rtype: A list + :returns: the commands necessary to remove the current configuration + of the provided objects + """ + commands = [] + requests = [] + delete = {} + + if not want: + delete = have.copy() + delete = {key: value for key, value in delete.items() if key not in self.default_config_dict or self.default_config_dict[key] != value} + commands = delete + requests.extend(self.get_delete_specific_login_timeout_param_requests(commands)) + else: + delete = get_diff(want, diff) + delete = {key: value for key, value in delete.items() if key not in self.default_config_dict or self.default_config_dict[key] != value} + commands = delete + requests.extend(self.get_delete_specific_login_timeout_param_requests(commands)) + + if len(requests) == 0: + commands = [] + + if commands: + commands = update_states(commands, "deleted") + + return commands, requests + + def get_modify_specific_login_timeout_param_requests(self, command): + """Get requests to modify specific Login Timeout configurations + based on the command specified + """ + requests = [] + config_dict = {} + + if not command: + return requests + + if 'exec_timeout' in command and command['exec_timeout'] is not None: + config_dict['exec-timeout'] = int(command['exec_timeout']) + + payload = {"openconfig-system-ext:config": config_dict} + + requests.append({'path': self.login_timeout_path, 'method': PATCH, 'data': payload}) + + return requests + + def get_delete_specific_login_timeout_param_requests(self, command): + """Get requests to delete specific Login Timeout configurations + based on the command specified + """ + requests = [] + + if not command: + return requests + + if 'exec_timeout' in command: + url = self.login_timeout_config_path['exec_timeout'] + requests.append({'path': url, 'method': DELETE}) + + return requests diff --git a/plugins/module_utils/network/sonic/facts/facts.py b/plugins/module_utils/network/sonic/facts/facts.py index 2aacd4988..f771a26f2 100644 --- a/plugins/module_utils/network/sonic/facts/facts.py +++ b/plugins/module_utils/network/sonic/facts/facts.py @@ -78,6 +78,8 @@ from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.facts.poe.poe import PoeFacts from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.facts.mgmt_servers.mgmt_servers import Mgmt_serversFacts from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.facts.ospf_area.ospf_area import Ospf_areaFacts +from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.facts.login_timeout.login_timeout import Login_timeoutFacts + FACT_LEGACY_SUBSETS = {} FACT_RESOURCE_SUBSETS = dict( @@ -140,7 +142,9 @@ login_lockout=Login_lockoutFacts, poe=PoeFacts, mgmt_servers=Mgmt_serversFacts, - ospf_area=Ospf_areaFacts + ospf_area=Ospf_areaFacts, + login_timeout=Login_timeoutFacts + ) diff --git a/plugins/module_utils/network/sonic/facts/login_timeout/__init__.py b/plugins/module_utils/network/sonic/facts/login_timeout/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/plugins/module_utils/network/sonic/facts/login_timeout/login_timeout.py b/plugins/module_utils/network/sonic/facts/login_timeout/login_timeout.py new file mode 100644 index 000000000..d46863397 --- /dev/null +++ b/plugins/module_utils/network/sonic/facts/login_timeout/login_timeout.py @@ -0,0 +1,104 @@ +# +# -*- coding: utf-8 -*- +# Copyright 2023 Dell Inc. or its subsidiaries. All Rights Reserved +# GNU General Public License v3.0+ +# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) +""" +The sonic login_timeout fact class +It is in this file the configuration is collected from the device +for a given resource, parsed, and the facts tree is populated +based on the configuration. +""" +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +from copy import deepcopy + +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common import ( + utils, +) +from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.argspec.login_timeout.login_timeout import Login_timeoutArgs + +from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.sonic import ( + to_request, + edit_config +) + +from ansible.module_utils.connection import ConnectionError + + +GET = "get" + + +class Login_timeoutFacts(object): + """ The sonic login_timeout fact class + """ + + def __init__(self, module, subspec='config', options='options'): + self._module = module + self.argument_spec = Login_timeoutArgs.argument_spec + spec = deepcopy(self.argument_spec) + if subspec: + if options: + facts_argument_spec = spec[subspec][options] + else: + facts_argument_spec = spec[subspec] + else: + facts_argument_spec = spec + + self.generated_spec = utils.generate_dict(facts_argument_spec) + + def populate_facts(self, connection, ansible_facts, data=None): + """ Populate the facts for login_timeout + :param connection: the device connection + :param ansible_facts: Facts dictionary + :param data: previously collected conf + :rtype: dictionary + :returns: facts + """ + if connection: # just for linting purposes, remove + pass + + objs = self.get_all_login_timeout_configs() + + ansible_facts['ansible_network_resources'].pop('login_timeout', None) + facts = {} + if objs: + params = utils.validate_config(self.argument_spec, {'config': objs}) + facts['login_timeout'] = params['config'] + + ansible_facts['ansible_network_resources'].update(facts) + return ansible_facts + + def render_config(self, spec, conf): + """ + Render config as dictionary structure and delete keys + from spec for null values + + :param spec: The facts tree, generated from the argspec + :param conf: The configuration + :rtype: dictionary + :returns: The generated config + """ + return conf + + def get_all_login_timeout_configs(self): + """Get the login timeout configured in the device""" + request = [{"path": "data/openconfig-system:system/openconfig-system-ext:login/session/config", "method": GET}] + login_timeout_data = {} + raw_login_timeout_data = {} +# import epdb +# epdb.serve(port=8989) + try: + response = edit_config(self._module, to_request(self._module, request)) + except ConnectionError as exc: + self._module.fail_json(msg=str(exc), code=exc.code) + login_timeout_data['exec_timeout'] = 0 + + if 'openconfig-system-ext:config' in response[0][1]: + raw_login_timeout_data = response[0][1]['openconfig-system-ext:config'] + + if 'exec-timeout' in raw_login_timeout_data: + login_timeout_data['exec_timeout'] = raw_login_timeout_data['exec-timeout'] + + return login_timeout_data diff --git a/plugins/modules/sonic_facts.py b/plugins/modules/sonic_facts.py index 7e15383f2..3f9c10763 100644 --- a/plugins/modules/sonic_facts.py +++ b/plugins/modules/sonic_facts.py @@ -113,6 +113,8 @@ - login_lockout - mgmt_servers - ospf_area + - login_timeout + """ EXAMPLES = """ diff --git a/plugins/modules/sonic_login_timeout.py b/plugins/modules/sonic_login_timeout.py new file mode 100644 index 000000000..060f74b78 --- /dev/null +++ b/plugins/modules/sonic_login_timeout.py @@ -0,0 +1,212 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# Copyright 2023 Dell Inc. or its subsidiaries. All Rights Reserved +# GNU General Public License v3.0+ +# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +############################################# +# WARNING # +############################################# +# +# This file is auto generated by the resource +# module builder playbook. +# +# Do not edit this file manually. +# +# Changes to this file will be over written +# by the resource module builder. +# +# Changes should be made in the model used to +# generate this file or in the resource module +# builder template. +# +############################################# + +""" +The module file for sonic_login_timeout +""" + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +ANSIBLE_METADATA = { + 'metadata_version': '1.1', + 'status': ['preview'], + 'supported_by': 'community', + 'license': 'Apache 2.0' +} + +DOCUMENTATION = """ +--- +module: sonic_login_timeout +version_added: '2.5.0' +short_description: Manage Global Login Timeout configurations on SONiC +description: + - This module provides configuration management of login timeout parameter. + - Login timeout feature is to terminate the inactive user sessions if the + user sessions are left idle for more than the configured session timeout. +author: 'Arul Kumar Shankara Narayanan(@arulkumar9690)' +options: + config: + description: The set of login timeout attribute configurations + type: dict + suboptions: + exec_timeout: + description: + - CLI session timeout value. + - The range is from 0 to 3600 + type: int + state: + description: + - Specifies the operation to be performed on the login timeout configured on the device. + - If the state is "merged", merge specified attributes with existing configured login attribute. + - For "deleted", delete the specified login attributes from the existing configuration. + - For "overridden", Overrides on-device login timeout configurations with the provided configuration. + - For "replaced", Replaces on-device login timeout configurations with the provided configuration. + type: str + choices: + - merged + - deleted + - overridden + - replaced + default: merged +""" +EXAMPLES = """ +# Using deleted +# +# Before State: +# ------------- +# +# sonic# show running-configuration | grep exec-timeout +# ! +# login exec-timeout 10 +# ! + + - name: Delete Login Timeout configurations + dellemc.enterprise_sonic.sonic_login_timeout: + config: + exec_timeout : 10 + state: deleted + +# After State: +# ------------ +# sonic# show running-configuration | grep exec-timeout +# sonic# + + +# Using Merged +# +# Before State: +# ------------- +# +# sonic# show running-configuration | grep exec-timeout +# sonic# + + - name: Modify Login Timeout configurations + dellemc.enterprise_sonic.sonic_login_timeout: + config: + exec_timeout : 15 + state: merged + +# After State: +# ------------ +# sonic# show running-configuration | grep exec-timeout +# ! +# login exec-timeout 15 +# ! + + +# Using overridden +# +# Before State: +# ------------- +# +# sonic# show running-configuration | grep exec-timeout +# ! +# login exec-timeout 10 +# ! +# sonic# + + - name: Modify Login Timeout configurations + dellemc.enterprise_sonic.sonic_login_lockout: + config: + exec_timeout : 20 + state: overridden + +# After State: +# ------------ +# sonic# show running-configuration | grep exec-timeout +# ! +# login exec-timeout 20 +# ! + + +# Using replaced +# +# Before State: +# ------------- +# +# sonic# show running-configuration | grep exec-timeout +# ! +# login exec-timeout 10 +# ! +# sonic# + + - name: Modify Login Timeout configurations + dellemc.enterprise_sonic.sonic_login_timeout: + config: + exec_timeout: 15 + state: replaced + +# After State: +# ------------ +# sonic# show running-configuration | grep exec-timeout +# ! +# login exec-timeout 15 +# ! + + +""" +RETURN = """ +before: + description: The configuration prior to the model invocation. + returned: always + type: dict + sample: > + The configuration returned will always be in the same format + of the parameters above. +after: + description: The resulting configuration model invocation. + returned: when changed + type: dict + sample: > + The configuration returned will always be in the same format + of the parameters above. +commands: + description: The set of commands pushed to the remote device. + returned: always + type: list + sample: ['command 1', 'command 2', 'command 3'] +""" + + +from ansible.module_utils.basic import AnsibleModule +from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.argspec.login_timeout.login_timeout import Login_timeoutArgs +from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.config.login_timeout.login_timeout import Login_timeout + + +def main(): + """ + Main entry point for module execution + + :returns: the result form module invocation + """ + module = AnsibleModule(argument_spec=Login_timeoutArgs.argument_spec, + supports_check_mode=True) + + result = Login_timeout(module).execute_module() + module.exit_json(**result) + + +if __name__ == '__main__': + main() diff --git a/tests/regression/roles/sonic_login_timeout/defaults/main.yml b/tests/regression/roles/sonic_login_timeout/defaults/main.yml new file mode 100644 index 000000000..24ef9bf63 --- /dev/null +++ b/tests/regression/roles/sonic_login_timeout/defaults/main.yml @@ -0,0 +1,35 @@ +--- +ansible_connection: httpapi +module_name: login_timeout + + +tests: + - name: test_case_01 + description: Add Login Timeout configuration + state: merged + input: + exec_timeout: 100 + + - name: test_case_02 + description: Update Login Timeout configuration + state: merged + input: + exec_timeout: 120 + + - name: test_case_03 + description: Add Login Timeout configuration + state: replaced + input: + exec_timeout: 150 + + - name: test_case_04 + description: Update Login Timeout configuration + state: overridden + input: + exec_timeout: 200 + + - name: test_case_05 + description: Delete specific Login Timeout configurations + state: deleted + input: + exec_timeout: 200 diff --git a/tests/regression/roles/sonic_login_timeout/meta/main.yml b/tests/regression/roles/sonic_login_timeout/meta/main.yml new file mode 100644 index 000000000..d0ceaf6f5 --- /dev/null +++ b/tests/regression/roles/sonic_login_timeout/meta/main.yml @@ -0,0 +1,5 @@ +--- +collections: + - dellemc.enterprise_sonic +dependencies: + - { role: common } diff --git a/tests/regression/roles/sonic_login_timeout/tasks/cleanup_tests.yaml b/tests/regression/roles/sonic_login_timeout/tasks/cleanup_tests.yaml new file mode 100644 index 000000000..d62b582c8 --- /dev/null +++ b/tests/regression/roles/sonic_login_timeout/tasks/cleanup_tests.yaml @@ -0,0 +1,5 @@ +- name: Delete Login Timeout configurations + dellemc.enterprise_sonic.sonic_login_timeout: + config: + state: deleted + ignore_errors: yes diff --git a/tests/regression/roles/sonic_login_timeout/tasks/main.yml b/tests/regression/roles/sonic_login_timeout/tasks/main.yml new file mode 100644 index 000000000..0e277317e --- /dev/null +++ b/tests/regression/roles/sonic_login_timeout/tasks/main.yml @@ -0,0 +1,22 @@ +--- +- ansible.builtin.debug: + msg: "sonic_login_timeout Test started ..." + +- name: "Preparations for {{ module_name }}" + ansible.builtin.include_tasks: preparation_tests.yaml + +- name: "Test {{ module_name }} started" + ansible.builtin.include_tasks: tasks_template.yaml + loop: "{{ tests }}" + +- name: "test_delete_all {{ module_name }} stated ..." + ansible.builtin.include_tasks: tasks_template_del.yaml + loop: "{{ test_delete_all }}" + when: test_delete_all is defined + +- name: "Cleanup of {{ module_name }}" + ansible.builtin.include_tasks: cleanup_tests.yaml + +- name: Display all variables/facts known for a host + ansible.builtin.debug: + var: hostvars[inventory_hostname].ansible_facts.test_reports diff --git a/tests/regression/roles/sonic_login_timeout/tasks/preparation_tests.yaml b/tests/regression/roles/sonic_login_timeout/tasks/preparation_tests.yaml new file mode 100644 index 000000000..836cee1d2 --- /dev/null +++ b/tests/regression/roles/sonic_login_timeout/tasks/preparation_tests.yaml @@ -0,0 +1,6 @@ +--- +- name: Delete old Login Timeout configurations + dellemc.enterprise_sonic.sonic_login_timeout: + config: {} + state: deleted + ignore_errors: yes diff --git a/tests/regression/roles/sonic_login_timeout/tasks/tasks_template.yaml b/tests/regression/roles/sonic_login_timeout/tasks/tasks_template.yaml new file mode 100644 index 000000000..3fe71c790 --- /dev/null +++ b/tests/regression/roles/sonic_login_timeout/tasks/tasks_template.yaml @@ -0,0 +1,22 @@ +--- +- name: "{{ item.name }} , {{ item.description }}" + dellemc.enterprise_sonic.sonic_login_timeout: + config: "{{ item.input }}" + state: "{{ item.state }}" + register: action_task_output + ignore_errors: yes + +- ansible.builtin.import_role: + name: common + tasks_from: action.facts.report.yaml + +- name: "{{ item.name }} , {{ item.description }} Idempotent" + dellemc.enterprise_sonic.sonic_login_timeout: + config: "{{ item.input }}" + state: "{{ item.state }}" + register: idempotent_task_output + ignore_errors: yes + +- ansible.builtin.import_role: + name: common + tasks_from: idempotent.facts.report.yaml diff --git a/tests/regression/roles/sonic_login_timeout/tasks/tasks_template_del.yaml b/tests/regression/roles/sonic_login_timeout/tasks/tasks_template_del.yaml new file mode 100644 index 000000000..72b5f9d01 --- /dev/null +++ b/tests/regression/roles/sonic_login_timeout/tasks/tasks_template_del.yaml @@ -0,0 +1,21 @@ +- name: "{{ item.name }} , {{ item.description }}" + dellemc.enterprise_sonic.sonic_login_timeout: + config: + state: "{{ item.state }}" + register: action_task_output + ignore_errors: yes + +- ansible.builtin.import_role: + name: common + tasks_from: action.facts.report.yaml + +- name: "{{ item.name }} , {{ item.description }} Idempotent" + dellemc.enterprise_sonic.sonic_login_timeout: + config: + state: "{{ item.state }}" + register: idempotent_task_output + ignore_errors: yes + +- ansible.builtin.import_role: + name: common + tasks_from: idempotent.facts.report.yaml diff --git a/tests/regression/test.yaml b/tests/regression/test.yaml index 87c657c77..476d5f283 100644 --- a/tests/regression/test.yaml +++ b/tests/regression/test.yaml @@ -71,4 +71,5 @@ - sonic_ospf_area - sonic_poe - sonic_mgmt_servers + - sonic_login_timeout - test_reports diff --git a/tests/unit/modules/network/sonic/fixtures/sonic_login_timeout.yaml b/tests/unit/modules/network/sonic/fixtures/sonic_login_timeout.yaml new file mode 100644 index 000000000..95c2d271b --- /dev/null +++ b/tests/unit/modules/network/sonic/fixtures/sonic_login_timeout.yaml @@ -0,0 +1,67 @@ +--- +merged_01: + module_args: + config: + exec_timeout: 300 + existing_system_config: + - path: "data/openconfig-system:system/openconfig-system-ext:login/session/config" + response: + code: 200 + value: + openconfig-system-ext:config: + exec-timeout: 600 + expected_config_requests: + - path: "data/openconfig-system:system/openconfig-system-ext:login/session/config" + method: "patch" + data: + openconfig-system-ext:config: + exec-timeout: 300 +deleted_01: + module_args: + state: deleted + existing_system_config: + - path: "data/openconfig-system:system/openconfig-system-ext:login/session/config" + response: + code: 200 + value: + openconfig-system-ext:config: + exec-timeout: 300 + expected_config_requests: + - path: "data/openconfig-system:system/openconfig-system-ext:login/session/config/exec-timeout" + method: "delete" +replaced_01: + module_args: + config: + exec_timeout: 100 + state: replaced + existing_system_config: + - path: "data/openconfig-system:system/openconfig-system-ext:login/session/config" + response: + code: 200 + value: + openconfig-system-ext:config: + exec-timeout: 600 + expected_config_requests: + - path: "data/openconfig-system:system/openconfig-system-ext:login/session/config" + method: "patch" + data: + openconfig-system-ext:config: + exec-timeout: 100 +overridden_01: + module_args: + config: + exec_timeout: 200 + state: overridden + existing_system_config: + - path: "data/openconfig-system:system/openconfig-system-ext:login/session/config" + response: + code: 200 + value: + openconfig-system-ext:config: + exec-timeout: 100 + expected_config_requests: + - path: "data/openconfig-system:system/openconfig-system-ext:login/session/config" + method: "patch" + data: + openconfig-system-ext:config: + exec-timeout: 200 diff --git a/tests/unit/modules/network/sonic/test_sonic_login_timeout.py b/tests/unit/modules/network/sonic/test_sonic_login_timeout.py new file mode 100644 index 000000000..427d426a2 --- /dev/null +++ b/tests/unit/modules/network/sonic/test_sonic_login_timeout.py @@ -0,0 +1,79 @@ +from __future__ import absolute_import, division, print_function + +__metaclass__ = type + +from ansible_collections.dellemc.enterprise_sonic.tests.unit.compat.mock import ( + patch, +) +from ansible_collections.dellemc.enterprise_sonic.plugins.modules import ( + sonic_login_timeout, +) +from ansible_collections.dellemc.enterprise_sonic.tests.unit.modules.utils import ( + set_module_args, +) +from .sonic_module import TestSonicModule + + +class TestSonicLoginTimeoutModule(TestSonicModule): + module = sonic_login_timeout + + @classmethod + def setUpClass(cls): + cls.mock_facts_edit_config = patch( + "ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.facts.login_timeout.login_timeout.edit_config" + ) + cls.mock_config_edit_config = patch( + "ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.config.login_timeout.login_timeout.edit_config" + ) + cls.mock_utils_edit_config = patch( + "ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.utils.utils.edit_config" + ) + cls.fixture_data = cls.load_fixtures('sonic_login_timeout.yaml') + + def setUp(self): + super(TestSonicLoginTimeoutModule, self).setUp() + self.facts_edit_config = self.mock_facts_edit_config.start() + self.config_edit_config = self.mock_config_edit_config.start() + self.utils_edit_config = self.mock_utils_edit_config.start() + + self.utils_edit_config.side_effect = self.config_side_effect + self.facts_edit_config.side_effect = self.facts_side_effect + self.config_edit_config.side_effect = self.config_side_effect + + def tearDown(self): + super(TestSonicLoginTimeoutModule, self).tearDown() + self.mock_facts_edit_config.stop() + self.mock_config_edit_config.stop() + self.mock_utils_edit_config.stop() + + def test_sonic_login_timeout_merged_01(self): + set_module_args(self.fixture_data['merged_01']['module_args']) + self.initialize_facts_get_requests(self.fixture_data['merged_01']['existing_system_config']) + self.initialize_config_requests(self.fixture_data['merged_01']['expected_config_requests']) + + result = self.execute_module(changed=True) + self.validate_config_requests() + + def test_sonic_login_timeout_deleted_01(self): + set_module_args(self.fixture_data['deleted_01']['module_args']) + self.initialize_facts_get_requests(self.fixture_data['deleted_01']['existing_system_config']) + self.initialize_config_requests(self.fixture_data['deleted_01']['expected_config_requests']) + + result = self.execute_module(changed=True) + self.validate_config_requests() + + def test_sonic_login_timeout_replaced_01(self): + set_module_args(self.fixture_data['replaced_01']['module_args']) + self.initialize_facts_get_requests(self.fixture_data['replaced_01']['existing_system_config']) + self.initialize_config_requests(self.fixture_data['replaced_01']['expected_config_requests']) + + result = self.execute_module(changed=True) + self.validate_config_requests() + + def test_sonic_login_timeout_overridden_01(self): + set_module_args(self.fixture_data['overridden_01']['module_args']) + self.initialize_facts_get_requests(self.fixture_data['overridden_01']['existing_system_config']) + self.initialize_config_requests(self.fixture_data['overridden_01']['expected_config_requests']) + + result = self.execute_module(changed=True) + self.validate_config_requests()