---
- name: Test kolla-ansible role defaults
  hosts: localhost
  connection: local
  tasks:
    - name: Create a temporary directory
      tempfile:
        state: directory
      register: tempfile_result

    - block:
        - name: Test the kolla-ansible role with default values
          include_role:
            name: ../../kolla-ansible
          vars:
            kolla_ansible_source_path: "{{ temp_path }}/src"
            kolla_ansible_ctl_install_type: "source"
            kolla_ansible_source_url: "http://github.com/openstack/kolla-ansible"
            kolla_ansible_source_version: "{{ openstack_branch }}"
            kolla_ansible_venv: "{{ temp_path }}/venv"
            kolla_config_path: "{{ temp_path }}/etc/kolla"
            kolla_node_custom_config_path: "{{ temp_path }}/etc/kolla/config"
            # Purposely does not exist to simulate the case when no group vars
            # are provided
            kolla_overcloud_group_vars_path: "{{ temp_path }}/etc/kayobe/kolla/inventory/group_vars"
            kolla_ansible_passwords_path: "{{ temp_path }}/passwords.yml"
            # Required config.
            kolla_base_distro: "fake-distro"
            kolla_install_type: "fake-install-type"
            kolla_docker_namespace: "fake-namespace"
            kolla_openstack_release: "fake-release"
            kolla_internal_vip_address: "10.0.0.1"
            kolla_internal_fqdn: "fake.internal.fqdn"
            kolla_external_vip_address: "10.0.0.2"
            kolla_external_fqdn: "fake.external.fqdn"
            kolla_ansible_certificates_path: "{{ temp_path }}/etc/kayobe/kolla/certificates"
            kolla_enable_tls_external: False
            kolla_enable_tls_internal: False
            kolla_enable_grafana: False
            kolla_openstack_logging_debug: False
            kolla_globals_paths_extra:
              - "{{ tempfile_result.path ~ '/etc/kayobe/' }}"
            apt_cache_valid_time: 3600

        - name: Verify kolla-ansible installation
          shell: ". {{ temp_path }}/venv/bin/activate && kolla-ansible -h"
          changed_when: False

        - name: Verify ansible installation
          command: "{{ temp_path }}/venv/bin/ansible -h"
          changed_when: False

        - name: Validate globals.yml contents
          assert:
            that:
              - item.key in globals_yml
              - globals_yml[item.key] == item.value
            msg: >
              Unexpected value for variable "{{ item.key }}" in globals.yml.
              Expected "{{ item.value }}", actual
              "{{ globals_yml.get(item.key, '<missing>') }}".
          with_dict: "{{ expected_variables }}"
          vars:
            # NOTE: Can't use set_fact for this, as it causes kolla-ansible
            # Jinja expressions to be evaluated.
            globals_yml: "{{ lookup('file', temp_path ~ '/etc/kolla/globals.yml') | from_yaml }}"
            expected_variables:
              config_strategy: "COPY_ALWAYS"
              kolla_base_distro: "fake-distro"
              kolla_install_type: "fake-install-type"
              openstack_release: "fake-release"
              kolla_internal_vip_address: "10.0.0.1"
              kolla_internal_fqdn: "fake.internal.fqdn"
              kolla_external_vip_address: "10.0.0.2"
              kolla_external_fqdn: "fake.external.fqdn"
              node_custom_config: "{{ temp_path }}/etc/kolla/config"
              docker_namespace: "fake-namespace"
              neutron_plugin_agent: "openvswitch"
              kolla_enable_tls_external: False
              kolla_enable_tls_internal: False
              openstack_logging_debug: False
              kolla_user: "kolla"
              kolla_group: "kolla"

        - name: Validate variables are absent from globals.yml
          assert:
            that: item not in globals_yml
            msg: >
              Unexpected variable "{{ item }}" found in globals.yml, value
              "{{ globals_yml.get(item) }}".
          with_items: "{{ unexpected_variables }}"
          vars:
            # NOTE: Can't use set_fact for this, as it causes kolla-ansible
            # Jinja expressions to be evaluated.
            globals_yml: "{{ lookup('file', temp_path ~ '/etc/kolla/globals.yml') | from_yaml }}"
            unexpected_variables:
              - docker_registry
              - docker_registry_username
              - docker_registry_password
              - neutron_type_drivers
              - neutron_tenant_network_types
              - enable_glance
              - enable_ironic
              - enable_ironic_neutron_agent
              - enable_kafka
              - enable_influxdb
              - enable_mariadb
              - enable_monasca
              - enable_neutron
              - enable_nova
              - enable_prometheus
              - enable_storm
              - enable_zookeeper
              - grafana_admin_username
              - network_interface
              - api_interface
              - kolla_external_vip_interface
              - storage_interface
              - cluster_interface
              - provision_interface
              - ironic_dnsmasq_interface
              - dns_interface
              - tunnel_interface
              - bifrost_network_interface
              - neutron_external_interface
              - neutron_bridge_name
              - ironic_dnsmasq_dhcp_range
              - ironic_dnsmasq_default_gateway

        - name: Check whether inventory files exist
          stat:
            path: "{{ temp_path ~ '/etc/kolla/inventory/' ~ item }}"
          with_items:
            - seed
            - overcloud
            - overcloud/group_vars
          register: inventory_stat

        - name: Validate inventory files
          assert:
            that:
              - item.stat.exists
              - item.stat.size > 0
            msg: >
              Inventory file {{ item.item }} was not found.
          with_items: "{{ inventory_stat.results }}"

        - name: Look for custom overcloud group vars
          find:
            paths: "{{ temp_path ~ '/etc/kolla/inventory/group_vars' }}"
          register: kolla_ansible_overcloud_group_vars

        - name: Check that no overcloud group vars are set
          assert:
            that:
              - kolla_ansible_overcloud_group_vars.matched == 0
            msg: >
              Overcloud group vars were found when they should not be set.

        - name: Validate passwords.yml contents
          assert:
            that: item in passwords_yml
            msg: >
              Expected variable "{{ item }}" not present in passwords.yml.
          with_items: "{{ expected_variables }}"
          vars:
            # NOTE: Can't use set_fact for this, as it causes kolla-ansible
            # Jinja expressions to be evaluated.
            passwords_yml: "{{ lookup('file', temp_path ~ '/etc/kolla/passwords.yml') | from_yaml }}"
            expected_variables:
              - database_password

      always:
        - name: Ensure the temporary directory is removed
          file:
            path: "{{ temp_path }}"
            state: absent
      rescue:
        - name: Flag that a failure occurred
          set_fact:
            test_failures: "{{ test_failures | default(0) | int + 1 }}"
      vars:
        temp_path: "{{ tempfile_result.path }}"
