From 968b4f54055fb502fef68b8645c5f59aaf92bbe1 Mon Sep 17 00:00:00 2001
From: Mark Goddard <mark@stackhpc.com>
Date: Tue, 20 Feb 2018 11:00:37 +0000
Subject: [PATCH] Add a molecule scenario for kolla-openstack -
 enable-everything

This scenario enables all services, and checks that expected
configuration files are generated. More validation of configuration file
contents could be added in future.
---
 .../molecule/enable-everything/Dockerfile.j2  |  9 ++
 .../molecule/enable-everything/INSTALL.rst    | 16 ++++
 .../molecule/enable-everything/create.yml     | 60 +++++++++++++
 .../molecule/enable-everything/destroy.yml    | 27 ++++++
 .../molecule/enable-everything/molecule.yml   | 88 +++++++++++++++++++
 .../molecule/enable-everything/playbook.yml   |  5 ++
 .../molecule/enable-everything/prepare.yml    | 23 +++++
 .../enable-everything/tests/test_default.py   | 86 ++++++++++++++++++
 8 files changed, 314 insertions(+)
 create mode 100644 ansible/roles/kolla-openstack/molecule/enable-everything/Dockerfile.j2
 create mode 100644 ansible/roles/kolla-openstack/molecule/enable-everything/INSTALL.rst
 create mode 100644 ansible/roles/kolla-openstack/molecule/enable-everything/create.yml
 create mode 100644 ansible/roles/kolla-openstack/molecule/enable-everything/destroy.yml
 create mode 100644 ansible/roles/kolla-openstack/molecule/enable-everything/molecule.yml
 create mode 100644 ansible/roles/kolla-openstack/molecule/enable-everything/playbook.yml
 create mode 100644 ansible/roles/kolla-openstack/molecule/enable-everything/prepare.yml
 create mode 100644 ansible/roles/kolla-openstack/molecule/enable-everything/tests/test_default.py

diff --git a/ansible/roles/kolla-openstack/molecule/enable-everything/Dockerfile.j2 b/ansible/roles/kolla-openstack/molecule/enable-everything/Dockerfile.j2
new file mode 100644
index 00000000..f8b4e753
--- /dev/null
+++ b/ansible/roles/kolla-openstack/molecule/enable-everything/Dockerfile.j2
@@ -0,0 +1,9 @@
+# Molecule managed
+
+FROM {{ item.image }}
+
+RUN if [ $(command -v apt-get) ]; then apt-get update && apt-get upgrade -y && apt-get install -y python sudo bash ca-certificates && apt-get clean; \
+    elif [ $(command -v dnf) ]; then dnf makecache && dnf --assumeyes install python sudo python-devel python2-dnf bash && dnf clean all; \
+    elif [ $(command -v yum) ]; then yum makecache fast && yum update -y && yum install -y python sudo yum-plugin-ovl bash && sed -i 's/plugins=0/plugins=1/g' /etc/yum.conf && yum clean all; \
+    elif [ $(command -v zypper) ]; then zypper refresh && zypper update -y && zypper install -y python sudo bash python-xml && zypper clean -a; \
+    elif [ $(command -v apk) ]; then apk update && apk add --no-cache python sudo bash ca-certificates; fi
diff --git a/ansible/roles/kolla-openstack/molecule/enable-everything/INSTALL.rst b/ansible/roles/kolla-openstack/molecule/enable-everything/INSTALL.rst
new file mode 100644
index 00000000..e26493b8
--- /dev/null
+++ b/ansible/roles/kolla-openstack/molecule/enable-everything/INSTALL.rst
@@ -0,0 +1,16 @@
+*******
+Install
+*******
+
+Requirements
+============
+
+* Docker Engine
+* docker-py
+
+Install
+=======
+
+.. code-block:: bash
+
+  $ sudo pip install docker-py
diff --git a/ansible/roles/kolla-openstack/molecule/enable-everything/create.yml b/ansible/roles/kolla-openstack/molecule/enable-everything/create.yml
new file mode 100644
index 00000000..10fac314
--- /dev/null
+++ b/ansible/roles/kolla-openstack/molecule/enable-everything/create.yml
@@ -0,0 +1,60 @@
+---
+- name: Create
+  hosts: localhost
+  connection: local
+  gather_facts: false
+  no_log: "{{ not lookup('env', 'MOLECULE_DEBUG') | bool }}"
+  vars:
+    molecule_file: "{{ lookup('env', 'MOLECULE_FILE') }}"
+    molecule_ephemeral_directory: "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}"
+    molecule_scenario_directory: "{{ lookup('env', 'MOLECULE_SCENARIO_DIRECTORY') }}"
+    molecule_yml: "{{ lookup('file', molecule_file) | molecule_from_yaml }}"
+  tasks:
+    - name: Create Dockerfiles from image names
+      template:
+        src: "{{ molecule_scenario_directory }}/Dockerfile.j2"
+        dest: "{{ molecule_ephemeral_directory }}/Dockerfile_{{ item.image | regex_replace('[^a-zA-Z0-9_]', '_') }}"
+      with_items: "{{ molecule_yml.platforms }}"
+      register: platforms
+
+    - name: Discover local Docker images
+      docker_image_facts:
+        name: "molecule_local/{{ item.item.name }}"
+      with_items: "{{ platforms.results }}"
+      register: docker_images
+
+    - name: Build an Ansible compatible image
+      docker_image:
+        path: "{{ molecule_ephemeral_directory }}"
+        name: "molecule_local/{{ item.item.image }}"
+        dockerfile: "{{ item.item.dockerfile | default(item.invocation.module_args.dest) }}"
+        force: "{{ item.item.force | default(true) }}"
+      with_items: "{{ platforms.results }}"
+      when: platforms.changed or docker_images.results | map(attribute='images') | select('equalto', []) | list | count >= 0
+
+    - name: Create molecule instance(s)
+      docker_container:
+        name: "{{ item.name }}"
+        hostname: "{{ item.name }}"
+        image: "molecule_local/{{ item.image }}"
+        state: started
+        recreate: false
+        log_driver: json-file
+        command: "{{ item.command | default('bash -c \"while true; do sleep 10000; done\"') }}"
+        privileged: "{{ item.privileged | default(omit) }}"
+        volumes: "{{ item.volumes | default(omit) }}"
+        capabilities: "{{ item.capabilities | default(omit) }}"
+        ports: "{{ item.exposed_ports | default(omit) }}"
+        ulimits: "{{ item.ulimits | default(omit) }}"
+      register: server
+      with_items: "{{ molecule_yml.platforms }}"
+      async: 7200
+      poll: 0
+
+    - name: Wait for instance(s) creation to complete
+      async_status:
+        jid: "{{ item.ansible_job_id }}"
+      register: docker_jobs
+      until: docker_jobs.finished
+      retries: 300
+      with_items: "{{ server.results }}"
diff --git a/ansible/roles/kolla-openstack/molecule/enable-everything/destroy.yml b/ansible/roles/kolla-openstack/molecule/enable-everything/destroy.yml
new file mode 100644
index 00000000..3ce74788
--- /dev/null
+++ b/ansible/roles/kolla-openstack/molecule/enable-everything/destroy.yml
@@ -0,0 +1,27 @@
+---
+- name: Destroy
+  hosts: localhost
+  connection: local
+  gather_facts: false
+  no_log: "{{ not lookup('env', 'MOLECULE_DEBUG') | bool }}"
+  vars:
+    molecule_file: "{{ lookup('env', 'MOLECULE_FILE') }}"
+    molecule_yml: "{{ lookup('file', molecule_file) | molecule_from_yaml }}"
+  tasks:
+    - name: Destroy molecule instance(s)
+      docker_container:
+        name: "{{ item.name }}"
+        state: absent
+        force_kill: "{{ item.force_kill | default(true) }}"
+      register: server
+      with_items: "{{ molecule_yml.platforms }}"
+      async: 7200
+      poll: 0
+
+    - name: Wait for instance(s) deletion to complete
+      async_status:
+        jid: "{{ item.ansible_job_id }}"
+      register: docker_jobs
+      until: docker_jobs.finished
+      retries: 300
+      with_items: "{{ server.results }}"
diff --git a/ansible/roles/kolla-openstack/molecule/enable-everything/molecule.yml b/ansible/roles/kolla-openstack/molecule/enable-everything/molecule.yml
new file mode 100644
index 00000000..f020def9
--- /dev/null
+++ b/ansible/roles/kolla-openstack/molecule/enable-everything/molecule.yml
@@ -0,0 +1,88 @@
+---
+dependency:
+  name: galaxy
+driver:
+  name: docker
+lint:
+  name: yamllint
+platforms:
+  - name: centos-7
+    image: centos:7
+  - name: ubuntu-1604
+    image: ubuntu:16.04
+provisioner:
+  name: ansible
+  inventory:
+    group_vars:
+      all:
+        kolla_extra_config_path:
+        kolla_enable_ceph: True
+        kolla_extra_ceph: |
+          [extra-ceph.conf]
+          foo=bar
+        kolla_enable_cinder: True
+        kolla_extra_cinder: |
+          [extra-cinder.conf]
+          foo=bar
+        kolla_enable_designate: True
+        kolla_extra_designate: |
+          [extra-designate.conf]
+          foo=bar
+        kolla_enable_glance: True
+        kolla_extra_glance: |
+          [extra-glance.conf]
+          foo=bar
+        kolla_enable_grafana: True
+        kolla_extra_grafana: |
+          [extra-grafana.ini]
+          foo=bar
+        kolla_enable_heat: True
+        kolla_extra_heat: |
+          [extra-heat.conf]
+          foo=bar
+        kolla_enable_horizon: True
+        kolla_enable_ironic: True
+        kolla_extra_ironic: |
+          [extra-ironic.conf]
+          foo=bar
+        kolla_extra_inspector: |
+          [extra-ironic-inspector.conf]
+          foo=bar
+        kolla_inspector_ipa_kernel_path: ${MOLECULE_TEMP_PATH:-/tmp}/ironic-agent.kernel
+        kolla_inspector_ipa_ramdisk_path: ${MOLECULE_TEMP_PATH:-/tmp}/ironic-agent.initramfs
+        kolla_extra_ironic_dnsmasq: |
+          extra=bar
+        kolla_enable_magnum: True
+        kolla_extra_magnum: |
+          [extra-magnum.conf]
+          foo=bar
+        kolla_enable_manila: True
+        kolla_enable_murano: True
+        kolla_enable_monasca: True
+        kolla_extra_murano: |
+          [extra-murano.conf]
+          foo=bar
+        kolla_enable_neutron: True
+        kolla_extra_neutron: |
+          [extra-neutron.conf]
+          foo=bar
+        kolla_extra_neutron_ml2: |
+          [extra-ml2_conf.ini]
+          foo=bar
+        kolla_enable_nova: True
+        kolla_extra_nova: |
+          [extra-nova.conf]
+          foo=bar
+        kolla_enable_sahara: True
+        kolla_extra_sahara: |
+          [extra-sahara.conf]
+          foo=bar
+        kolla_enable_swift: True
+  lint:
+    name: ansible-lint
+scenario:
+  name: enable-everything
+verifier:
+  name: testinfra
+  lint:
+    name: flake8
diff --git a/ansible/roles/kolla-openstack/molecule/enable-everything/playbook.yml b/ansible/roles/kolla-openstack/molecule/enable-everything/playbook.yml
new file mode 100644
index 00000000..c8bf6afd
--- /dev/null
+++ b/ansible/roles/kolla-openstack/molecule/enable-everything/playbook.yml
@@ -0,0 +1,5 @@
+---
+- name: Converge
+  hosts: all
+  roles:
+    - role: kolla-openstack
diff --git a/ansible/roles/kolla-openstack/molecule/enable-everything/prepare.yml b/ansible/roles/kolla-openstack/molecule/enable-everything/prepare.yml
new file mode 100644
index 00000000..3a1069d4
--- /dev/null
+++ b/ansible/roles/kolla-openstack/molecule/enable-everything/prepare.yml
@@ -0,0 +1,23 @@
+---
+- name: Prepare
+  hosts: all
+  gather_facts: false
+  tasks:
+    - name: Ensure ironic inspector kernel and ramdisk image directory exists
+      local_action:
+        module: file
+        path: "{{ item | dirname }}"
+        state: directory
+        recurse: True
+      with_items:
+        - "{{ kolla_inspector_ipa_kernel_path }}"
+        - "{{ kolla_inspector_ipa_ramdisk_path }}"
+
+    - name: Ensure ironic inspector kernel and ramdisk images exist
+      local_action:
+        module: file
+        path: "{{ item }}"
+        state: touch
+      with_items:
+        - "{{ kolla_inspector_ipa_kernel_path }}"
+        - "{{ kolla_inspector_ipa_ramdisk_path }}"
diff --git a/ansible/roles/kolla-openstack/molecule/enable-everything/tests/test_default.py b/ansible/roles/kolla-openstack/molecule/enable-everything/tests/test_default.py
new file mode 100644
index 00000000..7217b7b6
--- /dev/null
+++ b/ansible/roles/kolla-openstack/molecule/enable-everything/tests/test_default.py
@@ -0,0 +1,86 @@
+# Copyright (c) 2018 StackHPC Ltd.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+import os
+import os.path
+
+from kayobe.tests.molecule import utils
+
+import pytest
+import testinfra.utils.ansible_runner
+
+
+testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
+    os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')
+
+
+@pytest.mark.parametrize(
+    'path',
+    ['ceph',
+     'cinder',
+     'designate',
+     'fluentd/filter',
+     'fluentd/output',
+     'glance',
+     'grafana',
+     'heat',
+     'horizon',
+     'ironic',
+     'keystone',
+     'magnum',
+     'manila',
+     'murano',
+     'neutron',
+     'nova',
+     'sahara',
+     'swift'])
+def test_service_config_directory(host, path):
+    path = os.path.join('/etc/kolla/config', path)
+    utils.test_directory(host, path)
+
+
+@pytest.mark.parametrize(
+    'path',
+    ['ceph.conf',
+     'cinder.conf',
+     'designate.conf',
+     'glance.conf',
+     'grafana.ini',
+     'heat.conf',
+     'ironic.conf',
+     'ironic-inspector.conf',
+     'magnum.conf',
+     'neutron/ml2_conf.ini',
+     'murano.conf',
+     'neutron.conf',
+     'nova.conf',
+     'sahara.conf'])
+def test_service_ini_file(host, path):
+    # TODO(mgoddard): Check more of config file contents.
+    path = os.path.join('/etc/kolla/config', path)
+    extra_section = 'extra-%s' % os.path.basename(path)
+    expected = {extra_section: {'foo': 'bar'}}
+    utils.test_ini_file(host, path, expected=expected)
+
+
+@pytest.mark.parametrize(
+    'path',
+    ['ironic/ironic-agent.initramfs',
+     'ironic/ironic-agent.kernel',
+     'ironic/ironic-dnsmasq.conf',
+     'ironic/pxelinux.default'])
+def test_service_non_ini_file(host, path):
+    # TODO(mgoddard): Check config file contents.
+    path = os.path.join('/etc/kolla/config', path)
+    utils.test_file(host, path)
-- 
GitLab