Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mikemorency committed Dec 16, 2024
1 parent e89fe3e commit fdaa257
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 9 deletions.
18 changes: 9 additions & 9 deletions plugins/inventory/esxi_hosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,17 @@
type: list
elements: str
default: []
group_by_path:
group_by_paths:
description:
- If true, groups will be created based on the ESXI hosts' paths.
- Any slashes in the paths will be replaced by underscores in the group names.
- Paths will be sanitized to match Ansible group name standards. For example, any slashes or dashes in the paths will be replaced by underscores in the group names.
- A group is created for each step down in the path, with the group from the step above containing subsequent groups.
- For example, a path /DC/hosts/Cluster will create groups 'DC' which contains group 'DC_hosts' which contains group 'DC_hosts_Cluster'
- For example, a path /DC-01/hosts/Cluster will create groups 'DC_01' which contains group 'DC_01_hosts' which contains group 'DC_01_hosts_Cluster'
default: false
type: bool
group_by_path_prefix:
group_by_paths_prefix:
description:
- If O(group_by_path) is true, set this variable if you want to add a prefix to any groups created based on paths.
- If O(group_by_paths) is true, set this variable if you want to add a prefix to any groups created based on paths.
- By default, no prefix is added to the group names.
default: ''
type: str
Expand Down Expand Up @@ -113,7 +113,7 @@
plugin: vmware.vmware.esxi_hosts
# Create groups based on host paths
group_by_path: true
group_by_paths: true
# Create a group with hosts that support vMotion using the vmotionSupported property
properties: ["name", "capability"]
Expand Down Expand Up @@ -404,15 +404,15 @@ def add_host_to_groups_based_on_path(self, esxi_host: EsxiInventoryHost):
Optionally, the user can add a prefix to the groups created by this process.
The final group in the path will be where the ESXi host is added.
"""
if not self.get_option("group_by_path"):
if not self.get_option("group_by_paths"):
return

path_parts = esxi_host.path.split('/')
group_name_parts = []
last_created_group = None

if self.get_option("group_by_path_prefix"):
group_name_parts = [self.get_option("group_by_path_prefix")]
if self.get_option("group_by_paths_prefix"):
group_name_parts = [self.get_option("group_by_paths_prefix")]

for path_part in path_parts:
if not path_part:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
run_on_simulator: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
plugin: vmware.vmware.esxi_hosts
cache: false
group_by_paths: true
group_by_paths_prefix: test
gather_tags: true
13 changes: 13 additions & 0 deletions tests/integration/targets/vmware_inventory_esxi_hosts/run.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
- hosts: localhost
gather_facts: no
tasks:
- name: Import eco-vcenter credentials
ansible.builtin.include_vars:
file: ../../integration_config.yml
tags: eco-vcenter-ci

- name: Call esxi_hosts inventory role
ansible.builtin.import_role:
name: vmware_inventory_esxi_hosts
tags:
- eco-vcenter-ci
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
- block:
- name: Import common vars
ansible.builtin.include_vars:
file: ../group_vars.yml

- name: Run Inventory Plugin
ansible.builtin.command: ansible-inventory -i "{{ role_path }}/files/test.esxi_hosts.yml" --list
register: _inventory_out

- name: Parse Inventory Results as JSON
ansible.builtin.set_fact:
inventory_results: "{{ _inventory_out.stdout | from_json }}"

# you can't reference the 'all' property here for some reason. It reverts back to the test playbook inventory
# instead of the inventory_results
- name: Check Output
ansible.builtin.assert:
that:
- first_host.ansible_host
- first_host.tags is defined
- first_host.tags_by_category is defined
- >-
(inventory_results.poweredOn.hosts | length) ==
(inventory_results._meta.hostvars.values() | selectattr('summary.runtime.powerState', 'equalto', 'poweredOn') | length)
- (inventory_results | length) > 3
- ('test_' + vcenter_datacenter | replace('-', '_')) in inventory_results.keys()
vars:
first_host: "{{ (inventory_results._meta.hostvars.values() | first) }}"

0 comments on commit fdaa257

Please sign in to comment.