Lenovo cnos banner new module (#50145)

* Adding cnos_banner module to the list.
This commit is contained in:
Anil Kumar Muraleedharan
2019-01-09 21:13:57 +05:30
committed by Nathaniel Case
parent 95f97bb8d6
commit f5ebd9736c
14 changed files with 507 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_banner_sample] tag
# Following you should specify IP Adresses details
# Please change <username> and <password> with appropriate value for your switch.
[cnos_banner_sample]
10.241.107.39 ansible_network_os=cnos ansible_ssh_user=<username> ansible_ssh_pass=<password>

View File

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

View File

@@ -0,0 +1,22 @@
---
- name: collect all cli test cases
find:
paths: "{{ role_path }}/tests/cli"
patterns: "{{ testcase }}.yaml"
register: test_cases
delegate_to: localhost
- 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 }}"
with_items: "{{ test_items }}"
loop_control:
loop_var: test_case_to_run
- name: run test case (connection=local)
include: "{{ test_case_to_run }} ansible_connection=local"
with_first_found: "{{ 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,43 @@
---
- name: setup - remove login
cnos_banner:
banner: login
state: absent
provider: "{{ cli }}"
- name: Set login
cnos_banner:
banner: login
text: |
this is my login banner
that has a multiline
string
state: present
provider: "{{ cli }}"
register: result
- debug:
msg: "{{ result }}"
- assert:
that:
- "result.changed == true"
- name: Set login again (idempotent)
cnos_banner:
banner: login
text: |
this is my login banner
that has a multiline
string
state: present
provider: "{{ cli }}"
register: result
- assert:
that:
- "result.changed == false"
- "result.commands | length == 0"

View File

@@ -0,0 +1,45 @@
---
- name: setup - remove motd
cnos_banner:
banner: motd
state: absent
provider: "{{ cli }}"
- name: Set motd
cnos_banner:
banner: motd
text: |
this is my motd banner
that has a multiline
string
state: present
provider: "{{ cli }}"
register: result
- debug:
msg: "{{ result }}"
- assert:
that:
- "result.changed == true"
# - "'banner motd @\nthis is my motd banner\nthat has a multiline\nstring\n@' in result.commands"
- name: Set motd again (idempotent)
cnos_banner:
banner: motd
text: |
this is my motd banner
that has a multiline
string
state: present
provider: "{{ cli }}"
register: result
- assert:
that:
- "result.changed == false"
- "result.commands | length == 0"

View File

@@ -0,0 +1,36 @@
---
- name: Setup
cnos_banner:
banner: login
text: |
Junk login banner
over multiple lines
state: present
provider: "{{ cli }}"
- name: remove login
cnos_banner:
banner: login
state: absent
provider: "{{ cli }}"
register: result
- debug:
msg: "{{ result }}"
- assert:
that:
- "result.changed == true"
- "'no banner login' in result.commands" # does this break due to "contains?"
- name: remove login (idempotent)
cnos_banner:
banner: login
state: absent
provider: "{{ cli }}"
register: result
- assert:
that:
- "result.changed == false"
- "result.commands | length == 0"

View File

@@ -0,0 +1,9 @@
---
cli:
host: "{{ inventory_hostname }}"
port: 22
username: admin
password: admin
timeout: 30
authorize: True
auth_pass:

View File

@@ -0,0 +1,3 @@
this is a sample
mulitline banner
used for testing

View File

@@ -0,0 +1,62 @@
# 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_banner
from units.modules.utils import set_module_args
from .cnos_module import TestCnosModule, load_fixture
class TestCnosBannerModule(TestCnosModule):
module = cnos_banner
def setUp(self):
super(TestCnosBannerModule, self).setUp()
self.mock_exec_command = patch('ansible.modules.network.cnos.cnos_banner.exec_command')
self.exec_command = self.mock_exec_command.start()
self.mock_load_config = patch('ansible.modules.network.cnos.cnos_banner.load_config')
self.load_config = self.mock_load_config.start()
def tearDown(self):
super(TestCnosBannerModule, self).tearDown()
self.mock_exec_command.stop()
self.mock_load_config.stop()
def load_fixtures(self, commands=None):
self.exec_command.return_value = (0, load_fixture('cnos_banner_show_banner.txt').strip(), None)
self.load_config.return_value = dict(diff=None, session='session')
def test_cnos_banner_create(self):
for banner_type in ('login', 'motd'):
set_module_args(dict(banner=banner_type, text='test\nbanner\nstring'))
commands = ['banner {0} test'.format(banner_type), 'banner {0} banner'.format(banner_type), 'banner {0} string'.format(banner_type)]
self.execute_module(changed=True, commands=commands)
def test_cnos_banner_remove(self):
set_module_args(dict(banner='login', state='absent'))
commands = ['no banner login']
self.execute_module(changed=True, commands=commands)
def test_cnos_banner_nochange(self):
banner_text = load_fixture('cnos_banner_show_banner.txt').strip()
set_module_args(dict(banner='login', text=banner_text))
self.execute_module()