Skip to content

Commit

Permalink
Fix config module (#435)
Browse files Browse the repository at this point in the history
* Fix config module

* Add uts and integration tests

* Fix sanity

* Fix tests

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: Ashwini Mhatre <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 26, 2023
1 parent c2bee15 commit 73febeb
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 5 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/fix_config_module.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- "iosxr_config - Relax restrictions on I(src) parameter so it can be used more like I(lines). (https://github.com/ansible-collections/cisco.iosxr/issues/343)."
7 changes: 3 additions & 4 deletions plugins/modules/iosxr_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,6 @@ def run(module, result):
exclusive = module.params["exclusive"]
check_mode = module.check_mode
label = module.params["label"]

candidate_config = get_candidate(module)
running_config = get_running_config(module)

Expand Down Expand Up @@ -438,9 +437,9 @@ def main():
mutually_exclusive = [("lines", "src"), ("parents", "src")]

required_if = [
("match", "strict", ["lines"]),
("match", "exact", ["lines"]),
("replace", "block", ["lines"]),
("match", "strict", ["lines", "src"], True),
("match", "exact", ["lines", "src"], True),
("replace", "block", ["lines", "src"], True),
("replace", "config", ["src"]),
]

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
interface GigabitEthernet 0/0/0/5
description this is interface0
mtu 65
speed 10
no shutdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
- name: Setup
cisco.iosxr.iosxr_config:
commands:
- no interface GigabitEthernet 0/0/0/5
ignore_errors: true

- name: "Populate interface configuration with replace block and lines options"
register: result1
cisco.iosxr.iosxr_config:
lines: "{{ lookup('template', 'basic/interface_config.j2') }}"
replace: block

- ansible.builtin.assert:
that:
- result1.changed == true

- name: Setup
cisco.iosxr.iosxr_config:
commands:
- no interface GigabitEthernet 0/0/0/5
ignore_errors: true

- name: "Populate interface configuration with replace block and src options"
register: result2
iosxr_config:
src: basic/interface_config.j2
replace: block

- ansible.builtin.assert:
that:
- result2.changed == true
- result1.commands == result2.commands
37 changes: 36 additions & 1 deletion tests/unit/modules/network/iosxr/test_iosxr_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,41 @@ def test_iosxr_config_match_exact(self):

self.execute_module(changed=True, commands=commands, sort=False)

def test_iosxr_replace_block_src(self):
src = load_fixture("iosxr_config_src.cfg")
set_module_args(dict(replace="block", src=src))
self.conn.get_diff = MagicMock(
return_value=self.cliconf_obj.get_diff(src, self.running_config),
)
commands = [
"hostname foo",
"interface GigabitEthernet0/0",
"no ip address",
]
self.execute_module(changed=True, commands=commands)

def test_iosxr_replace_block_lines(self):
lines = [
"ip address 1.2.3.4 255.255.255.0",
"description test string",
"shutdown",
]
parents = ["interface GigabitEthernet0/0"]
set_module_args(dict(lines=lines, parents=parents, replace="block"))
commands = parents + lines
module = MagicMock()
module.params = {"lines": lines, "parents": parents, "src": None}
candidate_config = iosxr_config.get_candidate(module)
self.conn.get_diff = MagicMock(
return_value=self.cliconf_obj.get_diff(
candidate_config,
self.running_config,
diff_match="none",
path=parents,
),
)
self.execute_module(changed=True, commands=commands, sort=False)

def test_iosxr_config_src_and_lines_fails(self):
args = dict(src="foo", lines="foo")
set_module_args(args)
Expand All @@ -301,7 +336,7 @@ def test_iosxr_config_match_strict_requires_lines(self):
set_module_args(args)
self.execute_module(failed=True)

def test_iosxr_config_replace_block_requires_lines(self):
def test_iosxr_config_replace_block_requires_lines_or_src(self):
args = dict(replace="block")
set_module_args(args)
self.execute_module(failed=True)
Expand Down

0 comments on commit 73febeb

Please sign in to comment.