-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PR to fix to correctly identify task based on Ansible block and when …
…indentation (#250) * fix for ari issue 232 Signed-off-by: Sumit Jaiswal <[email protected]> * fix test Signed-off-by: Sumit Jaiswal <[email protected]> * fix test Signed-off-by: Sumit Jaiswal <[email protected]> * fix new line Signed-off-by: Sumit Jaiswal <[email protected]> --------- Signed-off-by: Sumit Jaiswal <[email protected]>
- Loading branch information
Showing
5 changed files
with
215 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# -*- mode:python; coding:utf-8 -*- | ||
|
||
# Copyright (c) 2024 RedHat. 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
71 changes: 71 additions & 0 deletions
71
test/testdata/inline_replace_data/block_and_when_play_fixed.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |