Molecule tests for application role.

Issue-ID: OOM-1812

Change-Id: Ifb6f5a10afb4014b20be77a4141371e561d346ce
Signed-off-by: Samuli Silvius <s.silvius@partner.samsung.com>
diff --git a/ansible/roles/application/.gitignore b/ansible/roles/application/.gitignore
new file mode 100644
index 0000000..155cbb2
--- /dev/null
+++ b/ansible/roles/application/.gitignore
@@ -0,0 +1 @@
+application/
diff --git a/ansible/roles/application/.yamllint b/ansible/roles/application/.yamllint
new file mode 100644
index 0000000..ad0be76
--- /dev/null
+++ b/ansible/roles/application/.yamllint
@@ -0,0 +1,11 @@
+extends: default
+
+rules:
+  braces:
+    max-spaces-inside: 1
+    level: error
+  brackets:
+    max-spaces-inside: 1
+    level: error
+  line-length: disable
+  truthy: disable
diff --git a/ansible/roles/application/molecule/default/Dockerfile.j2 b/ansible/roles/application/molecule/default/Dockerfile.j2
new file mode 100644
index 0000000..e6aa95d
--- /dev/null
+++ b/ansible/roles/application/molecule/default/Dockerfile.j2
@@ -0,0 +1,14 @@
+# Molecule managed
+
+{% if item.registry is defined %}
+FROM {{ item.registry.url }}/{{ item.image }}
+{% else %}
+FROM {{ item.image }}
+{% endif %}
+
+RUN if [ $(command -v apt-get) ]; then apt-get update && 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 python*-dnf bash && dnf clean all; \
+    elif [ $(command -v yum) ]; then yum makecache fast && 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 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; \
+    elif [ $(command -v xbps-install) ]; then xbps-install -Syu && xbps-install -y python sudo bash ca-certificates && xbps-remove -O; fi
diff --git a/ansible/roles/application/molecule/default/molecule.yml b/ansible/roles/application/molecule/default/molecule.yml
new file mode 100644
index 0000000..8f19d7f
--- /dev/null
+++ b/ansible/roles/application/molecule/default/molecule.yml
@@ -0,0 +1,60 @@
+---
+dependency:
+  name: galaxy
+driver:
+  name: docker
+lint:
+  name: yamllint
+platforms:
+  - name: instance
+    image: centos:7
+provisioner:
+  name: ansible
+  env:
+    ANSIBLE_ROLES_PATH: ../../../../test/roles
+  inventory:
+    group_vars:
+      all:
+        app_name: moleculetestapp
+        app_data_path: "/opt/{{ app_name }}"
+        app_helm_release_name: "{{ app_name }}"
+        app_kubernetes_namespace: "{{ app_name }}"
+        app_helm_charts_install_directory: application/helm_charts
+        app_helm_plugins_directory: "{{ app_helm_charts_install_directory}}/helm/plugins/"
+        app_helm_charts_infra_directory: "{{ app_data_path }}/helm_charts"
+        helm_bin_dir: /usr/local/bin
+        app_helm_build_targets:
+          - all
+          - onap
+        app_helm_chart_name: "{{ app_name }}"
+        application_pre_install_role:
+        application_post_install_role:
+  lint:
+    name: ansible-lint
+scenario:
+  name: default
+  test_sequence:
+    - lint
+    - cleanup
+    - destroy
+    - dependency
+    - syntax
+    - create
+    - prepare
+    - converge
+    # - idempotence
+    # --> Action: 'idempotence'
+    # ERROR: Idempotence test failed because of the following tasks:
+    # * [instance] => application : Get helm dir
+    # * [instance] => application : Helm init and upgrade
+    # * [instance] => application : Helm Serve
+    # * [instance] => application : Helm Add Repo
+    # * [instance] => application : Helm Install application moleculetestapp
+    - side_effect
+    - verify
+    - cleanup
+    - destroy
+verifier:
+  name: testinfra
+  lint:
+    name: flake8
diff --git a/ansible/roles/application/molecule/default/playbook.yml b/ansible/roles/application/molecule/default/playbook.yml
new file mode 100644
index 0000000..4121f12
--- /dev/null
+++ b/ansible/roles/application/molecule/default/playbook.yml
@@ -0,0 +1,5 @@
+---
+- name: Helm charts available
+  hosts: all
+  roles:
+    - application
diff --git a/ansible/roles/application/molecule/default/prepare.yml b/ansible/roles/application/molecule/default/prepare.yml
new file mode 100644
index 0000000..8a5288d
--- /dev/null
+++ b/ansible/roles/application/molecule/default/prepare.yml
@@ -0,0 +1,5 @@
+---
+- name: Prepare infra
+  hosts: all
+  roles:
+    - prepare-application
diff --git a/ansible/roles/application/molecule/default/tests/test_default.py b/ansible/roles/application/molecule/default/tests/test_default.py
new file mode 100644
index 0000000..3e0cbb4
--- /dev/null
+++ b/ansible/roles/application/molecule/default/tests/test_default.py
@@ -0,0 +1,29 @@
+import os
+
+import testinfra.utils.ansible_runner
+
+testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
+    os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')
+
+
+def test_helm_commands(host):
+    fc = host.file('/tmp/helm_simu_output').content_string
+    expected_content = """home
+init --upgrade --skip-refresh
+version --tiller-connection-timeout 10
+repo list
+serve
+repo list
+repo add local http://127.0.0.1:8879
+install --name moleculetestapp local/moleculetestapp --namespace \
+moleculetestapp -f /opt/moleculetestapp/override.yaml"""
+    assert fc == expected_content
+
+
+def test_helm_override_file(host):
+    fc = host.file('/opt/moleculetestapp/override.yaml').content_string
+    expected_content = """global:
+    cacert: 'this is dummy server certificate value
+
+        '"""
+    assert fc == expected_content
diff --git a/ansible/roles/application/tasks/install.yml b/ansible/roles/application/tasks/install.yml
index 103ecc8..cdc7ced 100644
--- a/ansible/roles/application/tasks/install.yml
+++ b/ansible/roles/application/tasks/install.yml
@@ -4,7 +4,7 @@
      {{ helm_bin_dir }}/helm init
      --upgrade
      --skip-refresh
-  changed_when: true # init is always changed type of action
+  changed_when: true  # init is always changed type of action
 
 # A correct way to implement this would be using --wait option in helm init invocation.
 # However, it does not work due to https://github.com/helm/helm/issues/4031 (fixed in newer helm release)
@@ -14,29 +14,29 @@
   until: result.rc == 0
   delay: 10
   retries: 12
-  changed_when: false # for idempotency
+  changed_when: false  # for idempotency
 
 - name: Get all helm repos
   command: "{{ helm_bin_dir }}/helm repo list"
   register: repos
-  changed_when: false # for idempotency
+  changed_when: false  # for idempotency
 
 - name: Remove stable repo
   command: "{{ helm_bin_dir }}/helm repo remove stable"
-  changed_when: true # when executed its a changed type of action
+  changed_when: true  # when executed its a changed type of action
   when: "'stable' in repos.stdout"
 
 - name: Helm Serve
   shell: "{{ helm_bin_dir }}/helm serve &"
   async: 45
-  poll: 3 # wait 3sec to get a chance for some stderr
+  poll: 3  # wait 3sec to get a chance for some stderr
   register: helm_serve
   changed_when: "'address already in use' not in helm_serve.stderr"
 
 - name: List helm repos
   command: "{{ helm_bin_dir }}/helm repo list"
   register: helm_repo_list
-  changed_when: false # for idempotency
+  changed_when: false  # for idempotency
   failed_when:
     - helm_repo_list.rc > 0
     - "'Error: no repositories to show' not in helm_repo_list.stderr"
@@ -44,7 +44,7 @@
 - name: Helm Add Repo
   command: "{{ helm_bin_dir }}/helm repo add {{ helm_repository_name | mandatory }} {{ helm_repository_url | mandatory }}"
   when: "'local' not in helm_repo_list.stdout"
-  changed_when: true # when executed its a changed type of action
+  changed_when: true  # when executed its a changed type of action
 
 - name: Build local helm repository
   make:
@@ -72,6 +72,6 @@
           {{ helm_repository_name }}/{{ app_helm_chart_name }}
           --namespace {{ app_kubernetes_namespace }}
           {{ '' if app_skip_helm_override else '-f ' + app_helm_override_file }}
-  changed_when: true # when executed its a changed type of action
+  changed_when: true  # when executed its a changed type of action
   register: helm_install
   failed_when: helm_install.stderr
diff --git a/ansible/test/roles/prepare-application/defaults/main.yml b/ansible/test/roles/prepare-application/defaults/main.yml
new file mode 100644
index 0000000..227bd4f
--- /dev/null
+++ b/ansible/test/roles/prepare-application/defaults/main.yml
@@ -0,0 +1,4 @@
+---
+simulate_helm: true
+app_helm_charts_install_directory: application/helm_charts
+helm_simulation_output_file: /tmp/helm_simu_output
\ No newline at end of file
diff --git a/ansible/test/roles/prepare-application/tasks/main.yml b/ansible/test/roles/prepare-application/tasks/main.yml
new file mode 100644
index 0000000..2f143a0
--- /dev/null
+++ b/ansible/test/roles/prepare-application/tasks/main.yml
@@ -0,0 +1,47 @@
+---
+- name: Create Application helm charts directory
+  file:
+    path: "{{ app_helm_charts_install_directory }}"
+    state: directory
+  delegate_to: localhost
+
+- name: Create Makefile to simulate helm charts dir and make building
+  copy:
+    content: |
+      all:
+      onap:
+    dest: "{{ app_helm_charts_install_directory }}/Makefile"
+  delegate_to: localhost
+  when: simulate_helm
+
+- name: Clean previous simulation output file
+  file:
+    path: "{{ helm_simulation_output_file }}"
+    state: absent
+
+- name: simulate helm binary
+  copy:
+    content: |
+      #!/bin/bash
+      echo "$@" >> {{ helm_simulation_output_file }}
+    dest: "{{ helm_bin_dir }}/helm"
+    mode: 0755
+  when: simulate_helm
+
+- name: Install make
+  package:
+    name: make
+    state: present
+
+- name: Create local certs dir for dummy certs
+  file:
+    path: certs
+    state: directory
+  delegate_to: localhost
+
+- name: Create dummy cert file to simulate offline server certificates in helm install with override.yml file
+  copy:
+    content: |
+      this is dummy server certificate value
+    dest: certs/rootCA.crt
+  delegate_to: localhost