Molecule tests for docker role

Issue-ID: OOM-1759

Change-Id: I9e1ee7f248147b3f7229625c7a20fa84e8e67aac
Signed-off-by: Samuli Silvius <s.silvius@partner.samsung.com>
diff --git a/ansible/test/roles/prepare-docker/tasks/docker-socket-override.yml b/ansible/test/roles/prepare-docker/tasks/docker-socket-override.yml
new file mode 100644
index 0000000..0cabadf
--- /dev/null
+++ b/ansible/test/roles/prepare-docker/tasks/docker-socket-override.yml
@@ -0,0 +1,13 @@
+---
+- name: Create docker.socket systemd dir for override
+  file:
+    path: /etc/systemd/system/docker.socket.d
+    state: directory
+
+- name: Fake dockerd dependent docker.socket service not to actually listen the docker socket as dockerd is not used in container only docker client
+  copy:
+    content: |
+      [Socket]
+      ListenStream=
+      ListenStream=/tmp/fake
+    dest: /etc/systemd/system/docker.socket.d/override.conf
diff --git a/ansible/test/roles/prepare-docker/tasks/enable-repos.yml b/ansible/test/roles/prepare-docker/tasks/enable-repos.yml
new file mode 100644
index 0000000..204bf03
--- /dev/null
+++ b/ansible/test/roles/prepare-docker/tasks/enable-repos.yml
@@ -0,0 +1,13 @@
+---
+- name: Enable docker repos back for Molecule testing purposes
+  copy:
+    remote_src: yes
+    src: "{{ item }}"
+    dest: "{{ (item | splitext)[0] }}"
+  loop: "{{ docker_needed_repos }}"
+
+- name: Disable offline repo for molecule testing purposes
+  lineinfile:
+    path: "{{ offline_repo_file }}"
+    regexp: 'enabled = 1'
+    line: 'enabled = 0'
diff --git a/ansible/test/roles/prepare-docker/tasks/main.yml b/ansible/test/roles/prepare-docker/tasks/main.yml
new file mode 100644
index 0000000..e7d8706
--- /dev/null
+++ b/ansible/test/roles/prepare-docker/tasks/main.yml
@@ -0,0 +1,21 @@
+---
+- name: Check repositories
+  stat:
+    path: "{{ item }}"
+  loop: "{{ docker_needed_repos + [offline_repo_file] }}"
+  register: repos
+
+- name: Set fact for offline repos created
+  set_fact:
+    offline_repo_created: "{{ (repos.results | map(attribute='stat.exists') | list) is all }}"
+
+- debug:
+    var: offline_repo_created
+
+- name: Enable repos back (if package-repository role already setup offline onap repo) for molecule testing purposes to install docker
+  include_tasks: enable-repos.yml
+  when: offline_repo_created
+
+- name: Prepare docker repos normally
+  include_tasks: prepare-docker-repos.yml
+  when: not offline_repo_created
diff --git a/ansible/test/roles/prepare-docker/tasks/prepare-docker-repos.yml b/ansible/test/roles/prepare-docker/tasks/prepare-docker-repos.yml
new file mode 100644
index 0000000..26f89d6
--- /dev/null
+++ b/ansible/test/roles/prepare-docker/tasks/prepare-docker-repos.yml
@@ -0,0 +1,15 @@
+---
+- name: Install Docker-CE repo
+  yum_repository:
+    name: docker-ce
+    description: Docker-ce YUM repo
+    baseurl: https://download.docker.com/linux/centos/7/x86_64/stable/
+    gpgcheck: true
+    gpgkey: https://download.docker.com/linux/centos/gpg
+
+- name: Install docker python bindings
+  import_tasks: python-bindings.yml
+
+- name: Fake dockerd on container
+  import_tasks: docker-socket-override.yml
+  when: ansible_env.container == 'docker'
diff --git a/ansible/test/roles/prepare-docker/tasks/python-bindings.yml b/ansible/test/roles/prepare-docker/tasks/python-bindings.yml
new file mode 100644
index 0000000..7a66a32
--- /dev/null
+++ b/ansible/test/roles/prepare-docker/tasks/python-bindings.yml
@@ -0,0 +1,3 @@
+---
+- name: Handle python2-docker dependency needed by Ansible's docker_* modules
+  import_tasks: python2-docker.yml
diff --git a/ansible/test/roles/prepare-docker/tasks/python2-docker.yml b/ansible/test/roles/prepare-docker/tasks/python2-docker.yml
new file mode 100644
index 0000000..9cb3b48
--- /dev/null
+++ b/ansible/test/roles/prepare-docker/tasks/python2-docker.yml
@@ -0,0 +1,38 @@
+---
+- name: Remove docker-python that is conflicting with python2-docker
+  package:
+    name: docker-python
+    state: absent
+
+- name: Create own-repo dir
+  file:
+    path: /root/own-repo
+    state: directory
+
+- name: Download python2-docker rpm for Ansible (docker_container module dep)
+  get_url:
+    url: https://cbs.centos.org/kojifiles/packages/python-docker/2.5.1/2.el7/noarch/python2-docker-2.5.1-2.el7.noarch.rpm
+    dest: /root/own-repo/
+  register: rpm
+
+- name: Install createrepo
+  package:
+    name: createrepo
+    state: present
+
+- name: Run createrepo
+  command: createrepo /root/own-repo/
+  when: rpm.changed
+
+- name: Create own-repo
+  yum_repository:
+    name: own-repo
+    description: Own repo for ansible deps
+    baseurl: file:///root/own-repo
+    enabled: true
+    gpgcheck: false
+
+- name: Install docker python module
+  package:
+    name: python2-docker
+    state: present