From 18de79e1269d8930e53a571038ee544a51fb6a20 Mon Sep 17 00:00:00 2001 From: Liam Young Date: Thu, 13 Oct 2022 08:44:01 +0000 Subject: [PATCH] First cut of zuul jobs for charms This includes: * project templates and jobs for charm projects to use * linter for this repo Change-Id: Id7be6711513f42aedd5aba841f9b19652540e8cf --- .ansible-lint | 23 +++++++++ .gitignore | 2 + playbooks/build-charm.yaml | 76 ++++++++++++++++++++++++++++ playbooks/func-tests.yaml | 29 +++++++++++ playbooks/microk8s-cloud.yaml | 82 +++++++++++++++++++++++++++++++ playbooks/microk8s-juju-post.yaml | 10 ++++ test-requirements.txt | 7 +++ tools/ansible-lint-roles-cache.sh | 39 +++++++++++++++ tox.ini | 23 +++++++++ zuul.d/jobs.yaml | 13 +++++ zuul.d/project-templates.yaml | 9 ++++ zuul.d/project.yaml | 7 +++ 12 files changed, 320 insertions(+) create mode 100644 .ansible-lint create mode 100644 .gitignore create mode 100644 playbooks/build-charm.yaml create mode 100644 playbooks/func-tests.yaml create mode 100644 playbooks/microk8s-cloud.yaml create mode 100644 playbooks/microk8s-juju-post.yaml create mode 100644 test-requirements.txt create mode 100755 tools/ansible-lint-roles-cache.sh create mode 100644 tox.ini create mode 100644 zuul.d/jobs.yaml create mode 100644 zuul.d/project-templates.yaml create mode 100644 zuul.d/project.yaml diff --git a/.ansible-lint b/.ansible-lint new file mode 100644 index 0000000..70a52f7 --- /dev/null +++ b/.ansible-lint @@ -0,0 +1,23 @@ +parseable: true +quiet: false +skip_list: + - meta-no-info # No 'galaxy_info' found + - no-changed-when # Commands should not change things if nothing needs doing + - no-tabs # Most files should not contain tabs + - role-name # Role name does not match ``^[a-z][a-z0-9_]+$`` pattern + - fqcn-builtins # It would probably be good to enforce this, but it's a lot + - risky-file-permissions # It would probably also good to enforce this if someone wants to look at them + - schema # This is a stricter form of schema per https://github.com/ansible/schemas. might also be good if someone has motivation? +use_default_rules: true +verbosity: 1 +mock_modules: + - zuul_console + - zuul_return +loop_var_prefix: zj_ +# Enable rules that are disabled by default: +enable_list: + - no-same-owner + +# Local variables: +# mode: yaml +# End: diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c58748f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.cache +.tox diff --git a/playbooks/build-charm.yaml b/playbooks/build-charm.yaml new file mode 100644 index 0000000..44c8e88 --- /dev/null +++ b/playbooks/build-charm.yaml @@ -0,0 +1,76 @@ +- hosts: all + tasks: + + - name: lxd apt packages are not present + apt: + name: + - lxd + - lxd-client + state: absent + purge: true + become: true + + - name: snapd is installed + apt: + name: snapd + become: true + + - name: lxd snap is installed + snap: + name: lxd + channel: latest/stable + become: true + + - name: lxd is initialised + command: lxd init --auto + become: true + + - name: current user is in lxd group + user: + name: "{{ ansible_user }}" + groups: lxd + append: true + become: true + + - name: reset ssh connection to apply permissions from new group + meta: reset_connection + + - name: charmcraft is installed + snap: + name: charmcraft + channel: latest/stable + classic: true + become: true + + - name: charm is packed + command: + cmd: charmcraft --verbosity trace pack + chdir: "{{ zuul.project.src_dir }}" + + - name: local charm is named consistently + command: + cmd: ./rename.sh + chdir: "{{ zuul.project.src_dir }}" + + - name: Compress built charm + archive: + dest: "{{ zuul.project.src_dir }}/{{ charm_build_name }}.charm.gz" + path: "{{ zuul.project.src_dir }}/{{ charm_build_name }}.charm" + format: gz + + - name: built charm is available in the zuul log root for auto artifact upload + fetch: + src: "{{ zuul.project.src_dir }}/{{ charm_build_name }}.charm.gz" + dest: "{{ zuul.executor.log_root }}/" + flat: true + become: true + + - name: Upload artifacts + zuul_return: + data: + zuul: + artifacts: + - name: charm + url: "{{ charm_build_name }}.charm.gz" + metadata: + type: charm diff --git a/playbooks/func-tests.yaml b/playbooks/func-tests.yaml new file mode 100644 index 0000000..22d1dca --- /dev/null +++ b/playbooks/func-tests.yaml @@ -0,0 +1,29 @@ +- hosts: all + tasks: + - name: test runner packages are installed + apt: + name: + - tox + - jq + - gzip + become: true + + - name: built charm is present locally (artefact from previous job) + include_role: + name: download-artifact + vars: + download_artifact_api: "https://zuul.opendev.org/api/tenant/{{ zuul.tenant }}" + download_artifact_type: charm + download_artifact_pipeline: check + download_artifact_job: build-charm + download_artifact_directory: "{{ zuul.project.src_dir }}" + + - name: Gunzip built charm + command: + cmd: gunzip {{ charm_build_name }}.charm.gz + chdir: "{{ zuul.project.src_dir }}" + + - name: run smoke tests + command: + cmd: tox -e func-smoke + chdir: "{{ zuul.project.src_dir }}" diff --git a/playbooks/microk8s-cloud.yaml b/playbooks/microk8s-cloud.yaml new file mode 100644 index 0000000..6b5a205 --- /dev/null +++ b/playbooks/microk8s-cloud.yaml @@ -0,0 +1,82 @@ +- hosts: all + tasks: + - name: snapd is installed + apt: + name: snapd + become: true + + - name: microk8s is installed + snap: + name: microk8s + classic: true + become: true + + - name: current user is in microk8s group + user: + name: "{{ ansible_user }}" + groups: microk8s + append: true + become: true + + - name: reset ssh connection to apply permissions from new group + meta: reset_connection + + - name: microk8s is started + command: + cmd: microk8s start + + - name: microk8s is running and ready + command: + cmd: microk8s status --wait-ready + register: res + failed_when: '"is running" not in res.stdout' + + - name: microk8s dns addon is enabled + command: + cmd: microk8s enable dns + register: res + changed_when: '"already enabled" not in res.stdout' + + - name: microk8s hostpath storage addon is enabled + command: + cmd: microk8s enable hostpath-storage + register: res + changed_when: '"already enabled" not in res.stdout' + + - name: microk8s metallb addon is enabled + command: + # ip range is an arbitrary choice; may need to be changed later + cmd: microk8s enable metallb:10.170.0.1-10.170.0.100 + register: res + changed_when: '"already enabled" not in res.stdout' + + - name: microk8s addons are ready + command: + cmd: microk8s status --format short + register: res + retries: 18 + delay: 10 # 18 * 10 = 3 minutes + until: > + "core/dns: enabled" in res.stdout and + "core/hostpath-storage: enabled" in res.stdout and + "core/metallb: enabled" in res.stdout + changed_when: res.attempts > 1 + + - name: juju is installed + snap: + name: juju + classic: true + become: true + + - name: juju is bootstrapped on microk8s + command: + cmd: juju bootstrap microk8s microk8s + register: res + changed_when: '"already exists" not in res.stderr' + failed_when: '"ERROR" in res.stderr and "already exists" not in res.stderr' + + - name: current juju controller is microk8s + command: + cmd: juju switch microk8s + register: res + changed_when: '"no change" not in res.stderr' diff --git a/playbooks/microk8s-juju-post.yaml b/playbooks/microk8s-juju-post.yaml new file mode 100644 index 0000000..efcbf68 --- /dev/null +++ b/playbooks/microk8s-juju-post.yaml @@ -0,0 +1,10 @@ +- hosts: all + tasks: + - name: debug output for debugging the functional test # noqa risky-shell-pipe + shell: | + set -x + MODEL="$(juju models --format=json | jq -r '.models[]["short-name"]' | grep '^zaza-')" + juju switch $MODEL + juju status + juju debug-log --replay + exit 0 diff --git a/test-requirements.txt b/test-requirements.txt new file mode 100644 index 0000000..2687342 --- /dev/null +++ b/test-requirements.txt @@ -0,0 +1,7 @@ +flake8 +# ansible-lint requires ansible>=2.9 +ansible>=5,<6 +# pin until https://github.com/ansible/ansible-lint/issues/2320 is fixed +ansible-lint>=6,<6.5 +bashate>=0.2 + diff --git a/tools/ansible-lint-roles-cache.sh b/tools/ansible-lint-roles-cache.sh new file mode 100755 index 0000000..d45527a --- /dev/null +++ b/tools/ansible-lint-roles-cache.sh @@ -0,0 +1,39 @@ +#!/bin/bash + +# This script is taken from the openstack-zuul-jobs +# projects https://github.com/openstack/openstack-zuul-jobs + +if [[ ! ${ANSIBLE_ROLES_PATH} =~ \.cache.* ]]; then + exit 0 +fi + +if [ ! -d .cache/ansible-lint ]; then + mkdir -p .cache/ansible-lint +fi + +pushd .cache/ansible-lint + +repos=(opendev/base-jobs + opendev/system-config + openstack/openstack-zuul-jobs + zuul/zuul-jobs) + +for repo in ${repos[@]}; do + dir=$(dirname $repo) + echo "Updating Ansible roles repo ${dir}" + if [ ! -d $repo ]; then + echo "Cloning fresh" + mkdir -p $dir + pushd $dir + git clone https://opendev.org/$repo + popd + else + echo "Updating repo" + pushd $repo + git fetch -a + git pull + popd + fi + echo "Done" +done + diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..08c6996 --- /dev/null +++ b/tox.ini @@ -0,0 +1,23 @@ +[tox] +minversion = 3.2.0 +skipsdist = True +envlist = linters + +[testenv] +basepython = python3 +deps = -r{toxinidir}/test-requirements.txt + +[testenv:linters] +whitelist_externals = bash +setenv = + ANSIBLE_ROLES_PATH={env:ANSIBLE_ROLES_PATH:{toxinidir}/.cache/ansible-lint/opendev/base-jobs/roles:{toxinidir}/.cache/ansible-lint/opendev/system-config/roles:{toxinidir}/.cache/ansible-lint/zuul/zuul-jobs/roles:{toxinidir}/.cache/ansible-lint/openstack/openstack-zuul-jobs/roles} +commands = + flake8 {posargs} + {toxinidir}/tools/ansible-lint-roles-cache.sh + ansible-lint + +[testenv:venv] +commands = {posargs} + +[flake8] +exclude = .venv,.tox,dist,doc,build,*.egg,.cache diff --git a/zuul.d/jobs.yaml b/zuul.d/jobs.yaml new file mode 100644 index 0000000..3bf175b --- /dev/null +++ b/zuul.d/jobs.yaml @@ -0,0 +1,13 @@ +- job: + name: charmbuild + run: playbooks/build-charm.yaml + provides: charm + timeout: 3600 +- job: + name: zaza-smoke-test + pre-run: playbooks/microk8s-cloud.yaml + run: playbooks/func-tests.yaml + cleanup-run: playbooks/microk8s-juju-post.yaml + requires: charm + dependencies: + - name: charmbuild diff --git a/zuul.d/project-templates.yaml b/zuul.d/project-templates.yaml new file mode 100644 index 0000000..fd0b0c6 --- /dev/null +++ b/zuul.d/project-templates.yaml @@ -0,0 +1,9 @@ +- project-template: + name: microk8s-func-test + description: | + This project template is defined to run zaza functional tests + inside microk8s + check: + jobs: + - charmbuild + - zaza-smoke-test diff --git a/zuul.d/project.yaml b/zuul.d/project.yaml new file mode 100644 index 0000000..4f1f831 --- /dev/null +++ b/zuul.d/project.yaml @@ -0,0 +1,7 @@ +- project: + check: + jobs: + - openstack-zuul-jobs-linters + gate: + jobs: + - openstack-zuul-jobs-linters