From ae2035f52377e72dd711221acf0bb27434d62a60 Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Wed, 27 Feb 2019 10:37:29 +0100 Subject: [PATCH] test molecule with gce --- .drone.yml | 7 +-- .yamllint | 13 +++++ molecule/gce-centos-7/INSTALL.rst | 16 +++++ molecule/gce-centos-7/create.yml | 65 +++++++++++++++++++++ molecule/gce-centos-7/destroy.yml | 39 +++++++++++++ molecule/gce-centos-7/molecule.yml | 23 ++++++++ molecule/gce-centos-7/playbook.yml | 5 ++ molecule/gce-centos-7/prepare.yml | 9 +++ molecule/gce-centos-7/tests/test_default.py | 14 +++++ 9 files changed, 186 insertions(+), 5 deletions(-) create mode 100644 .yamllint create mode 100644 molecule/gce-centos-7/INSTALL.rst create mode 100644 molecule/gce-centos-7/create.yml create mode 100644 molecule/gce-centos-7/destroy.yml create mode 100644 molecule/gce-centos-7/molecule.yml create mode 100644 molecule/gce-centos-7/playbook.yml create mode 100644 molecule/gce-centos-7/prepare.yml create mode 100644 molecule/gce-centos-7/tests/test_default.py diff --git a/.drone.yml b/.drone.yml index dbb7ae3..4effa97 100644 --- a/.drone.yml +++ b/.drone.yml @@ -22,10 +22,7 @@ steps: # depends_on: [ clone ] - name: molecule - image: centos:7 + image: quay.io/ansible/molecule pull: always commands: - - yum install -y epel-release - - yum install -y python-pip - - pip install ansible molecule -qq - - molecule --debug test + - molecule test --scenario-name gce-centos-7 diff --git a/.yamllint b/.yamllint new file mode 100644 index 0000000..3a2255e --- /dev/null +++ b/.yamllint @@ -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 diff --git a/molecule/gce-centos-7/INSTALL.rst b/molecule/gce-centos-7/INSTALL.rst new file mode 100644 index 0000000..5a135a5 --- /dev/null +++ b/molecule/gce-centos-7/INSTALL.rst @@ -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 diff --git a/molecule/gce-centos-7/create.yml b/molecule/gce-centos-7/create.yml new file mode 100644 index 0000000..a9fc543 --- /dev/null +++ b/molecule/gce-centos-7/create.yml @@ -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 }}" diff --git a/molecule/gce-centos-7/destroy.yml b/molecule/gce-centos-7/destroy.yml new file mode 100644 index 0000000..abe9cf0 --- /dev/null +++ b/molecule/gce-centos-7/destroy.yml @@ -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 diff --git a/molecule/gce-centos-7/molecule.yml b/molecule/gce-centos-7/molecule.yml new file mode 100644 index 0000000..0249651 --- /dev/null +++ b/molecule/gce-centos-7/molecule.yml @@ -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 diff --git a/molecule/gce-centos-7/playbook.yml b/molecule/gce-centos-7/playbook.yml new file mode 100644 index 0000000..bebc4b5 --- /dev/null +++ b/molecule/gce-centos-7/playbook.yml @@ -0,0 +1,5 @@ +--- +- name: Converge + hosts: all + roles: + - role: xoxys.nginx diff --git a/molecule/gce-centos-7/prepare.yml b/molecule/gce-centos-7/prepare.yml new file mode 100644 index 0000000..ddb01fb --- /dev/null +++ b/molecule/gce-centos-7/prepare.yml @@ -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 diff --git a/molecule/gce-centos-7/tests/test_default.py b/molecule/gce-centos-7/tests/test_default.py new file mode 100644 index 0000000..eedd64a --- /dev/null +++ b/molecule/gce-centos-7/tests/test_default.py @@ -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'