Skip to content

Commit

Permalink
Merge pull request #83 from equinix-labs/add_proper_integration_test_…
Browse files Browse the repository at this point in the history
…workflow

Fixing integrations tests, Take #2
  • Loading branch information
displague authored Apr 5, 2023
2 parents c634db8 + 2fdd87d commit b94c837
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 53 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jobs:
defaults:
run:
working-directory: .ansible/collections/ansible_collections/equinix/cloud
environment: 'METAL_API_TOKEN'
steps:
- name: checkout repo
uses: actions/checkout@v3
Expand All @@ -41,6 +42,6 @@ jobs:


- name: run tests
run: make test
run: make testall
env:
METAL_API_TOKEN: ${{ secrets.METAL_API_TOKEN }}
9 changes: 5 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ docs:
injected-docs:
DOCS_PATH=$(DOCS_PATH) ./scripts/specdoc_inject.sh

test: integration-test

testall: create-integration-config
./scripts/test_all.sh

integration-test: $(INTEGRATION_CONFIG)
integration-test: create-integration-config
ansible-test integration $(TEST_ARGS)

test: integration-test

$(INTEGRATION_CONFIG):
create-integration-config:
ifneq ("${METAL_API_TOKEN}", "")
@echo "metal_api_token: ${METAL_API_TOKEN}" > $(INTEGRATION_CONFIG);
else
Expand Down
56 changes: 17 additions & 39 deletions plugins/inventory/metal_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,51 +32,29 @@
file that ends with equinix_metal.(yml|yaml). ansible_host is set to first public
IP address of the device.
module: metal_device
extends_documentation_fragment: [constructed]
notes: []
options:
keyed_groups:
plugin:
choices: ['equinix_metal', 'equinix.cloud.metal_device']
description:
- List of groups to create based on the values of a variable.
elements: dict
required: false
suboptions:
key:
description:
- The key to group by.
required: false
type: str
prefix:
description:
- Prefix to prepend to the group name.
required: false
type: str
separator:
default: ''
description:
- Separator to use when joining the key and value.
required: false
type: str
type: list
- Token that ensures this is a source file for the plugin.
required: true
type: str
metal_api_token:
description:
- Equinix Metal API token. Can also be specified via METAL_AUTH_TOKEN environment
variable.
required: false
type: str
plugin:
choices:
- equinix_metal
- equinix.cloud.metal_device
description:
- Token that ensures this is a source file for the plugin.
required: true
env:
- name: METAL_AUTH_TOKEN
type: str
project_ids:
description:
- List of Equinix Metal project IDs to query for devices.
type: list
elements: str
required: false
type: list
requirements:
- python >= 3
- metal_python >= 0.0.1
Expand Down Expand Up @@ -220,18 +198,20 @@ def verify_file(self, path):
:param path: the path to the inventory config file
:return the contents of the config file
'''
if super(InventoryModule, self).verify_file(path):
if path.endswith(('equinix.yml', 'equinix.yaml', 'device.yml', 'device.yaml')):
return True
if not super().verify_file(path):
return False
endings = ('equinix.yml', 'equinix.yaml', 'device.yml', 'device.yaml')
if any((path.endswith(ending) for ending in endings)):
return True
self.display.debug("equinix.cloud inventory filename must end with 'equinix.yml' or 'equinix.yaml'")
return False

def parse(self, inventory, loader, path, cache):
def parse(self, inventory, loader, path, cache=True):
super().parse(inventory, loader, path)

self._read_config_data(path)
self._build_client()

strict = self.get_option('strict')
strict = self.get_option("strict")
configured_project_ids = self._get_project_ids()
devices = self._get_devices_from_project_ids(configured_project_ids)
projects = set([device['project_id'] for device in devices])
Expand All @@ -248,7 +228,6 @@ def parse(self, inventory, loader, path, cache):
if k not in EXCLUDE_ATTRIBUTES:
self.inventory.set_variable(label(device), k, v)


for device in devices:
variables = self.inventory.get_host(label(device)).get_vars()
self._add_host_to_composed_groups(
Expand Down Expand Up @@ -276,7 +255,6 @@ def _get_project_ids(self):
for pid in project_ids:
if not metal_client.is_valid_uuid(pid):
raise AnsibleError("Invalid project id: %s" % pid)
self._build_client()
if len(project_ids) == 0:
return [p["id"] for p in self._get_all_projects()]
return project_ids
Expand Down
2 changes: 1 addition & 1 deletion plugins/module_utils/metal/metal_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def ip_address_getter(resource: dict):
'id': 'id',
'name': 'name',
'organization_id': 'organization.id',
'payment_method_id': 'payment_method.id',
'payment_method_id': optional_str('payment_method.id'),
}

METAL_IP_RESERVATION_RESPONSE_ATTRIBUTE_MAP = {
Expand Down
11 changes: 11 additions & 0 deletions scripts/test_all.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

PARALLEL_JOBS="${PARALLEL_JOBS:=3}"

run_test() {
make TEST_ARGS="$1" test
}

export -f run_test

parallel -j $PARALLEL_JOBS --group --keep-order run_test ::: $(ls tests/integration/targets)
4 changes: 2 additions & 2 deletions tests/integration/targets/metal_device/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
- set_fact:
test_prefix: "{{ test_resource_name_prefix }}-{{ unique_id }}"
- set_fact:
test_os: nixos_22_05
test_os: ubuntu_22_04
- set_fact:
test_plan: c3.small.x86
- set_fact:
Expand Down Expand Up @@ -122,4 +122,4 @@
id: "{{ item.id }}"
state: absent
ignore_errors: yes
loop: "{{ test_projects.resources }}"
loop: "{{ test_projects.resources }}"
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
plan: c3.small.x86
operating_system: ubuntu_20_04
metro: sv
provisioning_wait_seconds: 1200
tags:
- ansible-inventory-node
4 changes: 2 additions & 2 deletions tests/integration/targets/metal_ip_assignment/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
- set_fact:
test_prefix: "{{ test_resource_name_prefix }}-{{ unique_id }}"
- set_fact:
test_os: nixos_22_05
test_os: ubuntu_22_04
- set_fact:
test_plan: c3.small.x86
- set_fact:
Expand Down Expand Up @@ -136,4 +136,4 @@
id: "{{ item.id }}"
state: absent
ignore_errors: yes
loop: "{{ test_projects.resources }}"
loop: "{{ test_projects.resources }}"
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
- set_fact:
test_metro: sv
- set_fact:
test_os: nixos_22_05
test_os: ubuntu_22_04
- set_fact:
test_plan: c3.small.x86
- set_fact:
wait_seconds: 1200

- name: create project for test
equinix.cloud.metal_project:
Expand All @@ -38,6 +40,7 @@
operating_system: "{{ test_os }}"
plan: "{{ test_plan }}"
metro: "{{ test_metro }}"
provisioning_wait_seconds: "{{ wait_seconds }}"
state: present
register: device

Expand Down Expand Up @@ -87,8 +90,7 @@

- name: list test projects
equinix.cloud.metal_project_info:
filters:
name: "{{ test_prefix }}"
name: "{{ test_prefix }}"
register: test_projects

- name: list devices in test projects
Expand All @@ -109,4 +111,4 @@
id: "{{ item.id }}"
state: absent
ignore_errors: yes
loop: "{{ test_projects.resources }}"
loop: "{{ test_projects.resources }}"

0 comments on commit b94c837

Please sign in to comment.