From 4f52141bc7061d523adafbe93c8940cf2c47c10e Mon Sep 17 00:00:00 2001 From: Sumit Jaiswal Date: Fri, 28 Jun 2024 00:06:50 +0530 Subject: [PATCH] fix test Signed-off-by: Sumit Jaiswal --- ansible_risk_insight/finder.py | 3 +- test/test_inline_replace.py | 63 ++++++++++ .../block_and_when_play.yml | 111 +++++++++--------- .../block_and_when_play_fixed.yml | 71 +++++++++++ 4 files changed, 194 insertions(+), 54 deletions(-) diff --git a/ansible_risk_insight/finder.py b/ansible_risk_insight/finder.py index 7b644c909..ec431a4fb 100644 --- a/ansible_risk_insight/finder.py +++ b/ansible_risk_insight/finder.py @@ -830,9 +830,10 @@ def update_the_yaml_target(file_path, line_number_list, new_content_list): stop_line_number = int(input_line_number[1]) diff_in_lines = stop_line_number - start_line_number temp_content = [] - data_copy.append('\n') start = start_line_number - 1 end = stop_line_number - 1 + if len(data_copy) != start: + data_copy.append('\n') for i in range(start, end): line_number = i if len(lines) == i: diff --git a/test/test_inline_replace.py b/test/test_inline_replace.py index e69de29bb..995be2b51 100644 --- a/test/test_inline_replace.py +++ b/test/test_inline_replace.py @@ -0,0 +1,63 @@ +# -*- mode:python; coding:utf-8 -*- + +# Copyright (c) 2022 IBM Corp. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from ansible_risk_insight.finder import update_the_yaml_target + + +def test_inline_replace_for_block_and_when(): + file_path = "test/testdata/inline_replace_data/block_and_when_play.yml" + file_path_out = "test/testdata/inline_replace_data/block_and_when_play_fixed.yml" + line_number = [ + "L6-11", + "L12-20", + "L23-30", + "L31-34", + "L39-46", + "L47-50", + "L55-61", + "L62-65" + ] + new_content = [ + '''- name: Validate server authentication input provided by user\n when:\n + - (username is not defined or password is not defined) and (cert_file is not defined or key_file is not defined) + and (auth_token is not defined)\n ansible.builtin.fail:\n msg: "username/password or cert_file/key_file or auth_token + is mandatory"\n''', + '''- name: Fail when more than one valid authentication method is provided\n when:\n + - ((username is defined or password is defined) and (cert_file is defined or key_file is defined) and + auth_token is defined) or ((username is defined or password is defined) and (cert_file is defined or key_file is defined)) + or ((username is defined or password is defined) and auth_token is defined) or ((cert_file is defined or key_file is defined) and + auth_token is defined)\n ansible.builtin.fail:\n msg: "Only one authentication method is allowed. + Provide either username/password or cert_file/key_file or auth_token."\n''', + ''' - ilo_network:\n category: Systems\n command: GetNetworkAdapters\n + baseuri: "{{ baseuri }}"\n username: "{{ username }}"\n password: "{{ password }}"\n + register: network_adapter_details\n''', + '- name: Physical network adapter details in the server\n ansible.builtin.debug:\n msg: "{{ network_adapter_details }}"\n', + ''' - ilo_network:\n category: Systems\n command: GetNetworkAdapters\n + baseuri: "{{ baseuri }}"\n cert_file: "{{ cert_file }}"\n key_file: "{{ key_file }}"\n + register: network_adapter_details\n''', + '- name: Physical network adapter details present in the server\n ansible.builtin.debug:\n msg: "{{ network_adapter_details }}"\n', + ''' - ilo_network:\n category: Systems\n command: GetNetworkAdapters\n + baseuri: "{{ baseuri }}"\n auth_token: "{{ auth_token }}"\n register: network_adapter_details\n''', + '- name: Physical network adapter details in the server\n ansible.builtin.debug:\n msg: "{{ network_adapter_details }}"\n' + ] + + update_the_yaml_target(file_path, line_number, new_content) + with open(file_path, 'r') as file: + data = file.read() + with open(file_path_out, 'r') as file: + data_fixed = file.read() + + assert data == data_fixed diff --git a/test/testdata/inline_replace_data/block_and_when_play.yml b/test/testdata/inline_replace_data/block_and_when_play.yml index d607febbe..b50c57984 100644 --- a/test/testdata/inline_replace_data/block_and_when_play.yml +++ b/test/testdata/inline_replace_data/block_and_when_play.yml @@ -1,66 +1,71 @@ ---- - hosts: myhosts connection: local - gather_facts: False + gather_facts: false tasks: - - name: Validate server authentication input provided by user - fail: - msg: "username/password or cert_file/key_file or auth_token is mandatory" - when: - - (username is not defined or password is not defined) and (cert_file is not defined or key_file is not defined) and (auth_token is not defined) - - name: Fail when more than one valid authentication method is provided - fail: - msg: "Only one authentication method is allowed. Provide either username/password or cert_file/key_file or auth_token." - when: - - ((username is defined or password is defined) and (cert_file is defined or key_file is defined) and auth_token is defined) or - ((username is defined or password is defined) and (cert_file is defined or key_file is defined)) or - ((username is defined or password is defined) and auth_token is defined) or - ((cert_file is defined or key_file is defined) and auth_token is defined) + - name: Validate server authentication input provided by user + when: + - (username is not defined or password is not defined) and (cert_file is not defined + or key_file is not defined) and (auth_token is not defined) + ansible.builtin.fail: + msg: username/password or cert_file/key_file or auth_token is mandatory - - name: Get physical network adapter details when username and password are defined - block: - - ilo_network: - category: Systems - command: GetNetworkAdapters - baseuri: "{{ baseuri }}" - username: "{{ username }}" - password: "{{ password }}" - register: network_adapter_details + - name: Fail when more than one valid authentication method is provided + when: + - ((username is defined or password is defined) and (cert_file is defined or key_file + is defined) and auth_token is defined) or ((username is defined or password + is defined) and (cert_file is defined or key_file is defined)) or ((username + is defined or password is defined) and auth_token is defined) or ((cert_file + is defined or key_file is defined) and auth_token is defined) + ansible.builtin.fail: + msg: Only one authentication method is allowed. Provide either username/password + or cert_file/key_file or auth_token. - - name: Physical network adapter details in the server - debug: - msg: "{{ network_adapter_details }}" + - name: Get physical network adapter details when username and password are defined + block: - when: username is defined and password is defined + - ilo_network: + category: Systems + command: GetNetworkAdapters + baseuri: '{{ baseuri }}' + username: '{{ username }}' + password: '{{ password }}' + register: network_adapter_details - - name: Get physical network adapter details when cert_file and key_file are defined - block: - - ilo_network: - category: Systems - command: GetNetworkAdapters - baseuri: "{{ baseuri }}" - cert_file: "{{ cert_file }}" - key_file: "{{ key_file }}" - register: network_adapter_details + - name: Physical network adapter details in the server + ansible.builtin.debug: + msg: '{{ network_adapter_details }}' - - name: Physical network adapter details present in the server - debug: - msg: "{{ network_adapter_details }}" + when: username is defined and password is defined - when: cert_file is defined and key_file is defined + - name: Get physical network adapter details when cert_file and key_file are defined + block: - - name: Get physical network adapter details when auth_token is defined - block: - - ilo_network: - category: Systems - command: GetNetworkAdapters - baseuri: "{{ baseuri }}" - auth_token: "{{ auth_token }}" - register: network_adapter_details + - ilo_network: + category: Systems + command: GetNetworkAdapters + baseuri: '{{ baseuri }}' + cert_file: '{{ cert_file }}' + key_file: '{{ key_file }}' + register: network_adapter_details - - name: Physical network adapter details in the server - debug: - msg: "{{ network_adapter_details }}" + - name: Physical network adapter details present in the server + ansible.builtin.debug: + msg: '{{ network_adapter_details }}' - when: auth_token is defined + when: cert_file is defined and key_file is defined + + - name: Get physical network adapter details when auth_token is defined + block: + + - ilo_network: + category: Systems + command: GetNetworkAdapters + baseuri: '{{ baseuri }}' + auth_token: '{{ auth_token }}' + register: network_adapter_details + + - name: Physical network adapter details in the server + ansible.builtin.debug: + msg: '{{ network_adapter_details }}' + when: auth_token is defined diff --git a/test/testdata/inline_replace_data/block_and_when_play_fixed.yml b/test/testdata/inline_replace_data/block_and_when_play_fixed.yml index e69de29bb..b50c57984 100644 --- a/test/testdata/inline_replace_data/block_and_when_play_fixed.yml +++ b/test/testdata/inline_replace_data/block_and_when_play_fixed.yml @@ -0,0 +1,71 @@ +- hosts: myhosts + connection: local + gather_facts: false + tasks: + + - name: Validate server authentication input provided by user + when: + - (username is not defined or password is not defined) and (cert_file is not defined + or key_file is not defined) and (auth_token is not defined) + ansible.builtin.fail: + msg: username/password or cert_file/key_file or auth_token is mandatory + + - name: Fail when more than one valid authentication method is provided + when: + - ((username is defined or password is defined) and (cert_file is defined or key_file + is defined) and auth_token is defined) or ((username is defined or password + is defined) and (cert_file is defined or key_file is defined)) or ((username + is defined or password is defined) and auth_token is defined) or ((cert_file + is defined or key_file is defined) and auth_token is defined) + ansible.builtin.fail: + msg: Only one authentication method is allowed. Provide either username/password + or cert_file/key_file or auth_token. + + - name: Get physical network adapter details when username and password are defined + block: + + - ilo_network: + category: Systems + command: GetNetworkAdapters + baseuri: '{{ baseuri }}' + username: '{{ username }}' + password: '{{ password }}' + register: network_adapter_details + + - name: Physical network adapter details in the server + ansible.builtin.debug: + msg: '{{ network_adapter_details }}' + + when: username is defined and password is defined + + - name: Get physical network adapter details when cert_file and key_file are defined + block: + + - ilo_network: + category: Systems + command: GetNetworkAdapters + baseuri: '{{ baseuri }}' + cert_file: '{{ cert_file }}' + key_file: '{{ key_file }}' + register: network_adapter_details + + - name: Physical network adapter details present in the server + ansible.builtin.debug: + msg: '{{ network_adapter_details }}' + + when: cert_file is defined and key_file is defined + + - name: Get physical network adapter details when auth_token is defined + block: + + - ilo_network: + category: Systems + command: GetNetworkAdapters + baseuri: '{{ baseuri }}' + auth_token: '{{ auth_token }}' + register: network_adapter_details + + - name: Physical network adapter details in the server + ansible.builtin.debug: + msg: '{{ network_adapter_details }}' + when: auth_token is defined