From ce69fc79a85871ca31de3df38f473a536219d93f Mon Sep 17 00:00:00 2001 From: Nikita Kretov Date: Thu, 7 Apr 2022 17:10:39 +0300 Subject: [PATCH 1/6] do not index .venv --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index e73c109..8f1f772 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea /libvirt_exporter libvirt-exporter +.venv From 2f6cd9e58a28af83441cc4c442c6e293e1df8f32 Mon Sep 17 00:00:00 2001 From: Nikita Kretov Date: Fri, 8 Apr 2022 16:45:10 +0300 Subject: [PATCH 2/6] create e2e tests --- ansible/README.md | 63 +++++++++++++++ ansible/molecule/default/converge.yml | 36 +++++++++ ansible/molecule/default/molecule.yml | 15 ++++ ansible/molecule/default/pool.xml.j2 | 17 ++++ ansible/molecule/default/prepare.yml | 91 +++++++++++++++++++++ ansible/molecule/default/verify.yml | 52 ++++++++++++ ansible/molecule/default/vm.xml.j2 | 109 ++++++++++++++++++++++++++ ansible/requirements.txt | 70 +++++++++++++++++ ansible/requirements.yml | 8 ++ 9 files changed, 461 insertions(+) create mode 100644 ansible/README.md create mode 100644 ansible/molecule/default/converge.yml create mode 100644 ansible/molecule/default/molecule.yml create mode 100644 ansible/molecule/default/pool.xml.j2 create mode 100644 ansible/molecule/default/prepare.yml create mode 100644 ansible/molecule/default/verify.yml create mode 100644 ansible/molecule/default/vm.xml.j2 create mode 100644 ansible/requirements.txt create mode 100644 ansible/requirements.yml diff --git a/ansible/README.md b/ansible/README.md new file mode 100644 index 0000000..cd4a54a --- /dev/null +++ b/ansible/README.md @@ -0,0 +1,63 @@ +END 2 END tests +--------------- + +Tested on: +- macOS: 12.3.1 +- Vagrant: 2.2.19 +- VMware Fusion: 12.2.3 + +Installation +============ + +Prerequisites (Instructions for macOS): +- VMware Fusion (personal license): https://customerconnect.vmware.com/web/vmware/evalcenter?p=fusion-player-personal +- Vagrant: https://www.vagrantup.com/downloads +- VMware Fusion provider for vagrant: https://www.vagrantup.com/docs/providers/vmware/installation +- Vagrant VMware Utility: https://www.vagrantup.com/docs/providers/vmware/vagrant-vmware-utility + +Ansible, molecule installation: +- create venv: ```cd ansible; mkdir .venv; python3 -m venv .venv``` +- update pip: ```source .venv/bin/activate; pip3 install -U pip``` +- install pip packages: ```pip install -r requirements.txt``` + +Ansible roles, collection installation: +- enter venv: ```source .venv/bin/activate``` +- install roles,collections: ```ansible-galaxy role install -r requirements.yml``` +- install collections: ```ansible-galaxy collection install -r requirements.yml``` + +Usage +===== + +All commands must be executed inside venv. + +Create test environment: ```molecule converge``` +Run tests: ```molecule verify``` + +Contribute +========== + +All tests located in verify playbook inside molecule tree. You can use variable ```libvirt_exporter_metrics``` to querry specific metrics and their values. Example: + +```yaml + - name: Assert that 'libvirt_up 1' in metrics + assert: + that: "'libvirt_up 1' in libvirt_exporter_metrics" +``` + +In this example, we use assert ansible module (https://docs.ansible.com/ansible/latest/collections/ansible/builtin/assert_module.html) to verify that string ```libvirt_up 1``` is present in libvirt-exporter response. + +Architecture +============ + +There are several main components of e2e tests: +- environment configuration +- source code (or artifacts) transfer to this environment +- service execution +- conditional checks (tests) + +In this realization next set of instruments were selected: +- fusion +- molecule + vagrant +- ansible + +Fusion is convinient, local and versatile vm player. \ No newline at end of file diff --git a/ansible/molecule/default/converge.yml b/ansible/molecule/default/converge.yml new file mode 100644 index 0000000..d7a032c --- /dev/null +++ b/ansible/molecule/default/converge.yml @@ -0,0 +1,36 @@ +--- + +- name: Converge + hosts: all + become: True + vars: + image_name: "localhost/alekseizakharov/libvirt-exporter:latest" + sources_remote_path: "/srv/sources" + sources_local_path: "{{ playbook_dir }}/../../../" + tasks: + - name: Copy local sources to remote dir + ansible.posix.synchronize: + src: "{{ sources_local_path }}" + dest: "{{ sources_remote_path }}" + rsync_opts: + - "--no-motd" + - "--exclude=.venv" + + - name: Build libvirt-exporter image + containers.podman.podman_image: + name: "{{ image_name }}" + path: "{{ sources_remote_path }}" + build: + format: docker + annotation: + app: libvirt-exporter + info: metrics exporter for libvirt + + - name: Run libvirt-exporter container + containers.podman.podman_container: + name: libvirt_exporter + image: "{{ image_name }}" + state: started + recreate: true + publish: "9177:9177" + volume: "/var/run/libvirt:/var/run/libvirt" diff --git a/ansible/molecule/default/molecule.yml b/ansible/molecule/default/molecule.yml new file mode 100644 index 0000000..913964f --- /dev/null +++ b/ansible/molecule/default/molecule.yml @@ -0,0 +1,15 @@ +--- + +prerun: False +driver: + name: vagrant +platforms: + - name: ubuntu + box: generic/ubuntu2010 + config_options: + ssh.keep_alive: True + ssh.remote_user: 'vagrant' +provisioner: + name: ansible +verifier: + name: ansible \ No newline at end of file diff --git a/ansible/molecule/default/pool.xml.j2 b/ansible/molecule/default/pool.xml.j2 new file mode 100644 index 0000000..488ff09 --- /dev/null +++ b/ansible/molecule/default/pool.xml.j2 @@ -0,0 +1,17 @@ + + vms + 28954164-9810-47bc-88f7-ab3d847c38df + 66844176384 + 6587465728 + 60256710656 + + + + /var/lib/libvirt/images + + 0711 + 0 + 0 + + + \ No newline at end of file diff --git a/ansible/molecule/default/prepare.yml b/ansible/molecule/default/prepare.yml new file mode 100644 index 0000000..d067008 --- /dev/null +++ b/ansible/molecule/default/prepare.yml @@ -0,0 +1,91 @@ +--- + +- name: Configure test vm + hosts: all + become: True + gather_facts: True + tasks: + - name: Install libvirtd + apt: + name: "{{ item.name }}={{ item.version }}" + update_cache: True + loop: + - name: libvirt-daemon + version: "6.6.0-1ubuntu3.5" + - name: libvirt-daemon-system + version: "6.6.0-1ubuntu3.5" + - name: libguestfs-tools + version: "1:1.42.0-9ubuntu1" + - name: virtinst + version: "1:2.2.1-4ubuntu2" + - name: python3-libvirt + version: "6.1.0-1" + - name: python3-lxml + version: "4.5.2-1ubuntu0.4" + - name: podman + version: "2.0.6+dfsg1-1ubuntu1.2" + - name: runc + version: "1.0.0~rc95-0ubuntu1~20.10.1" + + - name: Download prom2json + get_url: + url: 'https://github.com/prometheus/prom2json/releases/download/v1.3.0/prom2json-1.3.0.linux-amd64.tar.gz' + dest: /home/vagrant/prom2json-1.3.0.linux-amd64.tar.gz + + - name: Unpack prom2json + ansible.builtin.unarchive: + remote_src: True + src: /home/vagrant/prom2json-1.3.0.linux-amd64.tar.gz + dest: /home/vagrant + + - name: Install prom2json + copy: + remote_src: True + mode: '777' + src: /home/vagrant/prom2json-1.3.0.linux-amd64/prom2json + dest: /usr/local/bin/prom2json + + - name: List libvirt volume pools + community.libvirt.virt_pool: + command: list_pools + register: libvirt_pools + + - name: Create default libvirtd volume pool + community.libvirt.virt_pool: + command: define + name: vms + xml: '{{ lookup("template", "pool.xml.j2") }}' + when: "'default' not in libvirt_pools.list_pools" + + - name: Build vms pool + community.libvirt.virt_pool: + command: build + name: vms + when: "'vms' not in libvirt_pools.list_pools" + + - name: Create vms pool + community.libvirt.virt_pool: + state: active + name: vms + when: "'vms' not in libvirt_pools.list_pools" + + - name: Download qcow2 vm + get_url: + url: http://download.cirros-cloud.net/0.4.0/cirros-0.4.0-x86_64-disk.img + dest: /home/vagrant/cirros-0.4.0-x86_64-disk.qcow2 + + - name: Create volume + command: virsh vol-create-as vms cirros-image.qcow2 128M --format qcow2 + + - name: Upload image contents to volume + command: virsh vol-upload cirros-image.qcow2 /home/vagrant/cirros-0.4.0-x86_64-disk.qcow2 --pool vms + + - name: Define vm + community.libvirt.virt: + command: define + xml: "{{ lookup('template', 'vm.xml.j2') }}" + + - name: Create vm + community.libvirt.virt: + name: test-instance + state: running diff --git a/ansible/molecule/default/verify.yml b/ansible/molecule/default/verify.yml new file mode 100644 index 0000000..bbcc3bd --- /dev/null +++ b/ansible/molecule/default/verify.yml @@ -0,0 +1,52 @@ +--- + +- name: Verify + hosts: all + become: True + gather_facts: True + vars: + libvirt_exporter_url: "http://localhost:9177/metrics" + tasks: + - name: Assert that libvirt_exporter is serving + uri: + url: "{{ libvirt_exporter_url }}" + status_code: 200 + + - name: Get /metrics content from libvirt_eporter + command: "prom2json {{ libvirt_exporter_url }}" + register: libvirt_exporter_response + + - name: Prettify libvirt_exporter_result + set_fact: + libvirt_exporter_metrics: "{{ libvirt_exporter_response.stdout | from_json }}" + + - name: Assert that 'libvirt_up' in metrics + assert: + that: "libvirt_exporter_metrics | selectattr('name', 'equalto', 'libvirt_up') | first" + + - name: Assert that libvirt_up metric has necessary type + vars: + libvirt_up_metric: "{{ libvirt_exporter_metrics | selectattr('name', 'equalto', 'libvirt_up') | first }}" + block: + - name: Assert that libvirt_up metric has necessary type + assert: + that: "libvirt_up_metric.type == 'GAUGE'" + + - name: Assert that 'libvirt_domain_block_meta' in metrics + assert: + that: "libvirt_exporter_metrics | selectattr('name', 'equalto', 'libvirt_domain_block_meta') | first" + + - name: Assert that libvirt_domain_block_meta metric has necessary attributes + vars: + libvirt_domain_block_meta: "{{ libvirt_exporter_metrics | selectattr('name', 'equalto', 'libvirt_domain_block_meta') | first }}" + libvirt_domain_block_meta_metrics: "{{ libvirt_domain_block_meta.metrics | first }}" + block: + - name: Assert that libvirt_domain_block_meta metric has necessary type + assert: + that: "libvirt_domain_block_meta.type == 'GAUGE'" + - name: Assert libvirt_domain_block_meta labels + assert: + that: | + (libvirt_domain_block_meta_metrics.labels.bus == 'virtio') + and + (libvirt_domain_block_meta_metrics.labels.disk_type == 'file') diff --git a/ansible/molecule/default/vm.xml.j2 b/ansible/molecule/default/vm.xml.j2 new file mode 100644 index 0000000..a63822b --- /dev/null +++ b/ansible/molecule/default/vm.xml.j2 @@ -0,0 +1,109 @@ + + test-instance + 524288 + 524288 + 1 + + /machine + + + hvm + + + + + + + + qemu64 + + + + + + + + + destroy + restart + destroy + + + + + + /usr/bin/qemu-system-x86_64 + + + + + + +
+ + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + + + + + + + +
+ + + + + + + + + + + + + + + +
+ + + + + + + + + + +