summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/integration/targets/vyos_ntp_global/defaults/main.yaml3
-rw-r--r--tests/integration/targets/vyos_ntp_global/tasks/cli.yaml19
-rw-r--r--tests/integration/targets/vyos_ntp_global/tasks/main.yaml4
-rw-r--r--tests/integration/targets/vyos_ntp_global/tests/cli/_parsed.cfg5
-rw-r--r--tests/integration/targets/vyos_ntp_global/tests/cli/_populate_config.yaml13
-rw-r--r--tests/integration/targets/vyos_ntp_global/tests/cli/_remove_config.yaml8
-rw-r--r--tests/integration/targets/vyos_ntp_global/tests/cli/deleted.yaml40
-rw-r--r--tests/integration/targets/vyos_ntp_global/tests/cli/empty_config.yaml60
-rw-r--r--tests/integration/targets/vyos_ntp_global/tests/cli/gathered.yaml24
-rw-r--r--tests/integration/targets/vyos_ntp_global/tests/cli/merged.yaml58
-rw-r--r--tests/integration/targets/vyos_ntp_global/tests/cli/overridden.yaml53
-rw-r--r--tests/integration/targets/vyos_ntp_global/tests/cli/parsed.yaml17
-rw-r--r--tests/integration/targets/vyos_ntp_global/tests/cli/rendered.yaml33
-rw-r--r--tests/integration/targets/vyos_ntp_global/tests/cli/replaced.yaml42
-rw-r--r--tests/integration/targets/vyos_ntp_global/tests/cli/rtt.yaml70
-rw-r--r--tests/integration/targets/vyos_ntp_global/vars/main.yaml108
-rw-r--r--tests/sanity/ignore-2.13.txt11
-rw-r--r--tests/unit/modules/network/vyos/fixtures/vyos_ntp_config.cfg10
-rw-r--r--tests/unit/modules/network/vyos/test_vyos_ntp_global.py364
19 files changed, 931 insertions, 11 deletions
diff --git a/tests/integration/targets/vyos_ntp_global/defaults/main.yaml b/tests/integration/targets/vyos_ntp_global/defaults/main.yaml
new file mode 100644
index 0000000..852a6be
--- /dev/null
+++ b/tests/integration/targets/vyos_ntp_global/defaults/main.yaml
@@ -0,0 +1,3 @@
+---
+testcase: '[^_].*'
+test_items: []
diff --git a/tests/integration/targets/vyos_ntp_global/tasks/cli.yaml b/tests/integration/targets/vyos_ntp_global/tasks/cli.yaml
new file mode 100644
index 0000000..93eb2fe
--- /dev/null
+++ b/tests/integration/targets/vyos_ntp_global/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_ntp_global/tasks/main.yaml b/tests/integration/targets/vyos_ntp_global/tasks/main.yaml
new file mode 100644
index 0000000..b957d2f
--- /dev/null
+++ b/tests/integration/targets/vyos_ntp_global/tasks/main.yaml
@@ -0,0 +1,4 @@
+---
+- include: cli.yaml
+ tags:
+ - network_cli
diff --git a/tests/integration/targets/vyos_ntp_global/tests/cli/_parsed.cfg b/tests/integration/targets/vyos_ntp_global/tests/cli/_parsed.cfg
new file mode 100644
index 0000000..0608aef
--- /dev/null
+++ b/tests/integration/targets/vyos_ntp_global/tests/cli/_parsed.cfg
@@ -0,0 +1,5 @@
+set system ntp server 10.3.6.5 noselect
+set system ntp server 10.3.6.5 dynamic
+set system ntp server 0.pool.ntp.org
+set system ntp server 1.pool.ntp.org
+set system ntp server 2.pool.ntp.org
diff --git a/tests/integration/targets/vyos_ntp_global/tests/cli/_populate_config.yaml b/tests/integration/targets/vyos_ntp_global/tests/cli/_populate_config.yaml
new file mode 100644
index 0000000..035fcba
--- /dev/null
+++ b/tests/integration/targets/vyos_ntp_global/tests/cli/_populate_config.yaml
@@ -0,0 +1,13 @@
+---
+- name: setup
+ vyos.vyos.vyos_config:
+ lines:
+ - set system ntp server server4 dynamic
+ - set system ntp server server5 preempt
+ - set system ntp server 10.3.6.5 noselect
+ - set system ntp server 10.3.6.5 preempt
+ - set system ntp server server4 noselect
+ - set system ntp server 10.3.6.5 prefer
+ ignore_errors: true
+ vars:
+ ansible_connection: ansible.netcommon.network_cli
diff --git a/tests/integration/targets/vyos_ntp_global/tests/cli/_remove_config.yaml b/tests/integration/targets/vyos_ntp_global/tests/cli/_remove_config.yaml
new file mode 100644
index 0000000..6017bdb
--- /dev/null
+++ b/tests/integration/targets/vyos_ntp_global/tests/cli/_remove_config.yaml
@@ -0,0 +1,8 @@
+---
+- name: Remove pre-existing ntp_global config
+ vyos.vyos.vyos_ntp_global:
+ config:
+ state: deleted
+ ignore_errors: true
+ vars:
+ ansible_connection: ansible.netcommon.network_cli
diff --git a/tests/integration/targets/vyos_ntp_global/tests/cli/deleted.yaml b/tests/integration/targets/vyos_ntp_global/tests/cli/deleted.yaml
new file mode 100644
index 0000000..530e2ad
--- /dev/null
+++ b/tests/integration/targets/vyos_ntp_global/tests/cli/deleted.yaml
@@ -0,0 +1,40 @@
+---
+- debug:
+ msg: START vyos_ntp_global 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_ntp_global: &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 == populate.config
+
+ - name: Delete the existing configuration with the provided running configuration
+ (IDEMPOTENT)
+ register: result
+ vyos.vyos.vyos_ntp_global: *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_ntp_global/tests/cli/empty_config.yaml b/tests/integration/targets/vyos_ntp_global/tests/cli/empty_config.yaml
new file mode 100644
index 0000000..ec29f16
--- /dev/null
+++ b/tests/integration/targets/vyos_ntp_global/tests/cli/empty_config.yaml
@@ -0,0 +1,60 @@
+---
+- debug:
+ msg: START vyos_ntp_global 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_ntp_global:
+ 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_ntp_global:
+ 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_ntp_global:
+ 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_ntp_global:
+ 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_ntp_global:
+ 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_ntp_global/tests/cli/gathered.yaml b/tests/integration/targets/vyos_ntp_global/tests/cli/gathered.yaml
new file mode 100644
index 0000000..5ac350d
--- /dev/null
+++ b/tests/integration/targets/vyos_ntp_global/tests/cli/gathered.yaml
@@ -0,0 +1,24 @@
+---
+- debug:
+ msg: START vyos_ntp_global 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_ntp_global:
+ state: gathered
+
+ - vyos.vyos.vyos_facts:
+ gather_network_resources: ntp_global
+
+ - name: Assert that facts are correctly generated
+ assert:
+ that:
+ - result.changed == false
+ - result.gathered == ansible_facts['network_resources']['ntp_global']
diff --git a/tests/integration/targets/vyos_ntp_global/tests/cli/merged.yaml b/tests/integration/targets/vyos_ntp_global/tests/cli/merged.yaml
new file mode 100644
index 0000000..2a22ad8
--- /dev/null
+++ b/tests/integration/targets/vyos_ntp_global/tests/cli/merged.yaml
@@ -0,0 +1,58 @@
+---
+- debug:
+ msg: START vyos_ntp_global 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_ntp_global: &id001
+ config:
+ servers:
+ - server: server5
+
+ - server: server4
+ options:
+ - noselect
+ - dynamic
+
+ - server: 10.3.6.5
+ options:
+ - noselect
+ - preempt
+ - dynamic
+ - prefer
+ state: merged
+
+ - vyos.vyos.vyos_facts:
+ gather_network_resources: ntp_global
+
+ - assert:
+ that:
+ - result.commands|length == 7
+ - result.changed == true
+ - result.commands|symmetric_difference(merged.commands) == []
+ - result.after == ansible_facts['network_resources']['ntp_global']
+ - result.after == merged.after
+
+
+ - name: Assert that before dicts were correctly generated
+ assert:
+ that:
+ - result.before == populate.config
+
+ - name:
+ Merge the provided configuration with the existing running configuration
+ (IDEMPOTENT)
+ register: result
+ vyos.vyos.vyos_ntp_global: *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_ntp_global/tests/cli/overridden.yaml b/tests/integration/targets/vyos_ntp_global/tests/cli/overridden.yaml
new file mode 100644
index 0000000..2269e74
--- /dev/null
+++ b/tests/integration/targets/vyos_ntp_global/tests/cli/overridden.yaml
@@ -0,0 +1,53 @@
+---
+- debug:
+ msg: START vyos_ntp_global 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_ntp_global: &id001
+ config:
+ servers:
+ - server: server1
+ options:
+ - dynamic
+ - prefer
+
+ - server: server2
+ options:
+ - noselect
+ - preempt
+
+ - server: server_add
+ options:
+ - preempt
+ state: overridden
+
+ - vyos.vyos.vyos_facts:
+ gather_network_resources: ntp_global
+
+ - assert:
+ that:
+ - result.changed == true
+ - result.after == ansible_facts['network_resources']['ntp_global']
+ - result.after == overridden.after
+
+ - name: Override the existing configuration with the provided running configuration
+ (IDEMPOTENT)
+ register: result
+ vyos.vyos.vyos_ntp_global: *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_ntp_global/tests/cli/parsed.yaml b/tests/integration/targets/vyos_ntp_global/tests/cli/parsed.yaml
new file mode 100644
index 0000000..c660ac4
--- /dev/null
+++ b/tests/integration/targets/vyos_ntp_global/tests/cli/parsed.yaml
@@ -0,0 +1,17 @@
+---
+- debug:
+ msg: START vyos_ntp_global parsed integration tests on connection={{ ansible_connection
+ }}
+
+- name: Provide the running configuration for parsing (config to be parsed)
+ register: result
+ vyos.vyos.vyos_ntp_global:
+ 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_ntp_global/tests/cli/rendered.yaml b/tests/integration/targets/vyos_ntp_global/tests/cli/rendered.yaml
new file mode 100644
index 0000000..1c1edd7
--- /dev/null
+++ b/tests/integration/targets/vyos_ntp_global/tests/cli/rendered.yaml
@@ -0,0 +1,33 @@
+---
+- debug:
+ msg: START vyos_ntp_global rendered integration tests on connection={{
+ ansible_connection }}
+
+- include_tasks: _remove_config.yaml
+
+- block:
+
+ - name: Structure provided configuration into device specific commands
+ register: result
+ vyos.vyos.vyos_ntp_global: &id001
+ config:
+ servers:
+ - server: server5
+
+ - server: server4
+ options:
+ - noselect
+ - dynamic
+
+ - server: 10.3.6.5
+ options:
+ - noselect
+ - preempt
+ - dynamic
+ - prefer
+ state: rendered
+
+ - assert:
+ that:
+ - result.changed == false
+ - result.rendered|symmetric_difference(merged.commands) == []
diff --git a/tests/integration/targets/vyos_ntp_global/tests/cli/replaced.yaml b/tests/integration/targets/vyos_ntp_global/tests/cli/replaced.yaml
new file mode 100644
index 0000000..8d1241b
--- /dev/null
+++ b/tests/integration/targets/vyos_ntp_global/tests/cli/replaced.yaml
@@ -0,0 +1,42 @@
+---
+- debug:
+ msg: START vyos_ntp_global replaced integration tests on connection={{
+ ansible_connection }}
+
+- include_tasks: _remove_config.yaml
+
+- include_tasks: _populate_config.yaml
+
+- block:
+ - name: Replace the provided configuration with the existing running configuration
+ register: result
+ vyos.vyos.vyos_ntp_global: &id001
+ config:
+ servers:
+ - server: server_new
+ options:
+ - prefer
+ state: replaced
+
+ - vyos.vyos.vyos_facts:
+ gather_network_resources: ntp_global
+
+ - assert:
+ that:
+ - result.changed == true
+ - result.after == ansible_facts['network_resources']['ntp_global']
+ - result.after == replaced.after
+
+ - name:
+ Replace the provided configuration with the existing running configuration
+ (IDEMPOTENT)
+ register: result
+ vyos.vyos.vyos_ntp_global: *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_ntp_global/tests/cli/rtt.yaml b/tests/integration/targets/vyos_ntp_global/tests/cli/rtt.yaml
new file mode 100644
index 0000000..86f75f4
--- /dev/null
+++ b/tests/integration/targets/vyos_ntp_global/tests/cli/rtt.yaml
@@ -0,0 +1,70 @@
+---
+- debug:
+ msg: START vyos_ntp_global rtt integration tests on connection={{
+ ansible_connection }}
+
+- include_tasks: _remove_config.yaml
+
+- block:
+ - name: Merge the provided configuration with the existing running configuration
+ register: baseconfig
+ vyos.vyos.vyos_ntp_global:
+ config:
+ servers:
+ - server: 10.3.6.5
+ options:
+ - noselect
+ - dynamic
+ - preempt
+ - prefer
+
+ - server: server4
+ options:
+ - noselect
+ - dynamic
+
+ - server: 0.pool.ntp.org
+
+ - server: 1.pool.ntp.org
+
+ - server: 2.pool.ntp.org
+
+ state: merged
+
+ - vyos.vyos.vyos_facts:
+ gather_network_resources: ntp_global
+
+ - assert:
+ that:
+ - baseconfig.commands|length == 6
+ - baseconfig.changed == true
+ - baseconfig.commands|symmetric_difference(rtt.commands) == []
+ - baseconfig.after == ansible_facts['network_resources']['ntp_global']
+
+ - name: Merge the existing configuration with the provided running configuration
+ register: result
+ vyos.vyos.vyos_ntp_global:
+ config:
+ servers:
+ - server: server1
+ options:
+ - dynamic
+ - prefer
+
+ - server: server2
+ options:
+ - noselect
+ - preempt
+
+ - name: Revert back to base config using facts round trip
+ register: revert
+ vyos.vyos.vyos_ntp_global:
+ config: "{{ ansible_facts['network_resources']['ntp_global'] }}"
+ state: overridden
+
+ - name: Assert that config was reverted
+ assert:
+ that: baseconfig.after == revert.after
+
+ always:
+ - include_tasks: _remove_config.yaml
diff --git a/tests/integration/targets/vyos_ntp_global/vars/main.yaml b/tests/integration/targets/vyos_ntp_global/vars/main.yaml
new file mode 100644
index 0000000..3b4dbc4
--- /dev/null
+++ b/tests/integration/targets/vyos_ntp_global/vars/main.yaml
@@ -0,0 +1,108 @@
+---
+rtt:
+ commands:
+ - set system ntp server server4 dynamic
+ - set system ntp server 10.3.6.5 noselect
+ - set system ntp server 10.3.6.5 dynamic
+ - set system ntp server 10.3.6.5 preempt
+ - set system ntp server server4 noselect
+ - set system ntp server 10.3.6.5 prefer
+
+merged:
+ commands:
+ - set system ntp server server5
+ - set system ntp server server4 dynamic
+ - set system ntp server 10.3.6.5 noselect
+ - set system ntp server 10.3.6.5 dynamic
+ - set system ntp server 10.3.6.5 preempt
+ - set system ntp server server4 noselect
+ - set system ntp server 10.3.6.5 prefer
+
+ after:
+ servers:
+ - server: 0.pool.ntp.org
+ - server: 1.pool.ntp.org
+ - server: 10.3.6.5
+ options:
+ - dynamic
+ - noselect
+ - preempt
+ - prefer
+ - server: 2.pool.ntp.org
+
+ - server: server4
+ options:
+ - dynamic
+ - noselect
+
+ - server: server5
+
+replaced:
+ commands:
+ - delete system ntp server 10.3.6.5
+ - delete system ntp server server4
+ - delete system ntp server server5
+ - set system ntp server server_new prefer
+
+ after:
+ servers:
+ - server: 0.pool.ntp.org
+ - server: 1.pool.ntp.org
+ - server: 2.pool.ntp.org
+ - options:
+ - prefer
+ server: server_new
+
+overridden:
+ commands:
+ - delete system ntp server server_new
+ - set system ntp server server1 dynamic
+ - set system ntp server server1 prefer
+ - set system ntp server server2 noselect
+ - set system ntp server server2 preempt
+ - set system ntp server server_add preempt
+
+
+ after:
+ servers:
+ - server: 0.pool.ntp.org
+ - server: 1.pool.ntp.org
+ - server: 2.pool.ntp.org
+ - server: server1
+ options:
+ - dynamic
+ - prefer
+
+ - server: server2
+ options:
+ - noselect
+ - preempt
+ - server: server_add
+ options:
+ - preempt
+
+deleted:
+ commands:
+ - delete system ntp server 10.3.6.5
+ - delete system ntp server server4
+ - delete system ntp server server5
+
+parsed:
+ after:
+ servers:
+ - server: 0.pool.ntp.org
+
+ - server: 1.pool.ntp.org
+ - server: 10.3.6.5
+ options:
+ - dynamic
+ - noselect
+ - server: 2.pool.ntp.org
+
+populate:
+ config:
+ servers:
+ - server: 0.pool.ntp.org
+
+ - server: 1.pool.ntp.org
+ - server: 2.pool.ntp.org
diff --git a/tests/sanity/ignore-2.13.txt b/tests/sanity/ignore-2.13.txt
index 496cf86..c835eef 100644
--- a/tests/sanity/ignore-2.13.txt
+++ b/tests/sanity/ignore-2.13.txt
@@ -1,12 +1 @@
plugins/action/vyos.py action-plugin-docs # base class for deprecated network platform modules using `connection: local`
-plugins/module_utils/network/vyos/config/ospf_interfaces/ospf_interfaces.py compile-2.6!skip
-plugins/module_utils/network/vyos/config/ospf_interfaces/ospf_interfaces.py import-2.6!skip
-plugins/module_utils/network/vyos/config/route_maps/route_maps.py compile-2.6!skip
-plugins/module_utils/network/vyos/config/route_maps/route_maps.py import-2.6!skip
-plugins/modules/vyos_route_maps.py import-2.6!skip
-plugins/modules/vyos_prefix_lists.py import-2.6!skip
-plugins/module_utils/network/vyos/config/prefix_lists/prefix_lists.py import-2.6!skip
-plugins/module_utils/network/vyos/config/prefix_lists/prefix_lists.py compile-2.6!skip
-plugins/modules/vyos_logging_global.py import-2.6!skip
-plugins/module_utils/network/vyos/config/logging_global/logging_global.py import-2.6!skip
-plugins/module_utils/network/vyos/config/logging_global/logging_global.py compile-2.6!skip
diff --git a/tests/unit/modules/network/vyos/fixtures/vyos_ntp_config.cfg b/tests/unit/modules/network/vyos/fixtures/vyos_ntp_config.cfg
new file mode 100644
index 0000000..ec9bd7f
--- /dev/null
+++ b/tests/unit/modules/network/vyos/fixtures/vyos_ntp_config.cfg
@@ -0,0 +1,10 @@
+set system ntp allow-clients address '10.1.1.0/24'
+set system ntp allow-clients address '10.1.2.0/24'
+set system ntp listen-address '10.2.3.1'
+set system ntp listen-address '10.4.3.1'
+set system ntp server server1
+set system ntp server server3 noselect
+set system ntp server server3 dynamic
+set system ntp server time1.vyos.net
+set system ntp server time2.vyos.net
+set system ntp server time3.vyos.net
diff --git a/tests/unit/modules/network/vyos/test_vyos_ntp_global.py b/tests/unit/modules/network/vyos/test_vyos_ntp_global.py
new file mode 100644
index 0000000..c27704f
--- /dev/null
+++ b/tests/unit/modules/network/vyos/test_vyos_ntp_global.py
@@ -0,0 +1,364 @@
+# (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_ntp_global
+from ansible_collections.vyos.vyos.tests.unit.modules.utils import (
+ set_module_args,
+)
+from .vyos_module import TestVyosModule, load_fixture
+
+
+class TestVyosNTPModule(TestVyosModule):
+
+ module = vyos_ntp_global
+
+ def setUp(self):
+ super(TestVyosNTPModule, 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.ntp_global.ntp_global.Ntp_globalFacts.get_config"
+ )
+
+ self.execute_show_command = self.mock_execute_show_command.start()
+
+ def tearDown(self):
+ super(TestVyosNTPModule, 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_ntp_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_ntp_merged_idempotent(self):
+ set_module_args(
+ dict(
+ config=dict(
+ allow_clients=["10.1.1.0/24", "10.1.2.0/24"],
+ listen_addresses=["10.2.3.1", "10.4.3.1"],
+ servers=[
+ dict(server="server1"),
+ dict(
+ server="server3", options=["noselect", "dynamic"]
+ ),
+ dict(server="time1.vyos.net"),
+ dict(server="time2.vyos.net"),
+ dict(server="time3.vyos.net"),
+ ],
+ ),
+ state="merged",
+ )
+ )
+ self.execute_module(changed=False, commands=[])
+
+ def test_ntp_merged(self):
+ set_module_args(
+ dict(
+ config=dict(
+ allow_clients=["10.2.2.0/24", "10.3.3.0/24"],
+ listen_addresses=["10.3.4.1", "10.4.5.1"],
+ servers=[
+ dict(server="server4", options=["dynamic", "preempt"]),
+ dict(
+ server="server5",
+ options=[
+ "noselect",
+ "dynamic",
+ "preempt",
+ "prefer",
+ ],
+ ),
+ ],
+ ),
+ state="merged",
+ )
+ )
+
+ commands = [
+ "set system ntp allow-clients address 10.2.2.0/24",
+ "set system ntp allow-clients address 10.3.3.0/24",
+ "set system ntp listen-address 10.3.4.1",
+ "set system ntp listen-address 10.4.5.1",
+ "set system ntp server server4 dynamic",
+ "set system ntp server server4 preempt",
+ "set system ntp server server5 dynamic",
+ "set system ntp server server5 noselect",
+ "set system ntp server server5 preempt",
+ "set system ntp server server5 prefer",
+ ]
+
+ self.execute_module(changed=True, commands=commands)
+
+ def test_ntp_replaced(self):
+ set_module_args(
+ dict(
+ config=dict(
+ allow_clients=["10.3.4.0/24", "10.4.5.0/24"],
+ listen_addresses=["10.3.3.1", "10.4.4.1"],
+ servers=[
+ dict(server="server4", options=["noselect", "prefer"]),
+ dict(
+ server="server6",
+ options=[
+ "noselect",
+ "dynamic",
+ "prefer",
+ "preempt",
+ ],
+ ),
+ dict(server="time1.vyos.net"),
+ dict(server="time2.vyos.net"),
+ dict(server="time3.vyos.net"),
+ ],
+ ),
+ state="replaced",
+ )
+ )
+ commands = [
+ "delete system ntp allow-clients address 10.1.1.0/24",
+ "delete system ntp allow-clients address 10.1.2.0/24",
+ "delete system ntp listen-address 10.2.3.1",
+ "delete system ntp listen-address 10.4.3.1",
+ "delete system ntp server server1",
+ "delete system ntp server server3",
+ "set system ntp allow-clients address 10.3.4.0/24",
+ "set system ntp allow-clients address 10.4.5.0/24",
+ "set system ntp listen-address 10.3.3.1",
+ "set system ntp listen-address 10.4.4.1",
+ "set system ntp server server4 noselect",
+ "set system ntp server server4 prefer",
+ "set system ntp server server6 noselect",
+ "set system ntp server server6 dynamic",
+ "set system ntp server server6 prefer",
+ "set system ntp server server6 preempt",
+ ]
+ self.execute_module(changed=True, commands=commands)
+
+ def test_ntp_replaced_idempotent(self):
+ set_module_args(
+ dict(
+ config=dict(
+ allow_clients=["10.1.1.0/24", "10.1.2.0/24"],
+ listen_addresses=["10.2.3.1", "10.4.3.1"],
+ servers=[
+ dict(server="server1"),
+ dict(
+ server="server3", options=["noselect", "dynamic"]
+ ),
+ dict(server="time1.vyos.net"),
+ dict(server="time2.vyos.net"),
+ dict(server="time3.vyos.net"),
+ ],
+ ),
+ state="replaced",
+ )
+ )
+ self.execute_module(changed=False, commands=[])
+
+ def test_ntp_overridden(self):
+ set_module_args(
+ dict(
+ config=dict(
+ allow_clients=["10.9.9.0/24"],
+ listen_addresses=["10.9.9.1"],
+ servers=[
+ dict(server="server9"),
+ dict(
+ server="server6", options=["noselect", "dynamic"]
+ ),
+ dict(server="time1.vyos.net"),
+ dict(server="time2.vyos.net"),
+ dict(server="time3.vyos.net"),
+ ],
+ ),
+ state="overridden",
+ )
+ )
+ commands = [
+ "delete system ntp allow-clients address 10.1.1.0/24",
+ "delete system ntp allow-clients address 10.1.2.0/24",
+ "delete system ntp listen-address 10.2.3.1",
+ "delete system ntp listen-address 10.4.3.1",
+ "delete system ntp server server1",
+ "delete system ntp server server3",
+ "set system ntp allow-clients address 10.9.9.0/24",
+ "set system ntp listen-address 10.9.9.1",
+ "set system ntp server server9",
+ "set system ntp server server6 noselect",
+ "set system ntp server server6 dynamic",
+ ]
+ self.execute_module(changed=True, commands=commands)
+
+ def test_ntp_overridden_idempotent(self):
+ set_module_args(
+ dict(
+ config=dict(
+ allow_clients=["10.1.1.0/24", "10.1.2.0/24"],
+ listen_addresses=["10.2.3.1", "10.4.3.1"],
+ servers=[
+ dict(server="server1"),
+ dict(
+ server="server3", options=["noselect", "dynamic"]
+ ),
+ dict(server="time1.vyos.net"),
+ dict(server="time2.vyos.net"),
+ dict(server="time3.vyos.net"),
+ ],
+ ),
+ state="overridden",
+ )
+ )
+ self.execute_module(changed=False, commands=[])
+
+ def test_ntp_rendered(self):
+ set_module_args(
+ dict(
+ config=dict(
+ allow_clients=["10.7.7.0/24", "10.8.8.0/24"],
+ listen_addresses=["10.7.9.1"],
+ servers=[
+ dict(server="server79"),
+ dict(
+ server="server46", options=["noselect", "dynamic"]
+ ),
+ dict(server="time1.vyos.net"),
+ dict(server="time2.vyos.net"),
+ dict(server="time3.vyos.net"),
+ ],
+ ),
+ state="rendered",
+ )
+ )
+ rendered_commands = [
+ "set system ntp allow-clients address 10.7.7.0/24",
+ "set system ntp allow-clients address 10.8.8.0/24",
+ "set system ntp listen-address 10.7.9.1",
+ "set system ntp server server79",
+ "set system ntp server server46 noselect",
+ "set system ntp server server46 dynamic",
+ "set system ntp server time1.vyos.net",
+ "set system ntp server time2.vyos.net",
+ "set system ntp server time3.vyos.net",
+ ]
+ result = self.execute_module(changed=False)
+ self.assertEqual(
+ sorted(result["rendered"]),
+ sorted(rendered_commands),
+ result["rendered"],
+ )
+
+ def test_ntp_parsed(self):
+
+ commands = (
+ "set system ntp allow-clients address 10.7.7.0/24",
+ "set system ntp allow-clients address 10.6.7.0/24",
+ "set system ntp listen-address 10.7.9.1",
+ "set system ntp listen-address 10.7.7.1",
+ "set system ntp server check",
+ "set system ntp server server46 noselect",
+ "set system ntp server server46 prefer",
+ "set system ntp server time1.vyos.net",
+ "set system ntp server time2.vyos.net",
+ "set system ntp server time3.vyos.net",
+ )
+ parsed_str = "\n".join(commands)
+ set_module_args(dict(running_config=parsed_str, state="parsed"))
+ result = self.execute_module(changed=False)
+ parsed_list = {
+ "allow_clients": ["10.6.7.0/24", "10.7.7.0/24"],
+ "listen_addresses": ["10.7.7.1", "10.7.9.1"],
+ "servers": [
+ {"server": "check"},
+ {"server": "server46", "options": ["noselect", "prefer"]},
+ {"server": "time1.vyos.net"},
+ {"server": "time2.vyos.net"},
+ {"server": "time3.vyos.net"},
+ ],
+ }
+ self.assertEqual(parsed_list, result["parsed"])
+
+ def test_ntp_gathered(self):
+ set_module_args(dict(state="gathered"))
+ result = self.execute_module(changed=False)
+ gathered_list = {
+ "allow_clients": ["10.1.1.0/24", "10.1.2.0/24"],
+ "listen_addresses": ["10.2.3.1", "10.4.3.1"],
+ "servers": [
+ {"server": "server1"},
+ {"server": "server3", "options": ["dynamic", "noselect"]},
+ {"server": "time1.vyos.net"},
+ {"server": "time2.vyos.net"},
+ {"server": "time3.vyos.net"},
+ ],
+ }
+
+ self.assertEqual(gathered_list, result["gathered"])
+
+ def test_ntp_deleted(self):
+ set_module_args(
+ dict(
+ config=dict(
+ allow_clients=["10.1.1.0/24"],
+ listen_addresses=["10.2.3.1"],
+ servers=[
+ dict(server="server1"),
+ dict(server="server3", options=["noselect"]),
+ dict(server="time1.vyos.net"),
+ dict(server="time2.vyos.net"),
+ dict(server="time3.vyos.net"),
+ ],
+ ),
+ state="deleted",
+ )
+ )
+ commands = [
+ "delete system ntp allow-clients",
+ "delete system ntp listen-address",
+ "delete system ntp server server1",
+ "delete system ntp server server3",
+ "delete system ntp server time1.vyos.net",
+ "delete system ntp server time2.vyos.net",
+ "delete system ntp server time3.vyos.net",
+ ]
+ self.execute_module(changed=True, commands=commands)