Refactor CI pipelien and enable molecule testing #1

Merged
xoxys merged 131 commits from molecule-dev into master 2019-03-25 21:23:40 +00:00
9 changed files with 186 additions and 5 deletions
Showing only changes of commit ae2035f523 - Show all commits

View File

@ -22,10 +22,7 @@ steps:
# depends_on: [ clone ] # depends_on: [ clone ]
- name: molecule - name: molecule
image: centos:7 image: quay.io/ansible/molecule
pull: always pull: always
commands: commands:
- yum install -y epel-release - molecule test --scenario-name gce-centos-7
- yum install -y python-pip
- pip install ansible molecule -qq
- molecule --debug test

13
.yamllint Normal file
View File

@ -0,0 +1,13 @@
extends: default
rules:
braces:
max-spaces-inside: 1
level: error
brackets:
max-spaces-inside: 1
level: error
line-length: disable
# NOTE(retr0h): Templates no longer fail this lint rule.
# Uncomment if running old Molecule templates.
# truthy: disable

View File

@ -0,0 +1,16 @@
*******
Google Cloud Engine driver installation guide
*******
Requirements
============
* A GCE credentials rc file
* apache-libcloud
Install
=======
.. code-block:: bash
$ sudo pip install apache-libcloud

View File

@ -0,0 +1,65 @@
---
- name: Create
hosts: localhost
connection: local
gather_facts: false
no_log: "{{ not (lookup('env', 'MOLECULE_DEBUG') | bool or molecule_yml.provisioner.log|default(false) | bool) }}"
vars:
ssh_port: 22
ssh_user: "{{ lookup('env', 'USER') }}"
ssh_identity_file: "{{ lookup('env', 'HOME') }}/.ssh/google_compute_engine"
tasks:
- name: Create molecule instance(s)
gce:
instance_names: "{{ item.name }}"
zone: "{{ item.zone }}"
machine_type: "{{ item.machine_type }}"
image: "{{ item.image }}"
service_account_email: "{{ lookup('env', 'GCE_SERVICE_ACCOUNT_EMAIL') }}"
credentials_file: "{{ lookup('env', 'GCE_CREDENTIALS_FILE') }}"
project_id: "{{ lookup('env', 'GCE_PROJECT_ID') }}"
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: gce_jobs
until: gce_jobs.finished
retries: 300
with_items: "{{ server.results }}"
# Mandatory configuration for Molecule to function.
- name: Populate instance config dict
set_fact:
instance_conf_dict: {
'instance': "{{ item.instance_data[0].name }}",
'address': "{{ item.instance_data[0].public_ip }}",
'user': "{{ ssh_user }}",
'port': "{{ ssh_port }}",
'identity_file': "{{ ssh_identity_file }}", }
with_items: "{{ gce_jobs.results }}"
register: instance_config_dict
when: server.changed | bool
- name: Convert instance config dict to a list
set_fact:
instance_conf: "{{ instance_config_dict.results | map(attribute='ansible_facts.instance_conf_dict') | list }}"
when: server.changed | bool
- name: Dump instance config
copy:
content: "{{ instance_conf | to_json | from_json | molecule_to_yaml | molecule_header }}"
dest: "{{ molecule_instance_config }}"
when: server.changed | bool
- name: Wait for SSH
wait_for:
port: "{{ ssh_port }}"
host: "{{ item.address }}"
search_regex: SSH
delay: 10
with_items: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}"

View File

@ -0,0 +1,39 @@
---
- name: Destroy
hosts: localhost
connection: local
gather_facts: false
no_log: "{{ not (lookup('env', 'MOLECULE_DEBUG') | bool or molecule_yml.provisioner.log|default(false) | bool) }}"
tasks:
- name: Destroy molecule instance(s)
gce:
instance_names: "{{ item.name }}"
state: absent
zone: "{{ item.zone }}"
service_account_email: "{{ lookup('env', 'GCE_SERVICE_ACCOUNT_EMAIL') }}"
credentials_file: "{{ lookup('env', 'GCE_CREDENTIALS_FILE') }}"
project_id: "{{ lookup('env', 'GCE_PROJECT_ID') }}"
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: gce_jobs
until: gce_jobs.finished
retries: 300
with_items: "{{ server.results }}"
# Mandatory configuration for Molecule to function.
- name: Populate instance config
set_fact:
instance_conf: {}
- name: Dump instance config
copy:
content: "{{ instance_conf | to_json | from_json | molecule_to_yaml | molecule_header }}"
dest: "{{ molecule_instance_config }}"
when: server.changed | bool

View File

@ -0,0 +1,23 @@
---
dependency:
name: galaxy
driver:
name: gce
lint:
name: yamllint
enabled: False
platforms:
- name: instance
zone: europe-north1-a
machine_type: f1-micro
image: centos-7
provisioner:
name: ansible
lint:
name: ansible-lint
scenario:
name: gce-centos-7
verifier:
name: testinfra
lint:
name: flake8

View File

@ -0,0 +1,5 @@
---
- name: Converge
hosts: all
roles:
- role: xoxys.nginx

View File

@ -0,0 +1,9 @@
---
- name: Prepare
hosts: all
gather_facts: false
tasks:
- name: Install python for Ansible
raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal python-zipstream)
become: true
changed_when: false

View File

@ -0,0 +1,14 @@
import os
import testinfra.utils.ansible_runner
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')
def test_hosts_file(host):
f = host.file('/etc/hosts')
assert f.exists
assert f.user == 'root'
assert f.group == 'root'