Lenovo cnos user module (#53763)

* Adding cnos_user module to Ansible

* Update cnos_user.py

* Adding Functional test cases and unit test cases.

* Fixing Bug found in testing with Lenovo Mars.

* Review comments incorporated

* Review comments implemented.

* Copy paste mistake
This commit is contained in:
Anil Kumar Muraleedharan
2019-03-27 19:22:05 +05:30
committed by Sumit Jaiswal
parent 0ab28f314a
commit 8d742d9bff
12 changed files with 684 additions and 2 deletions

View File

@@ -0,0 +1,2 @@
# No Lenovo Switch simulator yet, so not enabled
unsupported

View File

@@ -0,0 +1,14 @@
# You have to paste this dummy information in /etc/ansible/hosts
# Notes:
# - Comments begin with the '#' character
# - Blank lines are ignored
# - Groups of hosts are delimited by [header] elements
# - You can enter hostnames or ip Addresses
# - A hostname/ip can be a member of multiple groups
#
# In the /etc/ansible/hosts file u have to enter [cnos_user_sample] tag
# Following you should specify IP Addresses details
# Please change <username> and <password> with appropriate value for your switch.
[cnos_sample_sample]
10.241.107.39 ansible_network_os=cnos ansible_ssh_user=admin ansible_ssh_pass=admin

View File

@@ -0,0 +1,2 @@
---
testcase: "*"

View File

@@ -0,0 +1,27 @@
---
- name: collect common test cases
find:
paths: "{{ role_path }}/tests/common"
patterns: "{{ testcase }}.yaml"
connection: local
register: test_cases
- name: collect cli test cases
find:
paths: "{{ role_path }}/tests/cli"
patterns: "{{ testcase }}.yaml"
connection: local
register: cli_cases
- set_fact:
test_cases:
files: "{{ test_cases.files }} + {{ cli_cases.files }}"
- name: set test_items
set_fact: test_items="{{ test_cases.files | map(attribute='path') | list }}"
- name: run test cases (connection=network_cli)
include: "{{ test_case_to_run }} ansible_connection=network_cli connection={{ cli }}"
with_items: "{{ test_items }}"
loop_control:
loop_var: test_case_to_run

View File

@@ -0,0 +1,2 @@
---
- { include: cli.yaml, tags: ['cli'] }

View File

@@ -0,0 +1,58 @@
---
- debug: msg="START connection={{ ansible_connection }} cnos_user basic test"
- name: Remove old entries of user
cnos_user:
aggregate:
- { name: ansibletest1 }
- { name: ansibletest2 }
- { name: ansibletest3 }
# provider: "{{ connection }}"
configured_password: admin
state: absent
# Start tests
- name: Create user
cnos_user:
name: ansibletest1
roles: network-operator
# provider: "{{ connection }}"
state: present
register: result
- assert:
that:
- 'result.changed == true'
- '"username" in result.commands[0]'
- '"role network-operator" in result.commands[1]'
- name: Collection of users
cnos_user:
aggregate:
- { name: ansibletest2 }
- { name: ansibletest3 }
# provider: "{{ connection }}"
state: present
roles: network-admin
register: result
- assert:
that:
- 'result.changed == true'
- name: tearDown
cnos_user:
aggregate:
- { name: ansibletest1 }
- { name: ansibletest2 }
- { name: ansibletest3 }
# provider: "{{ connection }}"
state: absent
register: result
- assert:
that:
- 'result.changed == true'
- '"no username" in result.commands[0]'
- debug: msg="END connection={{ ansible_connection }} cnos_user basic test"

View File

@@ -0,0 +1,84 @@
---
- debug: msg="START connection={{ ansible_connection }} cnos_user parameter test"
- block:
- name: Create user
cnos_user: &configure
name: netend
configured_password: Hello!234
update_password: on_create
roles: network-operator
state: present
register: result
- assert: &true
that:
- 'result.changed == true'
- block:
- name: conf idempotency
cnos_user: *configure
register: result
- assert: &false
that:
- 'result.changed == false'
- name: Remove user
cnos_user: &remove
name: netend
state: absent
register: result
- assert: *true
- name: remove idempotency
cnos_user: *remove
register: result
- assert: *false
- name: Collection of users
cnos_user: &coll
users:
- name: test1
- name: test2
configured_password: Hello!234
update_password: on_create
state: present
roles:
- network-admin
- network-operator
register: result
- assert: *true
- block:
- name: users idempotency
cnos_user: *coll
register: result
- assert: *true
- name: tearDown
cnos_user: &tear
name: ansible
purge: yes
register: result
- assert: *true
- name: teardown idempotency
cnos_user: *tear
register: result
- assert: *false
always:
- name: tearDown
cnos_user: *tear
register: result
ignore_errors: yes
- debug: msg="END connection={{ ansible_connection }} cnos_user parameter test"

View File

@@ -0,0 +1,8 @@
User:admin
role: network-admin
User:ansible
role: network-operator
no password set. Local login not allowed
this user is created by remote authentication
Remote login through RADIUS/TACACS+ is possible

View File

@@ -0,0 +1,89 @@
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
from units.compat.mock import patch
from ansible.modules.network.cnos import cnos_user
from units.modules.utils import set_module_args
from .cnos_module import TestCnosModule, load_fixture
class TestCnosUserModule(TestCnosModule):
module = cnos_user
def setUp(self):
super(TestCnosUserModule, self).setUp()
self.mock_get_config = patch('ansible.modules.network.cnos.cnos_user.get_config')
self.get_config = self.mock_get_config.start()
self.mock_load_config = patch('ansible.modules.network.cnos.cnos_user.load_config')
self.load_config = self.mock_load_config.start()
self.mock_run_commands = patch('ansible.modules.network.cnos.cnos_user.run_commands')
self.run_commands = self.mock_run_commands.start()
def tearDown(self):
super(TestCnosUserModule, self).tearDown()
self.mock_get_config.stop()
self.mock_load_config.stop()
self.mock_run_commands.stop()
def load_fixtures(self, commands=None, transport='cli'):
self.get_config.return_value = load_fixture('cnos_user_config.cfg')
self.load_config.return_value = dict(diff=None, session='session')
self.run_commands.return_value = [load_fixture('cnos_user_config.cfg')]
def test_cnos_user_create(self):
set_module_args(dict(name='test', configured_password='Anil'))
commands = ['username test', 'username test password Anil']
self.execute_module(changed=True, commands=commands)
def test_cnos_user_delete(self):
set_module_args(dict(name='ansible', state='absent'))
commands = []
self.execute_module(changed=False, commands=commands)
def test_cnos_user_password(self):
set_module_args(dict(name='ansible', configured_password='test'))
commands = ['username ansible', 'username ansible password test']
self.execute_module(changed=True, commands=commands)
def test_cnos_user_purge(self):
set_module_args(dict(purge=True))
commands = ['no username admin\n', 'no username ansible\n']
self.execute_module(changed=True, commands=commands)
def test_cnos_user_role(self):
set_module_args(dict(name='ansible', role='network-admin', configured_password='test'))
result = self.execute_module(changed=True)
self.assertIn('username ansible role network-admin', result['commands'])
def test_cnos_user_sshkey(self):
set_module_args(dict(name='ansible', sshkey='test'))
commands = ['username ansible', 'username ansible sshkey test']
self.execute_module(changed=True, commands=commands)
def test_cnos_user_update_password_changed(self):
set_module_args(dict(name='test', configured_password='test', update_password='on_create'))
commands = ['username test', 'username test password test']
self.execute_module(changed=True, commands=commands)
def test_cnos_user_update_password_always(self):
set_module_args(dict(name='ansible', configured_password='test', update_password='always'))
commands = ['username ansible', 'username ansible password test']
self.execute_module(changed=True, commands=commands)