mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Fix multiple EOS EAPI code and test issues (#26651)
This commit is contained in:
parent
035b4b95b0
commit
b81209c187
8 changed files with 178 additions and 148 deletions
|
@ -90,27 +90,37 @@ session_name:
|
|||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.eos import load_config, run_commands
|
||||
from ansible.module_utils.eos import eos_argument_spec, check_args
|
||||
from ansible.module_utils._text import to_text
|
||||
|
||||
def map_obj_to_commands(updates, module):
|
||||
commands = list()
|
||||
want, have = updates
|
||||
state = module.params['state']
|
||||
|
||||
if state == 'absent' and 'text' in have.keys() and have['text']:
|
||||
commands.append('no banner %s' % module.params['banner'])
|
||||
if state == 'absent' and have.get('text'):
|
||||
if isinstance(have['text'], str):
|
||||
commands.append('no banner %s' % module.params['banner'])
|
||||
elif have['text'].get('loginBanner') or have['text'].get('motd'):
|
||||
commands.append({'cmd': 'no banner %s' % module.params['banner']})
|
||||
|
||||
elif state == 'present':
|
||||
if want['text'] and (want['text'] != have.get('text')):
|
||||
if module.params['transport'] == 'cli':
|
||||
if isinstance(have['text'], str):
|
||||
if want['text'] != have['text']:
|
||||
commands.append('banner %s' % module.params['banner'])
|
||||
commands.extend(want['text'].strip().split('\n'))
|
||||
commands.append('EOF')
|
||||
else:
|
||||
else:
|
||||
have_text = have['text'].get('loginBanner') or have['text'].get('motd')
|
||||
if have_text:
|
||||
have_text = have_text.strip()
|
||||
|
||||
if to_text(want['text']) != have_text or not have_text:
|
||||
# For EAPI we need to construct a dict with cmd/input
|
||||
# key/values for the banner
|
||||
commands.append({'cmd': 'banner %s' % module.params['banner'],
|
||||
'input': want['text'].strip('\n')})
|
||||
|
||||
|
||||
return commands
|
||||
|
||||
def map_config_to_obj(module):
|
||||
|
|
|
@ -1,43 +1,65 @@
|
|||
---
|
||||
|
||||
- name: setup - remove login
|
||||
eos_banner:
|
||||
banner: login
|
||||
state: absent
|
||||
- name: Remove previous login banner
|
||||
eos_config:
|
||||
lines: no banner login
|
||||
authorize: yes
|
||||
provider: "{{ cli }}"
|
||||
|
||||
- name: Set login
|
||||
- name: Create login banner
|
||||
eos_banner:
|
||||
banner: login
|
||||
text: |
|
||||
this is my login banner
|
||||
that has a multiline
|
||||
string
|
||||
Junk login banner
|
||||
over multiple lines
|
||||
state: present
|
||||
authorize: yes
|
||||
provider: "{{ cli }}"
|
||||
register: result
|
||||
|
||||
- debug:
|
||||
msg: "{{ result }}"
|
||||
- assert:
|
||||
that:
|
||||
- "result.changed == true"
|
||||
- "'banner login' in result.commands[0]" # does this break due to "contains?"
|
||||
# Ensure sessions contains epoc. Will fail after 18th May 2033
|
||||
- "'ansible_1' in result.session_name"
|
||||
|
||||
- name: Create login banner again (idempotent)
|
||||
eos_banner:
|
||||
banner: login
|
||||
text: |
|
||||
Junk login banner
|
||||
over multiple lines
|
||||
state: present
|
||||
authorize: yes
|
||||
provider: "{{ cli }}"
|
||||
register: result
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- "result.changed == false"
|
||||
- "result.commands | length == 0"
|
||||
# Ensure sessions contains epoc. Will fail after 18th May 2033
|
||||
- "result.session_name is not defined"
|
||||
|
||||
- name: remove login
|
||||
eos_banner:
|
||||
banner: login
|
||||
state: absent
|
||||
authorize: yes
|
||||
provider: "{{ cli }}"
|
||||
register: result
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- "result.changed == true"
|
||||
- "'this is my login banner' in result.commands"
|
||||
- "'that has a multiline' in result.commands"
|
||||
- "'no banner login' in result.commands" # does this break due to "contains?"
|
||||
# Ensure sessions contains epoc. Will fail after 18th May 2033
|
||||
- "'ansible_1' in result.session_name"
|
||||
|
||||
- name: Set login again (idempotent)
|
||||
- name: remove login (idempotent)
|
||||
eos_banner:
|
||||
banner: login
|
||||
text: |
|
||||
this is my login banner
|
||||
that has a multiline
|
||||
string
|
||||
state: present
|
||||
state: absent
|
||||
authorize: yes
|
||||
provider: "{{ cli }}"
|
||||
register: result
|
||||
|
|
|
@ -1,13 +1,11 @@
|
|||
---
|
||||
|
||||
- name: setup - remove motd
|
||||
eos_banner:
|
||||
banner: motd
|
||||
state: absent
|
||||
- name: Remove previous motd banner
|
||||
eos_config:
|
||||
lines: no banner motd
|
||||
authorize: yes
|
||||
provider: "{{ cli }}"
|
||||
|
||||
- name: Set motd
|
||||
- name: Create motd
|
||||
eos_banner:
|
||||
banner: motd
|
||||
text: |
|
||||
|
@ -19,9 +17,6 @@
|
|||
provider: "{{ cli }}"
|
||||
register: result
|
||||
|
||||
- debug:
|
||||
msg: "{{ result }}"
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- "result.changed == true"
|
||||
|
@ -30,7 +25,7 @@
|
|||
# Ensure sessions contains epoc. Will fail after 18th May 2033
|
||||
- "'ansible_1' in result.session_name"
|
||||
|
||||
- name: Set motd again (idempotent)
|
||||
- name: Create motd again (idempotent)
|
||||
eos_banner:
|
||||
banner: motd
|
||||
text: |
|
||||
|
@ -49,6 +44,43 @@
|
|||
# Ensure sessions contains epoc. Will fail after 18th May 2033
|
||||
- "result.session_name is not defined"
|
||||
|
||||
- name: Remove motd
|
||||
eos_banner:
|
||||
banner: motd
|
||||
text: |
|
||||
this is my motd banner
|
||||
that has a multiline
|
||||
string
|
||||
state: absent
|
||||
authorize: yes
|
||||
provider: "{{ cli }}"
|
||||
register: result
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- "result.changed == true"
|
||||
- "'no banner motd' in result.commands"
|
||||
# Ensure sessions contains epoc. Will fail after 18th May 2033
|
||||
- "'ansible_1' in result.session_name"
|
||||
|
||||
- name: Remove motd again (idempotent)
|
||||
eos_banner:
|
||||
banner: motd
|
||||
text: |
|
||||
this is my motd banner
|
||||
that has a multiline
|
||||
string
|
||||
state: absent
|
||||
authorize: yes
|
||||
provider: "{{ cli }}"
|
||||
register: result
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- "result.changed == false"
|
||||
- "result.commands | length == 0"
|
||||
# Ensure sessions contains epoc. Will fail after 18th May 2033
|
||||
- "result.session_name is not defined"
|
||||
|
||||
# FIXME add in tests for everything defined in docs
|
||||
# FIXME Test state:absent + test:
|
||||
|
|
|
@ -1,48 +0,0 @@
|
|||
---
|
||||
- name: Setup
|
||||
eos_banner:
|
||||
banner: login
|
||||
text: |
|
||||
Junk login banner
|
||||
over multiple lines
|
||||
state: present
|
||||
authorize: yes
|
||||
provider: "{{ cli }}"
|
||||
|
||||
- name: remove login
|
||||
eos_banner:
|
||||
banner: login
|
||||
state: absent
|
||||
authorize: yes
|
||||
provider: "{{ cli }}"
|
||||
register: result
|
||||
|
||||
- debug:
|
||||
msg: "{{ result }}"
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- "result.changed == true"
|
||||
- "'no banner login' in result.commands" # does this break due to "contains?"
|
||||
# Ensure sessions contains epoc. Will fail after 18th May 2033
|
||||
- "'ansible_1' in result.session_name"
|
||||
|
||||
- name: remove login (idempotent)
|
||||
eos_banner:
|
||||
banner: login
|
||||
state: absent
|
||||
authorize: yes
|
||||
provider: "{{ cli }}"
|
||||
register: result
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- "result.changed == false"
|
||||
- "result.commands | length == 0"
|
||||
# Ensure sessions contains epoc. Will fail after 18th May 2033
|
||||
- "result.session_name is not defined"
|
||||
|
||||
|
||||
# FIXME add in tests for everything defined in docs
|
||||
# FIXME Test state:absent + test:
|
||||
# FIXME Without powers ensure "privileged mode required"
|
|
@ -1,9 +1,8 @@
|
|||
---
|
||||
|
||||
- name: setup - remove login
|
||||
eos_banner:
|
||||
banner: login
|
||||
state: absent
|
||||
- name: Remove previous login banner
|
||||
eos_config:
|
||||
lines: no banner login
|
||||
authorize: yes
|
||||
provider: "{{ eapi }}"
|
||||
|
||||
|
@ -19,9 +18,6 @@
|
|||
provider: "{{ eapi }}"
|
||||
register: result
|
||||
|
||||
- debug:
|
||||
msg: "{{ result }}"
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- "result.changed == true"
|
||||
|
@ -49,6 +45,43 @@
|
|||
# Ensure sessions contains epoc. Will fail after 18th May 2033
|
||||
- "result.session_name is not defined"
|
||||
|
||||
- name: Remove login
|
||||
eos_banner:
|
||||
banner: login
|
||||
text: |
|
||||
this is my login banner
|
||||
that has a multiline
|
||||
string
|
||||
state: absent
|
||||
authorize: yes
|
||||
provider: "{{ eapi }}"
|
||||
register: result
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- "result.changed == true"
|
||||
- "result.commands.0.cmd == 'no banner login'"
|
||||
# Ensure sessions contains epoc. Will fail after 18th May 2033
|
||||
- "'ansible_1' in result.session_name"
|
||||
|
||||
- name: Remove login again (idempotent)
|
||||
eos_banner:
|
||||
banner: login
|
||||
text: |
|
||||
this is my login banner
|
||||
that has a multiline
|
||||
string
|
||||
state: absent
|
||||
authorize: yes
|
||||
provider: "{{ eapi }}"
|
||||
register: result
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- "result.changed == false"
|
||||
- "result.commands | length == 0"
|
||||
# Ensure sessions contains epoc. Will fail after 18th May 2033
|
||||
- "result.session_name is not defined"
|
||||
|
||||
# FIXME add in tests for everything defined in docs
|
||||
# FIXME Test state:absent + test:
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
---
|
||||
|
||||
- name: setup - remove motd
|
||||
eos_banner:
|
||||
banner: motd
|
||||
state: absent
|
||||
- name: Remove previous motd banner
|
||||
eos_config:
|
||||
lines: no banner motd
|
||||
authorize: yes
|
||||
provider: "{{ eapi }}"
|
||||
|
||||
|
@ -19,9 +18,6 @@
|
|||
provider: "{{ eapi }}"
|
||||
register: result
|
||||
|
||||
- debug:
|
||||
msg: "{{ result }}"
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- "result.changed == true"
|
||||
|
@ -49,6 +45,43 @@
|
|||
# Ensure sessions contains epoc. Will fail after 18th May 2033
|
||||
- "result.session_name is not defined"
|
||||
|
||||
- name: Remove motd
|
||||
eos_banner:
|
||||
banner: motd
|
||||
text: |
|
||||
this is my motd banner
|
||||
that has a multiline
|
||||
string
|
||||
state: absent
|
||||
authorize: yes
|
||||
provider: "{{ eapi }}"
|
||||
register: result
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- "result.changed == true"
|
||||
- "result.commands.0.cmd == 'no banner motd'"
|
||||
# Ensure sessions contains epoc. Will fail after 18th May 2033
|
||||
- "'ansible_1' in result.session_name"
|
||||
|
||||
- name: Remove motd again (idempotent)
|
||||
eos_banner:
|
||||
banner: motd
|
||||
text: |
|
||||
this is my motd banner
|
||||
that has a multiline
|
||||
string
|
||||
state: absent
|
||||
authorize: yes
|
||||
provider: "{{ eapi }}"
|
||||
register: result
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- "result.changed == false"
|
||||
- "result.commands | length == 0"
|
||||
# Ensure sessions contains epoc. Will fail after 18th May 2033
|
||||
- "result.session_name is not defined"
|
||||
# FIXME add in tests for everything defined in docs
|
||||
# FIXME Test state:absent + test:
|
||||
# FIXME Without powers ensure "privileged mode required"
|
||||
|
|
|
@ -1,46 +0,0 @@
|
|||
---
|
||||
- name: Setup
|
||||
eos_banner:
|
||||
banner: login
|
||||
text: Junk login banner single line
|
||||
state: present
|
||||
authorize: yes
|
||||
provider: "{{ eapi }}"
|
||||
|
||||
- name: remove login
|
||||
eos_banner:
|
||||
banner: login
|
||||
state: absent
|
||||
authorize: yes
|
||||
provider: "{{ eapi }}"
|
||||
register: result
|
||||
|
||||
- debug:
|
||||
msg: "{{ result }}"
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- "result.changed == true"
|
||||
- "'no banner login' in result.commands" # does this break due to "contains?"
|
||||
# Ensure sessions contains epoc. Will fail after 18th May 2033
|
||||
- "'ansible_1' in result.session_name"
|
||||
|
||||
- name: remove login (idempotent)
|
||||
eos_banner:
|
||||
banner: login
|
||||
state: absent
|
||||
authorize: yes
|
||||
provider: "{{ eapi }}"
|
||||
register: result
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- "result.changed == false"
|
||||
- "result.commands | length == 0"
|
||||
# Ensure sessions contains epoc. Will fail after 18th May 2033
|
||||
- "result.session_name is not defined"
|
||||
|
||||
|
||||
# FIXME add in tests for everything defined in docs
|
||||
# FIXME Test state:absent + test:
|
||||
# FIXME Without powers ensure "privileged mode required"
|
|
@ -53,12 +53,6 @@ class TestEosBannerModule(TestEosModule):
|
|||
commands = ['banner login', 'test', 'banner', 'string', 'EOF']
|
||||
self.execute_module(changed=True, commands=commands)
|
||||
|
||||
def test_eos_banner_create_with_eapi_transport(self):
|
||||
set_module_args(dict(banner='login', text='test\nbanner\nstring',
|
||||
transport='eapi'))
|
||||
commands = [{'cmd': 'banner login', 'input': 'test\nbanner\nstring'}]
|
||||
self.execute_module(changed=True, commands=commands, transport='eapi')
|
||||
|
||||
def test_eos_banner_remove_with_cli_transport(self):
|
||||
set_module_args(dict(banner='login', state='absent', transport='cli'))
|
||||
commands = ['no banner login']
|
||||
|
|
Loading…
Reference in a new issue