Skip to content

Commit

Permalink
Merge pull request #16 from andreygubarev/feature-ci-test
Browse files Browse the repository at this point in the history
feature ci test
  • Loading branch information
andreygubarev authored Jun 17, 2023
2 parents 3dbfd82 + 5e3604e commit c7953f2
Show file tree
Hide file tree
Showing 33 changed files with 403 additions and 82 deletions.
21 changes: 20 additions & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,28 @@ jobs:
- name: Lint with pycodestyle
run: python -m pycodestyle molecule_qemu

test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'

- name: Install system dependencies
run: |
sudo apt-get update
sudo apt-get install -yq --no-install-recommends mkisofs qemu-system-x86 qemu-utils
- name: Run test
run: make test-platform-amd64

build:
runs-on: ubuntu-latest
needs: [lint-ansible, lint-python]
needs: [lint-ansible, lint-python, test]

steps:
- uses: actions/checkout@v3
Expand Down
26 changes: 15 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,24 @@ lint: virtualenv ## Lint
$(VIRTUALENV_VENV)/bin/ansible-lint -v molecule_qemu
$(VIRTUALENV_VENV)/bin/pycodestyle molecule_qemu

.PHONY: test
test: virtualenv ## Test
.PHONY: test ## Test
test:
$(VIRTUALENV_PIP) install -e .
cd tests && $(VIRTUALENV_MOLECULE) test -s default
cd tests && $(VIRTUALENV_MOLECULE) test

.PHONY: test-all
test-all: virtualenv ## Test
.PHONY: test-%
test-%: virtualenv
$(VIRTUALENV_PIP) install -e .
cd tests && \
$(VIRTUALENV_MOLECULE) destroy || true && \
$(VIRTUALENV_MOLECULE) reset && \
$(VIRTUALENV_MOLECULE) test -s default && \
$(VIRTUALENV_MOLECULE) test -s user && \
$(VIRTUALENV_MOLECULE) test -s vmnet-shared
cd tests && $(VIRTUALENV_MOLECULE) test -s $*

.PHONY: test-network
test-network: test-network-shared test-network-user ## Test network

.PHONY: test-os
test-os: test-os-debian-bullseye test-os-ubuntu-focal test-os-ubuntu-jammy ## Test OS

.PHONY: test-platform
test-platform: test-platform-amd64 test-platform-arm64 ## Test platform

.PHONY: clean
clean: ## Remove cache
Expand Down
139 changes: 81 additions & 58 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,60 +1,110 @@
# `molecule-qemu`

The development of the Molecule QEMU driver was motivated by the author's choice of the Apple M1 as their primary development machine. He wanted to test Ansible roles on the Apple M1 but preferred not to rely on `Docker` for testing due to challenges with Docker's `systemd` support. Author never liked to install supplementary software such as `Vagrant` and `VirtualBox` on development machine. Author is aware of `libvirt` and `virt-manager` but their complexity and the lack of support was frustrating.
The development of the Molecule `QEMU` driver was motivated by the author's choice of the Apple M1 as their primary development machine. He wanted to test Ansible roles on the Apple M1 but preferred not to rely on `Docker` for testing due to challenges with Docker's `systemd` support. Author never liked to install supplementary software such as `Vagrant` and `VirtualBox` on development machine. Author is aware of `libvirt` and `virt-manager` but their complexity and the lack of support was frustrating.

Supported platforms:
* MacOS 13.x (aaarch64)
* MacOS 13.x (arm64)
* Ubuntu 22.04 LTS (amd64) (tested on GitHub Actions)

Support guest OS:
* Ubuntu 20.04 LTS (aarch64, x86_64)
* Ubuntu 22.04 LTS (aarch64, x86_64)
* Debian 11 (aarch64, x86_64)
* Debian 11 (arm64, amd64)
* Ubuntu 20.04 LTS (arm64, amd64)
* Ubuntu 22.04 LTS (arm64, amd64)

Support of other platforms and guest OS is possible, but not tested. Please, open an issue if you want to add support for other platforms.

## Usage
Supported network modes:
* `user` - QEMU's user networking mode
* `vmnet-shared` - QEMU's `vmnet-shared` networking mode (MacOS only)

## Quick start

Install `molecule-qemu` python package:
```bash
pip install molecule-qemu
```

Create a new Molecule scenario using `molecule init` command:

```bash
molecule init scenario default --driver-name molecule-qemu --verifier-name testinfra
```

Edit `molecule.yml` and add platforms:

```yaml
---
dependency:
name: galaxy
driver:
name: molecule-qemu
platforms:
- name: debian-bullseye-arm64
image: https://cloud.debian.org/images/cloud/bullseye/latest/debian-11-genericcloud-arm64.qcow2
image_checksum: sha512:https://cloud.debian.org/images/cloud/bullseye/latest/SHA512SUMS
image_arch: aarch64
vm_network: vmnet-shared
provisioner:
name: ansible
inventory:
host_vars:
debian-bullseye-arm64: {}
verifier:
name: testinfra
```
### Dependencies
Install QEMU and CDRTools on macOS:
```bash
brew install qemu cdrtools
```

## Network modes
Install QEMU on Ubuntu:

```bash
apt-get install mkisofs qemu-system-x86 qemu-utils
```

Network mode is selected by setting `vm_network` in `molecule.yml`. Supported modes are: `user` and `vmnet-shared`. Default mode is `user`. All modes are mutually exclusive.
## Network modes

### `user` network mode

This is the default network mode. It uses QEMU's user networking mode.

Mode is selected by setting `vm_network: user` in `molecule.yml`.
Mode is selected by setting `vm_network: user` in `molecule.yml`. This is the default mode. SSH port is forwarded to the host and must be unique for each platform (use `ssh_port` option to set it). Example:

```yaml
- name: debian-bullseye-arm64
image: https://cloud.debian.org/images/cloud/bullseye/latest/debian-11-genericcloud-arm64.qcow2
image_checksum: sha512:https://cloud.debian.org/images/cloud/bullseye/latest/SHA512SUMS
image_arch: aarch64
vm_network: user # this is the default
ssh_port: 2022
```
### `vmnet-shared` network mode

This mode uses QEMU's `vmnet-shared` networking mode. It requires `vmnet.framework` to be installed on the host. This mode is only supported on MacOS. It requires *passwordless* `sudo` access for current user.

Mode is selected by setting `vm_network: vmnet-shared` in `molecule.yml`.
Mode is selected by setting `vm_network: vmnet-shared` in `molecule.yml`. Example:

```yaml
- name: debian-bullseye-arm64
image: https://cloud.debian.org/images/cloud/bullseye/latest/debian-11-genericcloud-arm64.qcow2
image_checksum: sha512:https://cloud.debian.org/images/cloud/bullseye/latest/SHA512SUMS
image_arch: aarch64
vm_network: vmnet-shared
```

# Examples

## Example scenario
```bash
molecule init scenario default --driver-name molecule-qemu --verifier-name testinfra
```
See [tests](https://github.com/andreygubarev/molecule-qemu/tree/main/tests/molecule) for more examples.

## Example `molecule.yml` for `user` network mode
## Sample platforms configuration

```yaml
---
dependency:
name: galaxy
driver:
name: molecule-qemu
platforms:
- name: debian-bullseye-arm64
image: https://cloud.debian.org/images/cloud/bullseye/latest/debian-11-genericcloud-arm64.qcow2
Expand Down Expand Up @@ -86,53 +136,26 @@ platforms:
image_checksum: sha256:https://cloud-images.ubuntu.com/jammy/current/SHA256SUMS
image_arch: x86_64
ssh_port: 10005
provisioner:
name: ansible
verifier:
name: ansible
```

## Example `molecule.yml` for `vmnet-shared` network mode
# Cloud Images URLs

```yaml
---
dependency:
name: galaxy
driver:
name: molecule-qemu
platforms:
- name: ubuntu-1
image: https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-arm64.img
image_checksum: sha256:https://cloud-images.ubuntu.com/focal/current/SHA256SUMS
image_arch: aarch64
ssh_user: ubuntu
vm_network: vmnet-shared
- name: ubuntu-2
image: https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img
image_checksum: sha256:https://cloud-images.ubuntu.com/focal/current/SHA256SUMS
image_arch: x86_64 # default
ssh_user: ubuntu
vm_network: vmnet-shared
provisioner:
name: ansible
verifier:
name: ansible
```
For convenience, here are the URLs for the cloud images used in the examples above.

# Cloud Images
## [Debian](https://cloud.debian.org/images/cloud/)
* https://cloud.debian.org/images/cloud/bullseye/latest/SHA512SUMS
* https://cloud.debian.org/images/cloud/bullseye/latest/debian-11-genericcloud-amd64.qcow2
* https://cloud.debian.org/images/cloud/bullseye/latest/debian-11-genericcloud-arm64.qcow2

## [Ubuntu](https://cloud-images.ubuntu.com/)
* https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-arm64.img
* https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img
* https://cloud-images.ubuntu.com/focal/current/SHA256SUMS
* https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-arm64.img
* https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img
* https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-arm64.img
* https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img
* https://cloud-images.ubuntu.com/jammy/current/SHA256SUMS
* https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-arm64.img
* https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img


## [Debian](https://cloud.debian.org/images/cloud/)
* https://cloud.debian.org/images/cloud/bullseye/latest/debian-11-genericcloud-amd64.qcow2
* https://cloud.debian.org/images/cloud/bullseye/latest/debian-11-genericcloud-arm64.qcow2
* https://cloud.debian.org/images/cloud/bullseye/latest/SHA512SUMS

# Reference

Expand Down
2 changes: 1 addition & 1 deletion molecule_qemu/playbooks/create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@
host: "{{ molecule_instances_ipv4[item.name] }}"
port: "{{ item.ssh_port }}"
delay: 30
timeout: "{{ 60 * molecule_instances | length }}"
timeout: "{{ 600 * molecule_instances | length }}"
search_regex: "OpenSSH"
loop: "{{ molecule_instances }}"
loop_control:
Expand Down
2 changes: 1 addition & 1 deletion tests/molecule/default/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ platforms:
image: https://cloud.debian.org/images/cloud/bullseye/latest/debian-11-genericcloud-arm64.qcow2
image_checksum: sha512:https://cloud.debian.org/images/cloud/bullseye/latest/SHA512SUMS
image_arch: aarch64
ssh_port: 10000
ssh_port: 2022
provisioner:
name: ansible
verifier:
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ dependency:
driver:
name: molecule-qemu
platforms:
- name: debian-bullseye-arm64
image: https://cloud.debian.org/images/cloud/bullseye/latest/debian-11-genericcloud-arm64.qcow2
image_checksum: sha512:https://cloud.debian.org/images/cloud/bullseye/latest/SHA512SUMS
image_arch: aarch64
vm_network: vmnet-shared
- name: debian-bullseye-amd64
image: https://cloud.debian.org/images/cloud/bullseye/latest/debian-11-genericcloud-amd64.qcow2
image_checksum: sha512:https://cloud.debian.org/images/cloud/bullseye/latest/SHA512SUMS
image_arch: x86_64
vm_network: vmnet-shared
- name: debian-bullseye-arm64
image: https://cloud.debian.org/images/cloud/bullseye/latest/debian-11-genericcloud-arm64.qcow2
image_checksum: sha512:https://cloud.debian.org/images/cloud/bullseye/latest/SHA512SUMS
image_arch: aarch64
vm_network: vmnet-shared
provisioner:
name: ansible
verifier:
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ dependency:
driver:
name: molecule-qemu
platforms:
- name: debian-bullseye-arm64
image: https://cloud.debian.org/images/cloud/bullseye/latest/debian-11-genericcloud-arm64.qcow2
image_checksum: sha512:https://cloud.debian.org/images/cloud/bullseye/latest/SHA512SUMS
image_arch: aarch64
ssh_port: 10000
- name: debian-bullseye-amd64
image: https://cloud.debian.org/images/cloud/bullseye/latest/debian-11-genericcloud-amd64.qcow2
image_checksum: sha512:https://cloud.debian.org/images/cloud/bullseye/latest/SHA512SUMS
image_arch: x86_64
ssh_port: 10000
- name: debian-bullseye-arm64
image: https://cloud.debian.org/images/cloud/bullseye/latest/debian-11-genericcloud-arm64.qcow2
image_checksum: sha512:https://cloud.debian.org/images/cloud/bullseye/latest/SHA512SUMS
image_arch: aarch64
ssh_port: 10001
provisioner:
name: ansible
Expand Down
File renamed without changes.
File renamed without changes.
12 changes: 12 additions & 0 deletions tests/molecule/os-debian-bullseye/converge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
- name: Converge
hosts: all

environment:
http_proxy: "{{ lookup('ansible.builtin.env', 'http_proxy') | default(omit) }}"
https_proxy: "{{ lookup('ansible.builtin.env', 'https_proxy') | default(omit) }}"

tasks:
- name: "Include tests"
ansible.builtin.include_role:
name: "tests"
20 changes: 20 additions & 0 deletions tests/molecule/os-debian-bullseye/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
dependency:
name: galaxy
driver:
name: molecule-qemu
platforms:
- name: debian-bullseye-amd64
image: https://cloud.debian.org/images/cloud/bullseye/latest/debian-11-genericcloud-amd64.qcow2
image_checksum: sha512:https://cloud.debian.org/images/cloud/bullseye/latest/SHA512SUMS
image_arch: x86_64
ssh_port: 10000
- name: debian-bullseye-arm64
image: https://cloud.debian.org/images/cloud/bullseye/latest/debian-11-genericcloud-arm64.qcow2
image_checksum: sha512:https://cloud.debian.org/images/cloud/bullseye/latest/SHA512SUMS
image_arch: aarch64
ssh_port: 10001
provisioner:
name: ansible
verifier:
name: ansible
15 changes: 15 additions & 0 deletions tests/molecule/os-debian-bullseye/prepare.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
- name: Prepare
hosts: all
become: true
gather_facts: false

environment:
http_proxy: "{{ lookup('ansible.builtin.env', 'http_proxy') | default(omit) }}"
https_proxy: "{{ lookup('ansible.builtin.env', 'https_proxy') | default(omit) }}"

tasks:
- name: Wait for SSH to become available
ansible.builtin.wait_for_connection:
delay: 5
timeout: 300
10 changes: 10 additions & 0 deletions tests/molecule/os-debian-bullseye/verify.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
# This is an example playbook to execute Ansible tests.

- name: Verify
hosts: all
gather_facts: false
tasks:
- name: Example assertion
ansible.builtin.assert:
that: true
12 changes: 12 additions & 0 deletions tests/molecule/os-ubuntu-focal/converge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
- name: Converge
hosts: all

environment:
http_proxy: "{{ lookup('ansible.builtin.env', 'http_proxy') | default(omit) }}"
https_proxy: "{{ lookup('ansible.builtin.env', 'https_proxy') | default(omit) }}"

tasks:
- name: "Include tests"
ansible.builtin.include_role:
name: "tests"
Loading

0 comments on commit c7953f2

Please sign in to comment.