From e1b2094e4e06da2769062c9c62bd62526dc8c0de Mon Sep 17 00:00:00 2001 From: Felix Matouschek Date: Mon, 3 Jun 2024 16:09:21 +0200 Subject: [PATCH 1/2] fix(tests,kubevirt_vm): Make waiting for VM more robust This changes the runme.sh of the kubevirt_vm integration tests to retry the connection to the VM until a login is possible. This is necessary since it is not possible with Ansible alone to retry a task in case the connection failed with the unreachable status. This can happen when the sshd of the VM already accepts connections but a login is not yet possible because the VM is still booting up. Signed-off-by: Felix Matouschek --- .../integration/targets/kubevirt_vm/runme.sh | 16 ++++++++++++- .../targets/kubevirt_vm/verify.yml | 23 ------------------ .../targets/kubevirt_vm/wait_for_vm.yml | 24 +++++++++++++++++++ 3 files changed, 39 insertions(+), 24 deletions(-) create mode 100644 tests/integration/targets/kubevirt_vm/wait_for_vm.yml diff --git a/tests/integration/targets/kubevirt_vm/runme.sh b/tests/integration/targets/kubevirt_vm/runme.sh index 916aca7..c21a6c7 100755 --- a/tests/integration/targets/kubevirt_vm/runme.sh +++ b/tests/integration/targets/kubevirt_vm/runme.sh @@ -13,4 +13,18 @@ ansible-playbook playbook.yml "$@" ansible-inventory -i test.kubevirt.yml -y --list "$@" -ansible-playbook verify.yml -i test.kubevirt.yml --private-key=files/testkey "$@" +# Retry connection to VM until a login is possible +# This is necessary since wait_for is not enough to wait for logins to be possible. +# wait_for is only able to wait until sshd accepts connections. +retries=0 +while ! ansible-playbook wait_for_vm.yml -i test.kubevirt.yml --private-key=files/testkey "$@"; do + if [ "$retries" -ge "10" ]; then + echo "Maximum retries reached, giving up" + exit 1 + fi + echo "Failed to wait for VM, retrying..." + sleep 10 + ((retries+=1)) +done + +ansible-playbook verify.yml --diff -i test.kubevirt.yml --private-key=files/testkey "$@" diff --git a/tests/integration/targets/kubevirt_vm/verify.yml b/tests/integration/targets/kubevirt_vm/verify.yml index 61f374d..28ca20a 100644 --- a/tests/integration/targets/kubevirt_vm/verify.yml +++ b/tests/integration/targets/kubevirt_vm/verify.yml @@ -1,27 +1,4 @@ --- -- name: Wait for SSH - connection: local - gather_facts: false - hosts: localhost - tasks: - - name: Wait up to 900 seconds for port 22 to become open - ansible.builtin.wait_for: - port: 22 - host: "{{ hostvars['default-testvm'].ansible_host }}" - delay: 30 - timeout: 900 - -- name: Connect to VM - gather_facts: true - hosts: default-testvm - remote_user: cloud-user - vars: - ansible_python_interpreter: /usr/bin/python3 - tasks: - - name: Print VM facts - ansible.builtin.debug: - var: ansible_facts - - name: Verify creation with existing VM connection: local gather_facts: false diff --git a/tests/integration/targets/kubevirt_vm/wait_for_vm.yml b/tests/integration/targets/kubevirt_vm/wait_for_vm.yml new file mode 100644 index 0000000..840d0b2 --- /dev/null +++ b/tests/integration/targets/kubevirt_vm/wait_for_vm.yml @@ -0,0 +1,24 @@ +--- +- name: Wait for SSH + connection: local + gather_facts: false + hosts: localhost + tasks: + - name: Wait up to 900 seconds for port 22 to become open + ansible.builtin.wait_for: + port: 22 + host: "{{ hostvars['default-testvm'].ansible_host }}" + search_regex: OpenSSH + delay: 10 + timeout: 900 + +- name: Connect to VM + gather_facts: true + hosts: default-testvm + remote_user: cloud-user + vars: + ansible_python_interpreter: /usr/bin/python3 + tasks: + - name: Print VM facts + ansible.builtin.debug: + var: ansible_facts From 2521e8ba245c7ceae8ace45da63366b6d8a4b7b5 Mon Sep 17 00:00:00 2001 From: Felix Matouschek Date: Mon, 3 Jun 2024 16:10:37 +0200 Subject: [PATCH 2/2] fix(tests,kubevirt_vm): Assert there are no or expected changes Assert there are no or expected changes in verify.yml of the kubevirt_vm integration test. This is necessary since in downstream tests the kubemacpool operator might add annotations to the VM which can trigger a changed result. Signed-off-by: Felix Matouschek --- tests/integration/targets/kubevirt_vm/verify.yml | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/integration/targets/kubevirt_vm/verify.yml b/tests/integration/targets/kubevirt_vm/verify.yml index 28ca20a..952f1b8 100644 --- a/tests/integration/targets/kubevirt_vm/verify.yml +++ b/tests/integration/targets/kubevirt_vm/verify.yml @@ -11,10 +11,15 @@ labels: app: test register: recreate - - name: Assert module reported no changes + - name: Assert module reported no or expected changes ansible.builtin.assert: that: - - not recreate.changed + - >- + not recreate.changed or + recreated.changed and + recreated.method == "update" and + recreated.diff.before.metadata.annotations.get('kubemacpool.io/transaction-timestamp') and + not recreated.diff.after.metadata.annotations - name: Delete VM connection: local