From 3d46258cffc909631951627d04a2349f33cae6b0 Mon Sep 17 00:00:00 2001 From: Mike Wiebe Date: Mon, 28 Aug 2017 15:00:56 -0400 Subject: [PATCH] Refactor/fix nxos_nxapi to use show run (#28675) * Refactor/fix nxos_nxapi to use show run * Fix unit tests * Python 3 compatibility --- lib/ansible/modules/network/nxos/nxos_nxapi.py | 18 +++++++++++------- .../targets/nxos_nxapi/tasks/main.yaml | 2 ++ .../tasks/platform/default/assert_changes.yaml | 7 +++++++ .../tasks/platform/n7k/assert_changes.yaml | 7 +++++++ .../nxos_nxapi/tests/cli/configure.yaml | 18 ++++++++++-------- .../targets/nxos_nxapi/tests/cli/disable.yaml | 2 -- .../targets/nxos_nxapi/tests/cli/enable.yaml | 2 +- .../nxos/fixtures/nxos_nxapi/n3k/show_nxapi | 2 -- .../nxos/fixtures/nxos_nxapi/n3k/show_run_all | 2 ++ .../nxos/fixtures/nxos_nxapi/n7k/show_nxapi | 4 ---- .../nxos/fixtures/nxos_nxapi/n7k/show_run_all | 5 +++++ 11 files changed, 45 insertions(+), 24 deletions(-) create mode 100644 test/integration/targets/nxos_nxapi/tasks/platform/default/assert_changes.yaml create mode 100644 test/integration/targets/nxos_nxapi/tasks/platform/n7k/assert_changes.yaml delete mode 100644 test/units/modules/network/nxos/fixtures/nxos_nxapi/n3k/show_nxapi create mode 100644 test/units/modules/network/nxos/fixtures/nxos_nxapi/n3k/show_run_all delete mode 100644 test/units/modules/network/nxos/fixtures/nxos_nxapi/n7k/show_nxapi create mode 100644 test/units/modules/network/nxos/fixtures/nxos_nxapi/n7k/show_run_all diff --git a/lib/ansible/modules/network/nxos/nxos_nxapi.py b/lib/ansible/modules/network/nxos/nxos_nxapi.py index ccab0640c1..9b24e8b915 100644 --- a/lib/ansible/modules/network/nxos/nxos_nxapi.py +++ b/lib/ansible/modules/network/nxos/nxos_nxapi.py @@ -196,7 +196,7 @@ def map_obj_to_commands(want, have, module): return commands def parse_http(data): - http_res = [r'HTTP Port:\s+(\d+)', r'HTTP Listen on port (\d+)'] + http_res = [r'nxapi http port (\d+)'] http_port = None for regex in http_res: @@ -208,7 +208,7 @@ def parse_http(data): return {'http': http_port is not None, 'http_port': http_port} def parse_https(data): - https_res = [r'HTTPS Port:\s+(\d+)', r'HTTPS Listen on port (\d+)'] + https_res = [r'nxapi https port (\d+)'] https_port = None for regex in https_res: @@ -220,15 +220,19 @@ def parse_https(data): return {'https': https_port is not None, 'https_port': https_port} def parse_sandbox(data): - match = re.search(r'Sandbox:\s+(.+)$', data, re.M) + sandbox = [item for item in data.split('\n') if re.search(r'.*sandbox.*', item)] value = False - if match: - value = match.group(1) == 'Enabled' + if sandbox and sandbox[0] == 'nxapi sandbox': + value = True return {'sandbox': value} def map_config_to_obj(module): - out = run_commands(module, ['show nxapi'], check_rc=False)[0] - if out == '': + out = run_commands(module, ['show run all | inc nxapi'], check_rc=False)[0] + match = re.search(r'no feature nxapi', out, re.M) + # There are two possible outcomes when nxapi is disabled on nxos platforms. + # 1. Nothing is displayed in the running config. + # 2. The 'no feature nxapi' command is displayed in the running config. + if match or out == '': return {'state': 'absent'} out = str(out).strip() diff --git a/test/integration/targets/nxos_nxapi/tasks/main.yaml b/test/integration/targets/nxos_nxapi/tasks/main.yaml index 415c99d8b1..70e67b38df 100644 --- a/test/integration/targets/nxos_nxapi/tasks/main.yaml +++ b/test/integration/targets/nxos_nxapi/tasks/main.yaml @@ -1,2 +1,4 @@ --- +# Only cli tests for this module since cli is used +# to test nxapi - { include: cli.yaml, tags: ['cli'] } diff --git a/test/integration/targets/nxos_nxapi/tasks/platform/default/assert_changes.yaml b/test/integration/targets/nxos_nxapi/tasks/platform/default/assert_changes.yaml new file mode 100644 index 0000000000..4dca027503 --- /dev/null +++ b/test/integration/targets/nxos_nxapi/tasks/platform/default/assert_changes.yaml @@ -0,0 +1,7 @@ +--- +- name: Assert configuration changes + assert: + that: + - result.stdout[0]['TABLE_listen_on_port']['ROW_listen_on_port'].l_port + - result.stdout[0]['TABLE_listen_on_port']['ROW_listen_on_port'].l_port | string | search("9443") + - result.stdout[0]['operation_status'].o_status == 'nxapi enabled' diff --git a/test/integration/targets/nxos_nxapi/tasks/platform/n7k/assert_changes.yaml b/test/integration/targets/nxos_nxapi/tasks/platform/n7k/assert_changes.yaml new file mode 100644 index 0000000000..965fc51ebf --- /dev/null +++ b/test/integration/targets/nxos_nxapi/tasks/platform/n7k/assert_changes.yaml @@ -0,0 +1,7 @@ +--- +- name: Assert configuration changes + assert: + that: + - result.stdout[0].http_port is not defined + - result.stdout[0].https_port | string | search("9443") + - result.stdout[0].sandbox_status == 'Enabled' diff --git a/test/integration/targets/nxos_nxapi/tests/cli/configure.yaml b/test/integration/targets/nxos_nxapi/tests/cli/configure.yaml index 0b53a7a46f..733523d155 100644 --- a/test/integration/targets/nxos_nxapi/tests/cli/configure.yaml +++ b/test/integration/targets/nxos_nxapi/tests/cli/configure.yaml @@ -1,6 +1,9 @@ --- - debug: msg="START cli/configure.yaml" +- set_fact: nxapi_sandbox_option="yes" + when: platform | search('N7K') + - name: Setup - put NXAPI in stopped state nxos_nxapi: state: absent @@ -9,7 +12,7 @@ - name: Configure NXAPI nxos_nxapi: enable_http: no - enable_sandbox: no + enable_sandbox: "{{nxapi_sandbox_option|default(omit)}}" enable_https: yes https_port: 9443 provider: "{{ cli }}" @@ -21,17 +24,16 @@ provider: "{{ cli }}" register: result -- name: Assert configuration changes - assert: - that: - - result.stdout[0].http_port is not defined - - result.stdout[0].https_port == 9443 - - result.stdout[0].sandbox_status == 'Disabled' +- include: targets/nxos_nxapi/tasks/platform/n7k/assert_changes.yaml + when: platform | match('N7K') + +- include: targets/nxos_nxapi/tasks/platform/default/assert_changes.yaml + when: not (platform | search('N7K')) - name: Configure NXAPI again nxos_nxapi: enable_http: no - enable_sandbox: no + enable_sandbox: "{{nxapi_sandbox_option|default(omit)}}" enable_https: yes https_port: 9443 provider: "{{ cli }}" diff --git a/test/integration/targets/nxos_nxapi/tests/cli/disable.yaml b/test/integration/targets/nxos_nxapi/tests/cli/disable.yaml index 7d3df6e40f..41affe2511 100644 --- a/test/integration/targets/nxos_nxapi/tests/cli/disable.yaml +++ b/test/integration/targets/nxos_nxapi/tests/cli/disable.yaml @@ -40,7 +40,5 @@ assert: that: result.changed == false -# FIXME https://github.com/ansible/ansible-modules-core/issues/4955 - ignore_errors: yes - debug: msg="END cli/disable.yaml" diff --git a/test/integration/targets/nxos_nxapi/tests/cli/enable.yaml b/test/integration/targets/nxos_nxapi/tests/cli/enable.yaml index 323e9c2cc9..37a154da45 100644 --- a/test/integration/targets/nxos_nxapi/tests/cli/enable.yaml +++ b/test/integration/targets/nxos_nxapi/tests/cli/enable.yaml @@ -9,7 +9,7 @@ - name: Enable NXAPI nxos_nxapi: - state: started + state: present provider: "{{ cli }}" register: result diff --git a/test/units/modules/network/nxos/fixtures/nxos_nxapi/n3k/show_nxapi b/test/units/modules/network/nxos/fixtures/nxos_nxapi/n3k/show_nxapi deleted file mode 100644 index 54dd7d778e..0000000000 --- a/test/units/modules/network/nxos/fixtures/nxos_nxapi/n3k/show_nxapi +++ /dev/null @@ -1,2 +0,0 @@ -nxapi enabled -HTTP Listen on port 80 diff --git a/test/units/modules/network/nxos/fixtures/nxos_nxapi/n3k/show_run_all b/test/units/modules/network/nxos/fixtures/nxos_nxapi/n3k/show_run_all new file mode 100644 index 0000000000..da0dfaa79f --- /dev/null +++ b/test/units/modules/network/nxos/fixtures/nxos_nxapi/n3k/show_run_all @@ -0,0 +1,2 @@ +feature nxapi +nxapi http port 80 diff --git a/test/units/modules/network/nxos/fixtures/nxos_nxapi/n7k/show_nxapi b/test/units/modules/network/nxos/fixtures/nxos_nxapi/n7k/show_nxapi deleted file mode 100644 index 834a46b0a4..0000000000 --- a/test/units/modules/network/nxos/fixtures/nxos_nxapi/n7k/show_nxapi +++ /dev/null @@ -1,4 +0,0 @@ - -NX-API: Enabled Sandbox: Disabled -HTTP Port: 80 HTTPS Port: Disabled - diff --git a/test/units/modules/network/nxos/fixtures/nxos_nxapi/n7k/show_run_all b/test/units/modules/network/nxos/fixtures/nxos_nxapi/n7k/show_run_all new file mode 100644 index 0000000000..0c4a3ea5a6 --- /dev/null +++ b/test/units/modules/network/nxos/fixtures/nxos_nxapi/n7k/show_run_all @@ -0,0 +1,5 @@ +feature nxapi +nxapi http port 80 +no nxapi https +no nxapi sandbox +