Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make inventory tests also work with podman #1384

Merged
merged 1 commit into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion tests/test_playbooks/inventory_plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
foreman_port: "3000"
postgres_version: "12"
postgres_host: "postgres"
postgres_port: 5432
collections:
- community.docker
- theforeman.foreman
tasks:
- include_tasks: tasks/inventory_plugin.yml
- name: test inventory
include_tasks: tasks/inventory_plugin.yml
4 changes: 3 additions & 1 deletion tests/test_playbooks/inventory_plugin_ansible.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
foreman_port: "3001"
postgres_version: "12"
postgres_host: "postgresansible"
postgres_port: 5433
collections:
- community.docker
- theforeman.foreman
tasks:
- include_tasks: tasks/inventory_plugin.yml
- name: test inventory
include_tasks: tasks/inventory_plugin.yml
158 changes: 4 additions & 154 deletions tests/test_playbooks/tasks/inventory_plugin.yml
Original file line number Diff line number Diff line change
@@ -1,164 +1,14 @@
---
- name: fetch PostgreSQL container
docker_image:
name: postgres:{{ postgres_version }}
source: pull

- name: fetch Foreman container
docker_image:
name: "{{ foreman_container }}:{{ foreman_version }}"
source: pull

- name: start PostgreSQL container
docker_container:
name: "{{ postgres_host }}"
image: postgres:{{ postgres_version }}
env:
POSTGRES_USER: foreman
POSTGRES_PASSWORD: foreman
POSTGRES_DATABASE: foreman
PGDATA: /var/lib/postgresql/data/pgdata

- name: start Foreman container
docker_container:
name: "{{ foreman_host }}"
image: "{{ foreman_container }}:{{ foreman_version }}"
command: bash -c "bundle exec bin/rails db:create db:migrate && bundle exec bin/rails db:seed && bundle exec bin/rails server -b 0.0.0.0"
env:
SEED_ADMIN_PASSWORD: changeme
RAILS_ENV: production
DATABASE_URL: postgres://foreman:foreman@{{ postgres_host }}/foreman?pool=5
links:
- "{{ postgres_host }}:{{ postgres_host }}"
published_ports:
- "0.0.0.0:{{ foreman_port }}:3000"

- name: wait for foreman to start
status_info:
username: admin
password: changeme
server_url: http://127.0.0.1:{{ foreman_port }}/
register: result
until:
- result is success
- result.status.status == 200
retries: 10
delay: 10

- name: start dynflow container
docker_container:
name: "{{ foreman_host }}dynflow"
image: "{{ foreman_container }}:{{ foreman_version }}"
command: bash -c "bundle exec rake dynflow:executor"
env:
RAILS_ENV: production
DATABASE_URL: postgres://foreman:foreman@{{ postgres_host }}/foreman?pool=5
links:
- "{{ postgres_host }}:{{ postgres_host }}"

- name: create test hostgroups
hostgroup:
username: admin
password: changeme
server_url: http://127.0.0.1:{{ foreman_port }}/
name: "{{ item.name }}"
parent: "{{ item.parent | default(omit) }}"
organizations:
- Default Organization
locations:
- Default Location
with_items: "{{ foreman_groups }}"

- name: create test hosts
host:
username: admin
password: changeme
server_url: http://127.0.0.1:{{ foreman_port }}/
name: "{{ item.key }}"
hostgroup: "{{ item.value }}"
build: false
organization: Default Organization
location: Default Location
managed: false
parameters:
- name: testparam1
value: testvalue1
- name: testparam2
value: testvalue2
with_dict: "{{ foreman_hosts }}"

- name: submit facts for hosts
uri:
url: http://127.0.0.1:{{ foreman_port }}/api/hosts/facts
url_username: admin
url_password: changeme
validate_certs: false
method: POST
body:
name: "{{ item.key }}"
facts:
fqdn: "{{ item.key }}"
domain: example.com
famtesthost: true
operatingsystem: CentOS
operatingsystemrelease: "7.7"
force_basic_auth: true
status_code: 201
body_format: json
with_dict: "{{ foreman_hosts }}"
ignore_errors: true
- name: setup
include_tasks: inventory_plugin_setup.yml

- name: Refresh inventory to ensure new instances exist in inventory
meta: refresh_inventory

- name: execute tests
block:
- name: test that all groups are present
assert:
that: >
'foreman_{{ item.label | default(item.name) | regex_replace('/', '_') }}' in groups
with_items: "{{ foreman_groups }}"

- name: test that all hosts are in the "all" group
assert:
that: >
'{{ item.key }}' in groups['all']
with_dict: "{{ foreman_hosts }}"

- name: test that all hosts are in the correct hostgroup
assert:
that: >
'{{ item.key }}' in groups['foreman_{{ item.value | regex_replace('/', '_') }}']
with_dict: "{{ foreman_hosts }}"

- name: dump all host details
debug:
var: "hostvars['{{ item.key }}']"
with_dict: "{{ foreman_hosts }}"

- name: test that all hosts have the domain fact set
assert:
that: >
hostvars['{{ item.key }}']['foreman_facts']['domain'] == 'example.com'
with_dict: "{{ foreman_hosts }}"

- name: test that all hosts have the OS fact set
assert:
that: >
hostvars['{{ item.key }}']['foreman_facts']['operatingsystem'] == 'CentOS'
with_dict: "{{ foreman_hosts }}"

- name: test that all hosts have the testparam1
assert:
that: >
hostvars['{{ item.key }}']['testparam1'] == 'testvalue1'
with_dict: "{{ foreman_hosts }}"

- name: test that all hosts have the testparam2
assert:
that: >
hostvars['{{ item.key }}']['testparam2'] == 'testvalue2'
with_dict: "{{ foreman_hosts }}"
- name: execute tests
include_tasks: inventory_plugin_tests.yml
always:
- name: remove containers
docker_container:
Expand Down
107 changes: 107 additions & 0 deletions tests/test_playbooks/tasks/inventory_plugin_setup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
---
- name: fetch PostgreSQL container
docker_image:
name: docker.io/library/postgres:{{ postgres_version }}
source: pull

- name: fetch Foreman container
docker_image:
name: "{{ foreman_container }}:{{ foreman_version }}"
source: pull

- name: start PostgreSQL container
docker_container:
name: "{{ postgres_host }}"
image: docker.io/library/postgres:{{ postgres_version }}
env:
POSTGRES_USER: foreman
POSTGRES_PASSWORD: foreman
POSTGRES_DATABASE: foreman
PGDATA: /var/lib/postgresql/data/pgdata
published_ports:
- "0.0.0.0:{{ postgres_port }}:5432"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Up above there is a playbook that uses 5433 for the postgresql port. Does that affect this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line says "expose port 5432 of the container on whatever evgeni set as postgres_port"

so yes, it does affect, but correctly ;)


- name: start Foreman container
docker_container:
name: "{{ foreman_host }}"
image: "{{ foreman_container }}:{{ foreman_version }}"
command: bash -c "bundle exec bin/rails db:create db:migrate && bundle exec bin/rails db:seed && bundle exec bin/rails server -b 0.0.0.0"
env:
SEED_ADMIN_PASSWORD: changeme
RAILS_ENV: production
DATABASE_URL: postgres://foreman:foreman@{{ ansible_default_ipv4['address'] }}:{{ postgres_port }}/foreman?pool=5
published_ports:
- "0.0.0.0:{{ foreman_port }}:3000"

- name: wait for foreman to start
status_info:
username: admin
password: changeme
server_url: http://127.0.0.1:{{ foreman_port }}/
register: result
until:
- result is success
- result.status.status == 200
retries: 10
delay: 10

- name: start dynflow container
docker_container:
name: "{{ foreman_host }}dynflow"
image: "{{ foreman_container }}:{{ foreman_version }}"
command: bash -c "bundle exec rake dynflow:executor"
env:
RAILS_ENV: production
DATABASE_URL: postgres://foreman:foreman@{{ ansible_default_ipv4['address'] }}:{{ postgres_port }}/foreman?pool=5

- name: create test hostgroups
hostgroup:
username: admin
password: changeme
server_url: http://127.0.0.1:{{ foreman_port }}/
name: "{{ item.name }}"
parent: "{{ item.parent | default(omit) }}"
organizations:
- Default Organization
locations:
- Default Location
with_items: "{{ foreman_groups }}"

- name: create test hosts
host:
username: admin
password: changeme
server_url: http://127.0.0.1:{{ foreman_port }}/
name: "{{ item.key }}"
hostgroup: "{{ item.value }}"
build: false
organization: Default Organization
location: Default Location
managed: false
parameters:
- name: testparam1
value: testvalue1
- name: testparam2
value: testvalue2
with_dict: "{{ foreman_hosts }}"

- name: submit facts for hosts
uri:
url: http://127.0.0.1:{{ foreman_port }}/api/hosts/facts
url_username: admin
url_password: changeme
validate_certs: false
method: POST
body:
name: "{{ item.key }}"
facts:
fqdn: "{{ item.key }}"
domain: example.com
famtesthost: true
operatingsystem: CentOS
operatingsystemrelease: "7.7"
force_basic_auth: true
status_code: 201
body_format: json
with_dict: "{{ foreman_hosts }}"
ignore_errors: true
47 changes: 47 additions & 0 deletions tests/test_playbooks/tasks/inventory_plugin_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
- name: test that all groups are present
assert:
that: >
'foreman_{{ item.label | default(item.name) | regex_replace('/', '_') }}' in groups
with_items: "{{ foreman_groups }}"

- name: test that all hosts are in the "all" group
assert:
that: >
'{{ item.key }}' in groups['all']
with_dict: "{{ foreman_hosts }}"

- name: test that all hosts are in the correct hostgroup
assert:
that: >
'{{ item.key }}' in groups['foreman_{{ item.value | regex_replace('/', '_') }}']
with_dict: "{{ foreman_hosts }}"

- name: dump all host details
debug:
var: "hostvars['{{ item.key }}']"
with_dict: "{{ foreman_hosts }}"

- name: test that all hosts have the domain fact set
assert:
that: >
hostvars['{{ item.key }}']['foreman_facts']['domain'] == 'example.com'
with_dict: "{{ foreman_hosts }}"

- name: test that all hosts have the OS fact set
assert:
that: >
hostvars['{{ item.key }}']['foreman_facts']['operatingsystem'] == 'CentOS'
with_dict: "{{ foreman_hosts }}"

- name: test that all hosts have the testparam1
assert:
that: >
hostvars['{{ item.key }}']['testparam1'] == 'testvalue1'
with_dict: "{{ foreman_hosts }}"

- name: test that all hosts have the testparam2
assert:
that: >
hostvars['{{ item.key }}']['testparam2'] == 'testvalue2'
with_dict: "{{ foreman_hosts }}"