From 4bdaf6b6284241a772f0960f294bfc9f259ff73f Mon Sep 17 00:00:00 2001 From: Flavio Percoco Date: Mon, 23 Oct 2017 12:55:11 +0200 Subject: [PATCH] Adding zuul coverage Change-Id: I451fbc00eaa765f3b32b7e273750c88ca38f4964 --- .zuul.yaml | 24 +++++++++++ defaults/main.yml | 2 +- meta/main.yml | 6 ++- requirements.txt | 2 + tasks/provision.yml | 6 +-- tests/get_logs.sh | 84 ++++++++++++++++++++++++++++++++++++ tests/post.yml | 28 ++++++++++++ tests/pre.yml | 55 +++++++++++++++++++++++ tests/pre_debian.yml | 1 + tests/pre_rhel.yml | 54 +++++++++++++++++++++++ tests/run.yml | 46 ++++++++++++++++++++ tests/templates/inventory.j2 | 12 ++++++ tests/templates/playbook.j2 | 12 ++++++ 13 files changed, 325 insertions(+), 7 deletions(-) create mode 100644 .zuul.yaml create mode 100644 requirements.txt create mode 100644 tests/get_logs.sh create mode 100644 tests/post.yml create mode 100644 tests/pre.yml create mode 100644 tests/pre_debian.yml create mode 100644 tests/pre_rhel.yml create mode 100644 tests/run.yml create mode 100644 tests/templates/inventory.j2 create mode 100644 tests/templates/playbook.j2 diff --git a/.zuul.yaml b/.zuul.yaml new file mode 100644 index 0000000..5694846 --- /dev/null +++ b/.zuul.yaml @@ -0,0 +1,24 @@ +- project: + name: openstack/ansible-role-k8s-mariadb + check: + jobs: + - ansible-role-k8s-mariadb-centos + +- nodeset: + name: ansible-role-k8s-centos + nodes: + - name: primary + label: centos-7 + +- job: + name: ansible-role-k8s-base + pre-run: tests/pre + run: tests/run + post-run: tests/post + attempts: 1 + timeout: 10800 + +- job: + name: ansible-role-k8s-mariadb-centos + parent: ansible-role-k8s-base + nodeset: ansible-role-k8s-centos diff --git a/defaults/main.yml b/defaults/main.yml index f94a5c5..3e34876 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,4 +1,4 @@ -coe_host: "https://127.0.0.1:8443" +coe_host: coe_config_context: coe_config_file: action: provision diff --git a/meta/main.yml b/meta/main.yml index 0f5b482..fe03c65 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -17,4 +17,8 @@ galaxy_info: - database - mariadb -dependencies: [] +dependencies: + - role: ansible.kubernetes-modules + install_python_requirements: no + + - role: ansible-role-k8s-tripleo diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..4075a79 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +ansible>=2,<2.4 +openshift diff --git a/tasks/provision.yml b/tasks/provision.yml index d61e2e4..3af33c1 100644 --- a/tasks/provision.yml +++ b/tasks/provision.yml @@ -170,8 +170,4 @@ namespace: "{{namespace}}" state: present data: - my.cnf: | - {{my_cnf | b64encode}} - - -- debug: var=create_deployment + my.cnf: "{{ my_cnf | b64encode }}" diff --git a/tests/get_logs.sh b/tests/get_logs.sh new file mode 100644 index 0000000..beea391 --- /dev/null +++ b/tests/get_logs.sh @@ -0,0 +1,84 @@ +#!/bin/bash + +set +o errexit + +check_failure() { + # All docker container's status are created, restarting, running, removing, + # paused, exited and dead. Containers without running status are treated as + # failure. removing is added in docker 1.13, just ignore it now. + failed_containers=$(docker ps -a --format "{{.Names}}" \ + --filter status=created \ + --filter status=restarting \ + --filter status=paused \ + --filter status=exited \ + --filter status=dead) + + if [[ -n "$failed_containers" ]]; then + exit 1; + fi +} + +copy_logs() { + LOG_DIR=/tmp/logs + + if [[ -d "$HOME/.ansible" ]]; then + cp -rvnL $HOME/.ansible/* ${LOG_DIR}/ansible/ + fi + + cp -rvnL /etc ${LOG_DIR}/ + cp -rvnL /var/log/* ${LOG_DIR}/system_logs/ + cp -rvnL /tmp/kubespray ${LOG_DIR}/ + + + if [[ -x "$(command -v journalctl)" ]]; then + journalctl --no-pager > ${LOG_DIR}/system_logs/syslog.txt + journalctl --no-pager -u docker.service > ${LOG_DIR}/system_logs/docker.log + else + cp /var/log/upstart/docker.log ${LOG_DIR}/system_logs/docker.log + fi + + cp -r /etc/sudoers.d ${LOG_DIR}/system_logs/ + cp /etc/sudoers ${LOG_DIR}/system_logs/sudoers.txt + + df -h > ${LOG_DIR}/system_logs/df.txt + free > ${LOG_DIR}/system_logs/free.txt + parted -l > ${LOG_DIR}/system_logs/parted-l.txt + mount > ${LOG_DIR}/system_logs/mount.txt + env > ${LOG_DIR}/system_logs/env.txt + + if [ `command -v dpkg` ]; then + dpkg -l > ${LOG_DIR}/system_logs/dpkg-l.txt + fi + if [ `command -v rpm` ]; then + rpm -qa > ${LOG_DIR}/system_logs/rpm-qa.txt + fi + + # final memory usage and process list + ps -eo user,pid,ppid,lwp,%cpu,%mem,size,rss,cmd > ${LOG_DIR}/system_logs/ps.txt + + if [ `command -v docker` ]; then + # docker related information + (docker info && docker images && docker ps -a) > ${LOG_DIR}/system_logs/docker-info.txt + + for container in $(docker ps -a --format "{{.Names}}"); do + docker logs --tail all ${container} > ${LOG_DIR}/docker_logs/${container}.txt + done + fi + + # Rename files to .txt; this is so that when displayed via + # logs.openstack.org clicking results in the browser shows the + # files, rather than trying to send it to another app or make you + # download it, etc. + + # Rename all .log files to .txt files + for f in $(find ${LOG_DIR}/{system_logs,docker_logs} -name "*.log"); do + mv $f ${f/.log/.txt} + done + + chmod -R 777 ${LOG_DIR} + find ${LOG_DIR}/{system_logs,docker_logs} -iname '*.txt' -execdir gzip -f -9 {} \+ + find ${LOG_DIR}/{system_logs,docker_logs} -iname '*.json' -execdir gzip -f -9 {} \+ +} + +copy_logs +check_failure diff --git a/tests/post.yml b/tests/post.yml new file mode 100644 index 0000000..97a9cfa --- /dev/null +++ b/tests/post.yml @@ -0,0 +1,28 @@ +--- +- hosts: all + vars: + logs_dir: "/tmp/logs" + tasks: + - name: Run diagnostics script + script: get_logs.sh + register: get_logs_result + become: true + failed_when: false + + - name: Print get_logs output + debug: + msg: "{{ get_logs_result.stdout }}" + + - name: Download logs to executor + synchronize: + dest: "{{ zuul.executor.log_root }}/{{ inventory_hostname }}" + mode: pull + src: "{{ logs_dir }}/" + ignore_errors: yes + + - name: Download /etc/hosts file to executor + synchronize: + src: "/etc/hosts" + dest: "{{ zuul.executor.log_root }}/{{inventory_hostname }}/" + mode: pull + ignore_errors: yes diff --git a/tests/pre.yml b/tests/pre.yml new file mode 100644 index 0000000..5237012 --- /dev/null +++ b/tests/pre.yml @@ -0,0 +1,55 @@ +--- +- hosts: all + vars: + logs_dir: "/tmp/logs" + tasks: + - name: "Ensure {{item}} dir exists" + file: + path: "{{item}}" + state: "directory" + with_items: + - "{{ logs_dir }}" + + - name: Ensure node directories + file: + path: "{{ logs_dir }}/{{ item }}" + state: "directory" + mode: 0777 + with_items: + - "docker_logs" + - "system_logs" + - "ansible" + + - include: pre_rhel.yml + when: ansible_os_family == 'RedHat' + + - include: pre_debian.yml + when: ansible_os_family == 'Debian' + + - name: Create symlink for this role + become: true + file: + src: "{{ ansible_env.HOME }}/{{ zuul.project.src_dir }}" + dest: "/etc/ansible/roles/ansible-role-k8s-mariadb" + state: link + delegate_to: primary + + - name: Clone kubespray + git: + repo: https://github.com/kubernetes-incubator/kubespray/ + dest: "/tmp/kubespray" + delegate_to: primary + + - name: Clone ansible-role-k8s-tripleo for now + become: true + git: + repo: https://github.com/tripleo-apb/ansible-role-k8s-tripleo + dest: "/etc/ansible/roles/ansible-role-k8s-tripleo" + delegate_to: primary + + - name: Clone ansible-role-k8s-tripleo for now + become: true + git: + repo: https://github.com/openstack/openstack-ansible-plugins + dest: "/etc/ansible/roles/openstack-ansible-plugins" + delegate_to: primary diff --git a/tests/pre_debian.yml b/tests/pre_debian.yml new file mode 100644 index 0000000..ed97d53 --- /dev/null +++ b/tests/pre_debian.yml @@ -0,0 +1 @@ +--- diff --git a/tests/pre_rhel.yml b/tests/pre_rhel.yml new file mode 100644 index 0000000..bf9955f --- /dev/null +++ b/tests/pre_rhel.yml @@ -0,0 +1,54 @@ +--- +- name: Add ASB repo for ansible-kubernetes-modules + become: true + yum_repository: + name: asb + description: Copr repo for ansible-service-broker-latest owned by @ansible-service-broker + file: asb + baseurl: https://copr-be.cloud.fedoraproject.org/results/@ansible-service-broker/ansible-service-broker-latest/epel-7-$basearch/ + gpgkey: https://copr-be.cloud.fedoraproject.org/results/@ansible-service-broker/ansible-service-broker-latest/pubkey.gpg + gpgcheck: true + enabled: true + skip_if_unavailable: true + repo_gpgcheck: false + +- name: Enable/Install epel-release/centos-release-openshift-origin + become: true + yum: + name: "{{item}}" + state: present + with_items: + - epel-release + - centos-release-openshift-origin + +# NOTE(flaper87): python-openshift requires a specific version of +# python-requests. We need to update it to the version in the asb repo, hence +# this step. We have to enable epel so we can meet the python2-pysocks +# dependency, which is a python-requests requirement. +- name: Force update for requests/urllib3 + become: true + yum: + name: "{{item}}" + state: latest + update_cache: true + enablerepo: asb,epel + disablerepo: centos-openstack-ocata + with_items: + - python-requests + +- name: Install required packages + become: true + yum: + name: "{{item}}" + state: latest + with_items: + - ansible + - python-netaddr + +- name: Install required packages from asb + become: true + yum: + name: "{{item}}" + state: latest + with_items: + - ansible-kubernetes-modules diff --git a/tests/run.yml b/tests/run.yml new file mode 100644 index 0000000..96b8cb3 --- /dev/null +++ b/tests/run.yml @@ -0,0 +1,46 @@ +--- +- hosts: all + vars: + project_name: ansible-role-k8s-mariadb + tasks: + - set_fact: + nodes: | + {% for host in hostvars %} + {{ host }} ansible_host={{ hostvars[host]['ansible_host'] }} ansible_become=true ansible_user={{ hostvars[host]['ansible_user'] }} + {% endfor %} + + - name: Build inventory + template: + src: "{{ zuul.executor.work_root }}/{{ zuul.project.src_dir }}/tests/templates/inventory.j2" + dest: "/tmp/kubespray/ci_inventory" + delegate_to: "primary" + + - name: Build playbook + template: + src: "{{ zuul.executor.work_root }}/{{ zuul.project.src_dir }}/tests/templates/playbook.j2" + dest: "{{ ansible_env.HOME }}/{{ zuul.project.src_dir }}/playbook.yml" + delegate_to: "primary" + + - shell: + cmd: | + set -e + set -x + + ansible-playbook -i ci_inventory --skip-tags bastion-ssh-config -e skip_downloads=true cluster.yml + executable: /bin/bash + chdir: "/tmp/kubespray" + delegate_to: "primary" + environment: '{{ zuul | zuul_legacy_vars }}' + register: kubespray_output + + - shell: + cmd: | + set -e + set -x + + ansible-playbook -vvvvvv playbook.yml + executable: /bin/bash + chdir: "{{ ansible_env.HOME }}/{{ zuul.project.src_dir }}" + delegate_to: "primary" + environment: '{{ zuul | zuul_legacy_vars }}' + register: "{{project_name}}-output" diff --git a/tests/templates/inventory.j2 b/tests/templates/inventory.j2 new file mode 100644 index 0000000..734b207 --- /dev/null +++ b/tests/templates/inventory.j2 @@ -0,0 +1,12 @@ +[kube-master] +{{nodes}} + +[kube-node] +{{nodes}} + +[etcd:children] +kube-master + +[k8s-cluster:children] +kube-master +kube-node \ No newline at end of file diff --git a/tests/templates/playbook.j2 b/tests/templates/playbook.j2 new file mode 100644 index 0000000..135e4c4 --- /dev/null +++ b/tests/templates/playbook.j2 @@ -0,0 +1,12 @@ +- name: Provision mariadb + hosts: localhost + gather_facts: false + connection: local + + vars: + namespace: default + coe_host: "http://localhost:8080" + + roles: + - role: {{project_name}} + playbook_debug: false