Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
Signed-off-by: Sumit Jaiswal <[email protected]>
  • Loading branch information
justjais committed Jun 27, 2024
1 parent 5a48d89 commit 4f52141
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 54 deletions.
3 changes: 2 additions & 1 deletion ansible_risk_insight/finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
63 changes: 63 additions & 0 deletions test/test_inline_replace.py
Original file line number Diff line number Diff line change
@@ -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
111 changes: 58 additions & 53 deletions test/testdata/inline_replace_data/block_and_when_play.yml
Original file line number Diff line number Diff line change
@@ -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
71 changes: 71 additions & 0 deletions test/testdata/inline_replace_data/block_and_when_play_fixed.yml
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

0 comments on commit 4f52141

Please sign in to comment.