diff options
author | GomathiselviS <gomathiselvi@gmail.com> | 2022-01-31 19:41:29 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-01 00:41:29 +0000 |
commit | 32371e3fe712b08bc3e3b47075741234ad784586 (patch) | |
tree | 1c96caf9ac884d36c88a7692071141d63f7b3628 /tests | |
parent | a4fc3def45320e633a773f2efa453c78d3b4afca (diff) | |
download | vyos.vyos-32371e3fe712b08bc3e3b47075741234ad784586.tar.gz vyos.vyos-32371e3fe712b08bc3e3b47075741234ad784586.zip |
Vyos snmp_server Resource Module (#227)
Vyos snmp_server Resource Module
SUMMARY
Added vyos_snmp_server resource module
ISSUE TYPE
New Module Pull Request
COMPONENT NAME
ADDITIONAL INFORMATION
Reviewed-by: Nilashish Chakraborty <nilashishchakraborty8@gmail.com>
Reviewed-by: Ashwini Mhatre <mashu97@gmail.com>
Reviewed-by: None <None>
Diffstat (limited to 'tests')
18 files changed, 1159 insertions, 0 deletions
diff --git a/tests/integration/targets/vyos_snmp_server/defaults/main.yaml b/tests/integration/targets/vyos_snmp_server/defaults/main.yaml new file mode 100644 index 0000000..852a6be --- /dev/null +++ b/tests/integration/targets/vyos_snmp_server/defaults/main.yaml @@ -0,0 +1,3 @@ +--- +testcase: '[^_].*' +test_items: [] diff --git a/tests/integration/targets/vyos_snmp_server/meta/main.yaml b/tests/integration/targets/vyos_snmp_server/meta/main.yaml new file mode 100644 index 0000000..91da2a7 --- /dev/null +++ b/tests/integration/targets/vyos_snmp_server/meta/main.yaml @@ -0,0 +1,2 @@ +--- +... diff --git a/tests/integration/targets/vyos_snmp_server/tasks/cli.yaml b/tests/integration/targets/vyos_snmp_server/tasks/cli.yaml new file mode 100644 index 0000000..93eb2fe --- /dev/null +++ b/tests/integration/targets/vyos_snmp_server/tasks/cli.yaml @@ -0,0 +1,19 @@ +--- +- name: Collect all cli test cases + find: + paths: '{{ role_path }}/tests/cli' + patterns: '{{ testcase }}.yaml' + use_regex: true + register: test_cases + delegate_to: localhost + +- name: Set test_items + set_fact: test_items="{{ test_cases.files | map(attribute='path') | list }}" + +- name: Run test case (connection=ansible.netcommon.network_cli) + include: '{{ test_case_to_run }}' + vars: + ansible_connection: ansible.netcommon.network_cli + with_items: '{{ test_items }}' + loop_control: + loop_var: test_case_to_run diff --git a/tests/integration/targets/vyos_snmp_server/tasks/main.yaml b/tests/integration/targets/vyos_snmp_server/tasks/main.yaml new file mode 100644 index 0000000..b957d2f --- /dev/null +++ b/tests/integration/targets/vyos_snmp_server/tasks/main.yaml @@ -0,0 +1,4 @@ +--- +- include: cli.yaml + tags: + - network_cli diff --git a/tests/integration/targets/vyos_snmp_server/tests/cli/_parsed.cfg b/tests/integration/targets/vyos_snmp_server/tests/cli/_parsed.cfg new file mode 100644 index 0000000..9750253 --- /dev/null +++ b/tests/integration/targets/vyos_snmp_server/tests/cli/_parsed.cfg @@ -0,0 +1,19 @@ +set service snmp community routers authorization 'ro' +set service snmp community routers client '203.0.113.10' +set service snmp community routers client '203.0.113.20' +set service snmp community routers network '192.0.2.0/24' +set service snmp community routers network '2001::/64' +set service snmp contact 'admin@example.com' +set service snmp listen-address 172.16.254.36 port '161' +set service snmp listen-address 2001::1 +set service snmp location 'UK, London' +set service snmp trap-target 203.0.113.10 +set service snmp v3 engineid '000000000000000000000002' +set service snmp v3 group default mode 'ro' +set service snmp v3 group default view 'default' +set service snmp v3 user vyos auth plaintext-key 'vyos12345678' +set service snmp v3 user vyos auth type 'sha' +set service snmp v3 user vyos group 'default' +set service snmp v3 user vyos privacy plaintext-key 'vyos12345678' +set service snmp v3 user vyos privacy type 'aes' +set service snmp v3 view default oid 1 diff --git a/tests/integration/targets/vyos_snmp_server/tests/cli/_populate_config.yaml b/tests/integration/targets/vyos_snmp_server/tests/cli/_populate_config.yaml new file mode 100644 index 0000000..f4d89f5 --- /dev/null +++ b/tests/integration/targets/vyos_snmp_server/tests/cli/_populate_config.yaml @@ -0,0 +1,18 @@ +--- +- name: setup + vyos.vyos.vyos_config: + lines: + - set service snmp community switches authorization rw + - set service snmp community bridges client 1.1.1.1 + - set service snmp community bridges client 12.1.1.10 + - set service snmp listen-address 20.1.1.1 + - set service snmp listen-address 100.1.2.1 port 33 + - set service snmp v3 user adminuser engineid 33 + - set service snmp v3 user adminuser auth type sha + - set service snmp v3 user adminuser auth plaintext-key abc1234567 + - set service snmp v3 user adminuser privacy type aes + - set service snmp v3 user adminuser privacy plaintext-key abc1234567 + - set service snmp contact admin2@ex.com + ignore_errors: true + vars: + ansible_connection: ansible.netcommon.network_cli diff --git a/tests/integration/targets/vyos_snmp_server/tests/cli/_remove_config.yaml b/tests/integration/targets/vyos_snmp_server/tests/cli/_remove_config.yaml new file mode 100644 index 0000000..12cfb75 --- /dev/null +++ b/tests/integration/targets/vyos_snmp_server/tests/cli/_remove_config.yaml @@ -0,0 +1,8 @@ +--- +- name: Remove pre-existing snmp_server config + vyos.vyos.vyos_snmp_server: + config: + state: deleted + ignore_errors: true + vars: + ansible_connection: ansible.netcommon.network_cli diff --git a/tests/integration/targets/vyos_snmp_server/tests/cli/deleted.yaml b/tests/integration/targets/vyos_snmp_server/tests/cli/deleted.yaml new file mode 100644 index 0000000..1fa32c5 --- /dev/null +++ b/tests/integration/targets/vyos_snmp_server/tests/cli/deleted.yaml @@ -0,0 +1,40 @@ +--- +- debug: + msg: START vyos_snmp_server deleted integration tests on connection={{ansible_connection }} + +- include_tasks: _remove_config.yaml + +- include_tasks: _populate_config.yaml + +- block: + + - name: Delete the provided configuration + register: result + vyos.vyos.vyos_snmp_server: &id001 + config: + state: deleted + + - name: Assert that before dicts were correctly generated + assert: + that: + - result.changed == true + - result.commands == deleted.commands + + - name: Assert that the after dicts were correctly generated + assert: + that: + - result.after == {} + + - name: Delete the existing configuration with the provided running configuration + (IDEMPOTENT) + register: result + vyos.vyos.vyos_snmp_server: *id001 + + - name: Assert that the previous task was idempotent + assert: + that: + - result['changed'] == false + + always: + + - include_tasks: _remove_config.yaml diff --git a/tests/integration/targets/vyos_snmp_server/tests/cli/empty_config.yaml b/tests/integration/targets/vyos_snmp_server/tests/cli/empty_config.yaml new file mode 100644 index 0000000..558b53f --- /dev/null +++ b/tests/integration/targets/vyos_snmp_server/tests/cli/empty_config.yaml @@ -0,0 +1,60 @@ +--- +- debug: + msg: START vyos_snmp_server empty_config integration tests on connection={{ + ansible_connection }} + +- name: Merged with empty config should give appropriate error message + register: result + ignore_errors: true + vyos.vyos.vyos_snmp_server: + config: + state: merged + +- assert: + that: + - result.msg == 'value of config parameter must not be empty for state merged' + +- name: Replaced with empty config should give appropriate error message + register: result + ignore_errors: true + vyos.vyos.vyos_snmp_server: + config: + state: replaced + +- assert: + that: + - result.msg == 'value of config parameter must not be empty for state replaced' + +- name: Overridden with empty config should give appropriate error message + register: result + ignore_errors: true + vyos.vyos.vyos_snmp_server: + config: + state: overridden + +- assert: + that: + - result.msg == 'value of config parameter must not be empty for state overridden' + +- name: Parsed with empty running_config should give appropriate error message + register: result + ignore_errors: true + vyos.vyos.vyos_snmp_server: + running_config: + state: parsed + +- assert: + that: + - result.msg == 'value of running_config parameter must not be empty for state + parsed' + +- name: Rendered with empty config should give appropriate error message + register: result + ignore_errors: true + vyos.vyos.vyos_snmp_server: + config: + state: rendered + +- assert: + that: + - result.msg == 'value of config parameter must not be empty for state rendered' diff --git a/tests/integration/targets/vyos_snmp_server/tests/cli/gathered.yaml b/tests/integration/targets/vyos_snmp_server/tests/cli/gathered.yaml new file mode 100644 index 0000000..184faa5 --- /dev/null +++ b/tests/integration/targets/vyos_snmp_server/tests/cli/gathered.yaml @@ -0,0 +1,24 @@ +--- +- debug: + msg: START vyos_snmp_server gathered integration tests on connection={{ + ansible_connection }} + +- include_tasks: _remove_config.yaml + +- include_tasks: _populate_config.yaml + +- block: + + - name: Gather config from the device in structured format. + register: result + vyos.vyos.vyos_snmp_server: + state: gathered + + - vyos.vyos.vyos_facts: + gather_network_resources: snmp_server + + - name: Assert that facts are correctly generated + assert: + that: + - result.changed == false + - result.gathered == ansible_facts['network_resources']['snmp_server'] diff --git a/tests/integration/targets/vyos_snmp_server/tests/cli/merged.yaml b/tests/integration/targets/vyos_snmp_server/tests/cli/merged.yaml new file mode 100644 index 0000000..e372166 --- /dev/null +++ b/tests/integration/targets/vyos_snmp_server/tests/cli/merged.yaml @@ -0,0 +1,63 @@ +--- +- debug: + msg: START vyos_snmp_server merged integration tests on connection={{ + ansible_connection }} + +- include_tasks: _remove_config.yaml + +- block: + - name: Merge the provided configuration with the existing running configuration + register: result + vyos.vyos.vyos_snmp_server: &id001 + config: + communities: + - name: "switches" + authorization_type: "rw" + - name: "bridges" + clients: ["1.1.1.1", "12.1.1.10"] + contact: "admin2@ex.com" + listen_addresses: + - address: "20.1.1.1" + - address: "100.1.2.1" + port: 33 + snmp_v3: + users: + - user: adminuser + authentication: + plaintext_key: "abc1234567" + type: "sha" + privacy: + plaintext_key: "abc1234567" + type: "aes" + state: merged + + - vyos.vyos.vyos_facts: + gather_network_resources: snmp_server + + - assert: + that: + - result.commands|length == 10 + - result.changed == true + - result.commands|symmetric_difference(merged.commands) == [] + - result.after == ansible_facts['network_resources']['snmp_server'] + - result.after == merged.after + + + - name: Assert that before dicts were correctly generated + assert: + that: + - result.before == {} + + - name: + Merge the provided configuration with the existing running configuration + (IDEMPOTENT) + register: result + vyos.vyos.vyos_snmp_server: *id001 + + - name: Assert that the previous task was idempotent + assert: + that: + - result['changed'] == false + + always: + - include_tasks: _remove_config.yaml diff --git a/tests/integration/targets/vyos_snmp_server/tests/cli/overridden.yaml b/tests/integration/targets/vyos_snmp_server/tests/cli/overridden.yaml new file mode 100644 index 0000000..5f3d9c9 --- /dev/null +++ b/tests/integration/targets/vyos_snmp_server/tests/cli/overridden.yaml @@ -0,0 +1,67 @@ +--- +- debug: + msg: START vyos_snmp_server overridden integration tests on connection={{ + ansible_connection }} + +- include_tasks: _remove_config.yaml + +- include_tasks: _populate_config.yaml + +- block: + + - name: Override the existing configuration with the provided running configuration + register: result + vyos.vyos.vyos_snmp_server: &id001 + config: + communities: + - name: "bridges" + networks: ["1.1.1.0/24", "12.1.1.0/24"] + location: "RDU" + listen_addresses: + - address: "100.1.2.1" + port: 33 + snmp_v3: + users: + - user: adminuser + engine_id: "33" + authentication: + plaintext_key: "abc1234567" + type: "sha" + privacy: + plaintext_key: "abc1234567" + type: "aes" + - user: guestuser2 + authentication: + plaintext_key: "opq1234567" + type: "sha" + privacy: + plaintext_key: "opq1234567" + type: "aes" + views: + - view: "default" + oid: 1 + state: overridden + + - vyos.vyos.vyos_facts: + gather_network_resources: snmp_server + + - assert: + that: + - result.changed == true + - result.after == ansible_facts['network_resources']['snmp_server'] + - result.commands|length == 13 + + + - name: Override the existing configuration with the provided running configuration + (IDEMPOTENT) + register: result + vyos.vyos.vyos_snmp_server: *id001 + + - name: Assert that the previous task was idempotent + assert: + that: + - result['changed'] == false + + always: + + - include_tasks: _remove_config.yaml diff --git a/tests/integration/targets/vyos_snmp_server/tests/cli/parsed.yaml b/tests/integration/targets/vyos_snmp_server/tests/cli/parsed.yaml new file mode 100644 index 0000000..e189134 --- /dev/null +++ b/tests/integration/targets/vyos_snmp_server/tests/cli/parsed.yaml @@ -0,0 +1,17 @@ +--- +- debug: + msg: START vyos_snmp_server parsed integration tests on connection={{ ansible_connection + }} + +- name: Provide the running configuration for parsing (config to be parsed) + register: result + vyos.vyos.vyos_snmp_server: + running_config: "{{ lookup('file', '_parsed.cfg') }}" + state: parsed + + +- name: Assert that config was correctly parsed + assert: + that: + - result.changed == false + - result.parsed == parsed.after diff --git a/tests/integration/targets/vyos_snmp_server/tests/cli/rendered.yaml b/tests/integration/targets/vyos_snmp_server/tests/cli/rendered.yaml new file mode 100644 index 0000000..e238827 --- /dev/null +++ b/tests/integration/targets/vyos_snmp_server/tests/cli/rendered.yaml @@ -0,0 +1,39 @@ +--- +- debug: + msg: START vyos_snmp_server rendered integration tests on connection={{ + ansible_connection }} + +- include_tasks: _remove_config.yaml + +- block: + - name: Render the given configuration in the form of native commands + register: result + vyos.vyos.vyos_snmp_server: + config: + communities: + - name: "switches" + authorization_type: "rw" + - name: "bridges" + clients: ["1.1.1.1", "12.1.1.10"] + contact: "admin2@ex.com" + listen_addresses: + - address: "20.1.1.1" + - address: "100.1.2.1" + port: 33 + snmp_v3: + users: + - user: adminuser + authentication: + plaintext_key: "abc1234567" + type: "sha" + privacy: + plaintext_key: "abc1234567" + type: "aes" + state: rendered + + - assert: + that: + - result.changed == false + - result.rendered|symmetric_difference(merged.commands) == [] + always: + - include_tasks: _remove_config.yaml diff --git a/tests/integration/targets/vyos_snmp_server/tests/cli/replaced.yaml b/tests/integration/targets/vyos_snmp_server/tests/cli/replaced.yaml new file mode 100644 index 0000000..0b5a564 --- /dev/null +++ b/tests/integration/targets/vyos_snmp_server/tests/cli/replaced.yaml @@ -0,0 +1,67 @@ +--- +- debug: + msg: START vyos_snmp_server replaced integration tests on connection={{ + ansible_connection }} + +- include_tasks: _remove_config.yaml + +- include_tasks: _populate_config.yaml + +- block: + + - name: Replace the existing configuration with the provided running configuration + register: result + vyos.vyos.vyos_snmp_server: &id001 + config: + communities: + - name: "bridges" + networks: ["1.1.1.0/24", "12.1.1.0/24"] + location: "RDU" + listen_addresses: + - address: "100.1.2.1" + port: 33 + snmp_v3: + users: + - user: adminuser + engine_id: "33" + authentication: + plaintext_key: "abc1234567" + type: "sha" + privacy: + plaintext_key: "abc1234567" + type: "aes" + - user: guestuser2 + authentication: + plaintext_key: "opq1234567" + type: "sha" + privacy: + plaintext_key: "opq1234567" + type: "aes" + views: + - view: "default" + oid: 1 + state: replaced + + - vyos.vyos.vyos_facts: + gather_network_resources: snmp_server + + - assert: + that: + - result.changed == true + - result.after == ansible_facts['network_resources']['snmp_server'] + - result.commands|length == 13 + + + - name: Replace the existing configuration with the provided running configuration + (IDEMPOTENT) + register: result + vyos.vyos.vyos_snmp_server: *id001 + + - name: Assert that the previous task was idempotent + assert: + that: + - result['changed'] == false + + always: + + - include_tasks: _remove_config.yaml diff --git a/tests/integration/targets/vyos_snmp_server/vars/main.yaml b/tests/integration/targets/vyos_snmp_server/vars/main.yaml new file mode 100644 index 0000000..060421b --- /dev/null +++ b/tests/integration/targets/vyos_snmp_server/vars/main.yaml @@ -0,0 +1,126 @@ +--- +merged: + before: {} + commands: + - set service snmp community switches authorization rw + - set service snmp community bridges client 1.1.1.1 + - set service snmp community bridges client 12.1.1.10 + - set service snmp listen-address 20.1.1.1 + - set service snmp listen-address 100.1.2.1 port 33 + - set service snmp v3 user adminuser auth type sha + - set service snmp v3 user adminuser auth plaintext-key ******** + - set service snmp v3 user adminuser privacy type aes + - set service snmp v3 user adminuser privacy plaintext-key ******** + - set service snmp contact admin2@ex.com + after: + communities: + - clients: + - 1.1.1.1 + - 12.1.1.10 + name: "bridges" + - authorization_type: "rw" + name: "switches" + contact: "admin2@ex.com" + listen_addresses: + - address: "100.1.2.1" + port: 33 + - address: "20.1.1.1" + snmp_v3: + users: + - authentication: + plaintext_key: "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER" + type: "sha" + privacy: + plaintext_key: "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER" + type: "aes" + user: "adminuser" + +overridden: + commands: + - delete service snmp contact admin2@ex.com + - delete service snmp listen-address 20.1.1.1 + - delete service snmp community switches authorization rw + - delete service snmp community bridges client 12.1.1.10 + - delete service snmp community bridges client 1.1.1.1 + - set service snmp community bridges network 1.1.1.0/24 + - set service snmp community bridges network 12.1.1.0/24 + - set service snmp v3 user guestuser2 auth type sha + - set service snmp v3 user guestuser2 auth plaintext-key ******** + - set service snmp v3 user guestuser2 privacy type aes + - set service snmp v3 user guestuser2 privacy plaintext-key ******** + - set service snmp v3 view default oid 1 + - set service snmp location \'RDU\' + + after: + communities: + - name: "bridges" + networks: + - "1.1.1.0/24" + - "12.1.1.0/24" + - name: "switches" + listen_addresses: + - address: "100.1.2.1" + port: 33 + location: "RDU, NC" + snmp_v3: + users: + - authentication: + plaintext_key: "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER" + type: "sha" + privacy: + plaintext_key: "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER" + type: "aes" + user: "adminuser" + - authentication: + plaintext_key: "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER" + type: "sha" + privacy: + plaintext_key: "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER" + type: "aes" + user: "guestuser2" + views: + - oid: 1 + view: "default" + +deleted: + commands: + - delete service snmp + after: {} + +parsed: + after: + communities: + - authorization_type: "ro" + clients: + - 203.0.113.10 + - 203.0.113.20 + name: "routers" + networks: + - 192.0.2.0/24 + - 2001::/64 + contact: "admin@example.com" + listen_addresses: + - address: "172.16.254.36" + port: 161 + - address: "2001::1" + location: "UK, London" + snmp_v3: + engine_id: "000000000000000000000002" + groups: + - group: "default" + mode: "ro" + view: "default" + users: + - authentication: + plaintext_key: "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER" + type: "sha" + group: "default" + privacy: + plaintext_key: "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER" + type: "aes" + user: "vyos" + views: + - oid: "1" + view: "default" + trap_target: + address: "203.0.113.10" diff --git a/tests/unit/modules/network/vyos/fixtures/vyos_snmp_server_config.cfg b/tests/unit/modules/network/vyos/fixtures/vyos_snmp_server_config.cfg new file mode 100644 index 0000000..689b18e --- /dev/null +++ b/tests/unit/modules/network/vyos/fixtures/vyos_snmp_server_config.cfg @@ -0,0 +1,12 @@ +set service snmp community bridges network 1.1.1.0/24 +set service snmp community bridges network 12.1.1.0/24 +set service snmp listen-address 100.1.2.1 port 33 +set service snmp location RDU, NC +set service snmp v3 user admin_user auth plaintext-key abc1234567 +set service snmp v3 user admin_user auth type sha +set service snmp v3 user admin_user privacy plaintext-key abc1234567 +set service snmp v3 user admin_user privacy type aes +set service snmp v3 user guest_user auth plaintext-key opq1234567 +set service snmp v3 user guest_user auth type sha +set service snmp v3 user guest_user privacy plaintext-key opq1234567 +set service snmp v3 user guest_user privacy type aes diff --git a/tests/unit/modules/network/vyos/test_vyos_snmp_server.py b/tests/unit/modules/network/vyos/test_vyos_snmp_server.py new file mode 100644 index 0000000..613960f --- /dev/null +++ b/tests/unit/modules/network/vyos/test_vyos_snmp_server.py @@ -0,0 +1,571 @@ +# (c) 2021 Red Hat Inc. +# +# 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 ansible_collections.vyos.vyos.tests.unit.compat.mock import patch +from ansible_collections.vyos.vyos.plugins.modules import vyos_snmp_server +from ansible_collections.vyos.vyos.tests.unit.modules.utils import ( + set_module_args, +) +from .vyos_module import TestVyosModule, load_fixture + + +class TestVyosSnmpServerModule(TestVyosModule): + + module = vyos_snmp_server + + def setUp(self): + super(TestVyosSnmpServerModule, self).setUp() + + self.mock_get_resource_connection_config = patch( + "ansible_collections.ansible.netcommon.plugins.module_utils.network.common.rm_base.resource_module_base.get_resource_connection" + ) + self.get_resource_connection_config = ( + self.mock_get_resource_connection_config.start() + ) + + self.mock_get_resource_connection_facts = patch( + "ansible_collections.ansible.netcommon.plugins.module_utils.network.common.facts.facts.get_resource_connection" + ) + self.get_resource_connection_facts = ( + self.mock_get_resource_connection_facts.start() + ) + + self.mock_execute_show_command = patch( + "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.snmp_server.snmp_server.Snmp_serverFacts.get_config" + ) + + self.execute_show_command = self.mock_execute_show_command.start() + + def tearDown(self): + super(TestVyosSnmpServerModule, self).tearDown() + self.mock_get_resource_connection_config.stop() + self.mock_get_resource_connection_facts.stop() + self.mock_execute_show_command.stop() + + def load_fixtures(self, commands=None, transport="cli", filename=None): + if filename is None: + filename = "vyos_snmp_server_config.cfg" + + def load_from_file(*args, **kwargs): + output = load_fixture(filename) + return output + + self.execute_show_command.side_effect = load_from_file + + def test_snmp_server_merged_idempotent(self): + set_module_args( + dict( + config=dict( + communities=[ + dict( + name="bridges", + networks=["12.1.1.0/24", "1.1.1.0/24"], + ) + ], + listen_addresses=[ + dict(address="100.1.2.1", port=33), + ], + location="RDU, NC", + snmp_v3=dict( + users=[ + dict( + user="admin_user", + authentication=dict( + type="sha", plaintext_key="abc1234567" + ), + privacy=dict( + type="aes", plaintext_key="abc1234567" + ), + ), + dict( + user="guest_user", + authentication=dict( + type="sha", plaintext_key="opq1234567" + ), + privacy=dict( + type="aes", plaintext_key="opq1234567" + ), + ), + ] + ), + ), + state="merged", + ) + ) + self.execute_module(changed=False, commands=[]) + + def test_snmp_server_replaced_idempotent(self): + set_module_args( + dict( + config=dict( + communities=[ + dict( + name="bridges", + networks=["12.1.1.0/24", "1.1.1.0/24"], + ) + ], + listen_addresses=[ + dict(address="100.1.2.1", port=33), + ], + location="RDU, NC", + snmp_v3=dict( + users=[ + dict( + user="admin_user", + authentication=dict( + type="sha", plaintext_key="abc1234567" + ), + privacy=dict( + type="aes", plaintext_key="abc1234567" + ), + ), + dict( + user="guest_user", + authentication=dict( + type="sha", plaintext_key="opq1234567" + ), + privacy=dict( + type="aes", plaintext_key="opq1234567" + ), + ), + ] + ), + ), + state="replaced", + ) + ) + self.execute_module(changed=False, commands=[]) + + def test_snmp_server_overridden_idempotent(self): + set_module_args( + dict( + config=dict( + communities=[ + dict( + name="bridges", + networks=["12.1.1.0/24", "1.1.1.0/24"], + ) + ], + listen_addresses=[ + dict(address="100.1.2.1", port=33), + ], + location="RDU, NC", + snmp_v3=dict( + users=[ + dict( + user="admin_user", + authentication=dict( + type="sha", plaintext_key="abc1234567" + ), + privacy=dict( + type="aes", plaintext_key="abc1234567" + ), + ), + dict( + user="guest_user", + authentication=dict( + type="sha", plaintext_key="opq1234567" + ), + privacy=dict( + type="aes", plaintext_key="opq1234567" + ), + ), + ] + ), + ), + state="overridden", + ) + ) + self.execute_module(changed=False, commands=[]) + + def test_snmp_server_merged(self): + set_module_args( + dict( + config=dict( + communities=[ + dict( + name="routers", + clients=["12.1.1.0/24", "1.1.1.0/24"], + authorization_type="rw", + ), + dict(name="switches", authorization_type="ro"), + ], + contact="admin@example.com", + description="snmp_config", + smux_peer="peer1", + trap_source="1.1.1.1", + trap_target=dict( + address="10.10.1.1", community="switches", port="80" + ), + snmp_v3=dict( + engine_id="34", + groups=[ + dict( + group="default", + mode="rw", + seclevel="priv", + view="view1", + ) + ], + trap_targets=[ + dict( + address="20.12.1.1", + authentication=dict( + type="sha", plaintext_key="abc1234567" + ), + privacy=dict( + type="aes", plaintext_key="abc1234567" + ), + ), + ], + ), + ), + state="merged", + ) + ) + commands = [ + "set service snmp community routers client 1.1.1.0/24", + "set service snmp community routers client 12.1.1.0/24", + "set service snmp community routers authorization rw", + "set service snmp community switches authorization ro", + "set service snmp v3 group default mode rw", + "set service snmp v3 group default seclevel priv", + "set service snmp v3 group default view view1", + "set service snmp v3 engineid 34", + "set service snmp contact admin@example.com", + "set service snmp description snmp_config", + "set service snmp smux-peer peer1", + "set service snmp trap-source 1.1.1.1", + "set service snmp trap-target 10.10.1.1", + ] + self.execute_module(changed=True, commands=commands) + + def test_snmp_server_replaced(self): + set_module_args( + dict( + config=dict( + communities=[ + dict( + name="routers", + clients=["12.1.1.0/24", "1.1.1.0/24"], + authorization_type="rw", + ), + dict(name="switches", authorization_type="ro"), + ], + contact="admin@example.com", + description="snmp_config", + smux_peer="peer1", + trap_source="1.1.1.1", + trap_target=dict( + address="10.10.1.1", community="switches", port="80" + ), + snmp_v3=dict( + engine_id="34", + groups=[ + dict( + group="default", + mode="rw", + seclevel="priv", + view="view1", + ) + ], + trap_targets=[ + dict( + address="20.12.1.1", + authentication=dict( + type="sha", plaintext_key="abc1234567" + ), + privacy=dict( + type="aes", plaintext_key="abc1234567" + ), + ), + ], + ), + ), + state="replaced", + ) + ) + commands = [ + "set service snmp community routers client 1.1.1.0/24", + "set service snmp community routers client 12.1.1.0/24", + "set service snmp community routers authorization rw", + "set service snmp community switches authorization ro", + "delete service snmp community bridges network 1.1.1.0/24", + "delete service snmp community bridges network 12.1.1.0/24", + "delete service snmp listen-address 100.1.2.1 port 33", + "set service snmp v3 group default mode rw", + "set service snmp v3 group default seclevel priv", + "set service snmp v3 group default view view1", + "delete service snmp v3 user admin_user auth type sha", + "delete service snmp v3 user admin_user auth plaintext-key abc1234567", + "delete service snmp v3 user admin_user privacy type aes", + "delete service snmp v3 user admin_user privacy plaintext-key abc1234567", + "delete service snmp v3 user guest_user auth type sha", + "delete service snmp v3 user guest_user auth plaintext-key opq1234567", + "delete service snmp v3 user guest_user privacy type aes", + "delete service snmp v3 user guest_user privacy plaintext-key opq1234567", + "set service snmp v3 engineid 34", + "set service snmp contact admin@example.com", + "set service snmp description snmp_config", + "set service snmp smux-peer peer1", + "set service snmp trap-source 1.1.1.1", + "set service snmp trap-target 10.10.1.1", + "delete service snmp location 'RDU, NC'", + ] + + self.execute_module(changed=True, commands=commands) + + def test_snmp_server_overridden(self): + set_module_args( + dict( + config=dict( + communities=[ + dict( + name="routers", + clients=["12.1.1.0/24", "1.1.1.0/24"], + authorization_type="rw", + ), + dict(name="switches", authorization_type="ro"), + ], + contact="admin@example.com", + description="snmp_config", + smux_peer="peer1", + trap_source="1.1.1.1", + trap_target=dict( + address="10.10.1.1", community="switches", port="80" + ), + snmp_v3=dict( + engine_id="34", + groups=[ + dict( + group="default", + mode="rw", + seclevel="priv", + view="view1", + ) + ], + trap_targets=[ + dict( + address="20.12.1.1", + authentication=dict( + type="sha", plaintext_key="abc1234567" + ), + privacy=dict( + type="aes", plaintext_key="abc1234567" + ), + ), + ], + ), + ), + state="overridden", + ) + ) + commands = [ + "set service snmp community routers client 1.1.1.0/24", + "set service snmp community routers client 12.1.1.0/24", + "set service snmp community routers authorization rw", + "set service snmp community switches authorization ro", + "delete service snmp community bridges network 1.1.1.0/24", + "delete service snmp community bridges network 12.1.1.0/24", + "delete service snmp listen-address 100.1.2.1 port 33", + "set service snmp v3 group default mode rw", + "set service snmp v3 group default seclevel priv", + "set service snmp v3 group default view view1", + "delete service snmp v3 user admin_user auth type sha", + "delete service snmp v3 user admin_user auth plaintext-key abc1234567", + "delete service snmp v3 user admin_user privacy type aes", + "delete service snmp v3 user admin_user privacy plaintext-key abc1234567", + "delete service snmp v3 user guest_user auth type sha", + "delete service snmp v3 user guest_user auth plaintext-key opq1234567", + "delete service snmp v3 user guest_user privacy type aes", + "delete service snmp v3 user guest_user privacy plaintext-key opq1234567", + "set service snmp v3 engineid 34", + "set service snmp contact admin@example.com", + "set service snmp description snmp_config", + "set service snmp smux-peer peer1", + "set service snmp trap-source 1.1.1.1", + "set service snmp trap-target 10.10.1.1", + "delete service snmp location 'RDU, NC'", + ] + + self.execute_module(changed=True, commands=commands) + + def test_snmp_server_deleted(self): + set_module_args( + dict( + state="deleted", + ) + ) + commands = ["delete service snmp"] + self.execute_module(changed=True, commands=commands) + + def test_snmp_server_rendered(self): + set_module_args( + dict( + config=dict( + communities=[ + dict( + name="routers", + clients=["12.1.1.0/24", "1.1.1.0/24"], + authorization_type="rw", + ), + dict(name="switches", authorization_type="ro"), + ], + contact="admin@example.com", + description="snmp_config", + smux_peer="peer1", + trap_source="1.1.1.1", + trap_target=dict( + address="10.10.1.1", community="switches", port="80" + ), + snmp_v3=dict( + engine_id="34", + groups=[ + dict( + group="default", + mode="rw", + seclevel="priv", + view="view1", + ) + ], + trap_targets=[ + dict( + address="20.12.1.1", + authentication=dict( + type="sha", plaintext_key="abc1234567" + ), + privacy=dict( + type="aes", plaintext_key="abc1234567" + ), + ), + ], + ), + ), + state="rendered", + ) + ) + commands = [ + "set service snmp community routers client 1.1.1.0/24", + "set service snmp community routers client 12.1.1.0/24", + "set service snmp community routers authorization rw", + "set service snmp community switches authorization ro", + "set service snmp v3 group default mode rw", + "set service snmp v3 group default seclevel priv", + "set service snmp v3 group default view view1", + "set service snmp v3 engineid 34", + "set service snmp contact admin@example.com", + "set service snmp description snmp_config", + "set service snmp smux-peer peer1", + "set service snmp trap-source 1.1.1.1", + "set service snmp trap-target 10.10.1.1", + ] + result = self.execute_module(changed=False) + self.assertEqual( + sorted(result["rendered"]), + sorted(commands), + result["rendered"], + ) + + def test_snmp_server_parsed(self): + commands = [ + "set service snmp community routers client 1.1.1.0/24", + "set service snmp community routers client 12.1.1.0/24", + "set service snmp community routers authorization rw", + "set service snmp community switches authorization ro", + "set service snmp v3 group default mode rw", + "set service snmp v3 group default seclevel priv", + "set service snmp v3 group default view view1", + "set service snmp v3 engineid 34", + "set service snmp contact admin@example.com", + "set service snmp description snmp_config", + "set service snmp smux-peer peer1", + "set service snmp trap-source 1.1.1.1", + "set service snmp trap-target 10.10.1.1", + ] + parsed_str = "\n".join(commands) + set_module_args(dict(running_config=parsed_str, state="parsed")) + result = self.execute_module(changed=False) + parsed_list = { + "communities": [ + { + "authorization_type": "rw", + "clients": ["1.1.1.0/24", "12.1.1.0/24"], + "name": "routers", + }, + {"authorization_type": "ro", "name": "switches"}, + ], + "contact": "admin@example.com", + "description": "snmp_config", + "smux_peer": "peer1", + "snmp_v3": { + "engine_id": "34", + "groups": [ + { + "group": "default", + "mode": "rw", + "seclevel": "priv", + "view": "view1", + } + ], + }, + "trap_source": "1.1.1.1", + "trap_target": {"address": "10.10.1.1"}, + } + self.assertEqual(parsed_list, result["parsed"]) + + def test_snmp_server_gathered(self): + set_module_args(dict(state="gathered")) + result = self.execute_module(changed=False) + gathered_list = { + "communities": [ + {"name": "bridges", "networks": ["1.1.1.0/24", "12.1.1.0/24"]}, + ], + "listen_addresses": [{"address": "100.1.2.1", "port": 33}], + "location": "RDU, NC", + "snmp_v3": { + "users": [ + { + "authentication": { + "plaintext_key": "abc1234567", + "type": "sha", + }, + "privacy": { + "plaintext_key": "abc1234567", + "type": "aes", + }, + "user": "admin_user", + }, + { + "authentication": { + "plaintext_key": "opq1234567", + "type": "sha", + }, + "privacy": { + "plaintext_key": "opq1234567", + "type": "aes", + }, + "user": "guest_user", + }, + ] + }, + } + self.assertEqual(gathered_list, result["gathered"]) |