diff --git a/tests/test_playbooks/inventory_plugin.yml b/tests/test_playbooks/inventory_plugin.yml index 856350377e..d9cf9915b8 100644 --- a/tests/test_playbooks/inventory_plugin.yml +++ b/tests/test_playbooks/inventory_plugin.yml @@ -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 diff --git a/tests/test_playbooks/inventory_plugin_ansible.yml b/tests/test_playbooks/inventory_plugin_ansible.yml index fb6141cb7f..40029a3b37 100644 --- a/tests/test_playbooks/inventory_plugin_ansible.yml +++ b/tests/test_playbooks/inventory_plugin_ansible.yml @@ -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 diff --git a/tests/test_playbooks/tasks/inventory_plugin.yml b/tests/test_playbooks/tasks/inventory_plugin.yml index 499872f25d..093f6c4e52 100644 --- a/tests/test_playbooks/tasks/inventory_plugin.yml +++ b/tests/test_playbooks/tasks/inventory_plugin.yml @@ -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: diff --git a/tests/test_playbooks/tasks/inventory_plugin_setup.yml b/tests/test_playbooks/tasks/inventory_plugin_setup.yml new file mode 100644 index 0000000000..9d4a4b699d --- /dev/null +++ b/tests/test_playbooks/tasks/inventory_plugin_setup.yml @@ -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" + +- 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 diff --git a/tests/test_playbooks/tasks/inventory_plugin_tests.yml b/tests/test_playbooks/tasks/inventory_plugin_tests.yml new file mode 100644 index 0000000000..ad2e522c90 --- /dev/null +++ b/tests/test_playbooks/tasks/inventory_plugin_tests.yml @@ -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 }}"