community.general/tests/integration/targets/prettytable/tasks/main.yml
2025-04-01 19:35:05 +04:00

205 lines
5.4 KiB
YAML

---
####################################################################
# WARNING: These are designed specifically for Ansible tests #
# and should not be used as examples of how to write Ansible roles #
####################################################################
# Copyright (c) 2020, Pavlo Bashynskyi (@levonet) <levonet@gmail.com>
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
- name: Install required libs
pip:
name: prettytable
state: present
delegate_to: localhost
become: false
# Test basic functionality
- name: Test basic table creation
prettytable:
data:
- name: Alice
age: 25
role: admin
- name: Bob
age: 30
role: user
register: basic_result
- name: Load expected basic table output
set_fact:
expected_basic_table_raw: "{{ lookup('file', 'basic_table.txt') }}"
- name: Remove copyright header from expected output
set_fact:
expected_basic_table: "{{ expected_basic_table_raw.split('\n')[4:] | join('\n') }}"
- name: Verify basic table output
assert:
that:
- basic_result.pretty_table | trim == expected_basic_table | trim
- not basic_result.failed
# Test column ordering
- name: Test table with custom column order
prettytable:
data:
- name: Alice
age: 25
role: admin
- name: Bob
age: 30
role: user
column_order:
- role
- name
- age
register: ordered_result
- name: Load expected ordered table output
set_fact:
expected_ordered_table_raw: "{{ lookup('file', 'ordered_table.txt') }}"
- name: Remove copyright header from expected output
set_fact:
expected_ordered_table: "{{ expected_ordered_table_raw.split('\n')[4:] | join('\n') }}"
- name: Verify ordered table output
assert:
that:
- ordered_result.pretty_table | trim == expected_ordered_table | trim
- not ordered_result.failed
# Test custom headers
- name: Test table with custom headers
prettytable:
data:
- name: Alice
age: 25
role: admin
header_names:
- "User Name"
- "User Age"
- "User Role"
register: headers_result
- name: Load expected headers table output
set_fact:
expected_headers_table_raw: "{{ lookup('file', 'custom_headers.txt') }}"
- name: Remove copyright header from expected output
set_fact:
expected_headers_table: "{{ expected_headers_table_raw.split('\n')[4:] | join('\n') }}"
- name: Verify custom headers output
assert:
that:
- headers_result.pretty_table | trim == expected_headers_table | trim
- not headers_result.failed
# Test column alignments
- name: Test table with alignments
prettytable:
data:
- date: "2023-01-01"
description: "Office supplies"
amount: 123.45
column_alignments:
amount: right
description: left
date: center
register: aligned_result
- name: Load expected aligned table output
set_fact:
expected_aligned_table_raw: "{{ lookup('file', 'aligned_table.txt') }}"
- name: Remove copyright header from expected output
set_fact:
expected_aligned_table: "{{ expected_aligned_table_raw.split('\n')[4:] | join('\n') }}"
- name: Verify aligned table output
assert:
that:
- aligned_result.pretty_table | trim == expected_aligned_table | trim
- not aligned_result.failed
# Test error conditions
- name: Test missing data parameter (should fail)
prettytable: {} # Empty dict to test missing required parameter
register: missing_data_result
ignore_errors: true
- name: Verify error for missing data
vars:
expected_msg: 'missing required arguments: data'
assert:
that:
- missing_data_result.failed
- expected_msg in missing_data_result.msg
- name: Test invalid data type (should fail)
prettytable:
data: "this is a string"
register: invalid_data_result
ignore_errors: true
- name: Verify error for invalid data
vars:
expected_msg: "Expected a list of dictionaries, got a string"
assert:
that:
- invalid_data_result.failed
- expected_msg == invalid_data_result.msg
- name: Test list with invalid items (should fail)
prettytable:
data:
- {"valid": "dict"}
- "invalid string item"
register: invalid_items_result
ignore_errors: true
- name: Verify error for invalid items
vars:
expected_msg: "All items in the list must be dictionaries, got a string"
assert:
that:
- invalid_items_result.failed
- expected_msg == invalid_items_result.msg
# Test empty data
- name: Test empty data list
prettytable:
data: []
register: empty_result
- name: Load expected empty table output
set_fact:
expected_empty_table_raw: "{{ lookup('file', 'empty_table.txt') }}"
- name: Remove copyright header from expected output
set_fact:
expected_empty_table: "{{ expected_empty_table_raw.split('\n')[4:] | join('\n') }}"
- name: Verify empty table output
assert:
that:
- empty_result.pretty_table | trim == expected_empty_table | trim
- not empty_result.failed
# Test invalid alignments
- name: Test invalid alignment value
prettytable:
data:
- col1: value1
col2: value2
column_alignments:
col1: invalid
register: invalid_alignment_result
- name: Verify invalid alignment handling
assert:
that:
- not invalid_alignment_result.failed # Should not fail, just warn